001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.xml;
024:
025: import java.util.*;
026: import org.xml.sax.*;
027: import org.w3c.dom.*;
028: import com.mchange.v1.util.DebugUtils;
029:
030: public final class DomParseUtils {
031: final static boolean DEBUG = true;
032:
033: /**
034: * @return null if child doesn't exist.
035: */
036: public static String allTextFromUniqueChild(Element elem,
037: String childTagName) throws DOMException {
038: return allTextFromUniqueChild(elem, childTagName, false);
039: }
040:
041: /**
042: * @return null if child doesn't exist.
043: */
044: public static String allTextFromUniqueChild(Element elem,
045: String childTagName, boolean trim) throws DOMException {
046: Element uniqueChild = uniqueChildByTagName(elem, childTagName);
047: if (uniqueChild == null)
048: return null;
049: else
050: return DomParseUtils.allTextFromElement(uniqueChild, trim);
051: }
052:
053: public static Element uniqueChild(Element elem, String childTagName)
054: throws DOMException {
055: return uniqueChildByTagName(elem, childTagName);
056: }
057:
058: /**
059: * @deprecated use uniqueChild(Element elem, String childTagName)
060: */
061: public static Element uniqueChildByTagName(Element elem,
062: String childTagName) throws DOMException {
063: NodeList nl = elem.getElementsByTagName(childTagName);
064: int len = nl.getLength();
065: if (DEBUG)
066: DebugUtils.myAssert(len <= 1, "There is more than one ("
067: + len + ") child with tag name: " + childTagName
068: + "!!!");
069: return (len == 1 ? (Element) nl.item(0) : null);
070: }
071:
072: public static String allText(Element elem) throws DOMException {
073: return allTextFromElement(elem);
074: }
075:
076: public static String allText(Element elem, boolean trim)
077: throws DOMException {
078: return allTextFromElement(elem, trim);
079: }
080:
081: /** @deprecated use allText(Element elem) */
082: public static String allTextFromElement(Element elem)
083: throws DOMException {
084: return allTextFromElement(elem, false);
085: }
086:
087: /** @deprecated use allText(Element elem, boolean trim) */
088: public static String allTextFromElement(Element elem, boolean trim)
089: throws DOMException {
090: StringBuffer textBuf = new StringBuffer();
091: NodeList nl = elem.getChildNodes();
092: for (int j = 0, len = nl.getLength(); j < len; ++j) {
093: Node node = nl.item(j);
094: if (node instanceof Text) //includes Text and CDATA!
095: textBuf.append(node.getNodeValue());
096: }
097: String out = textBuf.toString();
098: return (trim ? out.trim() : out);
099: }
100:
101: public static String[] allTextFromImmediateChildElements(
102: Element parent, String tagName) throws DOMException {
103: return allTextFromImmediateChildElements(parent, tagName, false);
104: }
105:
106: public static String[] allTextFromImmediateChildElements(
107: Element parent, String tagName, boolean trim)
108: throws DOMException {
109: NodeList nl = immediateChildElementsByTagName(parent, tagName);
110: int len = nl.getLength();
111: String[] out = new String[len];
112: for (int i = 0; i < len; ++i)
113: out[i] = allText((Element) nl.item(i), trim);
114: return out;
115: }
116:
117: public static NodeList immediateChildElementsByTagName(
118: Element parent, String tagName) throws DOMException {
119: return getImmediateChildElementsByTagName(parent, tagName);
120: }
121:
122: /**
123: * @deprecated use immediateChildrenByTagName( Element parent, String tagName )
124: */
125: public static NodeList getImmediateChildElementsByTagName(
126: Element parent, String tagName) throws DOMException {
127: final List nodes = new ArrayList();
128: for (Node child = parent.getFirstChild(); child != null; child = child
129: .getNextSibling())
130: if (child instanceof Element
131: && ((Element) child).getTagName().equals(tagName))
132: nodes.add(child);
133: return new NodeList() {
134: public int getLength() {
135: return nodes.size();
136: }
137:
138: public Node item(int i) {
139: return (Node) nodes.get(i);
140: }
141: };
142: }
143:
144: public static String allTextFromUniqueImmediateChild(Element elem,
145: String childTagName) throws DOMException {
146: Element uniqueChild = uniqueImmediateChildByTagName(elem,
147: childTagName);
148: if (uniqueChild == null)
149: return null;
150: return DomParseUtils.allTextFromElement(uniqueChild);
151: }
152:
153: public static Element uniqueImmediateChild(Element elem,
154: String childTagName) throws DOMException {
155: return uniqueImmediateChildByTagName(elem, childTagName);
156: }
157:
158: /**
159: * @deprecated use uniqueImmediateChild(Element elem, String childTagName)
160: */
161: public static Element uniqueImmediateChildByTagName(Element elem,
162: String childTagName) throws DOMException {
163: NodeList nl = getImmediateChildElementsByTagName(elem,
164: childTagName);
165: int len = nl.getLength();
166: if (DEBUG)
167: DebugUtils.myAssert(len <= 1, "There is more than one ("
168: + len + ") child with tag name: " + childTagName
169: + "!!!");
170: return (len == 1 ? (Element) nl.item(0) : null);
171: }
172:
173: /**
174: * @deprecated use Element.getAttribute(String val)
175: */
176: public static String attrValFromElement(Element element,
177: String attrName) throws DOMException {
178: Attr attr = element.getAttributeNode(attrName);
179: return (attr == null ? null : attr.getValue());
180: }
181:
182: private DomParseUtils() {
183: }
184: }
|