1 16 package org.apache.cocoon.woody.util; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 21 import org.apache.cocoon.xml.SaxBuffer; 22 import org.apache.cocoon.xml.dom.DOMStreamer; 23 import org.apache.commons.lang.BooleanUtils; 24 import org.apache.excalibur.xml.sax.XMLizable; 25 import org.apache.xerces.dom.NodeImpl; 26 import org.apache.xerces.parsers.DOMParser; 27 import org.apache.xerces.xni.Augmentations; 28 import org.apache.xerces.xni.NamespaceContext; 29 import org.apache.xerces.xni.QName; 30 import org.apache.xerces.xni.XMLAttributes; 31 import org.apache.xerces.xni.XMLLocator; 32 import org.apache.xerces.xni.XNIException; 33 import org.w3c.dom.CDATASection ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.NodeList ; 38 import org.w3c.dom.Text ; 39 import org.w3c.dom.UserDataHandler ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.SAXNotSupportedException ; 43 44 57 public class DomHelper { 58 59 64 public static String getLocation(Element element) { 65 String location = null; 66 if (element instanceof NodeImpl) { 67 location = (String )((NodeImpl)element).getUserData("location"); 68 } 69 70 if (location != null) { 71 return location; 72 } 73 return "(location unknown)"; 74 } 75 76 public static String getSystemIdLocation(Element element) { 77 String loc = getLocation(element); 78 if (loc.charAt(0) != '(') { 79 int end = loc.lastIndexOf(':'); 80 if (end > 0) { 81 int start = loc.lastIndexOf(':', end - 1); 82 if (start >= 0) { 83 return loc.substring(0, start); 84 } 85 } 86 } 87 return null; 88 } 89 90 public static int getLineLocation(Element element) { 91 String loc = getLocation(element); 92 if (loc.charAt(0) != '(') { 93 int end = loc.lastIndexOf(':'); 94 if (end > 0) { 95 int start = loc.lastIndexOf(':', end - 1); 96 if (start >= 0) { 97 return Integer.parseInt(loc.substring(start + 1, end)); 98 } 99 } 100 } 101 return -1; 102 } 103 104 public static int getColumnLocation(Element element) { 105 String loc = getLocation(element); 106 if (loc.charAt(0) != '(') { 107 int end = loc.lastIndexOf(':'); 108 if (end > 0) { 109 return Integer.parseInt(loc.substring(end)); 110 } 111 } 112 return -1; 113 } 114 115 119 public static Element [] getChildElements(Element element, 120 String namespace) { 121 ArrayList elements = new ArrayList (); 122 NodeList nodeList = element.getChildNodes(); 123 for (int i = 0; i < nodeList.getLength(); i++) { 124 Node node = nodeList.item(i); 125 if (node instanceof Element 126 && namespace.equals(node.getNamespaceURI())) 127 elements.add(node); 128 } 129 return (Element [])elements.toArray(new Element [0]); 130 } 131 132 136 public static Element [] getChildElements(Element element, 137 String namespace, String localName) { 138 ArrayList elements = new ArrayList (); 139 NodeList nodeList = element.getChildNodes(); 140 for (int i = 0; i < nodeList.getLength(); i++) { 141 Node node = nodeList.item(i); 142 if (node instanceof Element 143 && namespace.equals(node.getNamespaceURI()) 144 && localName.equals(node.getLocalName())) { 145 elements.add(node); 146 } 147 } 148 return (Element [])elements.toArray(new Element [0]); 149 } 150 151 155 public static Element getChildElement(Element element, String namespace, 156 String localName) { 157 Element node = null; 158 try { 159 node = getChildElement(element, namespace, localName, false); 160 } catch (Exception e) { 161 node = null; 162 } 163 return node; 164 } 165 166 171 public static Element getChildElement(Element element, String namespace, 172 String localName, boolean required) throws Exception { 173 NodeList nodeList = element.getChildNodes(); 174 for (int i = 0; i < nodeList.getLength(); i++) { 175 Node node = nodeList.item(i); 176 if (node instanceof Element 177 && namespace.equals(node.getNamespaceURI()) 178 && localName.equals(node.getLocalName())) { 179 return (Element )node; 180 } 181 } 182 if (required) { 183 throw new Exception ("Missing element \"" + localName + 184 "\" as child of element \"" + element.getTagName() + 185 "\" at " + DomHelper.getLocation(element)); 186 } else { 187 return null; 188 } 189 } 190 191 195 public static String getAttribute(Element element, String attributeName) 196 throws Exception { 197 String attrValue = element.getAttribute(attributeName); 198 if (attrValue.equals("")) { 199 throw new Exception ("Missing attribute \"" + attributeName + 200 "\" on element \"" + element.getTagName() + 201 "\" at " + getLocation(element)); 202 } 203 return attrValue; 204 } 205 206 210 public static String getAttribute(Element element, String attributeName, 211 String defaultValue) throws Exception { 212 String attrValue = element.getAttribute(attributeName); 213 if (attrValue.equals("")) { 214 return defaultValue; 215 } 216 return attrValue; 217 } 218 219 public static int getAttributeAsInteger(Element element, 220 String attributeName) throws Exception { 221 String attrValue = getAttribute(element, attributeName); 222 try { 223 return Integer.parseInt(attrValue); 224 } catch (NumberFormatException e) { 225 throw new Exception ("Cannot parse the value \"" + attrValue + 226 "\" as an integer in the attribute \"" + attributeName + 227 "\" on the element \"" + element.getTagName() + 228 "\" at " + getLocation(element)); 229 } 230 } 231 232 public static int getAttributeAsInteger(Element element, 233 String attributeName, int defaultValue) throws Exception { 234 String attrValue = element.getAttribute(attributeName); 235 if (attrValue.equals("")) { 236 return defaultValue; 237 } else { 238 try { 239 return Integer.parseInt(attrValue); 240 } catch (NumberFormatException e) { 241 throw new Exception ("Cannot parse the value \"" + attrValue + 242 "\" as an integer in the attribute \"" + 243 attributeName + "\" on the element \"" + 244 element.getTagName() + "\" at " + 245 getLocation(element)); 246 } 247 } 248 } 249 250 public static boolean getAttributeAsBoolean(Element element, 251 String attributeName, boolean defaultValue) { 252 String attrValue = element.getAttribute(attributeName); 253 Boolean result; 254 try { 255 result = BooleanUtils.toBooleanObject(attrValue, "true", "false", null); 256 } catch (IllegalArgumentException iae) { 257 result = null; 258 } 259 if (result != null) { 260 return result.booleanValue(); 261 } 262 try { 263 result = BooleanUtils.toBooleanObject(attrValue, "yes", "no", null); 264 } catch (IllegalArgumentException iae) { 265 result = null; 266 } 267 if (result != null) { 268 return result.booleanValue(); 269 } 270 return defaultValue; 271 } 272 273 public static String getElementText(Element element) { 274 StringBuffer value = new StringBuffer (); 275 NodeList nodeList = element.getChildNodes(); 276 for (int i = 0; i < nodeList.getLength(); i++) { 277 Node node = nodeList.item(i); 278 if (node instanceof Text || node instanceof CDATASection ) { 279 value.append(node.getNodeValue()); 280 } 281 } 282 return value.toString(); 283 } 284 285 292 public static XMLizable compileElementContent(Element element) { 293 SaxBuffer saxBuffer = new SaxBuffer(); 294 DOMStreamer domStreamer = new DOMStreamer(); 295 domStreamer.setContentHandler(saxBuffer); 296 297 NodeList childNodes = element.getChildNodes(); 298 for (int i = 0; i < childNodes.getLength(); i++) { 299 try { 300 domStreamer.stream(childNodes.item(i)); 301 } catch (SAXException e) { 302 throw new RuntimeException ( 305 "Error in DomHelper.compileElementContent: " + 306 e.toString()); 307 } 308 } 309 return saxBuffer; 310 } 311 312 317 public static Document parse(InputSource inputSource) 318 throws SAXException , SAXNotSupportedException , IOException { 319 DOMParser domParser = new LocationTrackingDOMParser(); 320 domParser.setFeature( 321 "http://apache.org/xml/features/dom/defer-node-expansion", 322 false); 323 domParser.setFeature( 324 "http://apache.org/xml/features/dom/create-entity-ref-nodes", 325 false); 326 domParser.parse(inputSource); 327 return domParser.getDocument(); 328 } 329 330 334 public static class LocationTrackingDOMParser extends DOMParser { 335 XMLLocator locator; 336 337 public void startDocument(XMLLocator xmlLocator, String s, 338 NamespaceContext namespaceContext, 339 Augmentations augmentations) throws XNIException { 340 super.startDocument(xmlLocator, s, namespaceContext, 341 augmentations); 342 this.locator = xmlLocator; 343 setLocation(); 344 } 345 346 public void startElement(QName qName, XMLAttributes xmlAttributes, 347 Augmentations augmentations) throws XNIException { 348 super.startElement(qName, xmlAttributes, augmentations); 349 setLocation(); 350 } 351 352 private final void setLocation() { 353 if (this.locator == null) { 359 throw new RuntimeException ( 360 "Error: locator is null. Check that you have the" + 361 " correct version of Xerces (such as the one that" + 362 " comes with Cocoon) in your endorsed library path."); 363 } 364 NodeImpl node = null; 365 try { 366 node = (NodeImpl)this.getProperty( 367 "http://apache.org/xml/properties/dom/current-element-node"); 368 } catch (org.xml.sax.SAXException ex) { 369 System.err.println("except" + ex); 370 } 371 if (node != null) { 372 String location = locator.getLiteralSystemId() + ":" + 373 locator.getLineNumber() + ":" + locator.getColumnNumber(); 374 node.setUserData("location", location, (UserDataHandler )null); 375 } 376 } 377 } 378 } 379 | Popular Tags |