DOMUtils.java

biz/hammurapi/xml/dom/DOMUtils.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 180:9
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 222:9
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 236:9
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 264:9
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 281:9
Java Inspector 045 Too many exceptions (more than 3) listed in throws clause 2 293:9
Java Inspector 089 Undocumented parameter e 2 82:9
Java Inspector 089 Undocumented parameter elementName 2 82:9
Java Inspector 089 Undocumented exception ConfigurationException 2 82:9
Java Inspector 089 Method return value is not documented 2 82:9
Java Inspector 089 Undocumented parameter e 2 102:9
Java Inspector 089 Undocumented parameter elementName 2 102:9
Java Inspector 089 Undocumented exception ConfigurationException 2 102:9
Java Inspector 089 Javadoc contains tag for exception which method doesn't throw TransformerException 2 102:9
Java Inspector 089 Method return value is not documented 2 102:9
Java Inspector 089 Undocumented method 2 106:9
Java Inspector 089 Undocumented method 2 110:9
Java Inspector 089 Undocumented method 2 114:9
Java Inspector 089 Undocumented method 2 122:9
Java Inspector 089 Undocumented parameter holder 2 154:9
Java Inspector 089 Undocumented parameter properties 2 154:9
Java Inspector 089 Undocumented exception ConfigurationException 2 154:9
Java Inspector 089 Undocumented method 2 170:9
Java Inspector 089 Undocumented method 2 180:9
Java Inspector 089 Undocumented method 2 189:9
Java Inspector 089 Undocumented method 2 195:9
Java Inspector 089 Undocumented method 2 202:9
Java Inspector 089 Undocumented method 2 209:9
Java Inspector 089 Undocumented method 2 222:9
Java Inspector 089 Undocumented method 2 229:9
Java Inspector 089 Undocumented method 2 236:9
Java Inspector 089 Undocumented method 2 245:9
Java Inspector 089 Undocumented method 2 251:9
Java Inspector 089 Undocumented method 2 264:9
Java Inspector 089 Method is not properly documented 2 281:9
Java Inspector 089 Parameter is is not documented 2 281:9
Java Inspector 089 Method return value is not properly documented 2 281:9
Java Inspector 089 Method is not properly documented 2 293:9
Java Inspector 089 Parameter is is not documented 2 293:9
Java Inspector 089 Method return value is not properly documented 2 293:9
Java Inspector 089 Parameter o is not documented 2 303:9
Java Inspector 089 Parameter holder is not documented 2 303:9
Java Inspector 089 Parameter o is not documented 2 319:9
Java Inspector 089 Undocumented method 2 330:9
Java Inspector 089 Undocumented method 2 338:9
Java Inspector 089 Undocumented method 2 342:9
Java Inspector 089 Undocumented method 2 352:9
Java Inspector 089 Undocumented method 2 356:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 85:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 86:43
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 89:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 90:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:47
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 117:58
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 117:90
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 123:75
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 134:46
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 134:74
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 155:54
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 158:46
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 159:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 161:68
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 162:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 163:67
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 166:64
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 213:39
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 213:49
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 214:39
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 214:63
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 332:53
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 74:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23
24package biz.hammurapi.xml.dom;
25
26import java.io.File;
27import java.io.FileInputStream;
28import java.io.FileOutputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32import java.io.Reader;
33import java.io.StringWriter;
34import java.io.Writer;
35import java.util.Iterator;
36import java.util.Map;
37import java.util.Properties;
38
39import javax.xml.namespace.QName;
40import javax.xml.parsers.DocumentBuilderFactory;
41import javax.xml.parsers.FactoryConfigurationError;
42import javax.xml.parsers.ParserConfigurationException;
43import javax.xml.transform.Result;
44import javax.xml.transform.Transformer;
45import javax.xml.transform.TransformerException;
46import javax.xml.transform.TransformerFactory;
47import javax.xml.transform.dom.DOMSource;
48import javax.xml.transform.stream.StreamResult;
49import javax.xml.transform.stream.StreamSource;
50import javax.xml.xpath.XPath;
51import javax.xml.xpath.XPathConstants;
52import javax.xml.xpath.XPathExpressionException;
53import javax.xml.xpath.XPathFactory;
54
55import org.w3c.dom.Document;
56import org.w3c.dom.Element;
57import org.w3c.dom.NamedNodeMap;
58import org.w3c.dom.Node;
59import org.w3c.dom.NodeList;
60import org.xml.sax.InputSource;
61import org.xml.sax.SAXException;
62
63import biz.hammurapi.config.ConfigurationException;
64import biz.hammurapi.convert.ConvertingService;
65
66/**
67 * Utility class for querying DOM.
68 *
69 * @author Pavel Vlasov
70 */
71public class DOMUtils {
72
73 /** Creates a new instance of DOMUtils */
74 private DOMUtils() {
75 // Utility class
76 }
77
78 /**
79 * Ensures that thre is one and only one element and returns it.
80 * @throws XPathExpressionException
81 */
82 public static Element getSingleElement(Element e, String elementName) throws ConfigurationException, XPathExpressionException {
83 NodeList nl = selectNodeList(e, elementName);
84 if (nl.getLength() == 0) {
85 throw new ConfigurationException("Element <" + elementName
86 + "> not found");
87 }
88 if (nl.getLength() > 1) {
89 throw new ConfigurationException("Duplicate element <"
90 + elementName + ">");
91 }
92
93 return (Element) nl.item(0);
94 }
95
96 /**
97 * Ensures that there is only one element and returns its text
98 * @throws TransformerException
99 * @throws XPathExpressionException
100 * @throws TransformerException
101 */
102 public static String getSingleElementText(Element e, String elementName) throws ConfigurationException, XPathExpressionException {
103 return getElementText(getSingleElement(e, elementName));
104 }
105
106 public static String getSingleNonBlankElementText(Element e, String elementName) throws ConfigurationException, XPathExpressionException {
107 return getNonBlankElementText(getSingleElement(e, elementName));
108 }
109
110 public static String getElementText(Element e) throws XPathExpressionException {
111 return String.valueOf(eval(e, "text()"));
112 }
113
114 public static String getNonBlankElementText(Element e) throws ConfigurationException, XPathExpressionException {
115 String res = getElementText(e);
116 if (res.trim().length() == 0) {
117 throw new ConfigurationException("Element <" + e.getNodeName() + "> is blank");
118 }
119 return res;
120 }
121
122 public static Properties dom2Properties(Element e, String prefix) throws XPathExpressionException {
123 String root = prefix == null ? e.getNodeName() : prefix + "."
124 + e.getNodeName();
125 Properties res = new Properties();
126 String text = getElementText(e);
127 if (text.trim().length() != 0) {
128 res.setProperty(root, text);
129 }
130
131 NamedNodeMap attributes = e.getAttributes();
132 for (int i=0, l=attributes.getLength(); i<l; i++) {
133 Node attribute=attributes.item(i);
134 res.setProperty(root+"("+attribute.getNodeName()+")", attribute.getNodeValue());
135 }
136
137
138 NodeList nl = e.getChildNodes();
139 for (int i = 0; i < nl.getLength(); i++) {
140 Node n = nl.item(i);
141 if (n.getNodeType() == Node.ELEMENT_NODE) {
142 res.putAll(dom2Properties((Element) n, root));
143 }
144 }
145
146 return res;
147 }
148
149 /**
150 * Reads elements &lt;property name="..."&gt;...&lt/property&gt; into
151 * properties
152 * @throws XPathExpressionException
153 */
154 public static void readProperties(Element holder, Properties properties) throws ConfigurationException, XPathExpressionException {
155 NodeList nl = selectNodeList(holder, "property");
156 for (int i=0, l=nl.getLength(); i<l; ++i) {
157 Element el = (Element) nl.item(i);
158 if (!el.hasAttribute("name")) {
159 throw new ConfigurationException("Unnamed property");
160 }
161 if (properties.containsKey(el.getAttribute("name"))) {
162 throw new ConfigurationException("Property already set: "
163 + el.getAttribute("name"));
164 }
165
166 properties.setProperty(el.getAttribute("name"), DOMUtils.getNonBlankElementText(el));
167 }
168 }
169
170 public static void serialize(Object o, String root, Result result) throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
171 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
172 Element rootElement = AbstractDomObject.addElement(doc, root);
173 toDom(o, rootElement);
174 TransformerFactory
175 .newInstance()
176 .newTransformer()
177 .transform(new DOMSource(doc), result);
178 }
179
180 public static void serialize(Object o, String root, File out) throws ParserConfigurationException, FactoryConfigurationError, IOException, TransformerException {
181 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
182 Element rootElement = AbstractDomObject.addElement(doc, root);
183 toDom(o, rootElement);
184 FileOutputStream os = new FileOutputStream(out);
185 serialize(doc, os);
186 os.close();
187 }
188
189 public static void serialize(Node node, File out) throws IOException, TransformerException {
190 FileOutputStream os = new FileOutputStream(out);
191 serialize(node, os);
192 os.close();
193 }
194
195 public static void serialize(Node node, OutputStream os) throws TransformerException {
196 TransformerFactory
197 .newInstance()
198 .newTransformer()
199 .transform(new DOMSource(node), new StreamResult(os));
200 }
201
202 public static void serialize(Node node, Writer w) throws TransformerException {
203 TransformerFactory
204 .newInstance()
205 .newTransformer()
206 .transform(new DOMSource(node), new StreamResult(w));
207 }
208
209 public static String toString(Node node) throws IOException, TransformerException {
210 Transformer transformer = TransformerFactory.newInstance().newTransformer();
211
212 //transformer.setOutputProperty("method", "xml");
213 transformer.setOutputProperty("indent", "yes");
214 transformer.setOutputProperty("omit-xml-declaration", "yes");
215
216 StringWriter sw=new StringWriter();
217 transformer.transform(new DOMSource(node), new StreamResult(sw));
218 sw.close();
219 return sw.toString();
220 }
221
222 public static String toXmlString(Object o, String root) throws IOException, TransformerException, ParserConfigurationException, FactoryConfigurationError {
223 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
224 Element rootElement = AbstractDomObject.addElement(doc, root);
225 toDom(o, rootElement);
226 return toString(rootElement);
227 }
228
229 public static void style(Object o, String root, OutputStream os, InputStream style, Map parameters) throws ParserConfigurationException, FactoryConfigurationError, TransformerException {
230 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
231 Element rootElement = AbstractDomObject.addElement(doc, root);
232 toDom(o, rootElement);
233 style(doc, os, style, parameters);
234 }
235
236 public static void style(Object o, String root, File out, InputStream style, Map parameters) throws ParserConfigurationException, FactoryConfigurationError, IOException, TransformerException {
237 Document doc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
238 Element rootElement = AbstractDomObject.addElement(doc, root);
239 toDom(o, rootElement);
240 FileOutputStream os = new FileOutputStream(out);
241 style(doc, os, style, parameters);
242 os.close();
243 }
244
245 public static void style(Document doc, File out, InputStream style, Map parameters) throws IOException, TransformerException {
246 FileOutputStream os = new FileOutputStream(out);
247 style(doc, os, style, parameters);
248 os.close();
249 }
250
251 public static void style(Document doc, OutputStream os, InputStream style, Map parameters) throws TransformerException {
252 TransformerFactory factory=TransformerFactory.newInstance();
253 Transformer transformer = style==null ? factory.newTransformer() : factory.newTransformer(new StreamSource(style));
254 if (parameters!=null) {
255 Iterator it=parameters.entrySet().iterator();
256 while (it.hasNext()) {
257 Map.Entry entry=(Map.Entry) it.next();
258 transformer.setParameter((String) entry.getKey(), entry.getValue());
259 }
260 }
261 transformer.transform(new DOMSource(doc), new StreamResult(os));
262 }
263
264 public static Document parse(File file) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
265 InputStream is=new FileInputStream(file);
266 try {
267 return parse(is);
268 } finally {
269 is.close();
270 }
271 }
272
273 /**
274 * @param is
275 * @return
276 * @throws FactoryConfigurationError
277 * @throws ParserConfigurationException
278 * @throws IOException
279 * @throws SAXException
280 */
281 public static Document parse(InputStream is) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
282 return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
283 }
284
285 /**
286 * @param is
287 * @return
288 * @throws FactoryConfigurationError
289 * @throws ParserConfigurationException
290 * @throws IOException
291 * @throws SAXException
292 */
293 public static Document parse(Reader is) throws SAXException, IOException, ParserConfigurationException, FactoryConfigurationError {
294 return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(is));
295 }
296
297 /**
298 * Serializes object to XML. Specially treats collections,
299 * maps, arrays and DomSerializable objects.
300 * @param o
301 * @param holder
302 */
303 public static void toDom(Object o, Element holder) {
304 if (o!=null) {
305 DomSerializable ds = (DomSerializable) ConvertingService.convert(o, DomSerializable.class);
306 if (ds!=null) {
307 ds.toDom(holder);
308 }
309 }
310 }
311
312 /**
313 * Serializes object to XML. Specially treats collections,
314 * maps, arrays and DomSerializable objects.
315 * @param o
316 * @param name Element name
317 * @param parent parent element
318 */
319 public static void toDom(Object o, String name, Element parent) {
320 if (o!=null) {
321 DomSerializable ds = (DomSerializable) ConvertingService.convert(o, DomSerializable.class);
322 if (ds!=null) {
323 Element holder=parent.getOwnerDocument().createElement(name);
324 parent.appendChild(holder);
325 ds.toDom(holder);
326 }
327 }
328 }
329
330 public static Document toDom(Object o) throws ParserConfigurationException, FactoryConfigurationError {
331 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
332 Element root=document.createElement("root");
333 document.appendChild(root);
334 toDom(o, root);
335 return document;
336 }
337
338 public static String eval(Object item, String str) throws XPathExpressionException {
339 return getXPath().evaluate(str, item);
340 }
341
342 public static Object eval(Object item, String str, QName returnType) throws XPathExpressionException {
343 return getXPath().evaluate(str, item, returnType);
344 }
345
346 private static XPath getXPath() {
347 XPathFactory xPathFactory = XPathFactory.newInstance();
348 XPath xpath = xPathFactory.newXPath();
349 return xpath;
350 }
351
352 public static NodeList selectNodeList(Node contextNode, String str) throws XPathExpressionException {
353 return (NodeList) eval(contextNode, str, XPathConstants.NODESET);
354 }
355
356 public static Node selectSingleNode(Node contextNode, String str) throws XPathExpressionException {
357 NodeList nl = selectNodeList(contextNode, str);
358 return nl.getLength()==0 ? null : nl.item(0);
359 }
360
361}