Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 202   Methods: 7
NCLOC: 111   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ImportTree.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: ImportTree.java 578602 2007-09-23 20:33:31Z natalia $
 18    */
 19   
 20    package org.apache.xindice.tools.command;
 21   
 22    import org.apache.xindice.tools.XMLTools;
 23   
 24    import org.xmldb.api.DatabaseManager;
 25    import org.xmldb.api.base.Collection;
 26   
 27    import java.io.File;
 28    import java.io.FileFilter;
 29   
 30    /**
 31    * ImportTree.java is designed to take a directory/file path and create a Collection
 32    * from it's name. The files within the directory are converted into Documents
 33    * and then stored into the newly created Collection with their filename as a document key
 34    *
 35    * NOTE: If the collection passed in with the -c option already exists it will be overwritten!
 36    *
 37    * @version $Revision: 578602 $, $Date: 2007-09-23 13:33:31 -0700 (Sun, 23 Sep 2007) $
 38    */
 39    public class ImportTree extends Command {
 40   
 41    Command addDocument = null;
 42    Command addCollection = null;
 43   
 44  0 public boolean execute(XMLTools.Config table) throws Exception {
 45   
 46    // Verify that the correct command line parameters were passed in
 47  0 if (table.getString(XMLTools.FILE_PATH) == null) {
 48  0 System.out.println("ERROR : Directory name and switch required");
 49  0 return false;
 50    }
 51   
 52  0 if (table.getString(XMLTools.COLLECTION) == null) {
 53  0 System.out.println("ERROR : Collection name and switch required");
 54  0 return false;
 55    }
 56   
 57  0 Collection col = null;
 58  0 try {
 59    // Setup the creation commands we'll use to add documents and create
 60    // collections.
 61  0 addDocument =
 62    (Command) Class.forName("org.apache.xindice.tools.command.AddDocument").newInstance();
 63   
 64  0 addCollection =
 65    (Command) Class.forName("org.apache.xindice.tools.command.AddCollection").newInstance();
 66   
 67  0 String startcollection = table.getString(XMLTools.COLLECTION);
 68   
 69    // Make sure the root collection exists
 70  0 String colstring = normalizeCollectionURI(startcollection, table.getBoolean(XMLTools.LOCAL));
 71  0 if ((col = DatabaseManager.getCollection(colstring)) == null) {
 72  0 System.out.println("ERROR : Collection not found!");
 73  0 return false;
 74    }
 75   
 76  0 File startdir = new File(table.getString(XMLTools.FILE_PATH));
 77   
 78    // Make call to process, this is called recursively!
 79  0 process(startdir, colstring, table);
 80   
 81    } finally {
 82    // Be sure to close collection objects
 83  0 if (col != null) {
 84  0 col.close();
 85    }
 86    }
 87   
 88  0 return true;
 89    }
 90   
 91    // Recursive function to create collections/documents
 92  0 void process(File directory, String baseCollection, XMLTools.Config table) {
 93  0 try {
 94    // The file extension to use for reading in files
 95  0 final String ext = table.getString(XMLTools.EXTENSION);
 96   
 97    // Make sure we received a directory so we don't create a collection
 98    // named after a file.
 99  0 if (!directory.isDirectory()) {
 100  0 System.out.println("ERROR: The specified import path is not a directory " +
 101    directory.getPath());
 102  0 return;
 103    }
 104   
 105    // Create the collection for this directory.
 106  0 baseCollection = createCollection(baseCollection, directory.getName());
 107   
 108    // Get a directory listing and loop over
 109  0 File[] subdirs = directory.listFiles(new ExtensionFileFilter(ext));
 110   
 111  0 for (int i = 0; i < subdirs.length; i++) {
 112    // Is this a directory? If so, call ourselves recursively
 113  0 if (subdirs[i].isDirectory()) {
 114  0 process(subdirs[i], baseCollection, table);
 115    } else {
 116    // Otherwise we have a file so import it.
 117  0 importFile(baseCollection, subdirs[i].getAbsolutePath(), subdirs[i].getName());
 118    }
 119    }
 120    } catch (Exception e) {
 121  0 System.out.println("ERROR : " + e.getMessage());
 122  0 if (table.getBoolean(XMLTools.VERBOSE)) {
 123  0 e.printStackTrace(System.err);
 124    }
 125    }
 126    }
 127   
 128  0 protected void importFile(String baseCollection, String path, String name)
 129    throws Exception {
 130  0 XMLTools.Config table = new XMLTools.Config();
 131    // Use functionality from AddDocument to add this document
 132  0 table.setString(XMLTools.COLLECTION, baseCollection);
 133  0 table.setString(XMLTools.FILE_PATH, path);
 134  0 table.setString(XMLTools.NAME_OF, name);
 135   
 136  0 addDocument.execute(table);
 137    }
 138   
 139  0 protected String createCollection(String baseCollection, String newCollection)
 140    throws Exception {
 141  0 if (!newCollection.equals(".")) {
 142  0 XMLTools.Config table = new XMLTools.Config();
 143  0 table.setString(XMLTools.COLLECTION, baseCollection);
 144  0 table.setString(XMLTools.NAME_OF, newCollection);
 145   
 146  0 addCollection.execute(table);
 147    } else {
 148  0 newCollection = "";
 149    }
 150   
 151  0 return baseCollection + "/" + newCollection;
 152    }
 153   
 154    /**
 155    * Allow us to filter directory listings over certain file extensions
 156    */
 157    private static class ExtensionFileFilter implements FileFilter {
 158   
 159    private String extension = "";
 160   
 161    /**
 162    * Constructs an <code>MyFileFiler</code> instance with a specific
 163    * extension.
 164    *
 165    * @param extension The file's extension
 166    */
 167  0 public ExtensionFileFilter(String extension) {
 168  0 this.extension = extension;
 169    }
 170   
 171    /**
 172    * Tests whether or not the specified abstract pathname should be
 173    * included in a pathname list.
 174    *
 175    * @param pathname The abstract pathname to be tested
 176    * @return <code>true</code> if and only if <code>pathname</code>
 177    * should be included
 178    */
 179  0 public boolean accept(File pathname) {
 180  0 if (this.extension.length() > 0) {
 181  0 return pathname.getName().endsWith("." + this.extension) || pathname.isDirectory();
 182    } else {
 183  0 return true;
 184    }
 185    }
 186    }
 187   
 188  0 public void usage() {
 189  0 System.out.println("Format: xindice import -c <context> [-l [-d <path>]] [-v] [parameters...]");
 190  0 System.out.println();
 191  0 System.out.println("Takes a directory path and creates a collection from it's name. The files");
 192  0 System.out.println("within the directory are converted into documents and then stored into the");
 193  0 System.out.println("newly created collection with their filename as a document key.");
 194  0 System.out.println();
 195  0 System.out.println("Command-specific switches:");
 196  0 System.out.println(" -f|--filepath <file>");
 197  0 System.out.println(" Directory tree");
 198  0 System.out.println(" -e|--extension <extention>");
 199  0 System.out.println(" File extension for documents to use in the filter");
 200  0 System.out.println();
 201    }
 202    }