Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 141   Methods: 3
NCLOC: 82   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AddMultipleDocuments.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: AddMultipleDocuments.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.FilenameFilter;
 29   
 30    /**
 31    * AddMultipleDocuments.java is designed to let the user add several Documents
 32    * to an existing Collection at one time.
 33    *
 34    * Documents are added with an ID the same as the file name.
 35    * This command assumes that the collection being added to already exists!
 36    *
 37    * @version $Revision: 578602 $, $Date: 2007-09-23 13:33:31 -0700 (Sun, 23 Sep 2007) $
 38    */
 39    public class AddMultipleDocuments extends Command {
 40   
 41  0 public boolean execute(XMLTools.Config table) throws Exception {
 42   
 43    // Verify that user has supplied necessary arguments
 44  0 if (table.getString(XMLTools.COLLECTION) == null) {
 45  0 System.out.println("ERROR : Collection and switch required");
 46  0 return false;
 47    }
 48   
 49  0 if ("".equals(table.getString(XMLTools.FILE_PATH))) {
 50  0 System.out.println("ERROR : Directory name and switch required");
 51  0 return false;
 52    }
 53   
 54  0 Collection col = null;
 55  0 try {
 56   
 57    // Get a Collection reference to the collection
 58  0 String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
 59    table.getBoolean(XMLTools.LOCAL));
 60  0 col = DatabaseManager.getCollection(colstring);
 61  0 if (col == null) {
 62  0 System.out.println("ERROR : Collection not found!");
 63  0 return false;
 64    }
 65   
 66    // Get a File object for the Directory passed in
 67  0 File dir = new File(table.getString(XMLTools.FILE_PATH));
 68  0 if (dir.isDirectory()) {
 69   
 70  0 String[] children = new String[]{};
 71  0 final String ext = table.getString(XMLTools.EXTENSION);
 72   
 73    // If the user supplied a file extension, filter on it, else use entire contents of the directory
 74  0 if (!ext.equals("")) {
 75  0 children = dir.list(new FilenameFilter() {
 76  0 public boolean accept(File none, String name) {
 77  0 return name.endsWith("." + ext);
 78    }
 79    });
 80  0 } else if (ext.equals("")) {
 81  0 children = dir.list();
 82    }
 83   
 84  0 System.out.println("Reading files from: " + dir.getCanonicalPath());
 85   
 86    // Use the functionality already provided in AddDocument command to add this document
 87  0 final Command cmd = new AddDocument();
 88   
 89    // Command parameters
 90  0 XMLTools.Config localtable = new XMLTools.Config();
 91  0 localtable.setString(XMLTools.COLLECTION, table.getString(XMLTools.COLLECTION));
 92  0 if (table.getBoolean(XMLTools.LOCAL)) {
 93  0 localtable.setBoolean(XMLTools.LOCAL, true);
 94    }
 95   
 96    // Loop over documents, adding to db
 97  0 for (int i = 0; i < children.length; i++) {
 98   
 99  0 File file = new File(dir, children[i]);
 100  0 if (file.isFile()) {
 101  0 try {
 102    // Populate hashtable to pass to AddDocument class
 103  0 localtable.setString(XMLTools.NAME_OF, file.getName());
 104  0 localtable.setString(XMLTools.FILE_PATH, file.getPath());
 105   
 106    // Execute the class!
 107  0 cmd.execute(localtable);
 108    } catch (Exception e) {
 109  0 System.out.println("Error Adding File: " + file.getName());
 110  0 if (table.getBoolean(XMLTools.VERBOSE)) {
 111  0 e.printStackTrace();
 112    }
 113    }
 114    }
 115    } // for loop
 116    }
 117   
 118    } finally {
 119    // Release all resources
 120  0 if (col != null) {
 121  0 col.close();
 122    }
 123    }
 124   
 125  0 return true;
 126    }
 127   
 128  0 public void usage() {
 129  0 System.out.println("Format: xindice addmultiple -c <context> [-l [-d <path>]] [-v] [parameters...]");
 130  0 System.out.println();
 131  0 System.out.println("Add several documents to an existing collection at one time. Documents are");
 132  0 System.out.println("added with an ID the same as the file name");
 133  0 System.out.println();
 134  0 System.out.println("Command-specific switches:");
 135  0 System.out.println(" -f|--filepath <path>");
 136  0 System.out.println(" Directory name for the files being added to a collection");
 137  0 System.out.println(" -e|--extension <extention>");
 138  0 System.out.println(" File extension for documents to use in the filter");
 139  0 System.out.println();
 140    }
 141    }