1 9 10 package org.jboss.portal.common.util; 11 12 import org.apache.xpath.CachedXPathAPI; 13 import org.w3c.dom.Attr ; 14 import org.w3c.dom.DOMException ; 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.NamedNodeMap ; 18 import org.w3c.dom.Node ; 19 import org.w3c.dom.NodeList ; 20 import org.w3c.dom.Text ; 21 import org.xml.sax.EntityResolver ; 22 import org.xml.sax.ErrorHandler ; 23 import org.xml.sax.InputSource ; 24 import org.xml.sax.SAXException ; 25 import org.xml.sax.helpers.DefaultHandler ; 26 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import javax.xml.transform.OutputKeys ; 33 import javax.xml.transform.Result ; 34 import javax.xml.transform.Source ; 35 import javax.xml.transform.Templates ; 36 import javax.xml.transform.Transformer ; 37 import javax.xml.transform.TransformerConfigurationException ; 38 import javax.xml.transform.TransformerException ; 39 import javax.xml.transform.TransformerFactory ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.stream.StreamResult ; 42 import javax.xml.transform.stream.StreamSource ; 43 import java.io.ByteArrayInputStream ; 44 import java.io.ByteArrayOutputStream ; 45 import java.io.FileNotFoundException ; 46 import java.io.IOException ; 47 import java.io.InputStream ; 48 import java.io.InputStreamReader ; 49 import java.io.OutputStream ; 50 import java.io.Reader ; 51 import java.io.StringReader ; 52 import java.io.StringWriter ; 53 import java.io.Writer ; 54 import java.net.URL ; 55 import java.util.ArrayList ; 56 import java.util.Properties ; 57 import java.util.List ; 58 59 66 public class XML 67 { 68 69 72 public static final String XML_LANG_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace"; 73 74 77 private static final DocumentBuilderFactory buildFactory = DocumentBuilderFactory.newInstance(); 78 79 82 private static final TransformerFactory transformerFactory = TransformerFactory.newInstance(); 83 84 87 private static Properties DEFAULT_FORMAT = new Properties (); 88 89 92 public static final String PARAM_YES = "yes"; 93 96 public static final String PARAM_NO = "no"; 97 100 public static final String ATTRIB_OMIT_XML_DECLARATION = "omit-xml-declaration"; 101 104 public static final String ATTRIB_CDATA_SECTION_ELEMENTS = "cdata-section-elements"; 105 108 public static final String ATTRIB_METHOD = "method"; 109 112 public static final String ATTRIB_INDENT = "indent"; 113 116 public static final String ATTRIB_HREF = "href"; 117 120 public static final String DIRECTIVE_IMPORT = "xsl:import"; 121 124 public static final String DIRECTIVE_INCLUDE = "xsl:include"; 125 128 public static final boolean DEFAULT_NAMESPACE_AWARE = true; 129 132 public static final boolean DEFAULT_VALIDATION = false; 133 134 private static final SAXParserFactory [][] SAXPARSERFACTORIES = new SAXParserFactory [2][2]; 136 private static TransformerFactory g_transformerFactory = null; 137 private static final String ERR_NO_NULL_ARGS_ALLOWED = "1"; 139 private static final String ERR_NO_NULL_DOC_AND_NODE_ALLOWED = "2"; 140 private static final String ERR_NO_NULL_PARENT_AND_NODE_ALLOWED = "3"; 141 private static final String ERR_NO_NULL_DOC_OR_PARENT_AND_NODE_ALLOWED = "4"; 142 private static final String ERR_NO_NULL_PARENT_NODE_ALLOWED = "5"; 143 private static final String ERR_NO_NULL_NODE_AND_ATTNAME_ALLOWED = "6"; 144 private static final String ERR_NO_NULL_DOC_AND_ATTNAME_ALLOWED = "7"; 145 private static final String ERR_NO_NULL_PARENT_AND_CONTENT_ALLOWED = "8"; 146 private static final String ERR_NO_NULL_SIBLING_AND_NODE_ALLOWED = "9"; 147 private static final String ERR_NO_NULL_DOC_AND_DIRECTIVENAME_ALLOWED = "10"; 148 private static final String ERR_IS_NOT_A_STYLESHEET = "11"; 149 private static final String ERR_NO_NULL_INPUTSOURCE_ALLOWED = "12"; 150 private static final String MSG_FOUND_BY_PARENT = "13"; 151 private static final String MSG_URI_NEEDS_RESOLUTION = "14"; 152 private static final String MSG_RELATIVE_URI = "15"; 153 private static final String MSG_NOT_RESOLVED = "16"; 154 private static final String ERR_NO_RESOLVE = "17"; 155 private static final String ERR_WRONG_NODETYPE = "18"; 156 private static final String ERR_SRC_AND_DEST_DOC_EQUAL = "19"; 157 158 static 159 { 160 DEFAULT_FORMAT.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 161 DEFAULT_FORMAT.setProperty(OutputKeys.INDENT, "yes"); 162 DEFAULT_FORMAT.setProperty(OutputKeys.METHOD, "xml"); 163 DEFAULT_FORMAT.setProperty(OutputKeys.ENCODING, "UTF-8"); 164 } 165 166 private static DocumentBuilderFactory [][] docBuilderFactories = new DocumentBuilderFactory [2][2]; 168 static 169 { 170 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 171 factory.setValidating(false); 172 factory.setNamespaceAware(false); 173 174 docBuilderFactories[0][0] = factory; 175 176 factory = DocumentBuilderFactory.newInstance(); 177 factory.setValidating(false); 178 factory.setNamespaceAware(true); 179 180 docBuilderFactories[0][1] = factory; 181 182 factory = DocumentBuilderFactory.newInstance(); 183 factory.setValidating(true); 184 factory.setNamespaceAware(false); 185 186 docBuilderFactories[1][0] = factory; 187 188 factory = DocumentBuilderFactory.newInstance(); 189 factory.setValidating(true); 190 factory.setNamespaceAware(true); 191 192 docBuilderFactories[1][1] = factory; 193 } 194 195 private static ThreadLocal documentBuilders = new ThreadLocal () 197 { 198 protected synchronized Object initialValue() 199 { 200 return new DocumentBuilder [2][2]; 201 } 202 }; 203 204 207 private XML() 208 { 209 } 210 211 214 public static DocumentBuilderFactory getDocumentBuilderFactory() 215 { 216 return buildFactory; 217 } 218 219 222 public static final String toString(Element element, Properties properties) throws ParserConfigurationException , TransformerException 223 { 224 Document doc = buildFactory.newDocumentBuilder().newDocument(); 225 element = (Element )doc.importNode(element, true); 226 doc.appendChild(element); 227 return toString(doc, properties); 228 } 229 230 233 public static final String toString(Element element) throws ParserConfigurationException , TransformerException 234 { 235 return toString(element, DEFAULT_FORMAT); 236 } 237 238 246 public static final String toString(Document doc) throws TransformerException 247 { 248 return toString(doc, DEFAULT_FORMAT); 249 } 250 251 254 public static String toString(Document doc, Properties format) throws TransformerException 255 { 256 Transformer transformer = transformerFactory.newTransformer(); 257 transformer.setOutputProperties(format); 258 StringWriter writer = new StringWriter (); 259 Source source = new DOMSource (doc); 260 Result result = new StreamResult (writer); 261 transformer.transform(source, result); 262 return writer.toString(); 263 } 264 265 268 public static Document toDocument(String text) throws ParserConfigurationException , SAXException , IOException 269 { 270 DocumentBuilder builder = buildFactory.newDocumentBuilder(); 271 StringReader reader = new StringReader (text); 272 InputSource source = new InputSource (); 273 source.setCharacterStream(reader); 274 Document doc = builder.parse(source); 275 return doc; 276 } 277 278 281 public static Element toElement(String text) throws ParserConfigurationException , SAXException , IOException 282 { 283 Document doc = toDocument(text); 284 return doc.getDocumentElement(); 285 } 286 287 295 public static String asString(Element element) throws IllegalArgumentException 296 { 297 return asString(element, true); 298 } 299 300 307 public static String asString(Element element, boolean trim) throws IllegalArgumentException 308 { 309 if (element == null) 310 { 311 throw new IllegalArgumentException ("No null element allowed"); 312 } 313 314 StringBuffer buffer = new StringBuffer (); 316 NodeList children = element.getChildNodes(); 317 for (int i = 0;i < children.getLength();i++) 318 { 319 Node child = children.item(i); 320 switch (child.getNodeType()) 321 { 322 case Node.CDATA_SECTION_NODE: 323 case Node.TEXT_NODE: 324 buffer.append(((Text )child).getData()); 325 break; 326 case Node.ELEMENT_NODE: 327 throw new IllegalArgumentException ("Mixed content now allowed"); 328 default: 329 break; 330 } 331 } 332 String result = buffer.toString(); 333 if (trim) 334 { 335 result.trim(); 336 } 337 return result; 338 } 339 340 350 public static Element getUniqueChild(Element element, boolean strict) throws IllegalArgumentException , 351 NoSuchElementException, TooManyElementException 352 { 353 if (element == null) 354 { 355 throw new IllegalArgumentException ("No element specified"); 356 } 357 Element childElt = null; 358 NodeList list = element.getChildNodes(); 359 for (int i = 0; i < list.getLength(); i++) 360 { 361 Node childNode = list.item(i); 362 if (childNode instanceof Element ) 363 { 364 if (childElt == null) 365 { 366 childElt = (Element )childNode; 367 } 368 else 369 { 370 throw new TooManyElementException("More than one child element for element " + element.getNodeName()); 371 } 372 } 373 } 374 if (strict && childElt == null) 375 { 376 throw new NoSuchElementException("No child element for element " + element.getNodeName()); 377 } 378 return childElt; 379 } 380 381 392 public static Element getUniqueChild(Element element, String name, boolean strict) throws IllegalArgumentException , 393 NoSuchElementException, TooManyElementException 394 { 395 List list = getChildren(element, name); 396 switch (list.size()) 397 { 398 case 0: 399 if (strict) 400 { 401 throw new NoSuchElementException("Missing child " + name + " of element " + element.getNodeName()); 402 } 403 else 404 { 405 return null; 406 } 407 case 1: 408 return (Element )list.get(0); 409 default: 410 throw new TooManyElementException("Too many children for element " + element.getNodeName()); 411 } 412 } 413 414 422 public static List getChildren(Element element, String name) throws IllegalArgumentException 423 { 424 if (element == null) 425 { 426 throw new IllegalArgumentException ("No element found"); 427 } 428 if (name == null) 429 { 430 throw new IllegalArgumentException ("No name specified"); 431 } 432 ArrayList result = new ArrayList (); 433 NodeList list = element.getChildNodes(); 434 for (int i = 0; i < list.getLength(); i++) 435 { 436 Node node = list.item(i); 437 if (node.getNodeType() == Node.ELEMENT_NODE) 438 { 439 Element childElt = (Element )node; 440 if (childElt.getTagName().equals(name)) 441 { 442 result.add(childElt); 443 } 444 } 445 } 446 return result; 447 } 448 449 459 public static DocumentBuilder getBuilder() throws ParserConfigurationException 460 { 461 return getBuilder(DEFAULT_VALIDATION, DEFAULT_NAMESPACE_AWARE); 463 } 464 465 475 public static DocumentBuilder getBuilder(final boolean validation, final boolean nameSpaceAware) 476 throws ParserConfigurationException 477 { 478 int validationIndex = validation ? 1 : 0; 480 int namespaceIndex = nameSpaceAware ? 1 : 0; 481 DocumentBuilder [][] docBuilder = (DocumentBuilder [][])documentBuilders.get(); 483 if (docBuilder[validationIndex][namespaceIndex] == null) 485 { 486 docBuilder[validationIndex][namespaceIndex] = docBuilderFactories[validationIndex][namespaceIndex].newDocumentBuilder(); 487 } 488 return docBuilder[validationIndex][namespaceIndex]; 489 } 490 491 498 public static Document createDocument() throws ParserConfigurationException 499 { 500 504 return getBuilder().newDocument(); 505 } 506 507 519 public static Document createDocument(final String strDoc) throws DOMException , IOException , SAXException , 520 ParserConfigurationException 521 { 522 523 if (strDoc == null) 524 { 525 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 526 } 527 528 final InputSource inputSrc = new InputSource (new StringReader (strDoc)); 529 530 return parseInputSource(inputSrc, DEFAULT_VALIDATION, DEFAULT_NAMESPACE_AWARE, null, null); 531 } 532 533 545 public static Document createDocument(final InputStream stream) throws DOMException , IOException , SAXException , 546 ParserConfigurationException 547 { 548 549 if (stream == null) 550 { 551 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 552 } 553 554 return parseInputSource(new InputSource (stream), DEFAULT_VALIDATION, DEFAULT_NAMESPACE_AWARE, null, null); 555 } 556 557 569 public static Document createDocument(final byte[] bytes) throws DOMException , IOException , SAXException , 570 ParserConfigurationException 571 { 572 573 if (bytes == null) 574 { 575 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 576 } 577 final java.io.ByteArrayInputStream bis = new java.io.ByteArrayInputStream (bytes); 578 return createDocument(bis); 579 } 580 581 597 public static Document createDocument(final InputStream stream, 598 final String encoding, 599 final boolean validation, 600 final boolean namespace) 601 throws DOMException , IOException , SAXException , ParserConfigurationException 602 { 603 604 if (stream == null || encoding == null) 605 { 606 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 607 } 608 609 final InputSource in = new InputSource (new InputStreamReader (stream, encoding)); 610 611 return parseInputSource(in, validation, namespace, null, null); 613 } 614 615 629 public static Document createDocument(final Reader reader, 630 final boolean validation, 631 final boolean namespace) 632 throws DOMException , IOException , SAXException , ParserConfigurationException 633 { 634 635 if (reader == null) 636 { 637 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 638 } 639 640 final InputSource in = new InputSource (reader); 641 642 return parseInputSource(in, validation, namespace, null, null); 644 } 645 646 657 public static Document createDocument(final URL documentURL) throws IOException , SAXException , 658 ParserConfigurationException 659 { 660 661 if (documentURL == null) 662 { 663 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 664 } 665 666 InputStream in = null; 667 668 try 669 { 670 in = documentURL.openConnection().getInputStream(); 671 return createDocument(in); 672 } 673 finally 674 { 675 if (in != null) 676 { 677 try 678 { 679 in.close(); 680 } 681 catch (IOException ioe) 682 { 683 } 684 } 685 } 686 } 687 688 705 public static Document createDocument(final InputSource inputSrc, 706 final boolean isValidating, 707 final boolean isNamespaceAware, 708 final ErrorHandler errorHandler, 709 final EntityResolver entityResolver) 710 throws DOMException , IOException , SAXException , ParserConfigurationException 711 { 712 713 if (inputSrc == null) 714 { 715 throw new IllegalArgumentException ((ERR_NO_NULL_INPUTSOURCE_ALLOWED)); 716 } 717 718 return parseInputSource(inputSrc, isValidating, isNamespaceAware, errorHandler, entityResolver); 719 } 720 721 733 private static Document parseInputSource(final InputSource in, 734 final boolean validation, 735 final boolean nameSpaceAware, 736 final ErrorHandler errorHandler, 737 final EntityResolver entityResolver) 738 throws SAXException , IOException , ParserConfigurationException 739 { 740 741 final DocumentBuilder parser = getBuilder(validation, nameSpaceAware); 742 743 if (errorHandler != null) 745 { 746 parser.setErrorHandler(errorHandler); 747 } 748 749 if (entityResolver != null) 751 { 752 parser.setEntityResolver(entityResolver); 753 } 754 else 755 { 756 if (!validation) 759 { 760 } 762 } 763 764 return parser.parse(in); 765 } 766 767 776 public static Node createDocumentRootNode(final Document parentDocument, 777 final String nodeName, 778 final Node contentNode) 779 { 780 return createDocumentRootNode(parentDocument, nodeName, contentNode, true); 781 } 782 783 795 public static Node createDocumentRootNode(final Document parentDocument, 796 final String nodeName, 797 final Node contentNode, 798 final boolean contentIsLocal) 799 { 800 801 if (parentDocument == null || nodeName == null) 802 { 803 throw new IllegalArgumentException ((ERR_NO_NULL_DOC_AND_NODE_ALLOWED)); 804 } 805 806 Node el = null; 807 808 el = parentDocument.createElement(nodeName); 809 810 if (contentNode != null) 811 { 812 Node cNode = null; 813 814 if (contentIsLocal) 815 { 816 cNode = contentNode; 817 } 818 else 819 { 820 cNode = parentDocument.importNode(contentNode, true); 821 } 822 823 el.appendChild(cNode); 824 } 825 826 parentDocument.appendChild(el); 827 828 return el; 829 } 830 831 840 public static Node createNode(final Document document, final String nodeName, final Node contentNode) 841 { 842 return createNode(document, nodeName, contentNode, true); 843 } 844 845 856 public static Node createNode(final Document document, 857 final String nodeName, 858 final Node contentNode, 859 final boolean contentIsLocal) 860 { 861 862 if (document == null || nodeName == null) 863 { 864 throw new IllegalArgumentException ((ERR_NO_NULL_DOC_AND_NODE_ALLOWED)); 865 } 866 867 Node el = null; 868 869 el = document.createElement(nodeName); 870 871 if (contentNode != null) 872 { 873 Node cNode = null; 874 875 if (contentIsLocal) 876 { 877 cNode = contentNode; 878 } 879 else 880 { 881 cNode = document.importNode(contentNode, true); 882 } 883 884 el.appendChild(cNode); 885 } 886 887 return el; 888 } 889 890 900 public static Node createChildNode(final Node parentNode, final String nodeName, final Node contentNode) 901 { 902 return createChildNode(parentNode, nodeName, contentNode, true); 903 } 904 905 917 public static Node createChildNode(final Node parentNode, 918 final String nodeName, 919 final Node contentNode, 920 final boolean contentIsLocal) 921 { 922 923 if (parentNode == null || nodeName == null) 924 { 925 throw new IllegalArgumentException ((ERR_NO_NULL_PARENT_AND_NODE_ALLOWED)); 926 } 927 928 if (parentNode.getNodeType() != Node.DOCUMENT_NODE && parentNode.getOwnerDocument() == null) 929 { 930 throw new IllegalArgumentException ((ERR_WRONG_NODETYPE)); 931 } 932 933 Node el = null; 934 935 if (parentNode.getNodeType() == Node.DOCUMENT_NODE) 936 { 937 el = ((Document )parentNode).createElement(nodeName); 938 } 939 else 940 { 941 el = parentNode.getOwnerDocument().createElement(nodeName); 942 } 943 944 parentNode.appendChild(el); 945 946 if (contentNode != null) 947 { 948 Node cNode = null; 949 950 if (contentIsLocal) 951 { 952 cNode = contentNode; 953 } 954 else 955 { 956 cNode = parentNode.getOwnerDocument().importNode(contentNode, true); 957 } 958 959 el.appendChild(cNode); 960 } 961 962 return el; 963 } 964 965 973 public static Node createChildNode(final Node parentNode, final String nodeName) 974 { 975 return createChildNode(parentNode, nodeName, null, true); 976 } 977 978 991 public static Node createTextNode(final Document doc, 992 final Node parentNode, 993 final String nodeName, 994 final String content) 995 { 996 997 if (doc == null || (nodeName == null && parentNode == null)) 998 { 999 throw new IllegalArgumentException ((ERR_NO_NULL_DOC_OR_PARENT_AND_NODE_ALLOWED)); 1000 } 1001 1002 Node el = null; 1003 1004 if (nodeName != null) 1005 { 1006 el = doc.createElement(nodeName); 1007 1008 if (parentNode != null) 1009 { 1010 parentNode.appendChild(el); 1011 } 1012 } 1013 else 1014 { 1015 el = parentNode; 1016 } 1017 1018 if (content != null) 1019 { 1020 el.appendChild(doc.createTextNode(content)); 1021 } 1022 1023 return el; 1024 } 1025 1026 1035 public static Node createTextNode(final Node parentNode, final String nodeName, final String content) 1036 { 1037 1038 if (parentNode == null) 1039 { 1040 throw new IllegalArgumentException ((ERR_NO_NULL_PARENT_NODE_ALLOWED)); 1041 } 1042 1043 return createTextNode(parentNode.getOwnerDocument(), parentNode, nodeName, content); 1044 } 1045 1046 1055 public static Attr createAttribute(final Element el, final String attributeName, final String attributeValue) 1056 { 1057 1058 if (el == null || attributeName == null) 1059 { 1060 throw new IllegalArgumentException ((ERR_NO_NULL_NODE_AND_ATTNAME_ALLOWED)); 1061 } 1062 1063 final Attr at = el.getOwnerDocument().createAttribute(attributeName); 1064 1065 if (attributeValue != null) 1066 { 1067 at.setValue(attributeValue); 1068 } 1069 1070 el.setAttributeNode(at); 1071 1072 return at; 1073 } 1074 1075 1084 public static Attr createAttribute(final Document doc, final String attributeName, final String attributeValue) 1085 { 1086 1087 if (doc == null || attributeName == null) 1088 { 1089 throw new IllegalArgumentException ((ERR_NO_NULL_DOC_AND_ATTNAME_ALLOWED)); 1090 } 1091 1092 final Attr at = doc.createAttribute(attributeName); 1093 at.setValue(attributeValue); 1094 1095 return at; 1096 } 1097 1098 1106 public static String getAttributeValue(final String attributeName, final Node node) 1107 { 1108 1109 if (attributeName == null || node == null) 1110 { 1111 throw new IllegalArgumentException ((ERR_NO_NULL_NODE_AND_ATTNAME_ALLOWED)); 1112 } 1113 1114 final org.w3c.dom.NamedNodeMap attribMap = node.getAttributes(); 1115 1116 if (attribMap != null && attribMap.getLength() > 0) 1117 { 1118 final Node n = attribMap.getNamedItem(attributeName); 1119 1120 if (n != null) 1121 { 1122 return n.getNodeValue(); 1123 } 1124 } 1125 1126 return null; 1127 } 1128 1129 1137 public static Attr getAttribute(final String attributeName, final Node node) 1138 { 1139 1140 if (attributeName == null || node == null) 1141 { 1142 throw new IllegalArgumentException ((ERR_NO_NULL_NODE_AND_ATTNAME_ALLOWED)); 1143 } 1144 1145 final org.w3c.dom.NamedNodeMap attribMap = node.getAttributes(); 1146 1147 if (attribMap != null && attribMap.getLength() > 0) 1148 { 1149 final Node n = attribMap.getNamedItem(attributeName); 1150 1151 if (n != null) 1152 { 1153 return (Attr )n; 1154 } 1155 } 1156 1157 return null; 1158 } 1159 1160 1167 public static String getElementBodyText(final Node parentNode) 1168 { 1169 1170 final Node textNode = parentNode.getFirstChild(); 1171 1172 if (textNode != null && Node.TEXT_NODE == textNode.getNodeType()) 1173 { 1174 return textNode.getNodeValue(); 1175 } 1176 1177 return ""; 1178 } 1179 1180 1192 public static Node createCDATANode(final Node parentNode, final String nodeName, final String content) 1193 { 1194 1195 if (parentNode == null) 1196 { 1197 throw new IllegalArgumentException ((ERR_NO_NULL_PARENT_AND_CONTENT_ALLOWED)); 1198 } 1199 1200 if (content == null) 1201 { 1202 return createChildNode(parentNode, nodeName); 1203 } 1204 1205 Node el = null; 1206 1207 if (nodeName != null) 1208 { 1209 el = parentNode.getOwnerDocument().createElement(nodeName); 1210 parentNode.appendChild(el); 1211 1212 } 1213 else 1214 { 1215 el = parentNode; 1216 } 1217 1218 el.appendChild(parentNode.getOwnerDocument().createCDATASection(content)); 1219 1220 return el; 1221 } 1222 1223 1232 public static Node addNodeBeforeSibling(final Node siblingNode, final String nodeName, final Node content) 1233 { 1234 return addNodeBeforeSibling(siblingNode, nodeName, content, true); 1235 } 1236 1237 1251 public static Node addNodeBeforeSibling(final Node siblingNode, 1252 final String nodeName, 1253 final Node content, 1254 final boolean contentIsLocal) 1255 { 1256 1257 if (siblingNode == null || (nodeName == null && content == null)) 1258 { 1259 throw new IllegalArgumentException ((ERR_NO_NULL_SIBLING_AND_NODE_ALLOWED)); 1260 } 1261 1262 Node el = null; 1263 1264 if (nodeName != null) 1265 { 1266 el = siblingNode.getOwnerDocument().createElement(nodeName); 1268 siblingNode.getParentNode().insertBefore(el, siblingNode); 1269 } 1270 1271 if (content != null) 1272 { 1273 1274 Node cNode = null; 1275 1276 if (contentIsLocal) 1277 { 1278 cNode = content; 1279 } 1280 else 1281 { 1282 cNode = siblingNode.getOwnerDocument().importNode(content, true); 1284 } 1285 1286 if (el != null) 1287 { 1288 el.appendChild(cNode); 1289 } 1290 else 1291 { 1292 el = siblingNode.getParentNode().insertBefore(cNode, siblingNode); 1294 } 1295 } 1296 1297 return el; 1298 } 1299 1300 1311 public static Node addNodeAfterSibling(final Node siblingNode, final String nodeName, final Node content) 1312 { 1313 return addNodeAfterSibling(siblingNode, nodeName, content, true); 1314 } 1315 1316 1329 public static Node addNodeAfterSibling(final Node siblingNode, 1330 final String nodeName, 1331 final Node content, 1332 final boolean contentIsLocal) 1333 { 1334 1335 if (siblingNode == null || (nodeName == null && content == null)) 1336 { 1337 throw new IllegalArgumentException ((ERR_NO_NULL_SIBLING_AND_NODE_ALLOWED)); 1338 } 1339 1340 Node el = null; 1341 Node cNode = null; 1342 1343 if (content != null) 1344 { 1345 1346 if (contentIsLocal) 1347 { 1348 cNode = content; 1349 } 1350 else 1351 { 1352 cNode = siblingNode.getOwnerDocument().importNode(content, true); 1353 } 1354 } 1355 1356 if (nodeName != null) 1357 { 1358 el = siblingNode.getOwnerDocument().createElement(nodeName); 1359 1360 if (cNode != null) 1361 { 1362 el.appendChild(cNode); 1363 } 1364 } 1365 1366 final Node nextSibling = siblingNode.getNextSibling(); 1367 1368 if (nextSibling == null) 1369 { 1370 if (el != null) 1371 { 1372 siblingNode.getParentNode().appendChild(el); 1373 } 1374 else 1375 { 1376 el = siblingNode.getParentNode().appendChild(cNode); 1379 } 1380 1381 } 1382 else 1383 { 1384 if (el != null) 1385 { 1386 siblingNode.getParentNode().insertBefore(el, nextSibling); 1387 } 1388 else 1389 { 1390 el = siblingNode.getParentNode().insertBefore(cNode, nextSibling); 1393 } 1394 } 1395 1396 return el; 1397 } 1398 1399 1407 public static Element appendChildElement(final Document document, final Element parent, final String key) 1408 { 1409 final Element element = document.createElement(key); 1410 parent.appendChild(element); 1411 1412 return element; 1413 } 1414 1415 1423 public static Node appendChildNode(final Node parentNode, final Node contentNode) 1424 { 1425 return appendChildNode(parentNode, contentNode, true); 1426 } 1427 1428 1439 public static Node appendChildNode(final Node parentNode, final Node contentNode, final boolean contentIsLocal) 1440 { 1441 1442 if (parentNode == null || contentNode == null) 1443 { 1444 throw new IllegalArgumentException ((ERR_NO_NULL_PARENT_AND_CONTENT_ALLOWED)); 1445 } 1446 1447 Node cNode = null; 1448 1449 if (contentIsLocal) 1450 { 1451 cNode = contentNode; 1452 } 1453 else 1454 { 1455 cNode = parentNode.getOwnerDocument().importNode(contentNode, true); 1456 } 1457 1458 parentNode.appendChild(cNode); 1459 1460 return cNode; 1461 } 1462 1463 1471 public static void importChildNodes(final Node src, final Node dest) 1472 { 1473 appendChildNodes(src, dest, false); 1474 } 1475 1476 1483 public static void moveChildNodes(final Node src, final Node dest) 1484 { 1485 appendChildNodes(src, dest, true); 1486 } 1487 1488 1500 private static void appendChildNodes(final Node src, final Node dest, final boolean contentIsLocal) 1501 { 1502 1503 if (src == null || dest == null) 1504 { 1505 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1506 } 1507 1508 if (!contentIsLocal && src.getOwnerDocument().equals(dest.getOwnerDocument())) 1509 { 1510 throw new IllegalArgumentException ((ERR_SRC_AND_DEST_DOC_EQUAL)); 1511 } 1512 1513 final NodeList nodesToImport = src.getChildNodes(); 1514 for (int i = 0; i < nodesToImport.getLength(); i++) 1515 { 1516 Node singleNode = null; 1517 1518 if (!contentIsLocal) 1519 { 1520 singleNode = dest.getOwnerDocument().importNode(nodesToImport.item(i), true); 1521 } 1522 else 1523 { 1524 singleNode = nodesToImport.item(i); 1525 } 1526 dest.appendChild(singleNode); 1527 } 1528 } 1529 1530 1541 public static Properties createOutputProperties(final boolean omitXMLDeclaration, 1542 final boolean enableIndenting, 1543 final String [] elements) 1544 { 1545 final Properties oprops = new Properties (); 1547 1548 oprops.put(ATTRIB_METHOD, "xml"); 1549 oprops.put(ATTRIB_OMIT_XML_DECLARATION, omitXMLDeclaration ? PARAM_YES : PARAM_NO); 1550 oprops.put(ATTRIB_INDENT, enableIndenting ? PARAM_YES : PARAM_NO); 1551 1552 if (elements != null) 1553 { 1554 final StringBuffer list = new StringBuffer (); 1556 for (int i = 0; i < elements.length; i++) 1557 { 1558 list.append(" ").append(elements[i]); 1559 } 1560 1561 oprops.put(ATTRIB_CDATA_SECTION_ELEMENTS, list.toString()); 1562 } 1563 1564 return oprops; 1565 } 1566 1567 1579 public static void serialize(final Node doc, 1580 final java.io.OutputStream output) 1581 throws IOException , TransformerConfigurationException , TransformerException 1582 { 1583 1584 if (output == null || doc == null) 1585 { 1586 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1587 } 1588 1589 final Transformer t = getTransformer(); 1590 1591 serialize(doc, output, t); 1592 } 1593 1594 1607 public static void serialize(final Node doc, 1608 final java.io.OutputStream output, 1609 final Transformer transformer) 1610 throws IOException , TransformerConfigurationException , TransformerException 1611 { 1612 1613 if (output == null || doc == null || transformer == null) 1614 { 1615 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1616 } 1617 1618 transformer.transform(new DOMSource (doc), new StreamResult (output)); 1619 output.flush(); 1620 1621 return; 1622 } 1623 1624 1636 public static void serialize(final Node doc, final Writer writer) 1637 throws IOException , TransformerConfigurationException , TransformerException 1638 { 1639 1640 if (writer == null || doc == null) 1641 { 1642 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1643 } 1644 1645 final Transformer t = getTransformer(); 1646 serialize(doc, writer, t); 1647 } 1648 1649 1663 public static void serialize(final Node doc, final Writer writer, final Transformer transformer) 1664 throws IOException , TransformerConfigurationException , TransformerException 1665 { 1666 1667 if (writer == null || doc == null || transformer == null) 1668 { 1669 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1670 } 1671 1672 final String result = serialize(doc, transformer); 1673 1674 writer.write(result); 1675 } 1676 1677 1692 public static String serialize(final Node doc) 1693 throws IOException , TransformerConfigurationException , TransformerException 1694 { 1695 1696 if (doc == null) 1697 { 1698 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1699 } 1700 1701 final Transformer t = getTransformer(); 1703 1704 return serialize(doc, t); 1705 } 1706 1707 1721 public static String serialize(final Node doc, final Transformer transformer) 1722 throws IOException , TransformerConfigurationException , TransformerException 1723 { 1724 1725 if (doc == null || transformer == null) 1726 { 1727 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1728 } 1729 1730 final ByteArrayOutputStream baos = new ByteArrayOutputStream (); 1732 transformer.transform(new DOMSource (doc), new StreamResult (baos)); 1733 1734 final String resultEncoding = transformer.getOutputProperties().getProperty("encoding"); 1735 1736 return baos.toString((resultEncoding == null ? "UTF-8" : resultEncoding)); 1737 } 1738 1739 1752 public static Node addDirectiveToXSL(final Document xslDom, 1753 final String directiveName, 1754 final String attributeName, 1755 String attributeValue) 1756 throws TransformerException 1757 { 1758 1759 if (xslDom == null || directiveName == null) 1760 { 1761 throw new IllegalArgumentException ((ERR_NO_NULL_DOC_AND_DIRECTIVENAME_ALLOWED)); 1763 } 1764 1765 final Node styleSheetNode = new CachedXPathAPI().selectSingleNode(xslDom.getDocumentElement(), 1767 "//xsl:stylesheet"); 1768 1769 if (styleSheetNode == null) 1770 { 1771 throw new IllegalArgumentException ((ERR_IS_NOT_A_STYLESHEET)); 1773 } 1774 1775 final Node firstChild = styleSheetNode.getFirstChild(); 1777 1778 Node directiveNode = null; 1779 1780 if (firstChild == null) 1781 { 1782 directiveNode = createChildNode(styleSheetNode, directiveName); 1783 } 1784 else 1785 { 1786 directiveNode = addNodeBeforeSibling(firstChild, directiveName, null); 1787 } 1788 1789 if (attributeName != null) 1790 { 1791 attributeValue = attributeValue == null ? "" : attributeValue; 1792 createAttribute((Element )directiveNode, attributeName, attributeValue); 1793 } 1794 1795 return directiveNode; 1796 } 1797 1798 1810 public static Node addDirectiveToXSLBeforeSibling(final Node sibling, 1811 final String directiveName, 1812 final String attributeName, 1813 String attributeValue) 1814 { 1815 1816 if (sibling == null || directiveName == null) 1817 { 1818 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 1820 } 1821 1822 final Node directiveNode = addNodeBeforeSibling(sibling, directiveName, null); 1823 1824 if (attributeName != null) 1825 { 1826 attributeValue = attributeValue == null ? "" : attributeValue; 1827 createAttribute((Element )directiveNode, attributeName, attributeValue); 1828 } 1829 1830 return directiveNode; 1831 } 1832 1833 1847 public static void xslTransform(final Document contentDom, 1848 final Document styleDom, 1849 final Writer out) 1850 throws TransformerException , TransformerConfigurationException , FileNotFoundException , IOException 1851 { 1852 getTransformer(styleDom).transform(new DOMSource (contentDom), new StreamResult (out)); 1853 } 1854 1855 1870 public static String xslTransform(final Document contentDom, 1871 final Transformer transformer, 1872 final OutputStream out) 1873 throws TransformerException , TransformerConfigurationException 1874 { 1875 transformer.transform(new DOMSource (contentDom), new StreamResult (out)); 1876 1877 return transformer.getOutputProperties().getProperty("encoding"); 1878 } 1879 1880 1892 public static void xslTransform(final Document contentDom, 1893 final Transformer transformer, 1894 final Writer writer) 1895 throws TransformerException , TransformerConfigurationException 1896 { 1897 transformer.transform(new DOMSource (contentDom), new StreamResult (writer)); 1898 } 1899 1900 1907 public static Transformer getTransformer() throws TransformerConfigurationException 1908 { 1909 1910 final Transformer trans = getTransformerFactory().newTransformer(); 1911 trans.setOutputProperty(OutputKeys.INDENT, PARAM_YES); 1912 1913 return trans; 1914 } 1915 1916 1924 public static Transformer getTransformer(final Properties transProps) throws TransformerConfigurationException 1925 { 1926 1927 final Transformer trans = getTransformerFactory().newTransformer(); 1928 1929 if (transProps != null) 1930 { 1931 trans.setOutputProperties(transProps); 1932 } 1933 1934 return trans; 1935 } 1936 1937 1945 public static Transformer getTransformer(final Source strSource) throws TransformerConfigurationException 1946 { 1947 1948 return getTransformer(getTemplates(strSource)); 1949 } 1950 1951 1959 public static Transformer getTransformer(final Templates templates) throws TransformerConfigurationException 1960 { 1961 return templates.newTransformer(); 1962 } 1963 1964 1973 public static Transformer getTransformer(final Document doc) throws TransformerException , IOException 1974 { 1975 1976 final ByteArrayOutputStream bOut = new ByteArrayOutputStream (); 1979 serialize(doc, bOut); 1980 final StreamSource source = new StreamSource (new ByteArrayInputStream (bOut.toByteArray())); 1981 1982 1987 return getTransformer(source); 1988 } 1989 1990 1998 public static Templates getTemplates(final Source source) throws TransformerConfigurationException 1999 { 2000 return getTransformerFactory().newTemplates(source); 2004 } 2005 2006 2016 public static NodeList getSelectedNodes(final Node contextNode, final String str) throws TransformerException 2017 { 2018 if (contextNode == null || str == null) 2019 { 2020 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2021 } 2022 return new CachedXPathAPI().selectNodeList(contextNode, str); 2023 } 2024 2025 2036 public static Node getSelectedNode(final Node contextNode, final String str) throws TransformerException 2037 { 2038 if (contextNode == null || str == null) 2039 { 2040 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2041 } 2042 return getSelectedNode(new CachedXPathAPI(), contextNode, str); 2043 } 2044 2045 2057 public static Node getSelectedNode(final CachedXPathAPI xpath, final Node contextNode, final String str) 2058 throws TransformerException 2059 { 2060 if (contextNode == null || str == null) 2061 { 2062 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2063 } 2064 return xpath.selectSingleNode(contextNode, str); 2065 } 2066 2067 2072 private static synchronized TransformerFactory getTransformerFactory() 2073 { 2074 if (g_transformerFactory == null) 2076 { 2077 2082 g_transformerFactory = TransformerFactory.newInstance(); 2089 } 2093 2094 return g_transformerFactory; 2095 } 2096 2097 2104 public static synchronized SAXParserFactory getSAXParserFactory(final boolean validation, 2105 final boolean nameSpaceAware) 2106 { 2107 2108 final int validationIndex = validation ? 1 : 0; 2110 final int namespaceIndex = nameSpaceAware ? 1 : 0; 2111 2112 if (SAXPARSERFACTORIES[validationIndex][namespaceIndex] == null) 2114 { 2115 2116 final SAXParserFactory factory = SAXParserFactory.newInstance(); 2117 factory.setValidating(validation); 2118 factory.setNamespaceAware(nameSpaceAware); 2119 2120 SAXPARSERFACTORIES[validationIndex][namespaceIndex] = factory; 2121 } 2122 2123 return SAXPARSERFACTORIES[validationIndex][namespaceIndex]; 2124 } 2125 2126 2138 public static void saxParse(final InputStream in, 2139 final DefaultHandler handler, 2140 final boolean validation, 2141 final boolean nameSpaceAware) 2142 throws ParserConfigurationException , SAXException , IOException 2143 { 2144 final SAXParser saxParser = getSAXParserFactory(validation, nameSpaceAware).newSAXParser(); 2145 saxParser.parse(in, handler); 2146 } 2147 2148 2160 public static void saxParse(final InputSource source, 2161 final DefaultHandler handler, 2162 final boolean validation, 2163 final boolean nameSpaceAware) 2164 throws ParserConfigurationException , SAXException , IOException 2165 { 2166 final SAXParser saxParser = getSAXParserFactory(validation, nameSpaceAware).newSAXParser(); 2167 saxParser.parse(source, handler); 2168 } 2169 2170 2179 public static String getFirstElementValue(final Node node, final String element) 2180 { 2181 2182 if (node == null || element == null) 2183 { 2184 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2185 } 2186 2187 final NodeList nodeList = node.getChildNodes(); 2188 for (int i = 0; i < nodeList.getLength(); i++) 2189 { 2190 if (nodeList.item(i).getNodeName().equals(element)) 2191 { 2192 return getElementValue(nodeList.item(i)); 2193 } 2194 } 2195 2196 return null; 2197 } 2198 2199 2208 public static String getElementValue(final Node node) 2209 { 2210 2211 if (node == null) 2212 { 2213 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2214 } 2215 2216 final StringBuffer nodeValue = new StringBuffer (); 2217 2218 if (node.hasChildNodes()) 2219 { 2220 Node curNode = node.getFirstChild(); 2221 while (curNode != null) 2222 { 2223 if (curNode.getNodeValue() != null) 2224 { 2225 nodeValue.append(curNode.getNodeValue()); 2226 } 2227 curNode = curNode.getNextSibling(); 2228 } 2229 } 2230 2231 return nodeValue.toString(); 2232 } 2233 2234 2241 public static void copyAttributes(final Element src, final Element dest) 2242 { 2243 2244 if (src == null || dest == null) 2245 { 2246 throw new IllegalArgumentException ((ERR_NO_NULL_ARGS_ALLOWED)); 2247 } 2248 2249 final NamedNodeMap attrs = src.getAttributes(); 2250 for (int i = 0; i < attrs.getLength(); i++) 2251 { 2252 final Attr attr = (Attr )attrs.item(i); 2253 dest.setAttribute(attr.getName(), attr.getValue()); 2254 } 2255 } 2256} 2257 | Popular Tags |