Lines 63-146
Link Here
|
63 |
private Util() {} |
63 |
private Util() {} |
64 |
|
64 |
|
65 |
/** |
65 |
/** |
66 |
* Search for an XML element in the direct children of a parent. |
|
|
67 |
* DOM provides a similar method but it does a recursive search |
68 |
* which we do not want. It also gives a node list and we want |
69 |
* only one result. |
70 |
* @param parent a parent element |
71 |
* @param name the intended local name |
72 |
* @param namespace the intended namespace |
73 |
* @return the one child element with that name, or null if none or more than one |
74 |
*/ |
75 |
public static Element findElement(Element parent, String name, String namespace) { |
76 |
Element result = null; |
77 |
NodeList l = parent.getChildNodes(); |
78 |
int len = l.getLength(); |
79 |
for (int i = 0; i < len; i++) { |
80 |
if (l.item(i).getNodeType() == Node.ELEMENT_NODE) { |
81 |
Element el = (Element)l.item(i); |
82 |
if (name.equals(el.getLocalName()) && |
83 |
((namespace != null && namespace.equals(el.getNamespaceURI())) || namespace == null)) { |
84 |
if (result == null) { |
85 |
result = el; |
86 |
} else { |
87 |
return null; |
88 |
} |
89 |
} |
90 |
} |
91 |
} |
92 |
return result; |
93 |
} |
94 |
|
95 |
/** |
96 |
* Extract nested text from an element. |
97 |
* Currently does not handle coalescing text nodes, CDATA sections, etc. |
98 |
* @param parent a parent element |
99 |
* @return the nested text, or null if none was found |
100 |
*/ |
101 |
public static String findText(Element parent) { |
102 |
NodeList l = parent.getChildNodes(); |
103 |
for (int i = 0; i < l.getLength(); i++) { |
104 |
if (l.item(i).getNodeType() == Node.TEXT_NODE) { |
105 |
Text text = (Text)l.item(i); |
106 |
return text.getNodeValue(); |
107 |
} |
108 |
} |
109 |
return null; |
110 |
} |
111 |
|
112 |
/** |
113 |
* Find all direct child elements of an element. |
114 |
* More useful than {@link Element#getElementsByTagNameNS} because it does |
115 |
* not recurse into recursive child elements. |
116 |
* Children which are all-whitespace text nodes are ignored; others cause |
117 |
* an exception to be thrown. |
118 |
* @param parent a parent element in a DOM tree |
119 |
* @return a list of direct child elements (may be empty) |
120 |
* @throws IllegalArgumentException if there are non-element children besides whitespace |
121 |
*/ |
122 |
public static List<Element> findSubElements(Element parent) throws IllegalArgumentException { |
123 |
NodeList l = parent.getChildNodes(); |
124 |
List<Element> elements = new ArrayList<Element>(l.getLength()); |
125 |
for (int i = 0; i < l.getLength(); i++) { |
126 |
Node n = l.item(i); |
127 |
if (n.getNodeType() == Node.ELEMENT_NODE) { |
128 |
elements.add((Element)n); |
129 |
} else if (n.getNodeType() == Node.TEXT_NODE) { |
130 |
String text = ((Text)n).getNodeValue(); |
131 |
if (text.trim().length() > 0) { |
132 |
throw new IllegalArgumentException("non-ws text encountered in " + parent + ": " + text); // NOI18N |
133 |
} |
134 |
} else if (n.getNodeType() == Node.COMMENT_NODE) { |
135 |
// skip |
136 |
} else { |
137 |
throw new IllegalArgumentException("unexpected non-element child of " + parent + ": " + n); // NOI18N |
138 |
} |
139 |
} |
140 |
return elements; |
141 |
} |
142 |
|
143 |
/** |
144 |
* Create an XML error handler that rethrows errors and fatal errors and logs warnings. |
66 |
* Create an XML error handler that rethrows errors and fatal errors and logs warnings. |
145 |
* @return a standard error handler |
67 |
* @return a standard error handler |
146 |
*/ |
68 |
*/ |