Source Code Cross Referenced for NamespaceHelper.java in  » Content-Management-System » apache-lenya-2.0 » org » apache » lenya » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » apache lenya 2.0 » org.apache.lenya.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:             * &lt;?xml version="1.0"&gt;<br/> &lt;svg:svg
071:             * xmlns:svg="http://www.w3.org/2000/svg"&gt;<br/> &lt;/svg:svg&gt;
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>&lt;prefix:text/&gt;<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>&lt;prefix:text&gt;Hello World!&lt;/prefix:text&gt;</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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.