Package org.xmldb.api.base

Examples of org.xmldb.api.base.Collection


    * @param table Description of Parameter
    * @return Description of the Returned Value
    * @exception Exception Description of Exception
    */
   public boolean execute(Hashtable table) throws Exception {
      Collection col = null;
      Collection tempcol = null;

      try {

         // Verify that that collection passed in is not null
         if (table.get(XMLTools.COLLECTION) != null && table.get(XMLTools.NAME_OF) != null) {

            // Get a Collection reference to pass on to individual commands
            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;
            }

            String newcol = (String) table.get(XMLTools.NAME_OF);

            // Create an instance of the collection manager and create the collection
           
           CollectionManager colman = (CollectionManager) col.getService("CollectionManager", XMLDBAPIVERSION);

           String colName = "";
           String parName = newcol;
    
           int idx = parName.lastIndexOf("/");
    
           if (idx != -1) {
              colName = parName.substring(idx + 1);
              parName = parName.substring(0, idx);
           }
           else if (idx == -1) {
              colName = parName;
           }
    
           if (colName.equals("")) {
              System.out.println("Cannot create a NULL collection");
              return false;
           }
    
           Document doc = new DocumentImpl();
    
           Element colEle = doc.createElement("collection");
           colEle.setAttribute("compressed", "true");
           colEle.setAttribute("name", colName);
    
           doc.appendChild(colEle);
    
           Element filEle = doc.createElement("filer");
           filEle.setAttribute("class", "org.apache.xindice.core.filer.BTreeFiler");
           //filEle.setAttribute("gzip", "true");
           if ( table.containsKey(XMLTools.PAGE_SIZE) ) {
              filEle.setAttribute("pagesize", (String)table.get(XMLTools.PAGE_SIZE));
           }

           if (table.containsKey(XMLTools.MAX_KEY_SIZE) ) {
              filEle.setAttribute("maxkeysize", (String)table.get(XMLTools.MAX_KEY_SIZE) );
           }
          
           colEle.appendChild(filEle);
          
           tempcol = colman.createCollection(newcol, doc);
       

            System.out.println("Created : " + table.get(XMLTools.COLLECTION) + "/" + newcol);
         } else {
            System.out.println("ERROR : Collection Context and New Collection name required");
         }
     
      } finally {
         // Release the collection objects
         if (col != null) {
            col.close();
         }
         if (tempcol != null) {
            tempcol.close();
         }
      }

      return true;
   }
View Full Code Here


    * @return Description of the Returned Value
    * @exception Exception Description of Exception
    */
   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;
         }
        
         // Parse in XML using Xerces
         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);
        
         XMLReader saxReader = spf.newSAXParser().getXMLReader();
        
         StringSerializer ser = new StringSerializer(null);
        
         saxReader.setContentHandler(ser);
         saxReader.setProperty("http://xml.org/sax/properties/lexical-handler",
                ser);
         saxReader.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());
         resource = null;
      }    
      finally {
         if (col != null) {
            col.close();
         }
         col = null;
      }
     
      return true;
View Full Code Here

*/

public class DeleteDocument extends Command {

    public boolean execute(Hashtable table ) throws Exception {       
        Collection col = null;
       
        try {
            if (table.get(XMLTools.COLLECTION) != null) {
                // Get a Collection reference to the collection to be deleted
                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;
                }
           
                if (table.get(XMLTools.NAME_OF) != null) {
                     Resource colresource = col.getResource((String)table.get(XMLTools.NAME_OF));
                       
                     col.removeResource(colresource);
                     System.out.println("DELETED: " + table.get(XMLTools.COLLECTION) + "/" + table.get(XMLTools.NAME_OF));
                }
               
                else {
                    // User did not supply document name
                    System.out.println("ERROR : Document Key and switch required");
                }
               
            }
            else
                // User did not supply collection name
                System.out.println("ERROR : Collection and switch required");

        } finally {
            if ( col != null ) {
                col.close();
            }
        }

      return true;
   }
View Full Code Here

*/

public class ListIndexers extends Command {
   public boolean execute(Hashtable table ) throws Exception {
   
      Collection col = null;
      String colstring = null;
       
      try {

         // Check to see if a collection was passed in, if not set to root
         if ( table.get(XMLTools.COLLECTION) == null ) {
            colstring = normalizeCollectionURI( "/", (String) table.get(XMLTools.LOCAL) );
         }
         else {
            // Get a Collection reference to the collection
            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 parent of the collection
         CollectionManager colman = (CollectionManager)col.getService("CollectionManager",XMLDBAPIVERSION);
           
         String[] idx = colman.listIndexers();

         System.out.println("Indexes:\n");

         for(int i = 0; i < idx.length; i++) {
            System.out.println(idx[i]);
         }
        
         System.out.println("\nTotal Indexes: " + idx.length);
        
      } finally {
         if ( col != null ) {
            col.close();
         }
      }
     
   return true;
  
View Full Code Here

*/

public class DeleteIndexer extends Command {
    public boolean execute(Hashtable table) throws Exception {

        Collection col = null ;

        try {
           if (table.get(XMLTools.COLLECTION) != null) {
             // 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);

              if (table.get(XMLTools.NAME_OF) != null) {
                 colman.dropIndexer((String)table.get(XMLTools.NAME_OF));
                 System.out.println("DELETED: " + (String)table.get(XMLTools.NAME_OF));
              }
              else {
                 System.out.println("ERROR : Name switch and Indexer name required");
              }
           }
           else
              System.out.println("ERROR : Collection and switch required");

        } finally {
            if ( col != null ) {
                col.close();
            }
        }

      return true;
   }
View Full Code Here

*/

public class RetrieveDocument extends Command {
    public boolean execute(Hashtable table ) throws Exception {

        Collection col = null;

        try {

            if (table.get(XMLTools.COLLECTION) != null) {
               // Create a collection instance
               String colstring = normalizeCollectionURI( (String)table.get(XMLTools.COLLECTION),
                                                          (String) table.get(XMLTools.LOCAL) );
               String docname = (String)table.get(XMLTools.NAME_OF);

               col = DatabaseManager.getCollection( colstring );
               // Make sure that the collection was found
               if ( col == null ) {
                  System.out.println("ERROR : Collection not found!");
                  return false;
               }

               if (table.get(XMLTools.NAME_OF) != null) {

                  XMLResource resource = (XMLResource)col.getResource(docname);
                  // Verify that we were able to find the document
                  if ( resource == null ) {
                     System.out.println("ERROR : Document not found!");
                     return false;
                  }
                   
                  String documentstr = (String)resource.getContent();

                  if ((documentstr != null) && (table.get(XMLTools.FILE_PATH) != "" )) {
                     try {
                        File file = new File((String)table.get(XMLTools.FILE_PATH));
                        // Create the directory structure if necessary
                        if( file.getParentFile() != null ) {
                           file.getParentFile().mkdirs();
                        }
                        
                        FileOutputStream out = new FileOutputStream(file);
                       
                        System.out.println("Writing...");
                       
                        out.write(documentstr.getBytes("utf-8"));
                        out.close();
                       
                        System.out.println("Wrote file " + table.get(XMLTools.FILE_PATH) );

                     } catch (IOException ie) {
                         System.out.println("ERROR : " + ie);
                         return false;
                    
                     } catch (NullPointerException e) {
                         System.out.println("ERROR : " + e);
                         System.out.println("No file destination given");
                         return false;
                     }
                  }
                  else {
                     // Send to stdout
                     System.out.println(documentstr);
                  }
               }
               else {
                  System.out.println("ERROR : Document Key required");
               }
            }
            else{
               System.out.println("ERROR : Collection context required");
            }

         } finally {
            // Release the collection object
            if ( col != null ) {
               col.close();
            }
         }

      return true;
   }
View Full Code Here

*/

public class AddIndexer extends Command {
    public boolean execute(Hashtable table ) throws Exception {
   
        Collection col = null;
       
        try {
       
            if (table.get(XMLTools.COLLECTION) != null) {

                // 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);
   
                if (table.get(XMLTools.NAME_OF) != null && table.get(XMLTools.PATTERN) != null ) {
               
                    Document doc = new DocumentImpl();

                    // Create the index element to hold attributes
                    Element idxEle = doc.createElement("index");
                    idxEle.setAttribute("class", XINDICE_VAL_INDEXER);
                    idxEle.setAttribute("name", (String)table.get(XMLTools.NAME_OF));
                    idxEle.setAttribute("pattern", (String)table.get(XMLTools.PATTERN) );


                    // Setup optional index attributes
                    if ( table.containsKey(XMLTools.TYPE) ) {
                        String t = (String)table.get(XMLTools.TYPE);
                        if ( t.equalsIgnoreCase("name") )
                           idxEle.setAttribute("class", XINDICE_NAME_INDEXER);
                        else
                           idxEle.setAttribute("type", (String)table.get(XMLTools.TYPE));
                    }

                    if ( table.containsKey(XMLTools.PAGE_SIZE) ) {
                        idxEle.setAttribute("pagesize", (String)table.get(XMLTools.PAGE_SIZE));
                    }

                    if (table.containsKey(XMLTools.MAX_KEY_SIZE) ) {
                        idxEle.setAttribute("maxkeysize", (String)table.get(XMLTools.MAX_KEY_SIZE) );
                    }

                    // Add Element to the document
                    doc.appendChild(idxEle);
                       
                    //If in verbose mode, show....
                    if ( (String)table.get(XMLTools.VERBOSE) == "true" ) {
                        String indexstr = TextWriter.toString(doc);
                        System.out.println("Index node element = ");
                        System.out.println("\t" + indexstr + "\n" );
                    }
                   

                    // Create the indexer for this collection manager
                    colman.createIndexer(doc);

                    System.out.println("CREATED : " + table.get(XMLTools.NAME_OF));
                   
                }
                else {
                    System.out.println("ERROR : Name and Pattern required");
                }

            }
            else {
                System.out.println("ERROR : Collection and switch required");
            }

        } finally {
            if ( col != null ) {
                col.close();
            }
        }
       
        return true;
    }
View Full Code Here

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;
    }
View Full Code Here

      this.driver = driver;
   }

   public String getName(String path)
         throws Exception {
      Collection col = DatabaseManager.getCollection(driver + "/" + path);
      return col.getName();
   }
View Full Code Here

   }

   public Collection createCollection(String path,
                                      String name)
         throws Exception {
      Collection col = DatabaseManager.getCollection(driver + "/" + path);
      CollectionManager service = (CollectionManager) col.getService("CollectionManager", "1.0");

      String collectionConfig = "<collection compressed=\"true\" name=\"" + name + "\">" +
            "   <filer class=\"org.apache.xindice.core.filer.BTreeFiler\" gzip=\"true\"/>" +
            "</collection>";
      return service.createCollection(name, DOMParser.toDocument(collectionConfig));
View Full Code Here

TOP

Related Classes of org.xmldb.api.base.Collection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.