001: package org.apache.lucene.xmlparser;
002:
003: import java.io.Reader;
004:
005: import javax.xml.parsers.DocumentBuilder;
006: import javax.xml.parsers.DocumentBuilderFactory;
007:
008: import org.w3c.dom.Document;
009: import org.w3c.dom.Element;
010: import org.w3c.dom.Node;
011: import org.xml.sax.InputSource;
012:
013: /**
014: * Licensed to the Apache Software Foundation (ASF) under one or more
015: * contributor license agreements. See the NOTICE file distributed with
016: * this work for additional information regarding copyright ownership.
017: * The ASF licenses this file to You under the Apache License, Version 2.0
018: * (the "License"); you may not use this file except in compliance with
019: * the License. You may obtain a copy of the License at
020: *
021: * http://www.apache.org/licenses/LICENSE-2.0
022: *
023: * Unless required by applicable law or agreed to in writing, software
024: * distributed under the License is distributed on an "AS IS" BASIS,
025: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
026: * See the License for the specific language governing permissions and
027: * limitations under the License.
028: */
029: public class DOMUtils {
030: public static Element getChildByTagOrFail(Element e, String name)
031: throws ParserException {
032: Element kid = getChildByTagName(e, name);
033: if (null == kid) {
034: throw new ParserException(e.getTagName() + " missing \""
035: + name + "\" child element");
036: }
037: return kid;
038: }
039:
040: public static Element getFirstChildOrFail(Element e)
041: throws ParserException {
042: Element kid = getFirstChildElement(e);
043: if (null == kid) {
044: throw new ParserException(e.getTagName()
045: + " does not contain a child element");
046: }
047: return kid;
048: }
049:
050: public static String getAttributeOrFail(Element e, String name)
051: throws ParserException {
052: String v = e.getAttribute(name);
053: if (null == v) {
054: throw new ParserException(e.getTagName() + " missing \""
055: + name + "\" attribute");
056: }
057: return v;
058: }
059:
060: public static String getAttributeWithInheritanceOrFail(Element e,
061: String name) throws ParserException {
062: String v = getAttributeWithInheritance(e, name);
063: if (null == v) {
064: throw new ParserException(e.getTagName() + " missing \""
065: + name + "\" attribute");
066: }
067: return v;
068: }
069:
070: public static String getNonBlankTextOrFail(Element e)
071: throws ParserException {
072: String v = getText(e);
073: if (null != v)
074: v = v.trim();
075: if (null == v || 0 == v.length()) {
076: throw new ParserException(e.getTagName() + " has no text");
077: }
078: return v;
079: }
080:
081: /* Convenience method where there is only one child Element of a given name */
082: public static Element getChildByTagName(Element e, String name) {
083: for (Node kid = e.getFirstChild(); kid != null; kid = kid
084: .getNextSibling()) {
085: if ((kid.getNodeType() == Node.ELEMENT_NODE)
086: && (name.equals(kid.getNodeName()))) {
087: return (Element) kid;
088: }
089: }
090: return null;
091: }
092:
093: /**
094: * Returns an attribute value from this node, or first parent node with this attribute defined
095: * @param element
096: * @param attributeName
097: * @return A non-zero-length value if defined, otherwise null
098: */
099: public static String getAttributeWithInheritance(Element element,
100: String attributeName) {
101: String result = element.getAttribute(attributeName);
102: if ((result == null) || ("".equals(result))) {
103: Node n = element.getParentNode();
104: if ((n == element) || (n == null)) {
105: return null;
106: }
107: if (n instanceof Element) {
108: Element parent = (Element) n;
109: return getAttributeWithInheritance(parent,
110: attributeName);
111: }
112: return null; //we reached the top level of the document without finding attribute
113: }
114: return result;
115: }
116:
117: /* Convenience method where there is only one child Element of a given name */
118: public static String getChildTextByTagName(Element e, String tagName) {
119: Element child = getChildByTagName(e, tagName);
120: if (child != null) {
121: return getText(child);
122: }
123: return null;
124: }
125:
126: /* Convenience method to append a new child with text*/
127: public static Element insertChild(Element parent, String tagName,
128: String text) {
129: Element child = parent.getOwnerDocument()
130: .createElement(tagName);
131: parent.appendChild(child);
132: if (text != null) {
133: child.appendChild(child.getOwnerDocument().createTextNode(
134: text));
135: }
136: return child;
137: }
138:
139: public static String getAttribute(Element element,
140: String attributeName, String deflt) {
141: String result = element.getAttribute(attributeName);
142: if ((result == null) || ("".equals(result))) {
143: return deflt;
144: }
145: return result;
146: }
147:
148: public static float getAttribute(Element element,
149: String attributeName, float deflt) {
150: String result = element.getAttribute(attributeName);
151: if ((result == null) || ("".equals(result))) {
152: return deflt;
153: }
154: return Float.parseFloat(result);
155: }
156:
157: public static int getAttribute(Element element,
158: String attributeName, int deflt) {
159: String result = element.getAttribute(attributeName);
160: if ((result == null) || ("".equals(result))) {
161: return deflt;
162: }
163: return Integer.parseInt(result);
164: }
165:
166: public static boolean getAttribute(Element element,
167: String attributeName, boolean deflt) {
168: String result = element.getAttribute(attributeName);
169: if ((result == null) || ("".equals(result))) {
170: return deflt;
171: }
172: return Boolean.getBoolean(result);
173: }
174:
175: /* Returns text of node and all child nodes - without markup */
176: //MH changed to Node from Element 25/11/2005
177: public static String getText(Node e) {
178: StringBuffer sb = new StringBuffer();
179: getTextBuffer(e, sb);
180: return sb.toString();
181: }
182:
183: public static Element getFirstChildElement(Element element) {
184: for (Node kid = element.getFirstChild(); kid != null; kid = kid
185: .getNextSibling()) {
186: if (kid.getNodeType() == Node.ELEMENT_NODE) {
187: return (Element) kid;
188: }
189: }
190: return null;
191: }
192:
193: private static void getTextBuffer(Node e, StringBuffer sb) {
194: for (Node kid = e.getFirstChild(); kid != null; kid = kid
195: .getNextSibling()) {
196: switch (kid.getNodeType()) {
197: case Node.TEXT_NODE: {
198: sb.append(kid.getNodeValue());
199: break;
200: }
201: case Node.ELEMENT_NODE: {
202: getTextBuffer(kid, sb);
203: break;
204: }
205: case Node.ENTITY_REFERENCE_NODE: {
206: getTextBuffer(kid, sb);
207: break;
208: }
209: }
210: }
211: }
212:
213: /**
214: * Helper method to parse an XML file into a DOM tree, given a reader.
215: * @param is reader of the XML file to be parsed
216: * @return an org.w3c.dom.Document object
217: */
218: public static Document loadXML(Reader is) {
219:
220: DocumentBuilderFactory dbf = DocumentBuilderFactory
221: .newInstance();
222: DocumentBuilder db = null;
223:
224: try {
225: db = dbf.newDocumentBuilder();
226: } catch (Exception se) {
227: throw new RuntimeException("Parser configuration error", se);
228: }
229:
230: // Step 3: parse the input file
231: org.w3c.dom.Document doc = null;
232: try {
233: doc = db.parse(new InputSource(is));
234: //doc = db.parse(is);
235: } catch (Exception se) {
236: throw new RuntimeException("Error parsing file:" + se, se);
237: }
238:
239: return doc;
240: }
241: }
|