1 package org.apache.lucene.xmlparser;
4 import javax.xml.parsers.DocumentBuilder;
5 import javax.xml.parsers.DocumentBuilderFactory;
7 import org.w3c.dom.Document;
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10 import org.xml.sax.InputSource;
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed with
14 * this work for additional information regarding copyright ownership.
15 * The ASF licenses this file to You under the Apache License, Version 2.0
16 * (the "License"); you may not use this file except in compliance with
17 * the License. You may obtain a copy of the License at
19 * http://www.apache.org/licenses/LICENSE-2.0
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
33 public static Element getChildByTagOrFail(Element e, String name) throws ParserException
35 Element kid = getChildByTagName(e, name);
38 throw new ParserException(e.getTagName() + " missing \"" + name
39 + "\" child element");
44 public static Element getFirstChildOrFail(Element e) throws ParserException
46 Element kid = getFirstChildElement(e);
49 throw new ParserException(e.getTagName()
50 + " does not contain a child element");
55 public static String getAttributeOrFail(Element e, String name) throws ParserException
57 String v = e.getAttribute(name);
60 throw new ParserException(e.getTagName() + " missing \"" + name
65 public static String getAttributeWithInheritanceOrFail(Element e, String name) throws ParserException
67 String v = getAttributeWithInheritance(e, name);
70 throw new ParserException(e.getTagName() + " missing \"" + name
75 public static String getNonBlankTextOrFail(Element e) throws ParserException
77 String v = getText(e);
80 if (null == v || 0 == v.length())
82 throw new ParserException(e.getTagName() + " has no text");
92 /* Convenience method where there is only one child Element of a given name */
93 public static Element getChildByTagName(Element e, String name)
95 for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling())
97 if( (kid.getNodeType()==Node.ELEMENT_NODE) && (name.equals(kid.getNodeName())) )
106 * Returns an attribute value from this node, or first parent node with this attribute defined
108 * @param attributeName
109 * @return A non-zero-length value if defined, otherwise null
111 public static String getAttributeWithInheritance(Element element, String attributeName)
113 String result=element.getAttribute(attributeName);
114 if( (result==null)|| ("".equals(result) ) )
116 Node n=element.getParentNode();
117 if((n==element)||(n==null))
121 if(n instanceof Element)
123 Element parent=(Element) n;
124 return getAttributeWithInheritance(parent,attributeName);
126 return null; //we reached the top level of the document without finding attribute
133 /* Convenience method where there is only one child Element of a given name */
134 public static String getChildTextByTagName(Element e, String tagName)
136 Element child=getChildByTagName(e,tagName);
139 return getText(child);
144 /* Convenience method to append a new child with text*/
145 public static Element insertChild(Element parent, String tagName, String text)
147 Element child = parent.getOwnerDocument().createElement(tagName);
148 parent.appendChild(child);
151 child.appendChild(child.getOwnerDocument().createTextNode(text));
156 public static String getAttribute(Element element, String attributeName, String deflt)
158 String result=element.getAttribute(attributeName);
159 if( (result==null)|| ("".equals(result) ) )
165 public static float getAttribute(Element element, String attributeName, float deflt)
167 String result=element.getAttribute(attributeName);
168 if( (result==null)|| ("".equals(result) ) )
172 return Float.parseFloat(result);
175 public static int getAttribute(Element element, String attributeName, int deflt)
177 String result=element.getAttribute(attributeName);
178 if( (result==null)|| ("".equals(result) ) )
182 return Integer.parseInt(result);
185 public static boolean getAttribute(Element element, String attributeName,
188 String result = element.getAttribute(attributeName);
189 if ((result == null) || ("".equals(result)))
193 return Boolean.valueOf(result).booleanValue();
196 /* Returns text of node and all child nodes - without markup */
197 //MH changed to Node from Element 25/11/2005
198 public static String getText(Node e)
200 StringBuilder sb=new StringBuilder();
201 getTextBuffer(e, sb);
202 return sb.toString();
205 public static Element getFirstChildElement(Element element)
207 for (Node kid = element.getFirstChild(); kid != null; kid = kid
210 if (kid.getNodeType() == Node.ELEMENT_NODE)
212 return (Element) kid;
218 private static void getTextBuffer(Node e, StringBuilder sb)
220 for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling())
222 switch(kid.getNodeType())
226 sb.append(kid.getNodeValue());
229 case Node.ELEMENT_NODE:
231 getTextBuffer(kid, sb);
234 case Node.ENTITY_REFERENCE_NODE:
236 getTextBuffer(kid, sb);
244 * Helper method to parse an XML file into a DOM tree, given a reader.
245 * @param is reader of the XML file to be parsed
246 * @return an org.w3c.dom.Document object
248 public static Document loadXML(Reader is)
251 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
252 DocumentBuilder db = null;
256 db = dbf.newDocumentBuilder();
260 throw new RuntimeException("Parser configuration error", se);
263 // Step 3: parse the input file
264 org.w3c.dom.Document doc = null;
267 doc = db.parse(new InputSource(is));
268 //doc = db.parse(is);
272 throw new RuntimeException("Error parsing file:" + se, se);