KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > NamespaceHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: NamespaceHelper.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
23
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.Text JavaDoc;
27
28
29 /**
30  * A NamespaceHelper object simplifies the creation of elements in a certain
31  * namespace. All elements are owned by a document that is passed to the
32  * {@link #NamespaceHelper(Document, String, String)} constructor or created
33  * using the {@link #NamespaceHelper(String, String, String)} constructor.
34  */

35 public class NamespaceHelper {
36     private String JavaDoc namespaceUri;
37     private String JavaDoc prefix;
38     private Document JavaDoc document;
39
40     /**
41      * Creates a new instance of NamespaceHelper using an existing document. The
42      * document is not affected. If you omit the prefix, the default namespace is used.
43      *
44      * @param document The document.
45      * @param namespaceUri The namespace URI.
46      * @param prefix The namespace prefix.
47      */

48     public NamespaceHelper(String JavaDoc namespaceUri, String JavaDoc prefix, Document JavaDoc document) {
49         this.namespaceUri = namespaceUri;
50         this.prefix = prefix;
51         this.document = document;
52     }
53
54     /**
55      * <p>
56      * Creates a new instance of NamespaceHelper. A new document is created
57      * using a document element in the given namespace with the given prefix.
58      * If you omit the prefix, the default namespace is used.
59      * </p>
60      * <p>
61      * NamespaceHelper("http://www.w3.org/2000/svg", "svg", "svg"):<br/>
62      * &lt;?xml version="1.0"&gt;<br/>
63      * &lt;svg:svg xmlns:svg="http://www.w3.org/2000/svg"&gt;<br/>
64      * &lt;/svg:svg&gt;
65      * </p>
66      *
67      * @param localName The local name of the document element.
68      * @param namespaceUri The namespace URI.
69      * @param prefix The namespace prefix.
70      *
71      * @throws ParserConfigurationException if an error occured
72      */

73     public NamespaceHelper(String JavaDoc namespaceUri, String JavaDoc prefix, String JavaDoc localName)
74         throws ParserConfigurationException JavaDoc {
75         this.namespaceUri = namespaceUri;
76         this.prefix = prefix;
77         setDocument(DocumentHelper.createDocument(getNamespaceURI(), getQualifiedName(localName),
78                 null));
79     }
80
81     /**
82      * Sets the document of this NamespaceHelper.
83      *
84      * @param document the document
85      */

86     protected void setDocument(Document JavaDoc document) {
87         this.document = document;
88     }
89
90     /**
91      * Returns the document that is used to create elements.
92      *
93      * @return A document object.
94      */

95     public Document JavaDoc getDocument() {
96         return document;
97     }
98
99     /**
100      * Returns the namespace URI of this NamespaceHelper.
101      *
102      * @return The namespace URI.
103      */

104     public String JavaDoc getNamespaceURI() {
105         return namespaceUri;
106     }
107
108     /**
109      * Returns the namespace prefix that is used to create elements.
110      *
111      * @return The namespace prefix.
112      */

113     public String JavaDoc getPrefix() {
114         return prefix;
115     }
116
117     /**
118      * Returns the qualified name for a local name using the prefix of this
119      * NamespaceHelper.
120      *
121      * @param localName The local name.
122      * @return The qualified name, i.e. prefix:localName.
123      */

124     public String JavaDoc getQualifiedName(String JavaDoc localName) {
125         if (getPrefix().equals("")) {
126             return localName;
127         } else {
128             return getPrefix() + ":" + localName;
129         }
130     }
131
132     /**
133      * <p>
134      * Creates an element within the namespace of this NamespaceHelper object with
135      * a given local name containing a text node.<br/>
136      * </p>
137      * <p>
138      * <code>createElement("text")</code>: <code>&lt;prefix:text/&gt;<code>.
139      * </p>
140      *
141      * @param localName The local name of the element.
142      * @return A new element.
143      */

144     public Element JavaDoc createElement(String JavaDoc localName) {
145         return getDocument().createElementNS(getNamespaceURI(), getQualifiedName(localName));
146     }
147
148     /**
149      * <p>
150      * Creates an element within the namespace of this NamespaceHelper object with
151      * a given local name containing a text node.
152      * </p>
153      * <p>
154      * <code>createElement("text", "Hello World!")</code>:
155      * <code>&lt;prefix:text&gt;Hello World!&lt;/prefix:text&gt;</code>.
156      * </p>
157      *
158      * @param localName The local name of the element.
159      * @param text The text for the text node inside the element.
160      * @return A new element containing a text node.
161      */

162     public Element JavaDoc createElement(String JavaDoc localName, String JavaDoc text) {
163         Element JavaDoc element = createElement(localName);
164         Text JavaDoc textNode = getDocument().createTextNode(text);
165         element.appendChild(textNode);
166
167         return element;
168     }
169
170     /**
171      * Returns all children of an element in the namespace
172      * of this NamespaceHelper.
173      *
174      * @param element The parent element.
175      *
176      * @return the children.
177      */

178     public Element JavaDoc[] getChildren(Element JavaDoc element) {
179         return DocumentHelper.getChildren(element, getNamespaceURI());
180     }
181
182     /**
183      * Returns all children of an element with a local name in the namespace
184      * of this NamespaceHelper.
185      *
186      * @param element The parent element.
187      * @param localName The local name of the children to return.
188      *
189      * @return the children.
190      */

191     public Element JavaDoc[] getChildren(Element JavaDoc element, String JavaDoc localName) {
192         return DocumentHelper.getChildren(element, getNamespaceURI(), localName);
193     }
194
195     /**
196      * Returns the first childr of an element with a local name in the namespace
197      * of this NamespaceHelper or <code>null</code> if none exists.
198      *
199      * @param element The parent element.
200      * @param localName The local name of the children to return.
201      *
202      * @return the first child.
203      */

204     public Element JavaDoc getFirstChild(Element JavaDoc element, String JavaDoc localName) {
205         return DocumentHelper.getFirstChild(element, getNamespaceURI(), localName);
206     }
207
208     /**
209      * Returns the next siblings of an element with a local name in the namespace
210      * of this NamespaceHelper or <code>null</code> if none exists.
211      *
212      * @param element The parent element.
213      * @param localName The local name of the children to return.
214      *
215      * @return the next siblings.
216      */

217     public Element JavaDoc[] getNextSiblings(Element JavaDoc element, String JavaDoc localName) {
218         return DocumentHelper.getNextSiblings(element, getNamespaceURI(), localName);
219     }
220 }
221
Popular Tags