1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.xml.util;
18
19 import java.io.OutputStream;
20 import java.io.StringWriter;
21 import java.io.Writer;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.GregorianCalendar;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.StringTokenizer;
30 import java.util.Map.Entry;
31
32 import javax.xml.datatype.DatatypeConfigurationException;
33 import javax.xml.datatype.DatatypeFactory;
34 import javax.xml.datatype.Duration;
35 import javax.xml.namespace.QName;
36 import javax.xml.transform.OutputKeys;
37 import javax.xml.transform.Transformer;
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
43 import org.opensaml.xml.Configuration;
44 import org.opensaml.xml.XMLObject;
45 import org.opensaml.xml.XMLRuntimeException;
46 import org.opensaml.xml.parse.XMLParserException;
47 import org.w3c.dom.Attr;
48 import org.w3c.dom.DOMImplementation;
49 import org.w3c.dom.Document;
50 import org.w3c.dom.Element;
51 import org.w3c.dom.NamedNodeMap;
52 import org.w3c.dom.Node;
53 import org.w3c.dom.NodeList;
54 import org.w3c.dom.Text;
55 import org.w3c.dom.ls.DOMImplementationLS;
56 import org.w3c.dom.ls.LSOutput;
57 import org.w3c.dom.ls.LSSerializer;
58 import org.w3c.dom.ls.LSSerializerFilter;
59
60
61
62
63 public final class XMLHelper {
64
65
66
67
68
69 public static final String LIST_DELIMITERS = " \n\r\t";
70
71
72 private static DatatypeFactory dataTypeFactory;
73
74
75 private XMLHelper() {
76
77 }
78
79
80
81
82
83
84 public static DatatypeFactory getDataTypeFactory() {
85 if (dataTypeFactory == null) {
86 try {
87 dataTypeFactory = DatatypeFactory.newInstance();
88 } catch (DatatypeConfigurationException e) {
89
90 }
91 }
92
93 return dataTypeFactory;
94 }
95
96
97
98
99
100
101
102
103 public static boolean hasXSIType(Element e) {
104 if (e != null) {
105 if (e.getAttributeNodeNS(XMLConstants.XSI_NS, "type") != null) {
106 return true;
107 }
108 }
109
110 return false;
111 }
112
113
114
115
116
117
118
119
120 public static QName getXSIType(Element e) {
121 if (hasXSIType(e)) {
122 Attr attribute = e.getAttributeNodeNS(XMLConstants.XSI_NS, "type");
123 String attributeValue = attribute.getTextContent().trim();
124 StringTokenizer tokenizer = new StringTokenizer(attributeValue, ":");
125 String prefix = null;
126 String localPart;
127 if (tokenizer.countTokens() > 1) {
128 prefix = tokenizer.nextToken();
129 localPart = tokenizer.nextToken();
130 } else {
131 localPart = tokenizer.nextToken();
132 }
133
134 return constructQName(e.lookupNamespaceURI(prefix), localPart, prefix);
135 }
136
137 return null;
138 }
139
140
141
142
143
144
145
146
147 public static Attr getIdAttribute(Element domElement) {
148 if (!domElement.hasAttributes()) {
149 return null;
150 }
151
152 NamedNodeMap attributes = domElement.getAttributes();
153 Attr attribute;
154 for (int i = 0; i < attributes.getLength(); i++) {
155 attribute = (Attr) attributes.item(i);
156 if (attribute.isId()) {
157 return attribute;
158 }
159 }
160
161 return null;
162 }
163
164
165
166
167
168
169
170
171 public static QName getNodeQName(Node domNode) {
172 if (domNode != null) {
173 return constructQName(domNode.getNamespaceURI(), domNode.getLocalName(), domNode.getPrefix());
174 }
175
176 return null;
177 }
178
179
180
181
182
183
184
185
186
187
188 public static Locale getLanguage(Element element) {
189 String lang = DatatypeHelper.safeTrimOrNullString(element.getAttributeNS(XMLConstants.XML_NS, "lang"));
190 if (lang != null) {
191 if (lang.contains("-")) {
192 lang = lang.substring(0, lang.indexOf("-"));
193 }
194 return new Locale(lang.toUpperCase());
195 } else {
196 return Locale.getDefault();
197 }
198 }
199
200
201
202
203
204
205
206
207
208 public static Attr constructAttribute(Document owningDocument, QName attributeName) {
209 return constructAttribute(owningDocument, attributeName.getNamespaceURI(), attributeName.getLocalPart(),
210 attributeName.getPrefix());
211 }
212
213
214
215
216
217
218
219
220
221
222
223 public static Attr constructAttribute(Document document, String namespaceURI, String localName, String prefix) {
224 String trimmedLocalName = DatatypeHelper.safeTrimOrNullString(localName);
225
226 if (trimmedLocalName == null) {
227 throw new IllegalArgumentException("Local name may not be null or empty");
228 }
229
230 String qualifiedName;
231 String trimmedPrefix = DatatypeHelper.safeTrimOrNullString(prefix);
232 if (trimmedPrefix != null) {
233 qualifiedName = trimmedPrefix + ":" + DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
234 } else {
235 qualifiedName = DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
236 }
237
238 if (DatatypeHelper.isEmpty(namespaceURI)) {
239 return document.createAttributeNS(null, qualifiedName);
240 } else {
241 return document.createAttributeNS(namespaceURI, qualifiedName);
242 }
243 }
244
245
246
247
248
249
250
251
252 public static QName getAttributeValueAsQName(Attr attribute) {
253 if (attribute == null || DatatypeHelper.isEmpty(attribute.getValue())) {
254 return null;
255 }
256
257 String attributeValue = attribute.getTextContent();
258 String[] valueComponents = attributeValue.split(":");
259 if (valueComponents.length == 1) {
260 return constructQName(attribute.lookupNamespaceURI(null), valueComponents[0], null);
261 } else {
262 return constructQName(attribute.lookupNamespaceURI(valueComponents[0]), valueComponents[1],
263 valueComponents[0]);
264 }
265 }
266
267
268
269
270
271
272
273
274
275 public static Boolean getAttributeValueAsBoolean(Attr attribute) {
276 if (attribute == null) {
277 return null;
278 }
279
280 String valueStr = attribute.getValue();
281 if (valueStr.equals("0") || valueStr.equals("false")) {
282 return Boolean.FALSE;
283 } else if (valueStr.equals("1") || valueStr.equals("true")) {
284 return Boolean.TRUE;
285 } else {
286 return null;
287 }
288 }
289
290
291
292
293
294
295
296
297 public static List<String> getAttributeValueAsList(Attr attribute) {
298 if (attribute == null) {
299 return Collections.emptyList();
300 }
301 return DatatypeHelper.stringToList(attribute.getValue(), LIST_DELIMITERS);
302 }
303
304
305
306
307
308
309
310
311
312
313
314 public static void marshallAttribute(QName attributeName, String attributeValue, Element domElement,
315 boolean isIDAttribute) {
316 Document document = domElement.getOwnerDocument();
317 Attr attribute = XMLHelper.constructAttribute(document, attributeName);
318 attribute.setValue(attributeValue);
319 domElement.setAttributeNodeNS(attribute);
320 if (isIDAttribute) {
321 domElement.setIdAttributeNode(attribute, true);
322 }
323 }
324
325
326
327
328
329
330
331
332
333
334
335 public static void marshallAttribute(QName attributeName, List<String> attributeValues, Element domElement,
336 boolean isIDAttribute) {
337 marshallAttribute(attributeName, DatatypeHelper.listToStringValue(attributeValues, " "), domElement,
338 isIDAttribute);
339 }
340
341
342
343
344
345
346
347 public static void marshallAttributeMap(AttributeMap attributeMap, Element domElement) {
348 Document document = domElement.getOwnerDocument();
349 Attr attribute = null;
350 for (Entry<QName, String> entry : attributeMap.entrySet()) {
351 attribute = XMLHelper.constructAttribute(document, entry.getKey());
352 attribute.setValue(entry.getValue());
353 domElement.setAttributeNodeNS(attribute);
354 if (Configuration.isIDAttribute(entry.getKey()) || attributeMap.isIDAttribute(entry.getKey())) {
355 domElement.setIdAttributeNode(attribute, true);
356 }
357 }
358 }
359
360
361
362
363
364
365
366 public static void unmarshallToAttributeMap(AttributeMap attributeMap, Attr attribute) {
367 QName attribQName = XMLHelper.constructQName(attribute.getNamespaceURI(), attribute.getLocalName(), attribute
368 .getPrefix());
369 attributeMap.put(attribQName, attribute.getValue());
370 if (attribute.isId() || Configuration.isIDAttribute(attribQName)) {
371 attributeMap.registerID(attribQName);
372 }
373 }
374
375
376
377
378
379
380
381
382 public static QName getElementContentAsQName(Element element) {
383 if (element == null) {
384 return null;
385 }
386
387 String elementContent = null;
388 NodeList nodeList = element.getChildNodes();
389 for (int i = 0; i < nodeList.getLength(); i++) {
390 Node node = nodeList.item(i);
391 if (node.getNodeType() == Node.TEXT_NODE) {
392 elementContent = DatatypeHelper.safeTrimOrNullString(((Text) node).getWholeText());
393 break;
394 }
395 }
396
397 if (elementContent == null) {
398 return null;
399 }
400
401 String[] valueComponents = elementContent.split(":");
402 if (valueComponents.length == 1) {
403 return constructQName(element.lookupNamespaceURI(null), valueComponents[0], null);
404 } else {
405 return constructQName(element.lookupNamespaceURI(valueComponents[0]), valueComponents[1],
406 valueComponents[0]);
407 }
408 }
409
410
411
412
413
414
415
416
417 public static List<String> getElementContentAsList(Element element) {
418 if (element == null) {
419 return Collections.emptyList();
420 }
421 return DatatypeHelper.stringToList(element.getTextContent(), LIST_DELIMITERS);
422 }
423
424
425
426
427
428
429
430
431
432
433 public static QName constructQName(String namespaceURI, String localName, String prefix) {
434 if (DatatypeHelper.isEmpty(prefix)) {
435 return new QName(namespaceURI, localName);
436 } else if (DatatypeHelper.isEmpty(namespaceURI)) {
437 return new QName(localName);
438 }
439
440 return new QName(namespaceURI, localName, prefix);
441 }
442
443
444
445
446
447
448
449
450
451 public static QName constructQName(String qname, XMLObject owningObject) {
452 return constructQName(qname, owningObject.getDOM());
453 }
454
455
456
457
458
459
460
461
462
463 public static QName constructQName(String qname, Element owningElement) {
464 String nsURI;
465 String nsPrefix;
466 String name;
467
468 if (qname.indexOf(":") > -1) {
469 StringTokenizer qnameTokens = new StringTokenizer(qname, ":");
470 nsPrefix = qnameTokens.nextToken();
471 name = qnameTokens.nextToken();
472 } else {
473 nsPrefix = "";
474 name = qname;
475 }
476
477 nsURI = lookupNamespaceURI(owningElement, nsPrefix);
478 return constructQName(nsURI, name, nsPrefix);
479 }
480
481
482
483
484
485
486
487
488
489 public static Element constructElement(Document document, QName elementName) {
490 return constructElement(document, elementName.getNamespaceURI(), elementName.getLocalPart(), elementName
491 .getPrefix());
492 }
493
494
495
496
497
498
499
500
501
502
503
504 public static Element constructElement(Document document, String namespaceURI, String localName, String prefix) {
505 String trimmedLocalName = DatatypeHelper.safeTrimOrNullString(localName);
506
507 if (trimmedLocalName == null) {
508 throw new IllegalArgumentException("Local name may not be null or empty");
509 }
510
511 String qualifiedName;
512 String trimmedPrefix = DatatypeHelper.safeTrimOrNullString(prefix);
513 if (trimmedPrefix != null) {
514 qualifiedName = trimmedPrefix + ":" + DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
515 } else {
516 qualifiedName = DatatypeHelper.safeTrimOrNullString(trimmedLocalName);
517 }
518
519 if (!DatatypeHelper.isEmpty(namespaceURI)) {
520 return document.createElementNS(namespaceURI, qualifiedName);
521 } else {
522 return document.createElementNS(null, qualifiedName);
523 }
524 }
525
526
527
528
529
530
531
532 public static void appendChildElement(Element parentElement, Element childElement) {
533 Document parentDocument = parentElement.getOwnerDocument();
534 adoptElement(childElement, parentDocument);
535
536 parentElement.appendChild(childElement);
537 }
538
539
540
541
542
543
544
545 public static void adoptElement(Element adoptee, Document adopter) {
546 if (!(adoptee.getOwnerDocument().equals(adopter))) {
547 if (adopter.adoptNode(adoptee) == null) {
548
549 throw new XMLRuntimeException("DOM Element node adoption failed");
550 }
551 }
552 }
553
554
555
556
557
558
559
560 public static void appendTextContent(Element domElement, String textContent) {
561 if (textContent == null) {
562 return;
563 }
564 Document parentDocument = domElement.getOwnerDocument();
565 Text textNode = parentDocument.createTextNode(textContent);
566 domElement.appendChild(textNode);
567 }
568
569
570
571
572
573
574
575
576 public static void appendNamespaceDeclaration(Element domElement, String namespaceURI, String prefix) {
577 String nsURI = DatatypeHelper.safeTrimOrNullString(namespaceURI);
578 String nsPrefix = DatatypeHelper.safeTrimOrNullString(prefix);
579
580
581 if (nsURI == null && nsPrefix == null) {
582 return;
583 }
584
585 String attributeName;
586 if (nsPrefix == null) {
587 attributeName = XMLConstants.XMLNS_PREFIX;
588 } else {
589 attributeName = XMLConstants.XMLNS_PREFIX + ":" + nsPrefix;
590 }
591
592 String attributeValue;
593 if (nsURI == null) {
594 attributeValue = "";
595 } else {
596 attributeValue = nsURI;
597 }
598
599 domElement.setAttributeNS(XMLConstants.XMLNS_NS, attributeName, attributeValue);
600 }
601
602
603
604
605
606
607
608
609
610
611
612
613
614 public static String lookupNamespaceURI(Element startingElement, String prefix) {
615 return lookupNamespaceURI(startingElement, null, prefix);
616 }
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631 public static String lookupNamespaceURI(Element startingElement, Element stopingElement, String prefix) {
632 String namespaceURI;
633
634
635 if (startingElement.hasAttributes()) {
636 NamedNodeMap map = startingElement.getAttributes();
637 int length = map.getLength();
638 for (int i = 0; i < length; i++) {
639 Node attr = map.item(i);
640 String attrPrefix = attr.getPrefix();
641 String value = attr.getNodeValue();
642 namespaceURI = attr.getNamespaceURI();
643 if (namespaceURI != null && namespaceURI.equals(XMLConstants.XMLNS_NS)) {
644
645 if (prefix == null && attr.getNodeName().equals(XMLConstants.XMLNS_PREFIX)) {
646
647 return value;
648 } else if (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_PREFIX)
649 && attr.getLocalName().equals(prefix)) {
650
651 return value;
652 }
653 }
654 }
655 }
656
657 if(startingElement != stopingElement){
658 Element ancestor = getElementAncestor(startingElement);
659 if (ancestor != null) {
660 return lookupNamespaceURI(ancestor, stopingElement, prefix);
661 }
662 }
663
664 return null;
665 }
666
667
668
669
670
671
672
673
674
675
676
677
678
679 public static String lookupPrefix(Element startingElement, String namespaceURI) {
680 return lookupPrefix(startingElement, null, namespaceURI);
681 }
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 public static String lookupPrefix(Element startingElement, Element stopingElement, String namespaceURI) {
697 String namespace;
698
699
700 if (startingElement.hasAttributes()) {
701 NamedNodeMap map = startingElement.getAttributes();
702 int length = map.getLength();
703 for (int i = 0; i < length; i++) {
704 Node attr = map.item(i);
705 String attrPrefix = attr.getPrefix();
706 String value = attr.getNodeValue();
707 namespace = attr.getNamespaceURI();
708 if (namespace != null && namespace.equals(XMLConstants.XMLNS_NS)) {
709
710 if (attr.getNodeName().equals(XMLConstants.XMLNS_PREFIX)
711 || (attrPrefix != null && attrPrefix.equals(XMLConstants.XMLNS_PREFIX))
712 && value.equals(namespaceURI)) {
713
714 String localname = attr.getLocalName();
715 String foundNamespace = startingElement.lookupNamespaceURI(localname);
716 if (foundNamespace != null && foundNamespace.equals(namespaceURI)) {
717 return localname;
718 }
719 }
720
721 }
722 }
723 }
724
725 if(startingElement != stopingElement){
726 Element ancestor = getElementAncestor(startingElement);
727 if (ancestor != null) {
728 return lookupPrefix(ancestor, stopingElement, namespaceURI);
729 }
730 }
731
732 return null;
733 }
734
735
736
737
738
739
740
741
742
743
744
745 public static List<Element> getChildElementsByTagNameNS(Element root, String namespaceURI, String localName) {
746 ArrayList<Element> children = new ArrayList<Element>();
747 NodeList childNodes = root.getChildNodes();
748
749 int numOfNodes = childNodes.getLength();
750 Node childNode;
751 Element e;
752 for (int i = 0; i < numOfNodes; i++) {
753 childNode = childNodes.item(i);
754 if (childNode.getNodeType() == Node.ELEMENT_NODE) {
755 e = (Element) childNode;
756 if (e.getNamespaceURI().equals(namespaceURI) && e.getLocalName().equals(localName)) {
757 children.add(e);
758 }
759 }
760 }
761
762 return children;
763 }
764
765
766
767
768
769
770
771
772
773
774 public static List<Element> getChildElementsByTagName(Element root, String localName) {
775 ArrayList<Element> children = new ArrayList<Element>();
776 NodeList childNodes = root.getChildNodes();
777
778 int numOfNodes = childNodes.getLength();
779 Node childNode;
780 Element e;
781 for (int i = 0; i < numOfNodes; i++) {
782 childNode = childNodes.item(i);
783 if (childNode.getNodeType() == Node.ELEMENT_NODE) {
784 e = (Element) childNode;
785 if (e.getLocalName().equals(localName)) {
786 children.add(e);
787 }
788 }
789 }
790
791 return children;
792 }
793
794
795
796
797
798
799
800
801 public static Map<QName, List<Element>> getChildElements(Element root) {
802 Map<QName, List<Element>> children = new HashMap<QName, List<Element>>();
803 NodeList childNodes = root.getChildNodes();
804
805 int numOfNodes = childNodes.getLength();
806 Node childNode;
807 Element e;
808 QName qname;
809 List<Element> elements;
810 for (int i = 0; i < numOfNodes; i++) {
811 childNode = childNodes.item(i);
812 if (childNode.getNodeType() == Node.ELEMENT_NODE) {
813 e = (Element) childNode;
814 qname = getNodeQName(e);
815 elements = children.get(qname);
816 if (elements == null) {
817 elements = new ArrayList<Element>();
818 children.put(qname, elements);
819 }
820
821 elements.add(e);
822 }
823 }
824
825 return children;
826 }
827
828
829
830
831
832
833
834
835 public static Element getElementAncestor(Node currentNode) {
836 Node parent = currentNode.getParentNode();
837 if (parent != null) {
838 short type = parent.getNodeType();
839 if (type == Node.ELEMENT_NODE) {
840 return (Element) parent;
841 }
842 return getElementAncestor(parent);
843 }
844 return null;
845 }
846
847
848
849
850
851
852
853
854 public static String nodeToString(Node node) {
855 StringWriter writer = new StringWriter();
856 writeNode(node, writer);
857 return writer.toString();
858 }
859
860
861
862
863
864
865
866
867 public static String prettyPrintXML(Node node) {
868 TransformerFactory tfactory = TransformerFactory.newInstance();
869 Transformer serializer;
870 try {
871 serializer = tfactory.newTransformer();
872 serializer.setOutputProperty(OutputKeys.INDENT, "yes");
873 serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
874
875 StringWriter output = new StringWriter();
876 serializer.transform(new DOMSource(node), new StreamResult(output));
877 return output.toString();
878 } catch (TransformerException e) {
879
880 e.printStackTrace();
881
882 throw new RuntimeException(e);
883 }
884 }
885
886
887
888
889
890
891
892
893 public static void writeNode(Node node, Writer output) {
894 DOMImplementation domImpl = node.getOwnerDocument().getImplementation();
895 DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
896 LSSerializer serializer = domImplLS.createLSSerializer();
897 serializer.setFilter(new LSSerializerFilter() {
898
899 public short acceptNode(Node arg0) {
900 return FILTER_ACCEPT;
901 }
902
903 public int getWhatToShow() {
904 return SHOW_ALL;
905 }
906 });
907
908 LSOutput serializerOut = domImplLS.createLSOutput();
909 serializerOut.setCharacterStream(output);
910
911 serializer.write(node, serializerOut);
912 }
913
914
915
916
917
918
919
920
921 public static void writeNode(Node node, OutputStream output) {
922 DOMImplementation domImpl;
923 if(node instanceof Document){
924 domImpl = ((Document)node).getImplementation();
925 }else{
926 domImpl = node.getOwnerDocument().getImplementation();
927 }
928
929 DOMImplementationLS domImplLS = (DOMImplementationLS) domImpl.getFeature("LS", "3.0");
930 LSSerializer serializer = domImplLS.createLSSerializer();
931 serializer.setFilter(new LSSerializerFilter() {
932
933 public short acceptNode(Node arg0) {
934 return FILTER_ACCEPT;
935 }
936
937 public int getWhatToShow() {
938 return SHOW_ALL;
939 }
940 });
941
942 LSOutput serializerOut = domImplLS.createLSOutput();
943 serializerOut.setByteStream(output);
944
945 serializer.write(node, serializerOut);
946 }
947
948
949
950
951
952
953
954
955 public static String qnameToContentString(QName qname) {
956 StringBuffer buf = new StringBuffer();
957
958 if (qname.getPrefix() != null) {
959 buf.append(qname.getPrefix());
960 buf.append(":");
961 }
962 buf.append(qname.getLocalPart());
963 return buf.toString();
964 }
965
966
967
968
969
970
971
972
973
974
975
976 public static void rootNamespaces(Element domElement) throws XMLParserException {
977 rootNamespaces(domElement, domElement);
978 }
979
980
981
982
983
984
985
986
987
988
989 private static void rootNamespaces(Element domElement, Element upperNamespaceSearchBound) throws XMLParserException {
990 String namespaceURI = null;
991 String namespacePrefix = domElement.getPrefix();
992
993
994 boolean nsDeclaredOnElement = false;
995 if (namespacePrefix == null) {
996 nsDeclaredOnElement = domElement.hasAttributeNS(null, XMLConstants.XMLNS_PREFIX);
997 } else {
998 nsDeclaredOnElement = domElement.hasAttributeNS(XMLConstants.XMLNS_NS, namespacePrefix);
999 }
1000
1001 if (!nsDeclaredOnElement) {
1002
1003
1004 namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);
1005
1006 if (namespaceURI == null) {
1007
1008
1009 namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
1010 if (namespaceURI != null) {
1011
1012
1013 appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
1014 } else {
1015
1016
1017
1018 if (namespacePrefix != null) {
1019 throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
1020 + " found on element " + getNodeQName(domElement));
1021 }
1022 }
1023 }
1024 }
1025
1026
1027 NamedNodeMap attributes = domElement.getAttributes();
1028 Node attributeNode;
1029 for (int i = 0; i < attributes.getLength(); i++) {
1030 namespacePrefix = null;
1031 namespaceURI = null;
1032 attributeNode = attributes.item(i);
1033
1034
1035 if (attributeNode.getNodeType() != Node.ATTRIBUTE_NODE) {
1036 continue;
1037 }
1038
1039 namespacePrefix = attributeNode.getPrefix();
1040 if (!DatatypeHelper.isEmpty(namespacePrefix)) {
1041
1042
1043 if (namespacePrefix.equals(XMLConstants.XMLNS_PREFIX)
1044 || namespacePrefix.equals(XMLConstants.XML_PREFIX)) {
1045 continue;
1046 }
1047
1048
1049 namespaceURI = lookupNamespaceURI(domElement, upperNamespaceSearchBound, namespacePrefix);
1050 if (namespaceURI == null) {
1051 namespaceURI = lookupNamespaceURI(upperNamespaceSearchBound, null, namespacePrefix);
1052 if (namespaceURI == null) {
1053 throw new XMLParserException("Unable to resolve namespace prefix " + namespacePrefix
1054 + " found on attribute " + getNodeQName(attributeNode) + " found on element "
1055 + getNodeQName(domElement));
1056 }
1057
1058 appendNamespaceDeclaration(domElement, namespaceURI, namespacePrefix);
1059 }
1060 }
1061 }
1062
1063
1064
1065 NodeList childNodes = domElement.getChildNodes();
1066 Node childNode;
1067 for (int i = 0; i < childNodes.getLength(); i++) {
1068 childNode = childNodes.item(i);
1069 if (childNode.getNodeType() == Node.ELEMENT_NODE) {
1070 rootNamespaces((Element) childNode, upperNamespaceSearchBound);
1071 }
1072 }
1073 }
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 public static boolean isElementNamed(Element e, String ns, String localName) {
1084 return e != null && DatatypeHelper.safeEquals(ns, e.getNamespaceURI())
1085 && DatatypeHelper.safeEquals(localName, e.getLocalName());
1086 }
1087
1088
1089
1090
1091
1092
1093
1094 public static Element getFirstChildElement(Node n) {
1095 Node child = n.getFirstChild();
1096 while (child != null && child.getNodeType() != Node.ELEMENT_NODE) {
1097 child = child.getNextSibling();
1098 }
1099
1100 if (child != null) {
1101 return (Element) child;
1102 } else {
1103 return null;
1104 }
1105 }
1106
1107
1108
1109
1110
1111
1112
1113 public static Element getNextSiblingElement(Node n) {
1114 Node sib = n.getNextSibling();
1115 while (sib != null && sib.getNodeType() != Node.ELEMENT_NODE) {
1116 sib = sib.getNextSibling();
1117 }
1118
1119 if (sib != null) {
1120 return (Element) sib;
1121 } else {
1122 return null;
1123 }
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133 public static long durationToLong(String duration) {
1134 Duration xmlDuration = getDataTypeFactory().newDuration(duration);
1135 return xmlDuration.getTimeInMillis(new GregorianCalendar());
1136 }
1137
1138
1139
1140
1141
1142
1143
1144
1145 public static String longToDuration(long duration) {
1146 Duration xmlDuration = getDataTypeFactory().newDuration(duration);
1147 return xmlDuration.toString();
1148 }
1149
1150 }