comparison artifact-database/src/main/java/de/intevation/artifactdatabase/XMLUtils.java @ 91:730ff077a58c

More javadoc, some code formatting. artifacts/trunk@847 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 28 Mar 2010 10:01:03 +0000
parents 48d1a9a082c2
children 73d0ebae81d7
comparison
equal deleted inserted replaced
90:68285f7bc476 91:730ff077a58c
32 import org.w3c.dom.Element; 32 import org.w3c.dom.Element;
33 33
34 import org.xml.sax.SAXException; 34 import org.xml.sax.SAXException;
35 35
36 /** 36 /**
37 * Some helper functions to ease work with XML concering namespaces, XPATH
38 * and so on.
37 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 39 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
38 */ 40 */
39 public final class XMLUtils 41 public final class XMLUtils
40 { 42 {
43 /**
44 * W3C URL of XForms
45 */
41 public static final String XFORM_URL = "http://www.w3.org/2002/xforms"; 46 public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
47 /**
48 * W3C prefix of XForms
49 */
42 public static final String XFORM_PREFIX = "xform"; 50 public static final String XFORM_PREFIX = "xform";
43 51
44 private static Logger logger = Logger.getLogger(XMLUtils.class); 52 private static Logger logger = Logger.getLogger(XMLUtils.class);
45 53
46 private XMLUtils() { 54 private XMLUtils() {
47 } 55 }
48 56
57 /**
58 * Helper class to generate elements and attributes with
59 * namespaces.
60 */
49 public static class ElementCreator 61 public static class ElementCreator
50 { 62 {
63 /**
64 * owner document of the elements to be created
65 */
51 protected Document document; 66 protected Document document;
67 /**
68 * namespace to be used
69 */
52 protected String ns; 70 protected String ns;
71 /**
72 * prefix to be used
73 */
53 protected String prefix; 74 protected String prefix;
54 75
76 /**
77 * Constructor to create an element/attribute creator
78 * with a given namespace and namespace prefix using a
79 * given owner document.
80 * @param document The owning document
81 * @param ns The namespace
82 * @param prefix The namespace prefix
83 */
55 public ElementCreator(Document document, String ns, String prefix) { 84 public ElementCreator(Document document, String ns, String prefix) {
56 this.document = document; 85 this.document = document;
57 this.ns = ns; 86 this.ns = ns;
58 this.prefix = prefix; 87 this.prefix = prefix;
59 } 88 }
60 89
90 /**
91 * Creates a new element using the owning document with
92 * the this creators namespace and namespace prefix.
93 * @param name The name of the element
94 * @return The new element
95 */
61 public Element create(String name) { 96 public Element create(String name) {
62 Element element = document.createElementNS(ns, name); 97 Element element = document.createElementNS(ns, name);
63 element.setPrefix(prefix); 98 element.setPrefix(prefix);
64 return element; 99 return element;
65 } 100 }
66 101
102 /**
103 * Adds a new attribute and its value to a given element.
104 * It does not set the namespace prefix.
105 * @param element The element to add the attribute to
106 * @param name The name of the attribute
107 * @param value The value of the attribute
108 */
67 public void addAttr(Element element, String name, String value) { 109 public void addAttr(Element element, String name, String value) {
68 addAttr(element, name, value, false); 110 addAttr(element, name, value, false);
69 } 111 }
70 112
113 /**
114 * Adds a new attribute and its value to a given element.
115 * If the namespace prefix is used is decided by the 'addPrefix' flag.
116 * @param element The element to add the attribute to
117 * @param name The name of the attribute
118 * @param value The value of the attribute
119 * @param addPrefix If true the creators namespace prefix is
120 * set on the attribute.
121 */
71 public void addAttr( 122 public void addAttr(
72 Element element, 123 Element element,
73 String name, 124 String name,
74 String value, 125 String value,
75 boolean addPrefix 126 boolean addPrefix
82 133
83 element.setAttributeNode(attr); 134 element.setAttributeNode(attr);
84 } 135 }
85 } // class ElementCreator 136 } // class ElementCreator
86 137
138 /**
139 * Creates a new XML document
140 * @return the new XML document ot null if something went wrong during
141 * creation.
142 */
87 public static final Document newDocument() { 143 public static final Document newDocument() {
88 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 144 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
89 factory.setNamespaceAware(true); 145 factory.setNamespaceAware(true);
90 146
91 try { 147 try {
95 logger.error(pce.getLocalizedMessage(), pce); 151 logger.error(pce.getLocalizedMessage(), pce);
96 } 152 }
97 return null; 153 return null;
98 } 154 }
99 155
156 /**
157 * Loads a XML document namespace aware from a file
158 * @param file The file to load.
159 * @return the XML document or null if something went wrong
160 * during loading.
161 */
100 public static final Document parseDocument(File file) { 162 public static final Document parseDocument(File file) {
101 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 163 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
102 factory.setNamespaceAware(true); 164 factory.setNamespaceAware(true);
103 165
104 try { 166 try {
114 logger.error(ioe.getLocalizedMessage(), ioe); 176 logger.error(ioe.getLocalizedMessage(), ioe);
115 } 177 }
116 return null; 178 return null;
117 } 179 }
118 180
181 /**
182 * Creates a new XPath without a namespace context.
183 * @return the new XPath.
184 */
119 public static final XPath newXPath() { 185 public static final XPath newXPath() {
120 return newXPath(null); 186 return newXPath(null);
121 } 187 }
122 188
189 /**
190 * Creates a new XPath with a given namespace context.
191 * @param namespaceContext The namespace context to be used or null
192 * if none should be used.
193 * @return The new XPath
194 */
123 public static final XPath newXPath(NamespaceContext namespaceContext) { 195 public static final XPath newXPath(NamespaceContext namespaceContext) {
124 XPathFactory factory = XPathFactory.newInstance(); 196 XPathFactory factory = XPathFactory.newInstance();
125 XPath xpath = factory.newXPath(); 197 XPath xpath = factory.newXPath();
126 if (namespaceContext != null) { 198 if (namespaceContext != null) {
127 xpath.setNamespaceContext(namespaceContext); 199 xpath.setNamespaceContext(namespaceContext);
128 } 200 }
129 return xpath; 201 return xpath;
130 } 202 }
131 203
204 /**
205 * Evaluates an XPath query on a given object and returns the result
206 * as a given type. No namespace context is used.
207 * @param root The object which is used as the root of the tree to
208 * be searched in.
209 * @param query The XPath query
210 * @param returnTyp The type of the result.
211 * @return The result of type 'returnTyp' or null if something
212 * went wrong during XPath evaluation.
213 */
132 public static final Object xpath(Object root, String query, QName returnTyp) { 214 public static final Object xpath(Object root, String query, QName returnTyp) {
133 return xpath(root, query, returnTyp, null); 215 return xpath(root, query, returnTyp, null);
134 } 216 }
135 217
218 /**
219 * Evaluates an XPath query on a given objectr and returns the result
220 * as a string. A given namespace context is used.
221 * @param root The object which is used as the root of the tree to
222 * be searched in.
223 * @param query The XPath query
224 * @param namespaceContext The namespace context to be used or null
225 * if none should be used.
226 * @return The result of the query or null if something went wrong
227 * during XPath evaluation.
228 */
136 public static final String xpathString( 229 public static final String xpathString(
137 Object root, String query, NamespaceContext namespaceContext 230 Object root, String query, NamespaceContext namespaceContext
138 ) { 231 ) {
139 return (String)xpath( 232 return (String)xpath(
140 root, query, XPathConstants.STRING, namespaceContext); 233 root, query, XPathConstants.STRING, namespaceContext);
141 } 234 }
142 235
236 /**
237 * Evaluates an XPath query on a given object and returns the result
238 * as a given type. Optionally a namespace context is used.
239 * @param root The object which is used as the root of the tree to
240 * be searched in.
241 * @param query The XPath query
242 * @param returnType The type of the result.
243 * @param namespaceContext The namespace context to be used or null
244 * if none should be used.
245 * @return The result of type 'returnTyp' or null if something
246 * went wrong during XPath evaluation.
247 */
143 public static final Object xpath( 248 public static final Object xpath(
144 Object root, 249 Object root,
145 String query, 250 String query,
146 QName returnType, 251 QName returnType,
147 NamespaceContext namespaceContext 252 NamespaceContext namespaceContext
161 } 266 }
162 267
163 return null; 268 return null;
164 } 269 }
165 270
271 /**
272 * Streams out an XML document to a given output stream.
273 * @param document The document to be streamed out.
274 * @param out The output stream to be used.
275 * @return true if operation succeeded else false.
276 */
166 public static boolean toStream(Document document, OutputStream out) { 277 public static boolean toStream(Document document, OutputStream out) {
167 try { 278 try {
168 Transformer transformer = 279 Transformer transformer =
169 TransformerFactory.newInstance().newTransformer(); 280 TransformerFactory.newInstance().newTransformer();
170 DOMSource source = new DOMSource(document); 281 DOMSource source = new DOMSource(document);
183 } 294 }
184 295
185 return false; 296 return false;
186 } 297 }
187 } 298 }
188 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 299 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org