You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-dev@xml.apache.org by vg...@apache.org on 2004/01/09 05:14:24 UTC

cvs commit: xml-xindice/java/src/org/apache/xindice/tools/command AddCollection.java AddDocument.java AddIndexer.java ExportTree.java

vgritsenko    2004/01/08 20:14:24

  Modified:    java/src/org/apache/xindice/tools/command AddCollection.java
                        AddDocument.java AddIndexer.java ExportTree.java
  Log:
  Enable inline meta data in AddCollection command of command line utility.
  Add binary resource support to AddDocument and ExportTreee commands.
  
  Revision  Changes    Path
  1.9       +6 -9      xml-xindice/java/src/org/apache/xindice/tools/command/AddCollection.java
  
  Index: AddCollection.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/AddCollection.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- AddCollection.java	7 Aug 2003 20:13:24 -0000	1.8
  +++ AddCollection.java	9 Jan 2004 04:14:24 -0000	1.9
  @@ -79,11 +79,7 @@
   public class AddCollection extends Command {
   
       /**
  -     * TODO: Description of the Method
  -     *
  -     * @param table Description of Parameter
  -     * @return Description of the Returned Value
  -     * @exception Exception Description of Exception
  +     * Creates collection
        */
       public boolean execute(Hashtable table) throws Exception {
   
  @@ -131,8 +127,10 @@
                   Document doc = new DocumentImpl();
   
                   Element colEle = doc.createElement("collection");
  -                colEle.setAttribute("compressed", "true");
                   colEle.setAttribute("name", colName);
  +                // FIXME Make this configurable
  +                colEle.setAttribute("compressed", "true");
  +                colEle.setAttribute("inline-metadata", "true");
   
                   doc.appendChild(colEle);
   
  @@ -150,7 +148,6 @@
                   colEle.appendChild(filEle);
   
                   tempcol = colman.createCollection(newcol, doc);
  -
   
                   System.out.println("Created : " + table.get(XMLTools.COLLECTION) + "/" + newcol);
               } else {
  
  
  
  1.11      +35 -25    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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AddDocument.java	22 Dec 2003 14:02:44 -0000	1.10
  +++ AddDocument.java	9 Jan 2004 04:14:24 -0000	1.11
  @@ -68,7 +68,9 @@
   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;
  @@ -86,11 +88,7 @@
   public class AddDocument extends Command {
   
       /**
  -     * TODO: Description of the Method
  -     *
  -     * @param table Description of Parameter
  -     * @return Description of the Returned Value
  -     * @exception Exception Description of Exception
  +     * Adds a document to the collection
        */
       public boolean execute(Hashtable table) throws Exception {
   
  @@ -115,28 +113,40 @@
                   return false;
               }
   
  -            // Parse in XML using Xerces
  -            // TODO: BinaryResource support
               File file = new File((String) table.get(XMLTools.FILE_PATH));
               InputStream fis = new FileInputStream(file);
   
  -            SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
  -            spf.setNamespaceAware(true);
  +            // 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);
  +            }
   
  -            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
  -            XMLResource resource =
  -                    (XMLResource) 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());
  
  
  
  1.9       +4 -9      xml-xindice/java/src/org/apache/xindice/tools/command/AddIndexer.java
  
  Index: AddIndexer.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/AddIndexer.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- AddIndexer.java	9 Aug 2003 05:01:56 -0000	1.8
  +++ AddIndexer.java	9 Jan 2004 04:14:24 -0000	1.9
  @@ -130,8 +130,8 @@
                       // Add Element to the document
                       doc.appendChild(idxEle);
   
  -                    //If in verbose mode, show....
  -                    if ((String) table.get(XMLTools.VERBOSE) == "true") {
  +                    // If in verbose mode, show....
  +                    if (table.get(XMLTools.VERBOSE) == "true") {
                           String indexstr = TextWriter.toString(doc);
                           System.out.println("Index node element = ");
                           System.out.println("\t" + indexstr + "\n");
  @@ -142,7 +142,6 @@
                       colman.createIndexer(doc);
   
                       System.out.println("CREATED : " + table.get(XMLTools.NAME_OF));
  -
                   } else {
                       System.out.println("ERROR : Name and Pattern required");
                   }
  @@ -159,8 +158,4 @@
   
           return true;
       }
  -
   }
  -
  -
  -
  
  
  
  1.10      +22 -34    xml-xindice/java/src/org/apache/xindice/tools/command/ExportTree.java
  
  Index: ExportTree.java
  ===================================================================
  RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/tools/command/ExportTree.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ExportTree.java	22 Dec 2003 14:02:44 -0000	1.9
  +++ ExportTree.java	9 Jan 2004 04:14:24 -0000	1.10
  @@ -60,19 +60,18 @@
   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.ErrorCodes;
  +import org.xmldb.api.base.Resource;
   import org.xmldb.api.base.XMLDBException;
  -import org.xmldb.api.modules.XMLResource;
   
   import java.io.File;
   import java.io.FileOutputStream;
   import java.util.Hashtable;
   
   /**
  - * ExportTree.java is designed to take a Collection tree and create a Directory
  + * ExportTree is designed to take a Collection tree and create a Directory
    * tree from it.  Export tree requires a collection name and a file path to create the tree
    *
    * @version CVS $Revision$, $Date$
  @@ -80,7 +79,7 @@
   public class ExportTree extends Command {
   
       /**
  -     * TODO: Description of the Method
  +     * Exports a collection into a file system
        *
        * @param table Description of Parameter
        * @return Description of the Returned Value
  @@ -108,7 +107,6 @@
               File dir = new File(fp, parent);
   
               System.out.println("Creating directory " + dir.getPath());
  -
               dir.mkdir();
   
               process(dir, table, col);
  @@ -117,13 +115,7 @@
           return true;
       }
   
  -    /**
  -     * TODO: Description of the Method
  -     *
  -     * @param parent Description of Parameter
  -     * @return Description of the Returned Value
  -     */
  -    public String parentDir(String parent) {
  +    private String parentDir(String parent) {
           String dirName = "";
           String parName = parent;
   
  @@ -139,12 +131,7 @@
       }
   
       /**
  -     * TODO: Description of the Method
  -     *
  -     * @param directory Description of Parameter
  -     * @param table Description of Parameter
  -     * @param col Description of Parameter
  -     * @return Description of the Returned Value
  +     * Export given collection to the directory
        */
       boolean process(File directory, Hashtable table, Collection col) throws Exception {
           try {
  @@ -171,21 +158,22 @@
               }
   
               if (files != null) {
  -                System.out.println("Extracting " + files.length + " files from " +
  -                                   (String) table.get(XMLTools.COLLECTION));
  +                System.out.println("Extracting " + files.length + " files from " + table.get(XMLTools.COLLECTION));
                   for (int j = 0; j < files.length; j++) {
  -                    // TODO: BinaryResource support
  -                    XMLResource res =
  -                            (XMLResource) col.getResource(files[j]);
  -                    String content = (String) res.getContent();
  -                    FileOutputStream output =
  -                            new FileOutputStream(new File(directory, files[j]));
  -
  -                    /* UTF8FIXED: as we omit encoding declaration in output XML
  -                     * for the moment, we MUST write file in UTF-8
  -                     */
  -                    output.write(content.getBytes("utf-8"));
  -                    output.close();
  +                    Resource res = col.getResource(files[j]);
  +                    Object content = res.getContent();
  +                    FileOutputStream output = new FileOutputStream(new File(directory, files[j]));
  +                    try {
  +                        if (content instanceof String) {
  +                            // UTF8FIXED: as we omit encoding declaration in output XML
  +                            // for the moment, we MUST write file in UTF-8
  +                            output.write(((String)content).getBytes("utf-8"));
  +                        } else {
  +                            output.write((byte[]) content);
  +                        }
  +                    } finally {
  +                        output.close();
  +                    }
                   }
               }