jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r160448 - incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java
Date Thu, 07 Apr 2005 20:02:24 GMT
Author: jukka
Date: Thu Apr  7 13:02:21 2005
New Revision: 160448

URL: http://svn.apache.org/viewcvs?view=rev&rev=160448
Log:
JCR-54: Added DOM utility classes to help JDOM removal.

Added:
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java
    incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java?view=auto&rev=160448
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java (added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMBuilder.java Thu
Apr  7 13:02:21 2005
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.util;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Document builder class. This class provides an intuitive
+ * interface for incrementally building DOM documents.
+ */
+public final class DOMBuilder {
+
+    /** Static factory for creating DOM DocumentBuilder instances. */
+    private static final DocumentBuilderFactory BUILDER_FACTORY =
+        DocumentBuilderFactory.newInstance();
+
+    /** Static factory for creating document to output stream transformers. */
+    private static final TransformerFactory TRANSFORMER_FACTORY =
+        TransformerFactory.newInstance();
+
+    /** The DOM document being built by this builder. */
+    private final Document document;
+
+    /** The current element. */
+    private Element current;
+
+    /**
+     * Creates a builder for a new DOM document. A new DOM document is
+     * instantiated and initialized to contain a root element with the given
+     * name. The root element is set as the current element of this builder.
+     *
+     * @param name name of the root element
+     * @throws ParserConfigurationException if a document cannot be created
+     */
+    public DOMBuilder(String name) throws ParserConfigurationException  {
+        DocumentBuilder builder = BUILDER_FACTORY.newDocumentBuilder();
+        document = builder.newDocument();
+        current = document.createElement(name);
+        document.appendChild(current);
+    }
+
+
+    /**
+     * Writes the document built by this builder into the given output stream.
+     * This method is normally invoked only when the document is fully built.
+     *
+     * @param xml XML output stream
+     * @throws IOException if the document could not be written
+     */
+    public void write(OutputStream xml) throws IOException {
+        try {
+            Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
+            transformer.transform(
+                    new DOMSource(document), new StreamResult(xml));
+        } catch (TransformerConfigurationException e) {
+            throw new IOException(e.getMessage());
+        } catch (TransformerException e) {
+            throw new IOException(e.getMessage());
+        }
+    }
+
+    /**
+     * Creates a new element with the given name as the child of the
+     * current element and makes the created element current. The
+     * {@link #endElement() endElement} method needs to be called
+     * to return back to the original element.
+     *
+     * @param name name of the new element
+     */
+    public void startElement(String name) {
+        Element element = document.createElement(name);
+        current.appendChild(element);
+        current = element;
+    }
+
+    /**
+     * Makes the parent element current. This method should be invoked
+     * after a child element created with the
+     * {@link #startElement(String) startElement} method has been fully
+     * built.
+     */
+    public void endElement() {
+        current = (Element) current.getParentNode();
+    }
+
+    /**
+     * Sets the named attribute of the current element.
+     *
+     * @param name attribute name
+     * @param value attribute value
+     */
+    public void setAttribute(String name, String value) {
+        current.setAttribute(name, value);
+    }
+
+    /**
+     * Sets the named boolean attribute of the current element.
+     *
+     * @param name attribute name
+     * @param value boolean attribute value
+     */
+    public void setAttribute(String name, boolean value) {
+        setAttribute(name, String.valueOf(value));
+    }
+
+    /**
+     * Adds the given string as text content to the current element.
+     *
+     * @param content text content
+     */
+    public void addContent(String content) {
+        current.appendChild(document.createTextNode(content));
+    }
+
+    /**
+     * Adds a new child element with the given name and text content.
+     * The created element will contain no attributes and no child elements
+     * of its own.
+     *
+     * @param name child element name
+     * @param content child element content
+     */
+    public void addContentElement(String name, String content) {
+        startElement(name);
+        addContent(content);
+        endElement();
+    }
+
+}

Added: incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java
URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java?view=auto&rev=160448
==============================================================================
--- incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java (added)
+++ incubator/jackrabbit/trunk/src/java/org/apache/jackrabbit/core/util/DOMWalker.java Thu
Apr  7 13:02:21 2005
@@ -0,0 +1,214 @@
+/*
+ * Copyright 2004-2005 The Apache Software Foundation or its licensors,
+ *                     as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.CharacterData;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+/**
+ * Document walker class. This class provides an intuitive
+ * interface for traversing a parsed DOM document.
+ */
+public final class DOMWalker {
+
+    /** Static factory for creating stream to DOM transformers. */
+    private static final TransformerFactory factory =
+        TransformerFactory.newInstance();
+
+    /** The DOM document being traversed by this walker. */
+    private final Document document;
+
+    /** The current element. */
+    private Element current;
+
+    /**
+     * Creates a walker for traversing a DOM document read from the given
+     * input stream. The root element of the document is set as the current
+     * element.
+     *
+     * @param xml XML input stream
+     * @throws IOException if a document cannot be read from the stream
+     */
+    public DOMWalker(InputStream xml) throws IOException {
+        try {
+            DOMResult result = new DOMResult();
+            Transformer transformer = factory.newTransformer();
+            transformer.transform(new StreamSource(xml), result);
+            document = (Document) result.getNode();
+            current = document.getDocumentElement();
+        } catch (TransformerException e) {
+            throw new IOException(e.getMessage());
+        }
+    }
+
+    /**
+     * Returns the namespace mappings defined in the current element.
+     * The returned property set contains the prefix to namespace
+     * mappings specified by the <code>xmlns</code> attributes of the
+     * current element.
+     *
+     * @return prefix to namespace mappings of the current element
+     */
+    public Properties getNamespaces() {
+        Properties namespaces = new Properties();
+        NamedNodeMap attributes = current.getAttributes();
+        for (int i = 0; i < attributes.getLength(); i++) {
+            Attr attribute = (Attr) attributes.item(i);
+            if (attribute.getName().startsWith("xmlns:")) {
+                namespaces.setProperty(
+                        attribute.getName().substring(6), attribute.getValue());
+            }
+        }
+        return namespaces;
+    }
+
+    /**
+     * Returns the name of the current element.
+     *
+     * @return element name
+     */
+    public String getName() {
+        return current.getNodeName();
+    }
+
+    /**
+     * Returns the value of the named attribute of the current element.
+     *
+     * @param name attribute name
+     * @return attribute value, or <code>null</code> if not found
+     */
+    public String getAttribute(String name) {
+        return current.getAttribute(name);
+    }
+
+    /**
+     * Returns the text content of the current element.
+     *
+     * @return text content
+     */
+    public String getContent() {
+        StringBuffer content = new StringBuffer();
+
+        NodeList nodes = current.getChildNodes();
+        for (int i = 0; i < nodes.getLength(); i++) {
+            Node node = nodes.item(i);
+            if (node.getNodeType() == Node.TEXT_NODE) {
+                content.append(((CharacterData) node).getData());
+            }
+        }
+
+        return content.toString();
+    }
+
+    /**
+     * Enters the named child element. If the named child element is
+     * found, then it is made the current element and <code>true</code>
+     * is returned. Otherwise the current element is not changed and
+     * <code>false</code> is returned.
+     * <p>
+     * The standard call sequence for this method is show below.
+     * <pre>
+     *     DOMWalker walker = ...;
+     *     if (walker.enterElement("...")) {
+     *         ...;
+     *         walker.leaveElement();
+     *     }
+     * </pre>
+     *
+     * @param name child element name
+     * @return <code>true</code> if the element was entered,
+     *         <code>false</code> otherwise
+     */
+    public boolean enterElement(String name) {
+        NodeList children = current.getChildNodes();
+        for (int i = 0; i < children.getLength(); i++) {
+            Node child = children.item(i);
+            if (child.getNodeType() == Node.ELEMENT_NODE
+                    && name.equals(child.getNodeName())) {
+                current = (Element) child;
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Leaves the current element. The parent element is set as the new
+     * current element.
+     *
+     * @see #enterElement(String)
+     */
+    public void leaveElement() {
+        current = (Element) current.getParentNode();
+    }
+
+    /**
+     * Iterates through the named child elements over multiple calls.
+     * This method makes it possible to use the following code to
+     * walk through all the child elements with the given name.
+     * <pre>
+     *     DOMWalker walker = ...;
+     *     while (walker.iterateElements("...")) {
+     *         ...;
+     *     }
+     * </pre>
+     *
+     * @param name name of the iterated elements
+     * @return <code>true</code> if another iterated element was entered, or
+     *         <code>false</code> if no more iterated elements were found
+     *         and the original element is restored as the current element
+     */
+    public boolean iterateElements(String name) {
+        Node next;
+        if (name.equals(current.getNodeName())) {
+            next = current.getNextSibling();
+        } else {
+            next = current.getFirstChild();
+        }
+
+        while (next != null) {
+            if (next.getNodeType() == Node.ELEMENT_NODE
+                    && name.equals(next.getNodeName())) {
+                current = (Element) next;
+                return true;
+            } else {
+                next = next.getNextSibling();
+            }
+        }
+
+        if (name.equals(current.getNodeName())) {
+            current = (Element) current.getParentNode();
+        }
+        return false;
+    }
+
+}



Mime
View raw message