1 package org.apache.lucene.xmlparser; 2 import java.io.Reader; 3 4 import javax.xml.parsers.DocumentBuilder; 5 import javax.xml.parsers.DocumentBuilderFactory; 6 7 import org.w3c.dom.Document; 8 import org.w3c.dom.Element; 9 import org.w3c.dom.Node; 10 import org.xml.sax.InputSource; 11 /** 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 18 * 19 * http://www.apache.org/licenses/LICENSE-2.0 20 * 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. 26 */ 27 28 /** 29 * 30 */ 31 public class DOMUtils 32 { 33 public static Element getChildByTagOrFail(Element e, String name) throws ParserException 34 { 35 Element kid = getChildByTagName(e, name); 36 if (null == kid) 37 { 38 throw new ParserException(e.getTagName() + " missing \"" + name 39 + "\" child element"); 40 } 41 return kid; 42 } 43 44 public static Element getFirstChildOrFail(Element e) throws ParserException 45 { 46 Element kid = getFirstChildElement(e); 47 if (null == kid) 48 { 49 throw new ParserException(e.getTagName() 50 + " does not contain a child element"); 51 } 52 return kid; 53 } 54 55 public static String getAttributeOrFail(Element e, String name) throws ParserException 56 { 57 String v = e.getAttribute(name); 58 if (null == v) 59 { 60 throw new ParserException(e.getTagName() + " missing \"" + name 61 + "\" attribute"); 62 } 63 return v; 64 } 65 public static String getAttributeWithInheritanceOrFail(Element e, String name) throws ParserException 66 { 67 String v = getAttributeWithInheritance(e, name); 68 if (null == v) 69 { 70 throw new ParserException(e.getTagName() + " missing \"" + name 71 + "\" attribute"); 72 } 73 return v; 74 } 75 public static String getNonBlankTextOrFail(Element e) throws ParserException 76 { 77 String v = getText(e); 78 if (null != v) 79 v = v.trim(); 80 if (null == v || 0 == v.length()) 81 { 82 throw new ParserException(e.getTagName() + " has no text"); 83 } 84 return v; 85 } 86 87 88 89 90 91 92 /* Convenience method where there is only one child Element of a given name */ 93 public static Element getChildByTagName(Element e, String name) 94 { 95 for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) 96 { 97 if( (kid.getNodeType()==Node.ELEMENT_NODE) && (name.equals(kid.getNodeName())) ) 98 { 99 return (Element)kid; 100 } 101 } 102 return null; 103 } 104 105 /** 106 * Returns an attribute value from this node, or first parent node with this attribute defined 107 * @param element 108 * @param attributeName 109 * @return A non-zero-length value if defined, otherwise null 110 */ 111 public static String getAttributeWithInheritance(Element element, String attributeName) 112 { 113 String result=element.getAttribute(attributeName); 114 if( (result==null)|| ("".equals(result) ) ) 115 { 116 Node n=element.getParentNode(); 117 if((n==element)||(n==null)) 118 { 119 return null; 120 } 121 if(n instanceof Element) 122 { 123 Element parent=(Element) n; 124 return getAttributeWithInheritance(parent,attributeName); 125 } 126 return null; //we reached the top level of the document without finding attribute 127 } 128 return result; 129 } 130 131 132 133 /* Convenience method where there is only one child Element of a given name */ 134 public static String getChildTextByTagName(Element e, String tagName) 135 { 136 Element child=getChildByTagName(e,tagName); 137 if(child!=null) 138 { 139 return getText(child); 140 } 141 return null; 142 } 143 144 /* Convenience method to append a new child with text*/ 145 public static Element insertChild(Element parent, String tagName, String text) 146 { 147 Element child = parent.getOwnerDocument().createElement(tagName); 148 parent.appendChild(child); 149 if(text!=null) 150 { 151 child.appendChild(child.getOwnerDocument().createTextNode(text)); 152 } 153 return child; 154 } 155 156 public static String getAttribute(Element element, String attributeName, String deflt) 157 { 158 String result=element.getAttribute(attributeName); 159 if( (result==null)|| ("".equals(result) ) ) 160 { 161 return deflt; 162 } 163 return result; 164 } 165 public static float getAttribute(Element element, String attributeName, float deflt) 166 { 167 String result=element.getAttribute(attributeName); 168 if( (result==null)|| ("".equals(result) ) ) 169 { 170 return deflt; 171 } 172 return Float.parseFloat(result); 173 } 174 175 public static int getAttribute(Element element, String attributeName, int deflt) 176 { 177 String result=element.getAttribute(attributeName); 178 if( (result==null)|| ("".equals(result) ) ) 179 { 180 return deflt; 181 } 182 return Integer.parseInt(result); 183 } 184 185 public static boolean getAttribute(Element element, String attributeName, 186 boolean deflt) 187 { 188 String result = element.getAttribute(attributeName); 189 if ((result == null) || ("".equals(result))) 190 { 191 return deflt; 192 } 193 return Boolean.valueOf(result).booleanValue(); 194 } 195 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) 199 { 200 StringBuilder sb=new StringBuilder(); 201 getTextBuffer(e, sb); 202 return sb.toString(); 203 } 204 205 public static Element getFirstChildElement(Element element) 206 { 207 for (Node kid = element.getFirstChild(); kid != null; kid = kid 208 .getNextSibling()) 209 { 210 if (kid.getNodeType() == Node.ELEMENT_NODE) 211 { 212 return (Element) kid; 213 } 214 } 215 return null; 216 } 217 218 private static void getTextBuffer(Node e, StringBuilder sb) 219 { 220 for (Node kid = e.getFirstChild(); kid != null; kid = kid.getNextSibling()) 221 { 222 switch(kid.getNodeType()) 223 { 224 case Node.TEXT_NODE: 225 { 226 sb.append(kid.getNodeValue()); 227 break; 228 } 229 case Node.ELEMENT_NODE: 230 { 231 getTextBuffer(kid, sb); 232 break; 233 } 234 case Node.ENTITY_REFERENCE_NODE: 235 { 236 getTextBuffer(kid, sb); 237 break; 238 } 239 } 240 } 241 } 242 243 /** 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 247 */ 248 public static Document loadXML(Reader is) 249 { 250 251 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 252 DocumentBuilder db = null; 253 254 try 255 { 256 db = dbf.newDocumentBuilder(); 257 } 258 catch (Exception se) 259 { 260 throw new RuntimeException("Parser configuration error", se); 261 } 262 263 // Step 3: parse the input file 264 org.w3c.dom.Document doc = null; 265 try 266 { 267 doc = db.parse(new InputSource(is)); 268 //doc = db.parse(is); 269 } 270 catch (Exception se) 271 { 272 throw new RuntimeException("Error parsing file:" + se, se); 273 } 274 275 return doc; 276 } 277 } 278 279 280