You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by ed...@apache.org on 2003/09/17 20:58:23 UTC
cvs commit: cocoon-lenya/src/java/org/apache/lenya/xml NamespaceHelper.java DocumentHelper.java
edith 2003/09/17 11:58:23
Modified: src/java/org/apache/lenya/xml NamespaceHelper.java
DocumentHelper.java
Log:
method getNextSiblings added
Revision Changes Path
1.15 +258 -245 cocoon-lenya/src/java/org/apache/lenya/xml/NamespaceHelper.java
Index: NamespaceHelper.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/xml/NamespaceHelper.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- NamespaceHelper.java 6 Aug 2003 08:29:07 -0000 1.14
+++ NamespaceHelper.java 17 Sep 2003 18:58:23 -0000 1.15
@@ -1,245 +1,258 @@
-/*
-$Id$
-<License>
-
- ============================================================================
- The Apache Software License, Version 1.1
- ============================================================================
-
- Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modifica-
- tion, 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 "Apache Lenya" 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
- apache@apache.org.
-
- 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 (INCLU-
- DING, 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 created by
- Michael Wechner <mi...@apache.org>. For more information on the Apache Soft-
- ware Foundation, please see <http://www.apache.org/>.
-
- Lenya includes software developed by the Apache Software Foundation, W3C,
- DOM4J Project, BitfluxEditor, Xopus, and WebSHPINX.
-</License>
-*/
-package org.apache.lenya.xml;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Text;
-
-import javax.xml.parsers.ParserConfigurationException;
-
-
-/**
- * A NamespaceHelper object simplifies the creation of elements in a certain
- * namespace. All elements are owned by a document that is passed to the
- * {@link #NamespaceHelper(Document, String, String)} constructor or created
- * using the {@link #NamespaceHelper(String, String, String)} constructor.
- *
- * @author andreas
- */
-public class NamespaceHelper {
- private String namespaceUri;
- private String prefix;
- private Document document;
-
- /**
- * Creates a new instance of NamespaceHelper using an existing document. The
- * document is not affected. If you omit the prefix, the default namespace is used.
- *
- * @param document The document.
- * @param namespaceUri The namespace URI.
- * @param prefix The namespace prefix.
- */
- public NamespaceHelper(String namespaceUri, String prefix, Document document) {
- this.namespaceUri = namespaceUri;
- this.prefix = prefix;
- this.document = document;
- }
-
- /**
- * <p>
- * Creates a new instance of NamespaceHelper. A new document is created
- * using a document element in the given namespace with the given prefix.
- * If you omit the prefix, the default namespace is used.
- * </p>
- * <p>
- * NamespaceHelper("http://www.w3.org/2000/svg", "svg", "svg"):<br/>
- * <?xml version="1.0"><br/>
- * <svg:svg xmlns:svg="http://www.w3.org/2000/svg"><br/>
- * </svg:svg>
- * </p>
- *
- * @param localName The local name of the document element.
- * @param namespaceUri The namespace URI.
- * @param prefix The namespace prefix.
- *
- * @throws ParserConfigurationException if an error occured
- */
- public NamespaceHelper(String namespaceUri, String prefix, String localName)
- throws ParserConfigurationException {
- this.namespaceUri = namespaceUri;
- this.prefix = prefix;
- setDocument(DocumentHelper.createDocument(getNamespaceURI(), getQualifiedName(localName),
- null));
- }
-
- /**
- * Sets the document of this NamespaceHelper.
- *
- * @param document the document
- */
- protected void setDocument(Document document) {
- this.document = document;
- }
-
- /**
- * Returns the document that is used to create elements.
- *
- * @return A document object.
- */
- public Document getDocument() {
- return document;
- }
-
- /**
- * Returns the namespace URI of this NamespaceHelper.
- *
- * @return The namespace URI.
- */
- public String getNamespaceURI() {
- return namespaceUri;
- }
-
- /**
- * Returns the namespace prefix that is used to create elements.
- *
- * @return The namespace prefix.
- */
- public String getPrefix() {
- return prefix;
- }
-
- /**
- * Returns the qualified name for a local name using the prefix of this
- * NamespaceHelper.
- *
- * @param localName The local name.
- * @return The qualified name, i.e. prefix:localName.
- */
- public String getQualifiedName(String localName) {
- if (getPrefix().equals("")) {
- return localName;
- } else {
- return getPrefix() + ":" + localName;
- }
- }
-
- /**
- * <p>
- * Creates an element within the namespace of this NamespaceHelper object with
- * a given local name containing a text node.<br/>
- * </p>
- * <p>
- * <code>createElement("text")</code>: <code><prefix:text/><code>.
- * </p>
- *
- * @param localName The local name of the element.
- * @return A new element.
- */
- public Element createElement(String localName) {
- return getDocument().createElementNS(getNamespaceURI(), getQualifiedName(localName));
- }
-
- /**
- * <p>
- * Creates an element within the namespace of this NamespaceHelper object with
- * a given local name containing a text node.
- * </p>
- * <p>
- * <code>createElement("text", "Hello World!")</code>:
- * <code><prefix:text>Hello World!</prefix:text></code>.
- * </p>
- *
- * @param localName The local name of the element.
- * @param text The text for the text node inside the element.
- * @return A new element containing a text node.
- */
- public Element createElement(String localName, String text) {
- Element element = createElement(localName);
- Text textNode = getDocument().createTextNode(text);
- element.appendChild(textNode);
-
- return element;
- }
-
- /**
- * Returns all children of an element in the namespace
- * of this NamespaceHelper.
- *
- * @param element The parent element.
- *
- * @return the children.
- */
- public Element[] getChildren(Element element) {
- return DocumentHelper.getChildren(element, getNamespaceURI());
- }
-
- /**
- * Returns all children of an element with a local name in the namespace
- * of this NamespaceHelper.
- *
- * @param element The parent element.
- * @param localName The local name of the children to return.
- *
- * @return the children.
- */
- public Element[] getChildren(Element element, String localName) {
- return DocumentHelper.getChildren(element, getNamespaceURI(), localName);
- }
-
- /**
- * Returns the first childr of an element with a local name in the namespace
- * of this NamespaceHelper or <code>null</code> if none exists.
- *
- * @param element The parent element.
- * @param localName The local name of the children to return.
- *
- * @return the first child.
- */
- public Element getFirstChild(Element element, String localName) {
- return DocumentHelper.getFirstChild(element, getNamespaceURI(), localName);
- }
-}
+/*
+$Id$
+<License>
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, 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 "Apache Lenya" 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
+ apache@apache.org.
+
+ 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 (INCLU-
+ DING, 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 created by
+ Michael Wechner <mi...@apache.org>. For more information on the Apache Soft-
+ ware Foundation, please see <http://www.apache.org/>.
+
+ Lenya includes software developed by the Apache Software Foundation, W3C,
+ DOM4J Project, BitfluxEditor, Xopus, and WebSHPINX.
+</License>
+*/
+package org.apache.lenya.xml;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Text;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+
+/**
+ * A NamespaceHelper object simplifies the creation of elements in a certain
+ * namespace. All elements are owned by a document that is passed to the
+ * {@link #NamespaceHelper(Document, String, String)} constructor or created
+ * using the {@link #NamespaceHelper(String, String, String)} constructor.
+ *
+ * @author andreas
+ */
+public class NamespaceHelper {
+ private String namespaceUri;
+ private String prefix;
+ private Document document;
+
+ /**
+ * Creates a new instance of NamespaceHelper using an existing document. The
+ * document is not affected. If you omit the prefix, the default namespace is used.
+ *
+ * @param document The document.
+ * @param namespaceUri The namespace URI.
+ * @param prefix The namespace prefix.
+ */
+ public NamespaceHelper(String namespaceUri, String prefix, Document document) {
+ this.namespaceUri = namespaceUri;
+ this.prefix = prefix;
+ this.document = document;
+ }
+
+ /**
+ * <p>
+ * Creates a new instance of NamespaceHelper. A new document is created
+ * using a document element in the given namespace with the given prefix.
+ * If you omit the prefix, the default namespace is used.
+ * </p>
+ * <p>
+ * NamespaceHelper("http://www.w3.org/2000/svg", "svg", "svg"):<br/>
+ * <?xml version="1.0"><br/>
+ * <svg:svg xmlns:svg="http://www.w3.org/2000/svg"><br/>
+ * </svg:svg>
+ * </p>
+ *
+ * @param localName The local name of the document element.
+ * @param namespaceUri The namespace URI.
+ * @param prefix The namespace prefix.
+ *
+ * @throws ParserConfigurationException if an error occured
+ */
+ public NamespaceHelper(String namespaceUri, String prefix, String localName)
+ throws ParserConfigurationException {
+ this.namespaceUri = namespaceUri;
+ this.prefix = prefix;
+ setDocument(DocumentHelper.createDocument(getNamespaceURI(), getQualifiedName(localName),
+ null));
+ }
+
+ /**
+ * Sets the document of this NamespaceHelper.
+ *
+ * @param document the document
+ */
+ protected void setDocument(Document document) {
+ this.document = document;
+ }
+
+ /**
+ * Returns the document that is used to create elements.
+ *
+ * @return A document object.
+ */
+ public Document getDocument() {
+ return document;
+ }
+
+ /**
+ * Returns the namespace URI of this NamespaceHelper.
+ *
+ * @return The namespace URI.
+ */
+ public String getNamespaceURI() {
+ return namespaceUri;
+ }
+
+ /**
+ * Returns the namespace prefix that is used to create elements.
+ *
+ * @return The namespace prefix.
+ */
+ public String getPrefix() {
+ return prefix;
+ }
+
+ /**
+ * Returns the qualified name for a local name using the prefix of this
+ * NamespaceHelper.
+ *
+ * @param localName The local name.
+ * @return The qualified name, i.e. prefix:localName.
+ */
+ public String getQualifiedName(String localName) {
+ if (getPrefix().equals("")) {
+ return localName;
+ } else {
+ return getPrefix() + ":" + localName;
+ }
+ }
+
+ /**
+ * <p>
+ * Creates an element within the namespace of this NamespaceHelper object with
+ * a given local name containing a text node.<br/>
+ * </p>
+ * <p>
+ * <code>createElement("text")</code>: <code><prefix:text/><code>.
+ * </p>
+ *
+ * @param localName The local name of the element.
+ * @return A new element.
+ */
+ public Element createElement(String localName) {
+ return getDocument().createElementNS(getNamespaceURI(), getQualifiedName(localName));
+ }
+
+ /**
+ * <p>
+ * Creates an element within the namespace of this NamespaceHelper object with
+ * a given local name containing a text node.
+ * </p>
+ * <p>
+ * <code>createElement("text", "Hello World!")</code>:
+ * <code><prefix:text>Hello World!</prefix:text></code>.
+ * </p>
+ *
+ * @param localName The local name of the element.
+ * @param text The text for the text node inside the element.
+ * @return A new element containing a text node.
+ */
+ public Element createElement(String localName, String text) {
+ Element element = createElement(localName);
+ Text textNode = getDocument().createTextNode(text);
+ element.appendChild(textNode);
+
+ return element;
+ }
+
+ /**
+ * Returns all children of an element in the namespace
+ * of this NamespaceHelper.
+ *
+ * @param element The parent element.
+ *
+ * @return the children.
+ */
+ public Element[] getChildren(Element element) {
+ return DocumentHelper.getChildren(element, getNamespaceURI());
+ }
+
+ /**
+ * Returns all children of an element with a local name in the namespace
+ * of this NamespaceHelper.
+ *
+ * @param element The parent element.
+ * @param localName The local name of the children to return.
+ *
+ * @return the children.
+ */
+ public Element[] getChildren(Element element, String localName) {
+ return DocumentHelper.getChildren(element, getNamespaceURI(), localName);
+ }
+
+ /**
+ * Returns the first childr of an element with a local name in the namespace
+ * of this NamespaceHelper or <code>null</code> if none exists.
+ *
+ * @param element The parent element.
+ * @param localName The local name of the children to return.
+ *
+ * @return the first child.
+ */
+ public Element getFirstChild(Element element, String localName) {
+ return DocumentHelper.getFirstChild(element, getNamespaceURI(), localName);
+ }
+
+ /**
+ * Returns the next siblings of an element with a local name in the namespace
+ * of this NamespaceHelper or <code>null</code> if none exists.
+ *
+ * @param element The parent element.
+ * @param localName The local name of the children to return.
+ *
+ * @return the next siblings.
+ */
+ public Element[] getNextSiblings(Element element, String localName) {
+ return DocumentHelper.getNextSiblings(element, getNamespaceURI(), localName);
+ }
+}
1.21 +411 -370 cocoon-lenya/src/java/org/apache/lenya/xml/DocumentHelper.java
Index: DocumentHelper.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/xml/DocumentHelper.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- DocumentHelper.java 31 Aug 2003 13:04:31 -0000 1.20
+++ DocumentHelper.java 17 Sep 2003 18:58:23 -0000 1.21
@@ -1,370 +1,411 @@
-/*
-<License>
-
- ============================================================================
- The Apache Software License, Version 1.1
- ============================================================================
-
- Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
-
- Redistribution and use in source and binary forms, with or without modifica-
- tion, 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 "Apache Lenya" 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
- apache@apache.org.
-
- 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 (INCLU-
- DING, 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 created by
- Michael Wechner <mi...@apache.org>. For more information on the Apache Soft-
- ware Foundation, please see <http://www.apache.org/>.
-
- Lenya includes software developed by the Apache Software Foundation, W3C,
- DOM4J Project, BitfluxEditor, Xopus, and WebSHPINX.
-</License>
-*/
-package org.apache.lenya.xml;
-
-import org.w3c.dom.DOMException;
-import org.w3c.dom.Document;
-import org.w3c.dom.DocumentType;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
-
-import org.xml.sax.SAXException;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Writer;
-
-import java.net.URI;
-import java.net.URL;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.OutputKeys;
-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;
-
-
-/**
- * Various utility methods to work with JAXP.
- *
- * @author Andreas Hartmann
- * @version $Id$
- */
-public class DocumentHelper {
- /**
- * Creates a non-validating and namespace-aware DocumentBuilder.
- *
- * @return A new DocumentBuilder object.
- * @throws ParserConfigurationException if an error occurs
- */
- public static DocumentBuilder createBuilder() throws ParserConfigurationException {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(true);
-
- return factory.newDocumentBuilder();
- }
-
- /**
- * Creates a document. A xmlns:prefix="namespaceUri" attribute is added to
- * the document element.
- *
- * @param namespaceUri The namespace URL of the root element.
- * @param qualifiedName The qualified name of the root element.
- * @param documentType The type of document to be created or null. When doctype is not null,
- * its Node.ownerDocument attribute is set to the document being created.
- * @return A new Document object.
- *
- * @throws DOMException if an error occurs
- * @throws ParserConfigurationException if an error occurs
- *
- * @see org.w3c.dom.DOMImplementation#createDocument(String, String, DocumentType)
- */
- public static Document createDocument(String namespaceUri, String qualifiedName,
- DocumentType documentType) throws DOMException, ParserConfigurationException {
- DocumentBuilder builder = createBuilder();
- Document document = builder.getDOMImplementation().createDocument(namespaceUri,
- qualifiedName, documentType);
-
- // add xmlns:prefix attribute
- String name = "xmlns";
- int index = qualifiedName.indexOf(":");
-
- if (index > -1) {
- name += (":" + qualifiedName.substring(0, index));
- }
-
- document.getDocumentElement().setAttribute(name, namespaceUri);
-
- return document;
- }
-
- /**
- * Reads a document from a file.
- * @return A document.
- * @param file The file to load the document from.
- *
- * @throws ParserConfigurationException if an error occurs
- * @throws SAXException if an error occurs
- * @throws IOException if an error occurs
- */
- public static Document readDocument(File file)
- throws ParserConfigurationException, SAXException, IOException {
- DocumentBuilder builder = createBuilder();
- return builder.parse(file);
- }
-
- /**
- * Reads a document from a URL.
- * @return A document.
- * @param url The URL to load the document from.
- *
- * @throws ParserConfigurationException if an error occurs
- * @throws SAXException if an error occurs
- * @throws IOException if an error occurs
- */
- public static Document readDocument(URL url)
- throws ParserConfigurationException, SAXException, IOException {
- DocumentBuilder builder = createBuilder();
- return builder.parse(url.toString());
- }
-
- /**
- * Reads a document from a URI.
- * @return A document.
- * @param uri The URI to load the document from.
- *
- * @throws ParserConfigurationException if an error occurs
- * @throws SAXException if an error occurs
- * @throws IOException if an error occurs
- */
- public static Document readDocument(URI uri)
- throws ParserConfigurationException, SAXException, IOException {
- DocumentBuilder builder = createBuilder();
- return builder.parse(uri.toString());
- }
-
- /**
- * Reads a document from an input stream.
- * @return A document.
- * @param stream The input stream to load the document from.
- *
- * @throws ParserConfigurationException if an error occurs
- * @throws SAXException if an error occurs
- * @throws IOException if an error occurs
- */
- public static Document readDocument(InputStream stream)
- throws ParserConfigurationException, SAXException, IOException {
- DocumentBuilder builder = createBuilder();
- return builder.parse(stream);
- }
-
- /**
- * Writes a document to a file. A new file is created if it does not exist.
- *
- * @param document The document to save.
- * @param file The file to save the document to.
- *
- * @throws IOException if an error occurs
- * @throws TransformerConfigurationException if an error occurs
- * @throws TransformerException if an error occurs
- */
- public static void writeDocument(Document document, File file)
- throws TransformerConfigurationException, TransformerException, IOException {
- file.getParentFile().mkdirs();
- file.createNewFile();
-
- DOMSource source = new DOMSource(document);
- StreamResult result = new StreamResult(file);
- getTransformer(document.getDoctype()).transform(source, result);
- }
-
- /**
- * Writes a document to a writer.
- *
- * @param document The document to write.
- * @param writer The writer to write the document to.
- *
- * @throws IOException if an error occurs
- * @throws TransformerConfigurationException if an error occurs
- * @throws TransformerException if an error occurs
- */
- public static void writeDocument(Document document, Writer writer)
- throws TransformerConfigurationException, TransformerException, IOException {
- DOMSource source = new DOMSource(document);
- StreamResult result = new StreamResult(writer);
- getTransformer(document.getDoctype()).transform(source, result);
- }
-
- /**
- * Get the tranformer.
- *
- * @param documentType the document type
- * @return a transformer
- *
- * @throws TransformerConfigurationException if an error occurs
- */
- protected static Transformer getTransformer(DocumentType documentType)
- throws TransformerConfigurationException {
- TransformerFactory factory = TransformerFactory.newInstance();
- Transformer transformer = factory.newTransformer();
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.METHOD, "xml");
-
- if (documentType != null) {
- transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
- transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
- }
-
- return transformer;
- }
-
- /**
- * Creates a document type.
- *
- * @param qualifiedName The qualified name of the document type.
- * @param publicId The public identifier.
- * @param systemId The system identifier.
- *
- * @return the document type
- *
- * @throws ParserConfigurationException if an error occurs
- *
- * @see DOMImplementation.createDocumentType(String, String, String)
- */
- public DocumentType createDocumentType(String qualifiedName, String publicId, String systemId)
- throws ParserConfigurationException {
- DocumentBuilder builder = createBuilder();
-
- return builder.getDOMImplementation().createDocumentType(qualifiedName, publicId, systemId);
- }
-
- /**
- * Returns the first child element of an element that belong to a certain namespace
- * or <code>null</code> if none exists.
- * @param element The parent element.
- * @param namespaceUri The namespace that the childen must belong to.
- * @return The first child element or <code>null</code> if none exists.
- */
- public static Element getFirstChild(Element element, String namespaceUri) {
- return getFirstChild(element, namespaceUri, "*");
- }
-
- /**
- * Returns the first child element of an element that belongs to a certain namespace
- * and has a certain local name or <code>null</code> if none exists.
- *
- * @param element The parent element.
- * @param namespaceUri The namespace that the childen must belong to.
- * @param localName The local name of the children.
- *
- * @return The child element or <code>null</code> if none exists.
- */
- public static Element getFirstChild(Element element, String namespaceUri, String localName) {
- Element[] children = getChildren(element, namespaceUri, localName);
-
- if (children.length > 0) {
- return children[0];
- } else {
- return null;
- }
- }
-
- /**
- * Returns all child elements of an element that belong to a certain namespace.
- *
- * @param element The parent element.
- * @param namespaceUri The namespace that the childen must belong to.
- *
- * @return The child elements.
- */
- public static Element[] getChildren(Element element, String namespaceUri) {
- return getChildren(element, namespaceUri, "*");
- }
-
- /**
- * Returns all child elements of an element that belong to a certain namespace
- * and have a certain local name.
- *
- * @param element The parent element.
- * @param namespaceUri The namespace that the childen must belong to.
- * @param localName The local name of the children.
- *
- * @return The child elements.
- */
- public static Element[] getChildren(Element element, String namespaceUri, String localName) {
- List childElements = new ArrayList();
- NodeList children = element.getElementsByTagNameNS(namespaceUri, localName);
-
- for (int i = 0; i < children.getLength(); i++) {
- if (children.item(i).getParentNode() == element) {
- childElements.add(children.item(i));
- }
- }
-
- return (Element[]) childElements.toArray(new Element[childElements.size()]);
- }
-
- /**
- * Returns the text inside an element. Only the child text nodes of this
- * element are collected.
- * @param element The element.
- * @return The text inside the element.
- */
- public static String getSimpleElementText(Element element) {
- StringBuffer buffer = new StringBuffer();
- NodeList children = element.getChildNodes();
-
- for (int i = 0; i < children.getLength(); i++) {
- Node child = children.item(i);
-
- if (child instanceof Text) {
- buffer.append(child.getNodeValue());
- }
- }
-
- return buffer.toString();
- }
-}
+/*
+<License>
+
+ ============================================================================
+ The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, 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 "Apache Lenya" 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
+ apache@apache.org.
+
+ 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 (INCLU-
+ DING, 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 created by
+ Michael Wechner <mi...@apache.org>. For more information on the Apache Soft-
+ ware Foundation, please see <http://www.apache.org/>.
+
+ Lenya includes software developed by the Apache Software Foundation, W3C,
+ DOM4J Project, BitfluxEditor, Xopus, and WebSHPINX.
+</License>
+*/
+package org.apache.lenya.xml;
+
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+
+import org.xml.sax.SAXException;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Writer;
+
+import java.net.URI;
+import java.net.URL;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.OutputKeys;
+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;
+
+
+/**
+ * Various utility methods to work with JAXP.
+ *
+ * @author Andreas Hartmann
+ * @version $Id$
+ */
+public class DocumentHelper {
+ /**
+ * Creates a non-validating and namespace-aware DocumentBuilder.
+ *
+ * @return A new DocumentBuilder object.
+ * @throws ParserConfigurationException if an error occurs
+ */
+ public static DocumentBuilder createBuilder() throws ParserConfigurationException {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+
+ return factory.newDocumentBuilder();
+ }
+
+ /**
+ * Creates a document. A xmlns:prefix="namespaceUri" attribute is added to
+ * the document element.
+ *
+ * @param namespaceUri The namespace URL of the root element.
+ * @param qualifiedName The qualified name of the root element.
+ * @param documentType The type of document to be created or null. When doctype is not null,
+ * its Node.ownerDocument attribute is set to the document being created.
+ * @return A new Document object.
+ *
+ * @throws DOMException if an error occurs
+ * @throws ParserConfigurationException if an error occurs
+ *
+ * @see org.w3c.dom.DOMImplementation#createDocument(String, String, DocumentType)
+ */
+ public static Document createDocument(String namespaceUri, String qualifiedName,
+ DocumentType documentType) throws DOMException, ParserConfigurationException {
+ DocumentBuilder builder = createBuilder();
+ Document document = builder.getDOMImplementation().createDocument(namespaceUri,
+ qualifiedName, documentType);
+
+ // add xmlns:prefix attribute
+ String name = "xmlns";
+ int index = qualifiedName.indexOf(":");
+
+ if (index > -1) {
+ name += (":" + qualifiedName.substring(0, index));
+ }
+
+ document.getDocumentElement().setAttribute(name, namespaceUri);
+
+ return document;
+ }
+
+ /**
+ * Reads a document from a file.
+ * @return A document.
+ * @param file The file to load the document from.
+ *
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(File file)
+ throws ParserConfigurationException, SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(file);
+ }
+
+ /**
+ * Reads a document from a URL.
+ * @return A document.
+ * @param url The URL to load the document from.
+ *
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(URL url)
+ throws ParserConfigurationException, SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(url.toString());
+ }
+
+ /**
+ * Reads a document from a URI.
+ * @return A document.
+ * @param uri The URI to load the document from.
+ *
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(URI uri)
+ throws ParserConfigurationException, SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(uri.toString());
+ }
+
+ /**
+ * Reads a document from an input stream.
+ * @return A document.
+ * @param stream The input stream to load the document from.
+ *
+ * @throws ParserConfigurationException if an error occurs
+ * @throws SAXException if an error occurs
+ * @throws IOException if an error occurs
+ */
+ public static Document readDocument(InputStream stream)
+ throws ParserConfigurationException, SAXException, IOException {
+ DocumentBuilder builder = createBuilder();
+ return builder.parse(stream);
+ }
+
+ /**
+ * Writes a document to a file. A new file is created if it does not exist.
+ *
+ * @param document The document to save.
+ * @param file The file to save the document to.
+ *
+ * @throws IOException if an error occurs
+ * @throws TransformerConfigurationException if an error occurs
+ * @throws TransformerException if an error occurs
+ */
+ public static void writeDocument(Document document, File file)
+ throws TransformerConfigurationException, TransformerException, IOException {
+ file.getParentFile().mkdirs();
+ file.createNewFile();
+
+ DOMSource source = new DOMSource(document);
+ StreamResult result = new StreamResult(file);
+ getTransformer(document.getDoctype()).transform(source, result);
+ }
+
+ /**
+ * Writes a document to a writer.
+ *
+ * @param document The document to write.
+ * @param writer The writer to write the document to.
+ *
+ * @throws IOException if an error occurs
+ * @throws TransformerConfigurationException if an error occurs
+ * @throws TransformerException if an error occurs
+ */
+ public static void writeDocument(Document document, Writer writer)
+ throws TransformerConfigurationException, TransformerException, IOException {
+ DOMSource source = new DOMSource(document);
+ StreamResult result = new StreamResult(writer);
+ getTransformer(document.getDoctype()).transform(source, result);
+ }
+
+ /**
+ * Get the tranformer.
+ *
+ * @param documentType the document type
+ * @return a transformer
+ *
+ * @throws TransformerConfigurationException if an error occurs
+ */
+ protected static Transformer getTransformer(DocumentType documentType)
+ throws TransformerConfigurationException {
+ TransformerFactory factory = TransformerFactory.newInstance();
+ Transformer transformer = factory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.METHOD, "xml");
+
+ if (documentType != null) {
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
+ transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
+ }
+
+ return transformer;
+ }
+
+ /**
+ * Creates a document type.
+ *
+ * @param qualifiedName The qualified name of the document type.
+ * @param publicId The public identifier.
+ * @param systemId The system identifier.
+ *
+ * @return the document type
+ *
+ * @throws ParserConfigurationException if an error occurs
+ *
+ * @see DOMImplementation.createDocumentType(String, String, String)
+ */
+ public DocumentType createDocumentType(String qualifiedName, String publicId, String systemId)
+ throws ParserConfigurationException {
+ DocumentBuilder builder = createBuilder();
+
+ return builder.getDOMImplementation().createDocumentType(qualifiedName, publicId, systemId);
+ }
+
+ /**
+ * Returns the first child element of an element that belong to a certain namespace
+ * or <code>null</code> if none exists.
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @return The first child element or <code>null</code> if none exists.
+ */
+ public static Element getFirstChild(Element element, String namespaceUri) {
+ return getFirstChild(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns the first child element of an element that belongs to a certain namespace
+ * and has a certain local name or <code>null</code> if none exists.
+ *
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ *
+ * @return The child element or <code>null</code> if none exists.
+ */
+ public static Element getFirstChild(Element element, String namespaceUri, String localName) {
+ Element[] children = getChildren(element, namespaceUri, localName);
+
+ if (children.length > 0) {
+ return children[0];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Returns all child elements of an element that belong to a certain namespace.
+ *
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ *
+ * @return The child elements.
+ */
+ public static Element[] getChildren(Element element, String namespaceUri) {
+ return getChildren(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns all child elements of an element that belong to a certain namespace
+ * and have a certain local name.
+ *
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ *
+ * @return The child elements.
+ */
+ public static Element[] getChildren(Element element, String namespaceUri, String localName) {
+ List childElements = new ArrayList();
+ NodeList children = element.getElementsByTagNameNS(namespaceUri, localName);
+
+ for (int i = 0; i < children.getLength(); i++) {
+ if (children.item(i).getParentNode() == element) {
+ childElements.add(children.item(i));
+ }
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+
+ /**
+ * Returns the text inside an element. Only the child text nodes of this
+ * element are collected.
+ * @param element The element.
+ * @return The text inside the element.
+ */
+ public static String getSimpleElementText(Element element) {
+ StringBuffer buffer = new StringBuffer();
+ NodeList children = element.getChildNodes();
+
+ for (int i = 0; i < children.getLength(); i++) {
+ Node child = children.item(i);
+
+ if (child instanceof Text) {
+ buffer.append(child.getNodeValue());
+ }
+ }
+
+ return buffer.toString();
+ }
+
+
+ /**
+ * Returns all following sibling elements of an element that belong to a certain namespace.
+ *
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ *
+ * @return The following sibling elements.
+ */
+ public static Element[] getNextSiblings(Element element, String namespaceUri) {
+ return getNextSiblings(element, namespaceUri, "*");
+ }
+
+ /**
+ * Returns all following sibling elements of an element that belong to a certain namespace.
+ * and have a certain local name.
+ *
+ * @param element The parent element.
+ * @param namespaceUri The namespace that the childen must belong to.
+ * @param localName The local name of the children.
+ *
+ * @return The following sibling elements.
+ */
+ public static Element[] getNextSiblings(Element element, String namespaceUri, String localName) {
+ List childElements = new ArrayList();
+ Element parent = (Element) element.getParentNode();
+ Element[] children=getChildren(parent, namespaceUri, localName);
+
+ int l = children.length;
+ for (int i = 0; i < children.length; i++) {
+ if (children[i] == element) {
+ l = i;
+ }
+ if (i>l){
+ childElements.add(children[i]);
+ }
+ }
+
+ return (Element[]) childElements.toArray(new Element[childElements.size()]);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org