001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.util;
010:
011: import org.w3c.dom.Document;
012: import org.w3c.dom.Element;
013: import org.w3c.dom.NamedNodeMap;
014: import org.w3c.dom.Node;
015: import org.w3c.dom.NodeList;
016:
017: import java.math.BigDecimal;
018: import java.text.ParseException;
019: import java.util.ArrayList;
020: import java.util.Date;
021:
022: /**
023: * @author Gennady Krizhevsky
024: */
025: public class XmlDomHelper {
026:
027: private final Document document;
028:
029: public static final Node[] EMPTY_NODE_ARRAY = new Node[0];
030:
031: public XmlDomHelper(Document document) {
032: this .document = document;
033: }
034:
035: public static String D2S(Date date) {
036: return TypeUtil.D2S(date, TypeUtil.yyyyMMdd_hhmmssSSS());
037: }
038:
039: public static Date S2D(String value) {
040: try {
041: return TypeUtil.S2D(value, TypeUtil.yyyyMMdd_hhmmssSSS());
042: } catch (ParseException e) {
043: new XmlHelperRuntimeException(
044: "ParseException: Cannot parse date value : "
045: + e.getMessage());
046: }
047: return null;
048: }
049:
050: public static Boolean S2B(String value) {
051: return TypeUtil.S2B(value, TypeUtil.TRUE_FALSE);
052: }
053:
054: public static boolean S2b(String value) {
055: return TypeUtil.S2b(value, TypeUtil.TRUE_FALSE);
056: }
057:
058: public static String B2S(Boolean value) {
059: return TypeUtil.B2S(value, TypeUtil.TRUE_FALSE);
060: }
061:
062: public static String b2S(boolean value) {
063: return TypeUtil.b2S(value, TypeUtil.TRUE_FALSE);
064: }
065:
066: public static Long S2L(String value) {
067: return TypeUtil.S2L(value);
068: }
069:
070: public static BigDecimal S2BigDecimal(String value) {
071: return TypeUtil.S2BigDecimal(value);
072: }
073:
074: public Node getFirstNodeByType(Node parentNode, int type) {
075: Node[] childNodes = getNodesByType(parentNode, type);
076: return childNodes.length > 0 ? childNodes[0] : null;
077: }
078:
079: public Element getFirstElement(Node parentNode) {
080: return (Element) getFirstNodeByType(parentNode,
081: Node.ELEMENT_NODE);
082: }
083:
084: public Element getElementByName(Node parentNode, String tagName,
085: boolean assertExists) {
086: return (Element) getNodeByName(parentNode, tagName,
087: Node.ELEMENT_NODE, assertExists);
088: }
089:
090: public Node getNodeByName(Node parentNode, String tagName,
091: int type, boolean assertExists) {
092: Node[] resultNodes = getNodesByName(parentNode, tagName, type,
093: assertExists);
094: if (resultNodes.length == 0 && assertExists) {
095: throw new XmlHelperRuntimeException(
096: "Cannot find node by tag " + tagName);
097: }
098: return resultNodes.length == 0 ? null : resultNodes[0];
099: }
100:
101: public Element[] getElementsByName(Node parentNode, String tagName,
102: boolean assertExists) {
103: Node[] nodes = getNodesByName(parentNode, tagName,
104: Node.ELEMENT_NODE, assertExists);
105: return nodes2elements(nodes);
106: }
107:
108: public static Element[] nodes2elements(Node[] nodes) {
109: Element[] elements = new Element[nodes.length];
110: for (int i = 0; i < elements.length; i++) {
111: elements[i] = (Element) nodes[i];
112: }
113: return elements;
114: }
115:
116: public Node[] getNodesByName(Node parentNode, String tagName,
117: int type, boolean assertExists) {
118: ArrayList nodeList = new ArrayList();
119: NodeList childNodes = parentNode.getChildNodes();
120: for (int i = 0; i < childNodes.getLength(); i++) {
121: Node node = childNodes.item(i);
122: if (tagName.equals(node.getNodeName())
123: && node.getNodeType() == type) {
124: nodeList.add(node);
125: }
126: }
127: if (nodeList.isEmpty() && assertExists) {
128: throw new XmlHelperRuntimeException(
129: "Cannot find nodes by tag " + tagName);
130: }
131: return (Node[]) nodeList.toArray(new Node[nodeList.size()]);
132: }
133:
134: public Node[] getNodesByType(Node parentNode, int type) {
135: ArrayList nodeList = new ArrayList();
136: NodeList childNodes = parentNode.getChildNodes();
137: for (int i = 0; i < childNodes.getLength(); i++) {
138: Node node = childNodes.item(i);
139: if (node.getNodeType() == type) {
140: nodeList.add(node);
141: }
142: }
143: return (Node[]) nodeList.toArray(new Node[nodeList.size()]);
144: }
145:
146: public String getAttributeByName(Node node, String tagName,
147: boolean assertExist) {
148: NamedNodeMap attributes = node.getAttributes();
149: if (attributes == null) {
150: if (assertExist) {
151: throw new XmlHelperRuntimeException(
152: "Cannot find element by tag " + tagName);
153: } else {
154: return null;
155: }
156: }
157: Node namedItem = attributes.getNamedItem(tagName);
158: if (namedItem == null && assertExist) {
159: throw new XmlHelperRuntimeException(
160: "Cannot find element by tag " + tagName);
161: }
162: return namedItem == null ? null : namedItem.getNodeValue();
163: }
164:
165: public boolean getAttributeByNameAsBoolean(Node node,
166: String tagName, boolean assertExist) {
167: String value = getAttributeByName(node, tagName, assertExist);
168: return value != null && S2b(value);
169: }
170:
171: public int getAttributeByNameAsInt(Node node, String tagName,
172: boolean assertExist) {
173: String value = getAttributeByName(node, tagName, assertExist);
174: int idx;
175: try {
176: idx = Integer.valueOf(value).intValue();
177: } catch (NumberFormatException e) {
178: throw new XmlHelperRuntimeException(
179: "Cannot convert idx value " + value + " to number");
180: }
181: return idx;
182: }
183:
184: public String getNodeText(Node valueNode) {
185: if (valueNode == null) {
186: return null;
187: }
188: Node firstChild = valueNode.getFirstChild();
189: return firstChild == null ? null : firstChild.getNodeValue();
190: }
191:
192: public Element createElement(Node parentNode, String tagName) {
193: Element childElement = createElement(tagName);
194: parentNode.appendChild(childElement);
195: return childElement;
196: }
197:
198: public Element createElement(String tagName) {
199: return document.createElement(tagName);
200: }
201:
202: public Element getFirstElementByName(Node parentNode,
203: String tagName, boolean assertExist) {
204: return (Element) getFirstNodeByName(parentNode, tagName,
205: Node.ELEMENT_NODE, assertExist);
206: }
207:
208: public Node getFirstNodeByName(Node parentNode, String tagName,
209: int type, boolean assertExist) {
210: Node[] childNodes = getNodesByType(parentNode, type);
211: if (assertExist
212: && (childNodes == null || childNodes.length == 0)) {
213: throw new XmlHelperRuntimeException(
214: "Cannot find child node by tag " + tagName);
215: }
216: Node childNode = null;
217: if (childNodes != null && childNodes.length > 0) {
218: childNode = extractPoNode(childNodes, tagName, assertExist);
219: }
220: return childNode;
221: }
222:
223: public Element[] getElements(Node node) {
224: return nodes2elements(getNodesByType(node, Node.ELEMENT_NODE));
225: }
226:
227: public Node extractPoNode(Node[] poElements, String tagName,
228: boolean assertExist) {
229: Node poNode = null;
230: for (int i = 0; i < poElements.length; i++) {
231: Node node = poElements[i];
232: if (tagName.equals(node.getNodeName())) {
233: poNode = node;
234: break;
235: }
236: }
237: if (assertExist && (poNode == null)) {
238: throw new XmlHelperRuntimeException(
239: "Cannot find element by tag " + tagName);
240: }
241: return poNode;
242: }
243:
244: }
|