vgritsenko 2004/01/15 06:23:21
Modified: java/src/org/apache/xindice/tools/command AddDocument.java
config commands.xml
Added: java/src/org/apache/xindice/tools/command AddResource.java
Log:
Add separate command, add_resource, for adding binary resources
Revision Changes Path
1.12 +21 -45
xml-xindice/java/src/org/apache/xindice/tools/command/AddDocument.java
Index: AddDocument.java
===================================================================
RCS file:
/home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/AddDocument.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- AddDocument.java 9 Jan 2004 04:14:24 -0000 1.11
+++ AddDocument.java 15 Jan 2004 14:23:20 -0000 1.12
@@ -60,17 +60,12 @@
package org.apache.xindice.tools.command;
import org.apache.xindice.tools.XMLTools;
-import org.apache.xindice.client.xmldb.xmlrpc.CollectionImpl;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
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 org.xmldb.api.modules.XMLResource;
-import org.xmldb.api.modules.BinaryResource;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
@@ -79,9 +74,9 @@
import java.util.Hashtable;
/**
- * AddDocument.java is designed to let the user add Single Documents
- * and/or Multiple Documents to an existing Collection or Nested Collection
- * within the current Database.
+ * AddDocument is designed to let the user add single document
+ * and/or multiple documents to any existing collection
+ * within the current database.
*
* @version CVS $Revision$, $Date$
*/
@@ -116,37 +111,22 @@
File file = new File((String) table.get(XMLTools.FILE_PATH));
InputStream fis = new FileInputStream(file);
- // XML or Binary? Check XML prolog "<?xml ".
- Resource resource;
- byte[] prolog = new byte[6];
- fis.read(prolog);
- fis = new FileInputStream(file);
- if (new String(prolog).equals("<?xml ")) {
- // Parse in XML using Xerces
- SAXParserFactory spf =
javax.xml.parsers.SAXParserFactory.newInstance();
- spf.setNamespaceAware(true);
-
- XMLReader reader = spf.newSAXParser().getXMLReader();
- StringSerializer ser = new StringSerializer(null);
- 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 = col.createResource((String)
table.get(XMLTools.NAME_OF),
- "XMLResource");
- resource.setContent(ser.toString());
- col.storeResource(resource);
- } else {
- resource = col.createResource((String)
table.get(XMLTools.NAME_OF),
- "BinaryResource");
- byte[] data = new byte[fis.available()];
- fis.read(data);
- resource.setContent(data);
- col.storeResource(resource);
- }
-
+ // Parse in XML using Xerces
+ SAXParserFactory spf =
javax.xml.parsers.SAXParserFactory.newInstance();
+ spf.setNamespaceAware(true);
+
+ XMLReader reader = spf.newSAXParser().getXMLReader();
+ StringSerializer ser = new StringSerializer(null);
+ 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());
@@ -160,8 +140,4 @@
return true;
}
-
}
-
-
-
1.1
xml-xindice/java/src/org/apache/xindice/tools/command/AddResource.java
Index: AddResource.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xindice" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 1999-2001, The dbXML
* Group, L.L.C., http://www.dbxmlgroup.com. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* CVS $Id: AddResource.java,v 1.1 2004/01/15 14:23:20 vgritsenko Exp $
*/
package org.apache.xindice.tools.command;
import org.apache.xindice.tools.XMLTools;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Hashtable;
/**
* AddResource is designed to let the user add single binary resource
* and/or multiple binary resources to any existing collection
* within the current database.
*
* @version CVS $Revision: 1.1 $, $Date: 2004/01/15 14:23:20 $
*/
public class AddResource extends Command {
/**
* Adds a resource 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 (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;
}
// Read data
File file = new File((String) table.get(XMLTools.FILE_PATH));
InputStream fis = new FileInputStream(file);
byte[] data = new byte[fis.available()];
fis.read(data);
fis.close();
// Create the BinaryResource and store the resource
Resource resource = col.createResource((String)
table.get(XMLTools.NAME_OF),
"BinaryResource");
resource.setContent(data);
col.storeResource(resource);
System.out.println("Added resource " +
table.get(XMLTools.COLLECTION) + "/" +
resource.getId());
resource = null;
} finally {
if (col != null) {
col.close();
}
col = null;
}
return true;
}
}
1.6 +6 -1 xml-xindice/config/commands.xml
Index: commands.xml
===================================================================
RCS file: /home/cvs/xml-xindice/config/commands.xml,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- commands.xml 5 Aug 2003 08:47:00 -0000 1.5
+++ commands.xml 15 Jan 2004 14:23:20 -0000 1.6
@@ -54,6 +54,11 @@
class="org.apache.xindice.tools.command.AddDocument"
helpclass="document"
description="Adds a document" />
+ <command switch="ar"
+ name="add_resource"
+ class="org.apache.xindice.tools.command.AddResource"
+ helpclass="document"
+ description="Adds a resource" />
<command switch="addmultiple"
name="add_multiple_documents"
class="org.apache.xindice.tools.command.AddMultipleDocuments"