o3documenthelper.java

来自「Java的面向对象数据库系统的源代码」· Java 代码 · 共 487 行 · 第 1/2 页

JAVA
487
字号
/* * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. * * This software is open source. * See the bottom of this file for the licence. * * $Id: O3DocumentHelper.java,v 1.3 2003/11/02 18:10:02 per_nyfelt Exp $ */package org.ozoneDB.xml.dom4j;import org.dom4j.*;import org.dom4j.io.SAXReader;import org.dom4j.rule.Pattern;import org.jaxen.VariableContext;import org.ozoneDB.OzoneInterface;import org.ozoneDB.OzoneRemote;import org.ozoneDB.xml.dom4j.io.DocumentBuilder;import org.ozoneDB.xml.dom4j.io.DocumentBuilderImpl;import org.ozoneDB.xml.dom4j.io.O3SAXReader;import org.ozoneDB.xml.dom4j.o3impl.OzoneDocumentFactoryImpl;import java.io.*;import java.net.Socket;import java.net.SocketAddress;import java.util.List;import java.util.Map;import java.util.StringTokenizer;/** <p><code>O3DocumentHelper</code> is a collection of helper methods * for using DOM4J.</p> * * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> * @author Per Nyfelt * @version $Revision: 1.3 $ */public class O3DocumentHelper {    private static OzoneDocumentFactory factory;    private static OzoneInterface db;    private static DocumentBuilder builder;    public static void configure(OzoneInterface db) throws Exception {        O3DocumentHelper.db = db;        factory = OzoneDocumentFactoryImpl.getInstance(db);    }    private static OzoneInterface getDb() throws RuntimeException {        if (db == null) {            throw new RuntimeException("You must run the configure method before using this helper");        }        return db;    }    public static OzoneDocumentFactory getFactory() throws RuntimeException {        if (factory == null) {            throw new RuntimeException("You must run the configure method before using this helper");        }        return factory;    }    // Static helper methods    public static Document fetchDocument(String name) {        try {            return (Document) getDb().objectForName(name);        } catch (Exception e) {            return null;        }    }    public static void deleteDocument(Document doc) {        if (doc instanceof OzoneRemote) {            getDb().deleteObject((OzoneRemote) doc);        } else {            throw new RuntimeException("The document is not an OzoneDocument");        }    }    public static Document createDocument() throws Exception {        return getFactory().createDocument();    }    public static Document createDocument(Element rootElement) throws Exception {        return getFactory().createDocument(rootElement);    }    /** creates a persistent Object with the given name */    public static Document createDocument(String name) throws Exception {        return getFactory().createDocument(name);    }    /** creates a persistent Object with the given name */    public static Document createDocument(Element rootElement, String name) throws Exception {        return getFactory().createDocument(rootElement, name);    }    public static Element createElement(QName qname) throws Exception {        return getFactory().createElement(qname);    }    public static Element createElement(String name) throws Exception {        return getFactory().createElement(name);    }    public static Attribute createAttribute(Element owner, QName qname, String value) throws Exception {        return getFactory().createAttribute(owner, qname, value);    }    public static Attribute createAttribute(Element owner, String name, String value) throws Exception {        return getFactory().createAttribute(owner, name, value);    }    public static CDATA createCDATA(String text) throws Exception {        return getFactory().createCDATA(text);    }    public static Comment createComment(String text) throws Exception {        return getFactory().createComment(text);    }    public static Text createText(String text) throws Exception {        return getFactory().createText(text);    }    public static Entity createEntity(String name, String text) throws Exception {        return getFactory().createEntity(name, text);    }    public static Namespace createNamespace(String prefix, String uri) throws Exception {        return getFactory().createNamespace(prefix, uri);    }    public static ProcessingInstruction createProcessingInstruction(String target, String data) throws Exception {        return getFactory().createProcessingInstruction(target, data);    }    public static ProcessingInstruction createProcessingInstruction(String target, Map data) throws Exception {        return getFactory().createProcessingInstruction(target, data);    }    public static QName createQName(String localName, Namespace namespace) throws Exception {        return getFactory().createQName(localName, namespace);    }    public static QName createQName(String localName) throws Exception {        return getFactory().createQName(localName);    }    /** <p><code>createXPath</code> parses an XPath expression     * and creates a new XPath <code>XPath</code> instance     * using the singleton {@link org.ozoneDB.xml.dom4j.OzoneDocumentFactory}.</p>     *     * @param xpathExpression is the XPath expression to create     * @return a new <code>XPath</code> instance     * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid     */    public static XPath createXPath(String xpathExpression) throws InvalidXPathException {        return getFactory().createXPath(xpathExpression);    }    /** <p><code>createXPath</code> parses an XPath expression     * and creates a new XPath <code>XPath</code> instance     * using the singleton {@link org.ozoneDB.xml.dom4j.OzoneDocumentFactory}.</p>     *     * @param xpathExpression is the XPath expression to create     * @param variableContext is the variable context to use when evaluating the XPath     * @return a new <code>XPath</code> instance     * @throws org.dom4j.InvalidXPathException if the XPath expression is invalid     */    public static XPath createXPath(String xpathExpression, VariableContext variableContext) throws InvalidXPathException, Exception {        return getFactory().createXPath(xpathExpression, variableContext);    }    /** <p><code>createXPathFilter</code> parses a NodeFilter     * from the given XPath filter expression using the singleton     * {@link org.ozoneDB.xml.dom4j.OzoneDocumentFactory}.     * XPath filter expressions occur within XPath expressions such as     * <code>self::node()[ filterExpression ]</code></p>     *     * @param xpathFilterExpression is the XPath filter expression     * to create     * @return a new <code>NodeFilter</code> instance     */    public static NodeFilter createXPathFilter(String xpathFilterExpression) throws Exception {        return getFactory().createXPathFilter(xpathFilterExpression);    }    /** <p><code>createPattern</code> parses the given     * XPath expression to create an XSLT style {@link org.dom4j.rule.Pattern} instance     * which can then be used in an XSLT processing model.</p>     *     * @param xpathPattern is the XPath pattern expression     * to create     * @return a new <code>Pattern</code> instance     */    public static Pattern createPattern(String xpathPattern) throws Exception {        return getFactory().createPattern(xpathPattern);    }    /** <p><code>selectNodes</code> performs the given XPath     * expression on the {@link java.util.List} of {@link org.dom4j.Node} instances appending     * all the results together into a single list.</p>     *     * @param xpathFilterExpression is the XPath filter expression     * to evaluate     * @param nodes is the list of nodes on which to evalute the XPath     * @return the results of all the XPath evaluations as a single list     */    public static List selectNodes(String xpathFilterExpression, List nodes) throws Exception {        XPath xpath = createXPath(xpathFilterExpression);        return xpath.selectNodes(nodes);    }    /** <p><code>selectNodes</code> performs the given XPath     * expression on the {@link java.util.List} of {@link org.dom4j.Node} instances appending     * all the results together into a single list.</p>     *     * @param xpathFilterExpression is the XPath filter expression     * to evaluate     * @param node is the Node on which to evalute the XPath     * @return the results of all the XPath evaluations as a single list     */    public static List selectNodes(String xpathFilterExpression, Node node) throws Exception {        XPath xpath = createXPath(xpathFilterExpression);        return xpath.selectNodes(node);    }    /** <p><code>sort</code> sorts the given List of Nodes     * using an XPath expression as a {@link java.util.Comparator}.     *     * @param list is the list of Nodes to sort     * @param xpathExpression is the XPath expression used for comparison     */    public static void sort(List list, String xpathExpression) throws Exception {        XPath xpath = createXPath(xpathExpression);        xpath.sort(list);    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?