1 17 18 19 20 package org.apache.lenya.xml; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.Writer ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 import javax.xml.parsers.ParserConfigurationException ; 34 import javax.xml.transform.OutputKeys ; 35 import javax.xml.transform.Transformer ; 36 import javax.xml.transform.TransformerConfigurationException ; 37 import javax.xml.transform.TransformerException ; 38 import javax.xml.transform.TransformerFactory ; 39 import javax.xml.transform.dom.DOMSource ; 40 import javax.xml.transform.stream.StreamResult ; 41 42 import org.apache.xml.resolver.tools.CatalogResolver; 43 import org.w3c.dom.DOMException ; 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.DocumentType ; 46 import org.w3c.dom.Element ; 47 import org.w3c.dom.Node ; 48 import org.w3c.dom.NodeList ; 49 import org.w3c.dom.Text ; 50 import org.xml.sax.SAXException ; 51 52 53 56 public class DocumentHelper { 57 63 public static DocumentBuilder createBuilder() throws ParserConfigurationException { 64 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 65 factory.setNamespaceAware(true); 66 DocumentBuilder builder = factory.newDocumentBuilder(); 67 68 CatalogResolver cr = new CatalogResolver(); 69 builder.setEntityResolver(cr); 70 return builder; 71 } 72 73 88 public static Document createDocument(String namespaceUri, String qualifiedName, 89 DocumentType documentType) throws DOMException , ParserConfigurationException { 90 DocumentBuilder builder = createBuilder(); 91 Document document = builder.getDOMImplementation().createDocument(namespaceUri, 92 qualifiedName, documentType); 93 94 String name = "xmlns"; 96 int index = qualifiedName.indexOf(":"); 97 98 if (index > -1) { 99 name += (":" + qualifiedName.substring(0, index)); 100 } 101 102 document.getDocumentElement().setAttributeNS("http://www.w3.org/2000/xmlns/", name, namespaceUri); 103 104 return document; 105 } 106 107 116 public static Document readDocument(File file) 117 throws ParserConfigurationException , SAXException , IOException { 118 DocumentBuilder builder = createBuilder(); 119 return builder.parse(file); 120 } 121 122 131 public static Document readDocument(URL url) 132 throws ParserConfigurationException , SAXException , IOException { 133 DocumentBuilder builder = createBuilder(); 134 return builder.parse(url.toString()); 135 } 136 137 146 public static Document readDocument(URI uri) 147 throws ParserConfigurationException , SAXException , IOException { 148 DocumentBuilder builder = createBuilder(); 149 return builder.parse(uri.toString()); 150 } 151 152 161 public static Document readDocument(String string) 162 throws ParserConfigurationException , SAXException , IOException { 163 DocumentBuilder builder = createBuilder(); 164 return builder.parse(string); 165 } 166 167 176 public static Document readDocument(InputStream stream) 177 throws ParserConfigurationException , SAXException , IOException { 178 DocumentBuilder builder = createBuilder(); 179 return builder.parse(stream); 180 } 181 182 192 public static void writeDocument(Document document, File file) 193 throws TransformerConfigurationException , TransformerException , IOException { 194 file.getParentFile().mkdirs(); 195 file.createNewFile(); 196 197 DOMSource source = new DOMSource (document); 198 StreamResult result = new StreamResult (file); 199 getTransformer(document.getDoctype()).transform(source, result); 200 } 201 202 212 public static void writeDocument(Document document, Writer writer) 213 throws TransformerConfigurationException , TransformerException , IOException { 214 DOMSource source = new DOMSource (document); 215 StreamResult result = new StreamResult (writer); 216 getTransformer(document.getDoctype()).transform(source, result); 217 } 218 219 227 protected static Transformer getTransformer(DocumentType documentType) 228 throws TransformerConfigurationException { 229 TransformerFactory factory = TransformerFactory.newInstance(); 230 Transformer transformer = factory.newTransformer(); 231 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 232 transformer.setOutputProperty(OutputKeys.METHOD, "xml"); 233 234 if (documentType != null) { 235 transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId()); 236 transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId()); 237 } 238 239 return transformer; 240 } 241 242 255 public DocumentType createDocumentType(String qualifiedName, String publicId, String systemId) 256 throws ParserConfigurationException { 257 DocumentBuilder builder = createBuilder(); 258 259 return builder.getDOMImplementation().createDocumentType(qualifiedName, publicId, systemId); 260 } 261 262 269 public static Element getFirstChild(Element element, String namespaceUri) { 270 return getFirstChild(element, namespaceUri, "*"); 271 } 272 273 283 public static Element getFirstChild(Element element, String namespaceUri, String localName) { 284 Element [] children = getChildren(element, namespaceUri, localName); 285 286 if (children.length > 0) { 287 return children[0]; 288 } else { 289 return null; 290 } 291 } 292 293 298 public static Element [] getChildren(Element element) { 299 List childElements = new ArrayList (); 300 NodeList children = element.getElementsByTagName("*"); 301 302 for (int i = 0; i < children.getLength(); i++) { 303 if (children.item(i).getParentNode() == element) { 304 childElements.add(children.item(i)); 305 } 306 } 307 308 return (Element []) childElements.toArray(new Element [childElements.size()]); 309 } 310 311 319 public static Element [] getChildren(Element element, String namespaceUri) { 320 return getChildren(element, namespaceUri, "*"); 321 } 322 323 333 public static Element [] getChildren(Element element, String namespaceUri, String localName) { 334 List childElements = new ArrayList (); 335 NodeList children = element.getElementsByTagNameNS(namespaceUri, localName); 336 337 for (int i = 0; i < children.getLength(); i++) { 338 if (children.item(i).getParentNode() == element) { 339 childElements.add(children.item(i)); 340 } 341 } 342 343 return (Element []) childElements.toArray(new Element [childElements.size()]); 344 } 345 346 352 public static String getSimpleElementText(Element element) { 353 StringBuffer buffer = new StringBuffer (); 354 NodeList children = element.getChildNodes(); 355 356 for (int i = 0; i < children.getLength(); i++) { 357 Node child = children.item(i); 358 359 if (child instanceof Text ) { 360 buffer.append(child.getNodeValue()); 361 } 362 } 363 364 return buffer.toString(); 365 } 366 367 373 public static void setSimpleElementText(Element element, String text) { 374 NodeList children = element.getChildNodes(); 375 376 for (int i = 0; i < children.getLength(); i++) { 377 Node child = children.item(i); 378 element.removeChild(child); 379 } 380 381 Node textNode = element.getOwnerDocument().createTextNode(text); 382 element.appendChild(textNode); 383 } 384 385 386 394 public static Element [] getNextSiblings(Element element, String namespaceUri) { 395 return getNextSiblings(element, namespaceUri, "*"); 396 } 397 398 408 public static Element [] getNextSiblings(Element element, String namespaceUri, String localName) { 409 List childElements = new ArrayList (); 410 Element parent = (Element ) element.getParentNode(); 411 Element [] children=getChildren(parent, namespaceUri, localName); 412 413 int l = children.length; 414 for (int i = 0; i < children.length; i++) { 415 if (children[i] == element) { 416 l = i; 417 } 418 if (i>l){ 419 childElements.add(children[i]); 420 } 421 } 422 423 return (Element []) childElements.toArray(new Element [childElements.size()]); 424 } 425 } 426 | Popular Tags |