/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: AddDocument.java 511426 2007-02-25 03:25:02Z vgritsenko $
*/
package org.apache.xindice.tools.command;
import org.apache.xindice.tools.XMLTools;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
/**
* AddDocument is designed to let the user add single document
* and/or multiple documents to any existing collection
* within the current database.
*
* @version $Revision: 511426 $, $Date: 2007-02-24 22:25:02 -0500 (Sat, 24 Feb 2007) $
*/
public class AddDocument extends Command {
/**
* Adds a document to the collection
*/
public boolean execute(Hashtable table) throws Exception {
Collection col = null;
if (table.get(XMLTools.COLLECTION) == null) {
System.out.println("ERROR : Collection and switch required");
return false;
}
if ("".equals(table.get(XMLTools.FILE_PATH))) {
System.out.println("ERROR : File path required");
return false;
}
try {
// Create a collection instance
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;
}
File file = new File((String) table.get(XMLTools.FILE_PATH));
InputStream fis = new FileInputStream(file);
// Parse in XML using Xerces
SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
XMLReader reader = spf.newSAXParser().getXMLReader();
StringSerializer ser = new StringSerializer();
reader.setContentHandler(ser);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
reader.parse(new InputSource(fis));
fis.close();
// Create the XMLResource and store the document
Resource resource = col.createResource((String) table.get(XMLTools.NAME_OF),
"XMLResource");
resource.setContent(ser.toString());
col.storeResource(resource);
System.out.println("Added document " + table.get(XMLTools.COLLECTION) + "/" +
resource.getId());
resource = null;
} finally {
if (col != null) {
col.close();
}
col = null;
}
return true;
}
}