cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject cvs commit: xml-cocoon2/src/org/apache/cocoon/components/source XMLDBSource.java
Date Mon, 24 Dec 2001 20:24:21 GMT
vgritsenko    01/12/24 12:24:21

  Modified:    src/org/apache/cocoon/components/source XMLDBSource.java
  Log:
  - change URL query syntax to use #: xmldb:///path/to/collection/#/xpath
  - add support to query single resources, not only collections
  
  Revision  Changes    Path
  1.5       +89 -75    xml-cocoon2/src/org/apache/cocoon/components/source/XMLDBSource.java
  
  Index: XMLDBSource.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/components/source/XMLDBSource.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- XMLDBSource.java	2001/12/22 19:03:04	1.4
  +++ XMLDBSource.java	2001/12/24 20:24:21	1.5
  @@ -21,11 +21,11 @@
   import org.apache.log.Logger;
   
   import org.xmldb.api.DatabaseManager;
  -import org.xmldb.api.base.Collection;   
  -import org.xmldb.api.base.Database;   
  -import org.xmldb.api.base.ErrorCodes;   
  -import org.xmldb.api.base.Resource;   
  -import org.xmldb.api.base.Service;   
  +import org.xmldb.api.base.Collection;
  +import org.xmldb.api.base.Database;
  +import org.xmldb.api.base.ErrorCodes;
  +import org.xmldb.api.base.Resource;
  +import org.xmldb.api.base.Service;
   import org.xmldb.api.base.XMLDBException;
   import org.xmldb.api.base.ResourceSet;
   import org.xmldb.api.base.ResourceIterator;
  @@ -50,7 +50,7 @@
    *
    * @author <a href="mailto:gianugo@apache.org">Gianugo Rabellino</a>
    * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
  - * @version $Id: XMLDBSource.java,v 1.4 2001/12/22 19:03:04 gianugo Exp $
  + * @version $Id: XMLDBSource.java,v 1.5 2001/12/24 20:24:21 vgritsenko Exp $
    */
   
   public class XMLDBSource extends AbstractSAXSource {
  @@ -67,8 +67,8 @@
       /** The requested URL */
       protected String url;
   
  -    /** The URL parameters */
  -    protected String urlParameters = null;
  +    /** The part of URL after # sign */
  +    protected String query = null;
   
       /** The System ID */
       protected String systemId;
  @@ -111,7 +111,7 @@
   
       /**
        * The constructor.
  -     * 
  +     *
        * @param environment the Cocoon Environment.
        * @param url the URL being queried.
        * @param driver the XML:DB driver class name.
  @@ -128,9 +128,9 @@
   
           this.driver = driver;
   
  -        if ((start = url.indexOf('?')) != -1) {
  +        if ((start = url.indexOf('#')) != -1) {
               this.url = url.substring(0, start);
  -            this.urlParameters = url.substring(start + 1);
  +            this.query = url.substring(start + 1);
           } else {
               this.url = url;
           }
  @@ -174,7 +174,7 @@
   
       /**
        * Stream SAX events to a given ContentHandler. If the requested
  -     * resource is a collection, build an XML view of it. 
  +     * resource is a collection, build an XML view of it.
        *
        */
   
  @@ -197,38 +197,41 @@
       }
   
       private void resourceToSAX(ContentHandler handler) throws SAXException, ProcessingException
{
  -
  -        Collection collection;
  -        XMLResource xmlResource;
   
  -        String col = url.substring(0, url.lastIndexOf('/'));
  -        String res = url.substring(url.lastIndexOf('/') + 1);
  +        final String col = url.substring(0, url.lastIndexOf('/'));
  +        final String res = url.substring(url.lastIndexOf('/') + 1);
   
           try {
  -            collection = DatabaseManager.getCollection(col);
  +            Collection collection = DatabaseManager.getCollection(col);
               if (collection == null) {
                   throw new ResourceNotFoundException("Document " + url + " not found");
               }
   
  -            xmlResource = (XMLResource) collection.getResource(res);
  +            XMLResource xmlResource = (XMLResource) collection.getResource(res);
               if (xmlResource == null) {
                   throw new ResourceNotFoundException("Document " + url + " not found");
               }
  +
  +            if (query != null) {
  +                // Query resource
  +                if (log.isDebugEnabled()) {
  +                    this.log.debug("Querying resource " + res + " from collection " + url
+ "; query= " + this.query);
  +                }
   
  -            xmlResource.getContentAsSAX(handler);
  +                queryToSAX(handler, collection, res);
  +            } else {
  +                // Return entire resource
  +                if (log.isDebugEnabled()) {
  +                    this.log.debug("Obtaining resource " + res + " from collection " +
col);
  +                }
  +
  +                xmlResource.getContentAsSAX(handler);
  +            }
  +
               collection.close();
           } catch (XMLDBException xde) {
  -
               throw new ProcessingException("Unable to fetch content: " +
                       xde.getMessage(), xde);
  -
  -        } catch (NullPointerException npe) {
  -
  -            this.log.error("The XML:DB driver raised an exception");
  -            this.log.error("probably the document was not found");
  -
  -            throw new ProcessingException("Null pointer exception while " +
  -                    "retrieving document : " + npe.getMessage());
           }
       }
   
  @@ -243,63 +246,75 @@
                           " not found");
               }
   
  -            if (urlParameters != null) {
  -                queryToSAX(handler, collection);
  -                return;
  -            }
  +            if (query != null) {
  +                // Query collection
  +                if (log.isDebugEnabled()) {
  +                    this.log.debug("Querying collection " + url + "; query= " + this.query);
  +                }
   
  -            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);
  +                queryToSAX(handler, collection, null);
  +            } else {
  +                // List collection
  +                if (log.isDebugEnabled()) {
  +                    this.log.debug("Listing collection " + url);
  +                }
   
  -            handler.startDocument();
  -            handler.startPrefixMapping(PREFIX, URI);
  -            handler.startElement(URI, COLLECTIONS, QCOLLECTIONS, attributes);
  +                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 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);
  +                }
   
  -            // 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();
               }
   
  -            handler.endElement(URI, COLLECTIONS, QCOLLECTIONS);
  -            handler.endPrefixMapping(PREFIX);
  -            handler.endDocument();
  +            collection.close();
           } catch (XMLDBException xde) {
               this.log.error("Collection listing failed. " + xde.getMessage());
               throw new SAXException("Collection listing failed. " + xde.getMessage());
           }
       }
   
  -    public void queryToSAX(ContentHandler handler, Collection collection) throws SAXException
{
  +    private void queryToSAX(ContentHandler handler, Collection collection, String resource)
throws SAXException {
   
           AttributesImpl attributes = new AttributesImpl();
   
           try {
               XPathQueryService service =
                   (XPathQueryService) collection.getService("XPathQueryService", "1.0");
  -            ResourceSet resultSet = service.query(urlParameters);
  +            ResourceSet resultSet = (resource == null) ?
  +                    service.query(query) : service.queryResource(resource, query);
   
  -            attributes.addAttribute("", QUERY_ATTR, QUERY_ATTR, "CDATA", urlParameters);
  +            attributes.addAttribute("", QUERY_ATTR, QUERY_ATTR, "CDATA", query);
               attributes.addAttribute("", RESULTS_COUNT_ATTR,
                       RESULTS_COUNT_ATTR, "CDATA", Long.toString(resultSet.getSize()));
   
  @@ -312,10 +327,10 @@
               // Print search results
               ResourceIterator results = resultSet.getIterator();
               while (results.hasMoreResources()) {
  -                XMLResource resource = (XMLResource)results.nextResource();
  +                XMLResource result = (XMLResource)results.nextResource();
   
  -                final String id = resource.getId();
  -                final String documentId = resource.getDocumentId();
  +                final String id = result.getId();
  +                final String documentId = result.getDocumentId();
   
                   attributes.clear();
                   if (id != null) {
  @@ -328,7 +343,7 @@
                   }
                   handler.startElement(URI, RESULT, QRESULT, attributes);
   
  -                resource.getContentAsSAX(includeHandler);
  +                result.getContentAsSAX(includeHandler);
   
                   handler.endElement(URI, RESULT, RESULT);
               }
  @@ -337,8 +352,8 @@
               handler.endPrefixMapping(PREFIX);
               handler.endDocument();
           } catch (XMLDBException xde) {
  -            this.log.error("Collection query failed. " + xde.getMessage());
  -            throw new SAXException("Collection query failed. " + xde.getMessage());
  +            this.log.error("Query failed. " + xde.getMessage());
  +            throw new SAXException("Query failed. " + xde.getMessage());
           }
       }
   
  @@ -347,11 +362,10 @@
           this.log = null;
           this.manager = null;
           this.url = null;
  -        this.urlParameters = null;
  +        this.query = null;
       }
   
       public String getSystemId() {
           return url;
       }
  -
   }
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org


Mime
View raw message