AbstractDomObject.java

biz/hammurapi/xml/dom/AbstractDomObject.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 073 [java.lang.StringBuffer] In Java 5 use StringBuilder instead of StringBuffer if access is single-threaded, e.g. StringBuffer is used as a local variable . 2 49:17
Java Inspector 089 Type is not documented 2 37:1
Java Inspector 089 Undocumented method 2 39:9
Java Inspector 089 Method is not properly documented 2 48:9
Java Inspector 089 Parameter node is not documented 2 48:9
Java Inspector 089 Parameter root is not documented 2 66:9
Java Inspector 089 Parameter name is not documented 2 66:9
Java Inspector 089 Parameter text is not documented 2 66:9
Java Inspector 089 Method return value is not documented 2 66:9
Java Inspector 089 Parameter root is not documented 2 81:9
Java Inspector 089 Parameter name is not documented 2 81:9
Java Inspector 089 Method return value is not properly documented 2 81: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 javax.xml.xpath.XPathExpressionException;
27
28import org.w3c.dom.Document;
29import org.w3c.dom.Element;
30import org.w3c.dom.Node;
31import org.w3c.dom.NodeList;
32import org.w3c.dom.Text;
33
34/**
35 * @author Pavel Vlasov
36 */
37public class AbstractDomObject {
38
39 public static String getElementText(Element root, String child) throws XPathExpressionException {
40 Node n=DOMUtils.selectSingleNode(root, child);
41 return n==null ? null : getElementText(n);
42 }
43
44 /**
45 * @param node
46 * @return Concatenation of all text sub
47 */
48 public static String getElementText(Node node) {
49 StringBuffer ret=new StringBuffer();
50 NodeList childNodes = node.getChildNodes();
51 for (int i=0, j=childNodes.getLength(); i<j; i++) {
52 Node item = childNodes.item(i);
53 if (item instanceof Text) {
54 ret.append(item.getNodeValue());
55 }
56 }
57 return ret.toString();
58 }
59
60 /**
61 * Adds element with text if text is not null.
62 * @param root
63 * @param name
64 * @param text
65 */
66 public static Element addTextElement(Element root, String name, String text) {
67 if (text!=null) {
68 Element e=addElement(root, name);
69 e.appendChild(root.getOwnerDocument().createTextNode(text));
70 return e;
71 }
72 return null;
73 }
74
75 /**
76 * Adds element with specified name.
77 * @param root
78 * @param name
79 * @return
80 */
81 public static Element addElement(Node root, String name) {
82 Document doc = root instanceof Document ? (Document) root : root.getOwnerDocument();
83 Element ret=doc.createElement(name);
84 root.appendChild(ret);
85 return ret;
86 }
87}
88