[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: xml store



Hi,

Mon, 03 Jan 2000 Dmitry Melekhov wrote:
>Hello!
>
>I just download ozone and want to know is it possible
>to use ozone for store xml documents and then do queries.
>I see in samples only call
>pDoc = (Document) database.objectForName (docName);
>
>Is it possible to find documents with some value
>of tag? I.e. something like this:
>Enumeration documents = (Enumeration)
>database.searchXML("email","dm@aspec.ru");

Samples included in the current distribution contain only
code to work with (e.g. query) single and known(!) Documents.
I´ve attached a new sample file which shows how to query an 
Document with the help of XPath from the Xalan-Package.

For tasks like yours we have started the "ozone XML Repository
Project". The aim is to build an API which allows the users
to query a set of different XML Documents "with just one xpath
query string". You may take a look at the mail archive to reread
the "XML Repository" mails.

>
>Sorry, I am not experienced java programmer, so it is
>hard to me to look into sources :(
>
>Dmitry Melekhov
>http://www.aspec.ru/~dm
>2:5050/11.23@fidonet

Regards,
Lars
--
________________________________________________________________
Lars Martin                         mailto:lars@softwarebuero.de
softwarebuero m&b (SMB)              http://www.softwarebuero.de
// Copyright 1997-1999 by softwarebuero m&b (SMB). All rights reserved.
//
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by softwarebuero m&b (SMB).
//
// $Id: Query.java,v 1.2 1999/12/29 17:11:44 lars Exp $

import java.io.*;
import java.util.*;
import org.w3c.dom.*;
import org.ozoneDB.*;
import org.ozoneDB.xml.dom.*;
import org.ozoneDB.xml.util.*;
import org.apache.xalan.xpath.*;

/** Simple sample code to show how to run an XPath Query on an Persistent Document
from the API.
@version $Revision: 1.2 $ $Date: 1999/12/29 17:11:44 $
@author <a href="/ozone-users/01-2000/http://www.softwarebuero.de">SMB</a>
*/
public class Query {

    public static void main (String args[]) {

        RemoteDatabase  database = new RemoteDatabase();
        Document        pDoc;
        String          docName = "test.xml";
        String          qString = "/";

        if (args.length > 0)
            docName = args[0];
        if (args.length > 1)
            qString = args[1];
        if (args.length > 2) {
            printUsage ();
            System.exit (0);
            }

        System.out.println ("Querying file: " + docName);
        System.out.println ("Query: " + qString);

        try  {
            // open the connection on localhost at port 3333
            database.open ("localhost", 3333);

            // reload our database classes if we changed them
            database.reloadClasses();

            // try to find the stored (persistent) DOM
            pDoc = (Document) database.objectForName (docName);
    
            if (pDoc != null) {

                XPathQuery xPQuery = (XPathQuery) database.createObject (XPathQueryImpl.class.getName(), 0, null);

                long start = System.currentTimeMillis ();
                XObject result = (XObject) xPQuery.execute (qString, pDoc);
                System.out.println ("Time to query Document (cold): " + (System.currentTimeMillis()-start) + "ms");

                // printQueryResult (result);

                start = System.currentTimeMillis ();
                result = (XObject) xPQuery.execute (qString, pDoc);
                System.out.println ("Time to query Document (warm): " + (System.currentTimeMillis()-start) + "ms");                

                // delete the Query object
                database.deleteObject ((OzoneRemote)xPQuery);

                }
            else {
                System.out.println ("Document \"" + docName + "\" does not exist in DataBase!");
                }
            }
        catch (Exception except) {
            except.printStackTrace ();
            }
        }

    /** Prints a short hierarchical summary of the specified node and all sub nodes.
    */
    static void list(Node node, StringBuffer indent) {
        System.out.print (indent.toString());
        System.out.println ( "Node name [" + node.getNodeName () + "] value [" + node.getNodeValue () + "]" );
        Node _child = (Node) node.getFirstChild ();
        for (; _child != null; _child = (Node) _child.getNextSibling ()) {
            indent.append ("   ");
            list (_child, indent);
            indent.setLength (indent.length() - 3);
            }
        }

    /** Print the Result Object in dependence of the Result-Type.
    */
    static void printQueryResult (XObject result) {
        if (result != null) {
            int _resultType = result.getType ();
            System.out.print ("Query-Result ");
            try {
                // cast the query result to 
                switch (_resultType) {
                    case XObject.CLASS_BOOLEAN:
                        System.out.println ("(Boolean): " + result.bool ());
                        break;
                    case XObject.CLASS_NUMBER:
                        System.out.println ("(Number): " + result.num ());
                        break;
                    case XObject.CLASS_STRING:
                        System.out.println ("(String): " + result.str ());
                        break;
                    case XObject.CLASS_NODESET:
                        NodeList _nodeList = result.nodeset ();
                        int _length = _nodeList.getLength ();
                        System.out.println ("(NodeList): " + _length + " Entries");
                        for (int i= 0; i<_length; i++) {
                            System.out.println ((i+1) + ". Entry:");
                            list (_nodeList.item (i), new StringBuffer(""));
                            }
                        break;
                    case XObject.CLASS_RTREEFRAG:
                        System.out.println ("(DocumentFragment): -");
                        break;
                    default:
                        System.out.println ("(Unknown): -");
                        break;
                    }
                }
            catch (Exception except) {
                except.printStackTrace ();
                }
            }
        else {
            System.out.println ("Query-Result was null!");
            }
        }

    /** Print the Useage of this sample.
    */
    static void printUsage () {
        System.out.println ("Useage:");
        System.out.println ("   java Query [<xml-file>] [<xpath-query>]");
        System.out.println ("\nAvailable Arguments:");
        System.out.println ("   <xml-file> The name of the XML-File to apply the XPath query.");
        System.out.println ("              Default: 'test.xml'");
        System.out.println ("   <xpath-query> The query to select parts of the document.");
        System.out.println ("              Default: '/'");
        }
    }
// Copyright 1997-1999 by softwarebuero m&b (SMB). All rights reserved.
//
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by softwarebuero m&b (SMB).
//
// $Id: XPathQuery.java,v 1.6 1999/12/28 14:55:54 lars Exp $

package org.ozoneDB.xml.util;

import org.w3c.dom.*;
import org.ozoneDB.*;
import org.apache.xalan.xpath.*;

/** The XPathQuery is an small interface to access the Xalan-XPath-Implementation
from within the ozone DatBase.
@version $Revision: 1.6 $ $Date: 1999/12/28 14:55:54 $
@author <a href="/ozone-users/01-2000/http://www.softwarebuero.de">SMB</a>
@see org.ozoneDB.xml.util.XPathQueryImpl
*/
public interface XPathQuery extends OzoneRemote {

    /** Executes the specified Query on the specified persistent Document.
    */
    public XObject execute (String qString, Document pDoc) 
                            throws org.xml.sax.SAXException;

    }

// Copyright 1997-1999 by softwarebuero m&b (SMB). All rights reserved.
//
// You can redistribute this software and/or modify it under the terms of
// the Ozone Library License version 1 published by softwarebuero m&b (SMB).
//
// $Id: XPathQueryImpl.java,v 1.7 1999/12/28 14:55:54 lars Exp $

package org.ozoneDB.xml.util;

import org.ozoneDB.*;
import org.ozoneDB.xml.dom.*;
import org.w3c.dom.*;
import org.apache.xalan.xpath.*;

/** The XPathQueryImpl class implements a small Wrapper-Class to access the
Xalan-XPath-Implementation from within ozone. To afford the maximum speed
this class implements the OzoneObject-Interface.
@version $Revision: 1.7 $ $Date: 1999/12/28 14:55:54 $
@author <a href="/ozone-users/01-2000/http://www.softwarebuero.de">SMB</a>
*/
public class XPathQueryImpl extends OzoneObject
                            implements XPathQuery {

    transient protected XPathSupportDefault xpathSupport;
    transient protected XPathProcessorImpl parser;
    transient protected XPath xpath;

    /** Constructor.
    The constructur initializes some basic but internal variables needed by XPath.
    */
    public XPathQueryImpl () {
        // Since we don't have a XML Parser involved here, install some
        // default support for things like namespaces, etc.
        xpathSupport = new XPathSupportDefault ();

        // Create the XPath object.
        xpath = new XPath (xpathSupport, null);

        // Create a XPath parser.
        parser = new XPathProcessorImpl(xpathSupport);

        // Since raw XPath doesn't have a document function, you have to 
        // install one.  The same will be true of a few other functions 
        // that are currently built-in to XPath.
        xpath.installFunction("document", new org.apache.xalan.xslt.FuncDocument());
        }

    /** Executes the specified Query on the specified persistent Document.
    @param qString The XPath-Query wich is to apply.
    @param pDoc The persistent Document on which the Query should be applied.
    @return The resulting XObject containing the NodeList.
    @throw org.xml.sax.SAX.Exception If some error occur while executing the
    XPath-Query.
    */
    public XObject execute (String qString, Document pDoc) 
                            throws org.xml.sax.SAXException {

        // FIXME: if this is to time-consuming, an explicit "init" function could
        // save some milliseconds!
        // parse the specified Query-String and build an Parse-Tree
        parser.initXPath (xpath, qString, null);

        // execute the XPath query on the specified Document
        XObject result = xpath.execute (xpathSupport, (Node)pDoc.getDocumentElement(), null);

        return result;
        }

    }