Package org.xmldb.api.base

Examples of org.xmldb.api.base.Collection


    Class cl = Class.forName(driver);
    Database database = (Database) cl.newInstance();
    database.setProperty("create-database", "true");
    DatabaseManager.registerDatabase(database);

    Collection col = null;
    XMLResource res = null;
    try {
      // get the collection
      col = DatabaseManager.getCollection(URI + "/security");
      col.setProperty(OutputKeys.INDENT, "no");
      res = (XMLResource)col.getResource("example");

      if(res == null) {
        System.out.println("document not found!");
      } else {
        System.out.println(res.getContent());
      }
    } finally {
      //dont forget to clean up!

      if(res != null) {
        //try { ((EXistResource)res).freeResources(); } catch(XMLDBException xe) {xe.printStackTrace();}
      }

      if(col != null) {
        try { col.close(); } catch(XMLDBException xe) {xe.printStackTrace();}
      }
    }
  }
View Full Code Here


        final String col = url.substring(0, url.lastIndexOf('/'));
        final String res = url.substring(url.lastIndexOf('/') + 1);

        try {
            Collection collection = DatabaseManager.getCollection(col, user, password);
            if (collection == null) {
                throw new ResourceNotFoundException("Document " + url + " not found");
            }

            XMLResource xmlResource = (XMLResource) collection.getResource(res);
            if (xmlResource == null) {
                throw new ResourceNotFoundException("Document " + url + " not found");
            }

            if (query != null) {
                // Query resource
                if (this.getLogger().isDebugEnabled()) {
                    this.getLogger().debug("Querying resource " + res + " from collection " + url + "; query= " + this.query);
                }

                queryToSAX(handler, collection, res);
            } else {
                // Return entire resource
                if (this.getLogger().isDebugEnabled()) {
                    this.getLogger().debug("Obtaining resource " + res + " from collection " + col);
                }

                xmlResource.getContentAsSAX(handler);
            }

            collection.close();
        } catch (XMLDBException xde) {
            String error = "Unable to fetch content. Error "
                     + xde.errorCode + ": " + xde.getMessage();
            throw new SAXException(error, xde);
        }
View Full Code Here

    private void collectionToSAX(ContentHandler handler) throws SAXException, ProcessingException {

        AttributesImpl attributes = new AttributesImpl();

        try {
            Collection collection = DatabaseManager.getCollection(url, user, password);
            if (collection == null) {
                throw new ResourceNotFoundException("Collection " + url +
                        " not found");
            }

            if (query != null) {
                // Query collection
                if (this.getLogger().isDebugEnabled()) {
                    this.getLogger().debug("Querying collection " + url + "; query= " + this.query);
                }

                queryToSAX(handler, collection, null);
            } else {
                // List collection
                if (this.getLogger().isDebugEnabled()) {
                    this.getLogger().debug("Listing collection " + url);
                }

                final String ncollections = Integer.toString(collection.getChildCollectionCount());
                final String nresources = Integer.toString(collection.getResourceCount());
                attributes.addAttribute("", RESOURCE_COUNT_ATTR,
                        RESOURCE_COUNT_ATTR, "CDATA", nresources);
                attributes.addAttribute("", COLLECTION_COUNT_ATTR,
                        COLLECTION_COUNT_ATTR, "CDATA", ncollections);
//                attributes.addAttribute("", COLLECTION_BASE_ATTR,
//                        COLLECTION_BASE_ATTR, "CDATA", url);

                handler.startDocument();
                handler.startPrefixMapping(PREFIX, URI);
                handler.startElement(URI, COLLECTIONS, QCOLLECTIONS, attributes);

                // Print child collections
                String[] collections = collection.listChildCollections();
                for (int i = 0; i < collections.length; i++) {
                    attributes.clear();
                    attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, collections[i]);
                    handler.startElement(URI, COLLECTION,
                            QCOLLECTION, attributes);
                    handler.endElement(URI, COLLECTION, COLLECTION);
                }

                // Print child resources
                String[] resources = collection.listResources();
                for (int i = 0; i < resources.length; i++) {
                    attributes.clear();
                    attributes.addAttribute("", NAME_ATTR, NAME_ATTR, CDATA, resources[i]);
                    handler.startElement(URI, RESOURCE,
                            QRESOURCE, attributes);
                    handler.endElement(URI, RESOURCE, RESOURCE);
                }

                handler.endElement(URI, COLLECTIONS, QCOLLECTIONS);
                handler.endPrefixMapping(PREFIX);
                handler.endDocument();
            }

            collection.close();
        } catch (XMLDBException xde) {
            String error = "Collection listing failed. Error " + xde.errorCode + ": " + xde.getMessage();
            throw new SAXException(error, xde);
        }
    }
View Full Code Here

*/

public class ListCollectionDocuments extends Command {
    public boolean execute(Hashtable table ) throws Exception {
   
        Collection col = null;
        String colstring = null;
        String[] documentarray = null;
       
        try {
   
            if (table.get(XMLTools.COLLECTION) != null) {
               
                // Create a collection instance
                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;
                }
                documentarray = col.listResources();
               
                System.out.println();
               
                for ( int  i=0 ; i < documentarray.length ; i++ )
                  {
                    System.out.println("\t" + documentarray[i] );
                  }

                System.out.println("\nTotal documents: " + documentarray.length);
            }
            else
                System.out.println("ERROR : Collection and switch required");
               
        } finally {
            // Release the collection object
            if ( col != null ) {
                col.close();
            }
        }

      return true;
   }
View Full Code Here

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

        Collection col = null;

        try {
            // Verify that the collection passed in is not null
            if (table.get(XMLTools.COLLECTION) != null && table.get(XMLTools.NAME_OF) != 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;
               }

                // Create a collection manager instance for the parent of the collection
                CollectionManager colman = (CollectionManager)col.getService("CollectionManager",XMLDBAPIVERSION);

                // Drop the collection
                colman.dropCollection( (String)table.get(XMLTools.NAME_OF) );

                System.out.println("Deleted: " + table.get(XMLTools.COLLECTION) + "/" + (String)table.get(XMLTools.NAME_OF) );
            }
            else
                System.out.println("Error : Collection Context and Name required");

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

        return true;
   }
View Full Code Here

*/

public class XPathQuery extends Command {

    public boolean execute(Hashtable table ) throws Exception {
        Collection col = null;
       
        try {
           
            if ( (String)table.get(XMLTools.COLLECTION) == null ) {
                System.out.println("ERROR : Collection name and switch required");
                return false;
            }

            if ( (String)table.get(XMLTools.QUERY) == "" ) {
                System.out.println("ERROR : Query and switch required");
                return false;
            }
           
           
            String colstring = normalizeCollectionURI( (String)table.get(XMLTools.COLLECTION),
                                                       (String) table.get(XMLTools.LOCAL) );
            String querystring = (String)table.get(XMLTools.QUERY);
            XPathQueryService service = null;
            ResourceIterator results = null;

            col = DatabaseManager.getCollection( colstring );
           
            if ( col == null ) {
               System.out.println("ERROR : Collection not found!");
               return false;
            }
           
            service = (XPathQueryService)col.getService("XPathQueryService","1.0");
            addNamespaces(service, (String)table.get("namespaces"));
           
            ResourceSet resultSet = service.query(querystring);
            results = resultSet.getIterator();
           
            while (results.hasMoreResources() ) {
                XMLResource resource = (XMLResource)results.nextResource();
                String documentstr = (String)resource.getContent();
                System.out.println(documentstr);
            }
           
        } catch (Exception e) {
            System.out.println("ERROR : " + e.getMessage() );
            if (table.get(XMLTools.VERBOSE).equals("true")) {
               if (log.isDebugEnabled()) {
                  log.debug("No message", e);
               }
            }
            return false;
           
        } finally {
            if ( col != null ) {
                col.close();
            }
        }
       
      return true;
   }
View Full Code Here

    * @param table Description of Parameter
    * @return Description of the Returned Value
    */
   public boolean execute(Hashtable table) throws Exception {

      Collection col = null;

      if (table.get(XMLTools.FILE_PATH).equals("")) {
         System.out.println("ERROR: Directory name and switch required");
         return false;
      }
View Full Code Here

public class ImportTree extends Command {
   Command addDocument = null;
   Command addCollection = null;
  
   public boolean execute(Hashtable table) throws Exception {
      Collection col = null;
      try {
         // Setup the creation commands we'll use to add documents and create
         // collections.
         addDocument =
            (Command) Class.forName("org.apache.xindice.tools.command.AddDocument").newInstance();
  
         addCollection =
            (Command) Class.forName("org.apache.xindice.tools.command.AddCollection").newInstance();
           
         String startcollection = null;

         // Verify that the correct command line parameters were passed in
         if (table.get(XMLTools.FILE_PATH) == null) {
            System.out.println("ERROR : Directory name and switch required");
            return false;
         }
         if (table.get(XMLTools.COLLECTION) == null) {
            System.out.println("ERROR : Collection name and switch required");
            return false;
         }
        
         startcollection = (String) table.get(XMLTools.COLLECTION);
        
         // Make sure the root collection exists
         String colstring = normalizeCollectionURI(startcollection,
                                                   (String) table.get(XMLTools.LOCAL));
         if ((col = DatabaseManager.getCollection(colstring)) == null) {
            System.out.println("ERROR : Collection not found!");
            return false;
         }

         File startdir = new File((String) table.get(XMLTools.FILE_PATH));

         // Make call to process, this is called recursively!
         process(startdir, startcollection, table);

      } finally {
         // Be sure to close collection objects
         if (col != null) {
            col.close();
         }
      }

      return true;
   }
View Full Code Here

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

      String[] colarray = null;
      String colstring = null;
      Collection col = null;
       
      try {
             
         if (table.get(XMLTools.COLLECTION) == null) {
            System.out.println("ERROR : Collection context required");
            return false;
         }

         // list the COLLECTIONURI + The collection passed in
         colstring = normalizeCollectionURI( (String)table.get(XMLTools.COLLECTION),
                                             (String) table.get(XMLTools.LOCAL) );


         // Get a Collection reference
         col = DatabaseManager.getCollection( colstring );
         if ( col == null ) {
            System.out.println("ERROR : Collection not found!");
            return false;
         } else {
            colarray = col.listChildCollections();
           
            System.out.println();
           
            for ( int i=0 ; i < colarray.length ; i++ ) {
                System.out.println("\t" + colarray[i] );
            }

            System.out.println("\nTotal collections: " + colarray.length);
         }
      } finally {
         // Close collection objects and release
         if ( col != null ) {
             col.close();
         }
      }
      return true;
   }
View Full Code Here

public class Shutdown extends Command {

   //public boolean execute(Hashtable table, Database db)
   public boolean execute(Hashtable table) throws Exception {

      Collection col = null;

      if (table.get(XMLTools.COLLECTION) == null) {
         System.out.println("ERROR : Collection context required ");
         return false;
      }

      try {

         // 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 );

         DatabaseInstanceManager man = (DatabaseInstanceManager)col.getService("DatabaseInstanceManager",XMLDBAPIVERSION);

         // Shutdown the server
         man.shutdown();

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

      return true;
   }
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.