kevinross    2003/07/10 14:36:18

  Modified:    java/examples/guide/src/org/apache/xindice/examples
                        RetrieveDocument.java AddDocument.java
                        DeleteDocument.java Example1.java XUpdate.java
                        CreateCollection.java
  Added:       java/examples/guide/src/org/apache/xindice/examples
                        AbstractExample.java ListCollections.java
  Log:
  more cleanup to make them easier to understand
  
  Revision  Changes    Path
  1.4       +10 -16    
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/RetrieveDocument.java
  
  Index: RetrieveDocument.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/RetrieveDocument.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- RetrieveDocument.java     5 Jun 2003 04:00:48 -0000       1.3
  +++ RetrieveDocument.java     10 Jul 2003 21:36:18 -0000      1.4
  @@ -58,31 +58,25 @@
    *
    * $Id$
    */
  -
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.XMLDBException;
   import org.xmldb.api.modules.XMLResource;
   
   /**
    * Simple XML:DB API Example to retrieve a document from the database.
    */
  -public class RetrieveDocument {
  +public class RetrieveDocument extends AbstractExample {
  +
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
   
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/addressbook");
  +                     collection = 
getCollection("xmldb:xindice:///db/addressbook");
   
  -                     XMLResource document = (XMLResource) 
col.getResource(args[0]);
  -                     if (document != null) {
  +                     XMLResource xmlResource = (XMLResource) 
collection.getResource(args[0]);
  +                     if (xmlResource != null) {
                                System.out.println("Document " + args[0]);
  -                             System.out.println(document.getContent());
  +                             System.out.println(xmlResource.getContent());
                        }
                        else {
                                System.out.println("Document not found");
  @@ -92,8 +86,8 @@
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  
  
  
  1.4       +10 -18    
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/AddDocument.java
  
  Index: AddDocument.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/AddDocument.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AddDocument.java  5 Jun 2003 04:00:48 -0000       1.3
  +++ AddDocument.java  10 Jul 2003 21:36:18 -0000      1.4
  @@ -62,39 +62,34 @@
   import java.io.File;
   import java.io.FileInputStream;
   
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.XMLDBException;
   import org.xmldb.api.modules.XMLResource;
   
   /**
    * Simple XML:DB API Example to insert a new document into the database.
    */
  -public class AddDocument {
  +public class AddDocument  extends AbstractExample {
  +     
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
  -
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/addressbook");
   
  +                     collection = 
getCollection("xmldb:xindice:///db/addressbook");
  +                     
                        String data = readFileFromDisk(args[0]);
   
  -                     XMLResource document = (XMLResource) 
col.createResource(null, "XMLResource");
  +                     XMLResource document = (XMLResource) 
collection.createResource(null, "XMLResource");
                        document.setContent(data);
  -                     col.storeResource(document);
  +                     collection.storeResource(document);
                        System.out.println("Document " + args[0] + " inserted 
as " + document.getId());
                }
                catch (XMLDBException e) {
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  @@ -102,12 +97,9 @@
        public static String readFileFromDisk(String fileName) throws Exception 
{
                File file = new File(fileName);
                FileInputStream insr = new FileInputStream(file);
  -
                byte[] fileBuffer = new byte[(int) file.length()];
  -
                insr.read(fileBuffer);
                insr.close();
  -
                return new String(fileBuffer);
        }
   }
  
  
  
  1.4       +8 -15     
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/DeleteDocument.java
  
  Index: DeleteDocument.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/DeleteDocument.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DeleteDocument.java       5 Jun 2003 04:00:48 -0000       1.3
  +++ DeleteDocument.java       10 Jul 2003 21:36:18 -0000      1.4
  @@ -58,37 +58,30 @@
    *
    * $Id$
    */
  -
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.Resource;
   import org.xmldb.api.base.XMLDBException;
   
   /**
    * Simple XML:DB API Example to delete a document from the database.
    */
  -public class DeleteDocument {
  +public class DeleteDocument extends AbstractExample {
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
   
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/addressbook");
  +                     collection = 
getCollection("xmldb:xindice:///db/addressbook");
   
  -                     Resource document = col.getResource(args[0]);
  -                     col.removeResource(document);
  +                     Resource document = collection.getResource(args[0]);
  +                     collection.removeResource(document);
                        System.out.println("Document " + args[0] + " removed");
                }
                catch (XMLDBException e) {
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  
  
  
  1.5       +13 -19    
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/Example1.java
  
  Index: Example1.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/Example1.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Example1.java     5 Jun 2003 04:00:48 -0000       1.4
  +++ Example1.java     10 Jul 2003 21:36:18 -0000      1.5
  @@ -58,10 +58,7 @@
    *
    * $Id$
    */
  -
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.Resource;
   import org.xmldb.api.base.ResourceIterator;
   import org.xmldb.api.base.ResourceSet;
  @@ -71,33 +68,30 @@
   /**
    * Simple XML:DB API example to query the database.
    */
  -public class Example1 {
  +public class Example1 extends AbstractExample {
  +     
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
   
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/addressbook");
  +                     collection = 
getCollection("xmldb:xindice:///db/addressbook");
   
                        String xpath = "//person[fname='John']";
  -                     XPathQueryService service = (XPathQueryService) 
col.getService("XPathQueryService", "1.0");
  -                     ResourceSet resultSet = service.query(xpath);
  -                     ResourceIterator results = resultSet.getIterator();
  +                     XPathQueryService service = (XPathQueryService) 
collection.getService("XPathQueryService", "1.0");
  +                     ResourceSet resourceSet = service.query(xpath);
  +                     ResourceIterator resourceIterator = 
resourceSet.getIterator();
   
  -                     while (results.hasMoreResources()) {
  -                             Resource res = results.nextResource();
  -                             System.out.println((String) res.getContent());
  +                     while (resourceIterator.hasMoreResources()) {
  +                             Resource resource = 
resourceIterator.nextResource();
  +                             System.out.println((String) 
resource.getContent());
                        }
                }
                catch (XMLDBException e) {
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  
  
  
  1.5       +8 -14     
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/XUpdate.java
  
  Index: XUpdate.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/XUpdate.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XUpdate.java      5 Jun 2003 04:00:48 -0000       1.4
  +++ XUpdate.java      10 Jul 2003 21:36:18 -0000      1.5
  @@ -58,26 +58,20 @@
    *
    * $Id$
    */
  -
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.XMLDBException;
   import org.xmldb.api.modules.XUpdateQueryService;
   
   /**
    * Simple XML:DB API example to update the database.
    */
  -public class XUpdate {
  +public class XUpdate  extends AbstractExample {
  +     
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
   
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/addressbook");
  +                     collection = 
getCollection("xmldb:xindice:///db/addressbook");
   
                        String xupdate =
                                "<xu:modifications version=\"1.0\""
  @@ -88,7 +82,7 @@
                                        + "   </xu:update>"
                                        + "</xu:modifications>";
   
  -                     XUpdateQueryService service = (XUpdateQueryService) 
col.getService("XUpdateQueryService", "1.0");
  +                     XUpdateQueryService service = (XUpdateQueryService) 
collection.getService("XUpdateQueryService", "1.0");
                        long count = service.update(xupdate);
                        System.out.println(count + " entries updated.");
                }
  @@ -96,8 +90,8 @@
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  
  
  
  1.4       +9 -14     
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/CreateCollection.java
  
  Index: CreateCollection.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/examples/guide/src/org/apache/xindice/examples/CreateCollection.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CreateCollection.java     5 Jun 2003 04:00:48 -0000       1.3
  +++ CreateCollection.java     10 Jul 2003 21:36:18 -0000      1.4
  @@ -58,30 +58,25 @@
    *
    * $Id$
    */
  -
   import org.apache.xindice.client.xmldb.services.CollectionManager;
   import org.apache.xindice.xml.dom.DOMParser;
  -import org.xmldb.api.DatabaseManager;
   import org.xmldb.api.base.Collection;
  -import org.xmldb.api.base.Database;
   import org.xmldb.api.base.XMLDBException;
   
   /**
    * Simple XML:DB API example to create a collection.
    */
  -public class CreateCollection {
  +public class CreateCollection  extends AbstractExample {
        public static void main(String[] args) throws Exception {
  -             Collection col = null;
  +             Collection collection = null;
                try {
  -                     String driver = 
"org.apache.xindice.client.xmldb.DatabaseImpl";
  -                     Class c = Class.forName(driver);
   
  -                     Database database = (Database) c.newInstance();
  -                     DatabaseManager.registerDatabase(database);
  -                     col = 
DatabaseManager.getCollection("xmldb:xindice:///db/");
  +                     collection = getCollection("xmldb:xindice:///db/");
  +//                   collection = 
getCollection("xmldb:xindice://localhost:8888/db/");
  +//                   collection = 
getCollection("xmldb:xindice-embed:///db/");
   
                        String collectionName = "mycollection";
  -                     CollectionManager service = (CollectionManager) 
col.getService("CollectionManager", "1.0");
  +                     CollectionManager service = (CollectionManager) 
collection.getService("CollectionManager", "1.0");
   
                        // Build up the Collection XML configuration.
                        String collectionConfig =
  @@ -99,8 +94,8 @@
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                }
                finally {
  -                     if (col != null) {
  -                             col.close();
  +                     if (collection != null) {
  +                             collection.close();
                        }
                }
        }
  
  
  
  1.1                  
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/AbstractExample.java
  
  Index: AbstractExample.java
  ===================================================================
  package org.apache.xindice.examples;
  
  /*
   * 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/>.
   *
   * $Id: AbstractExample.java,v 1.1 2003/07/10 21:36:18 kevinross Exp $
   */
  import org.apache.xindice.util.XindiceException;
  import org.xmldb.api.DatabaseManager;
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.Database;
  import org.xmldb.api.base.XMLDBException;
  
  /**
   * Simple XML:DB API Example 
   */
  public abstract class AbstractExample {
  
        private static boolean alreadyInitialized = false;
  
        protected static void ensureInitialized() throws 
ClassNotFoundException, InstantiationException, IllegalAccessException, 
XMLDBException {
  
                if (alreadyInitialized)
                        return;
  
                String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
                Class driverClass = Class.forName(driver);
                Database database = (Database) driverClass.newInstance();
                DatabaseManager.registerDatabase(database);
                alreadyInitialized = true;
        }
  
        protected static Collection getCollection(String collectionUri)
                throws ClassNotFoundException, InstantiationException, 
IllegalAccessException, XMLDBException, XindiceException {
  
                ensureInitialized();
                Collection collection = 
DatabaseManager.getCollection(collectionUri);
                if (collection == null) {
                        throw new 
XindiceException("DatabaseManager.getCollection(" + collectionUri + ") returned 
null.");
                }
  
                return collection;
        }
  }
  
  
  
  1.1                  
xml-xindice/java/examples/guide/src/org/apache/xindice/examples/ListCollections.java
  
  Index: ListCollections.java
  ===================================================================
  package org.apache.xindice.examples;
  
  /*
   * 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/>.
   *
   * $Id: ListCollections.java,v 1.1 2003/07/10 21:36:18 kevinross Exp $
   */
  
  import org.xmldb.api.base.Collection;
  import org.xmldb.api.base.XMLDBException;
  
  /**
   * Simple XML:DB API example to list collections.
   */
  public class ListCollections extends AbstractExample {
        
        public static void main(String[] args) throws Exception {
                Collection collection = null;
                try {
  
                        collection = 
getCollection("xmldb:xindice://localhost:8888/db/");
                        //collection = 
getCollection("xmldb:xindice-embed:///db/");
  
  
                        String[] childCollections = 
collection.listChildCollections();
                        System.out.println("Children of collection [" + 
collection.getName() + "]");
                        for (int i = 0; i < childCollections.length; i++) {
  
                                System.out.println("\t" + (i + 1) + ". " + 
childCollections[i]);
                        }
                }
                catch (XMLDBException e) {
                        System.err.println("XML:DB Exception occured " + 
e.errorCode + " " + e.getMessage());
                        e.printStackTrace();
                }               
                catch (Exception e) {
                        System.err.println("Exception occured " + e.toString());
                        e.printStackTrace();
                }               
                finally {
                        if (collection != null) {
                                collection.close();
                        }
                }
        }
  }
  
  
  

Reply via email to