001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: NamespaceHelper.java 514615 2007-03-05 09:15:10Z andreas $ */
020:
021: package org.apache.lenya.xml;
022:
023: import java.io.OutputStream;
024:
025: import javax.xml.parsers.ParserConfigurationException;
026:
027: import org.apache.lenya.util.Assert;
028: import org.w3c.dom.Document;
029: import org.w3c.dom.Element;
030: import org.w3c.dom.Text;
031:
032: /**
033: * A NamespaceHelper object simplifies the creation of elements in a certain
034: * namespace. All elements are owned by a document that is passed to the
035: * {@link #NamespaceHelper(String, String, Document)} constructor or created
036: * using the {@link #NamespaceHelper(String, String, String)} constructor.
037: */
038: public class NamespaceHelper {
039: private String namespaceUri;
040: private String prefix;
041: private Document document;
042:
043: /**
044: * Creates a new instance of NamespaceHelper using an existing document. The
045: * document is not affected. If the prefix is <code>null</code>, the default namespace
046: * is used.
047: * @param _document The document.
048: * @param _namespaceUri The namespace URI.
049: * @param _prefix The namespace prefix.
050: */
051: public NamespaceHelper(String _namespaceUri, String _prefix,
052: Document _document) {
053:
054: Assert.notNull("namespace URI", _namespaceUri);
055: Assert.notNull("DOM", _document);
056:
057: this .namespaceUri = _namespaceUri;
058: this .prefix = _prefix;
059: this .document = _document;
060: }
061:
062: /**
063: * <p>
064: * Creates a new instance of NamespaceHelper. A new document is created
065: * using a document element in the given namespace with the given prefix. If
066: * the prefix is null, the default namespace is used.
067: * </p>
068: * <p>
069: * NamespaceHelper("http://www.w3.org/2000/svg", "svg", "svg"):<br/>
070: * <?xml version="1.0"><br/> <svg:svg
071: * xmlns:svg="http://www.w3.org/2000/svg"><br/> </svg:svg>
072: * </p>
073: * @param localName The local name of the document element.
074: * @param _namespaceUri The namespace URI.
075: * @param _prefix The namespace prefix.
076: * @throws ParserConfigurationException if an error occured
077: */
078: public NamespaceHelper(String _namespaceUri, String _prefix,
079: String localName) throws ParserConfigurationException {
080: this (_namespaceUri, _prefix, DocumentHelper.createDocument(
081: _namespaceUri, getQualifiedName(_prefix, localName),
082: null));
083: }
084:
085: /**
086: * Returns the document that is used to create elements.
087: * @return A document object.
088: */
089: public Document getDocument() {
090: return this .document;
091: }
092:
093: /**
094: * Returns the namespace URI of this NamespaceHelper.
095: * @return The namespace URI.
096: */
097: public String getNamespaceURI() {
098: return this .namespaceUri;
099: }
100:
101: /**
102: * Returns the namespace prefix that is used to create elements.
103: * @return The namespace prefix.
104: */
105: public String getPrefix() {
106: return this .prefix;
107: }
108:
109: /**
110: * Returns the qualified name for a local name using the prefix of this
111: * NamespaceHelper.
112: * @param prefix The namespace prefix.
113: * @param localName The local name.
114: * @return The qualified name, i.e. prefix:localName.
115: */
116: public static String getQualifiedName(String prefix,
117: String localName) {
118: if (prefix == null || prefix.equals("")) {
119: return localName;
120: }
121: return prefix + ":" + localName;
122: }
123:
124: /**
125: * <p>
126: * Creates an element within the namespace of this NamespaceHelper object
127: * with a given local name containing a text node.<br/>
128: * </p>
129: * <p>
130: * <code>createElement("text")</code>: <code><prefix:text/><code>.
131: * </p>
132: * @param localName The local name of the element.
133: * @return A new element.
134: */
135: public Element createElement(String localName) {
136: return getDocument().createElementNS(getNamespaceURI(),
137: getQualifiedName(getPrefix(), localName));
138: }
139:
140: /**
141: * <p>
142: * Creates an element within the namespace of this NamespaceHelper object
143: * with a given local name containing a text node.
144: * </p>
145: * <p>
146: * <code>createElement("text", "Hello World!")</code>:
147: * <code><prefix:text>Hello World!</prefix:text></code>.
148: * </p>
149: * @param localName The local name of the element.
150: * @param text The text for the text node inside the element.
151: * @return A new element containing a text node.
152: */
153: public Element createElement(String localName, String text) {
154: Element element = createElement(localName);
155: Text textNode = getDocument().createTextNode(text);
156: element.appendChild(textNode);
157:
158: return element;
159: }
160:
161: /**
162: * Returns all children of an element in the namespace of this
163: * NamespaceHelper.
164: * @param element The parent element.
165: * @return the children.
166: */
167: public Element[] getChildren(Element element) {
168: return DocumentHelper.getChildren(element, getNamespaceURI());
169: }
170:
171: /**
172: * Returns all children of an element with a local name in the namespace of
173: * this NamespaceHelper.
174: * @param element The parent element.
175: * @param localName The local name of the children to return.
176: * @return the children.
177: */
178: public Element[] getChildren(Element element, String localName) {
179: return DocumentHelper.getChildren(element, getNamespaceURI(),
180: localName);
181: }
182:
183: /**
184: * Returns the first childr of an element with a local name in the namespace
185: * of this NamespaceHelper or <code>null</code> if none exists.
186: * @param element The parent element.
187: * @param localName The local name of the children to return.
188: * @return the first child.
189: */
190: public Element getFirstChild(Element element, String localName) {
191: return DocumentHelper.getFirstChild(element, getNamespaceURI(),
192: localName);
193: }
194:
195: /**
196: * Returns the next siblings of an element with a local name in the
197: * namespace of this NamespaceHelper or <code>null</code> if none exists.
198: * @param element The parent element.
199: * @param localName The local name of the children to return.
200: * @return the next siblings.
201: */
202: public Element[] getNextSiblings(Element element, String localName) {
203: return DocumentHelper.getNextSiblings(element,
204: getNamespaceURI(), localName);
205: }
206:
207: /**
208: * Returns the preceding siblings of an element with a local name in the
209: * namespace of this NamespaceHelper or <code>null</code> if none exists.
210: * @param element The parent element.
211: * @param localName The local name of the children to return.
212: * @return the preceding siblings.
213: */
214: public Element[] getPrecedingSiblings(Element element,
215: String localName) {
216: return DocumentHelper.getPrecedingSiblings(element,
217: getNamespaceURI(), localName);
218: }
219:
220: /**
221: * Saves the XML.
222: * @param stream The stream to write to.
223: */
224: public void save(OutputStream stream) {
225: try {
226: DocumentHelper.writeDocument(getDocument(), stream);
227: } catch (Exception e) {
228: throw new RuntimeException(e);
229: }
230: }
231: }
|