*/
public class AddMultipleDocuments extends Command {
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
try {
// Verify that user has supplied necessary arguments
if (table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection and switch required");
return false;
}
if (table.get(XMLTools.FILE_PATH) == "") {
System.out.println("ERROR : Directory name and switch required");
return false;
}
// Get a Collection reference to the collection
String colstring = normalizeCollectionURI((String) table.get(XMLTools.COLLECTION),
(String) table.get(XMLTools.LOCAL));
col = DatabaseManager.getCollection(colstring);
if (col == null) {
System.out.println("ERROR : Collection not found!");
return false;
}
// Create a collection manager instance for the collection
// CollectionManager colman = (CollectionManager)col.getService("CollectionManager",XMLDBAPIVERSION);
// Get a File object for the Directory passed in
File dir = new File((String) table.get(XMLTools.FILE_PATH));
if (dir.isDirectory()) {
String[] children = new String[]{};
final String ext = (String) table.get(XMLTools.EXTENSION);
// If the user supplied a file extension, filter on it, else use entire contents of the directory
if (!ext.equals("")) {
children = dir.list(new FilenameFilter() {
public boolean accept(File none, String name) {
return name.endsWith("." + ext);
}
});
} else if (ext.equals("")) {
children = dir.list();
}
System.out.println("Reading files from: " + dir.getName());
// Loop over documents, adding to db
for (int i = 0; i < children.length; i++) {
File files = new File(dir, children[i]);
if (files.isFile()) {
FileInputStream insr = new FileInputStream(files);
byte[] fileBuffer = new byte[(int) files.length()];
insr.read(fileBuffer);
try {
// Use the functionality already provided in Command.AddDocument to add this document
Command cmd = (Command) Class.forName("org.apache.xindice.tools.command.AddDocument").newInstance();
Hashtable localtable = new Hashtable();
String filepath = dir.getPath() + "/" + files.getName();
// Populate hashtable to pass to AddDocument class
localtable.put(XMLTools.COLLECTION, table.get(XMLTools.COLLECTION));
localtable.put(XMLTools.NAME_OF, files.getName());
localtable.put(XMLTools.FILE_PATH, filepath);
// Execute the class!
cmd.execute(localtable);
} catch (Exception e) {
System.out.println("Error Adding File: " + files.getName());
//System.out.println(e);
continue;
}
} else
continue;
} // for loop
}
} finally {
// Release all resources
if (col != null) {
col.close();
}
}
return true;
}