Debian Codegraph @

native/jpype_module/src/main/java/org/jpype/javadoc/DomUtilities.java

raw symbols SCIP
1 /* ****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 **************************************************************************** */
16 package org.jpype.javadoc;
17
18 /**
19 * The usual set of method required to work on DOM.
20 *
21 * DOM leaves a lot of basic stuff incomplete.
22 */
23 import java.util.function.BiConsumer;
24 import java.util.function.Consumer;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27 import org.w3c.dom.Text;
28
29 public class DomUtilities
30 {
31
32 /**
33 * Traverse all children in depth first order applying an operation.
34 *
35 * This is hardened against some level of DOM changes.
36 *
37 * @param node
38 * @param operator
39 * @param type
40 */
41 public static void traverseDFS(Node node, Consumer<Node> operator, short type)
42 {
43 Node child = node.getFirstChild();
44 while (child != null)
45 {
46 // Get a referent to what we are processing next in case the tree changes.
47 Node next = child.getNextSibling();
48
49 // Apply transforms to children first
50 if (child.getNodeType() == Node.ELEMENT_NODE)
51 traverseDFS(child, operator, type);
52
53 // Then process the outer element
54 if (child.getNodeType() == type)
55 operator.accept(child);
56
57 // Proceed
58 child = next;
59 }
60 }
61
62 /**
63 * Traverse the children of a node applying an operation.
64 *
65 * This is hardened against some level of DOM changes.
66 *
67 * @param node
68 * @param operator
69 * @param type
70 */
71 public static void traverseChildren(Node node, Consumer<Node> operator, short type)
72 {
73 Node child = node.getFirstChild();
74 while (child != null)
75 {
76 // Get the next node to process in case this one is changed or removed.
77 Node next = child.getNextSibling();
78 if (child.getNodeType() == type)
79 operator.accept(child);
80
81 // Proceed.
82 child = next;
83 }
84 }
85
86 /**
87 * Traverse all children in depth first order applying an operation.
88 *
89 * This is hardened against some level of DOM changes.
90 *
91 * @param node
92 * @param operator
93 * @param type
94 */
95 public static <T> void traverseDFS(Node node,
96 BiConsumer<Node, T> operator, short type, T data)
97 {
98 Node child = node.getFirstChild();
99 while (child != null)
100 {
101 // Get a referent to what we are processing next in case the tree changes.
102 Node next = child.getNextSibling();
103
104 // Apply transforms to children first
105 if (child.getNodeType() == Node.ELEMENT_NODE)
106 traverseDFS(child, operator, type, data);
107
108 // Then process the outer element
109 if (child.getNodeType() == type)
110 operator.accept(child, data);
111
112 // Proceed
113 child = next;
114 }
115 }
116
117 /**
118 * Traverse the children of a node applying an operation.
119 *
120 * This is hardened against some level of DOM changes.
121 *
122 * @param node
123 * @param operator
124 * @param type
125 */
126 public static <T> void traverseChildren(Node node, BiConsumer<Node, T> operator, short type, T data)
127 {
128 Node child = node.getFirstChild();
129 while (child != null)
130 {
131 // Get the next node to process in case this one is changed or removed.
132 Node next = child.getNextSibling();
133 if (child.getNodeType() == type)
134 operator.accept(child, data);
135
136 // Proceed.
137 child = next;
138 }
139 }
140
141 /**
142 * Remove all attributes from a node.
143 *
144 * @param node
145 */
146 public static void clearAttributes(Node node)
147 {
148 while (node.getAttributes().getLength() > 0)
149 {
150 Node att = node.getAttributes().item(0);
151 node.getAttributes().removeNamedItem(att.getNodeName());
152 }
153 }
154
155 /**
156 * Remove all children from a node.
157 *
158 * @param node
159 */
160 public static void clearChildren(Node node)
161 {
162 while (node.hasChildNodes())
163 node.removeChild(node.getFirstChild());
164 }
165
166 /**
167 * Determine if a block contains a new line.
168 *
169 * @param n
170 * @return
171 */
172 public static boolean containsNL(Node n)
173 {
174 Node child = n.getFirstChild();
175 while (child != null)
176 {
177 if (child.getNodeType() == Node.TEXT_NODE)
178 {
179 if (child.getNodeValue().contains("\n"))
180 return true;
181 }
182 child = child.getNextSibling();
183 }
184 return false;
185 }
186
187 /**
188 * Combine all text with neighbors in immediate children.
189 *
190 * @param node
191 */
192 public static void combineText(Node node)
193 {
194 // merge text nodes
195 Node child = node.getFirstChild();
196 while (child != null)
197 {
198 Node next = child.getNextSibling();
199 if (child.getNodeType() != Node.TEXT_NODE)
200 {
201 child = next;
202 continue;
203 }
204 if (next != null && next.getNodeType() == Node.TEXT_NODE)
205 {
206 child.setTextContent(child.getNodeValue() + next.getNodeValue());
207 child.getParentNode().removeChild(next);
208 continue;
209 }
210 child = next;
211 }
212 }
213
214 /**
215 * Merge the contents of a node with its parent.
216 *
217 * @param parent
218 * @param node
219 */
220 public static void mergeNode(Node parent, Node node)
221 {
222 while (node.hasChildNodes())
223 {
224 parent.insertBefore(node.getFirstChild(), node);
225 }
226 parent.removeChild(node);
227 }
228
229 static void transferContents(Node dest, Node source)
230 {
231 while (source.hasChildNodes())
232 {
233 dest.appendChild(source.getFirstChild());
234 }
235 }
236
237 /**
238 * Traverse a node and replaces all extra whitespace with one space.
239 *
240 * This should be applied to any element where white space is not relevant.
241 *
242 * @param node
243 */
244 public static void removeWhitespace(Node node)
245 {
246 // merge text nodes
247 NodeList children = node.getChildNodes();
248 for (int i = 0; i < children.getLength(); ++i)
249 {
250 Node child = children.item(i);
251 if (child.getNodeType() != Node.TEXT_NODE)
252 continue;
253 Text t = (Text) child;
254 String c = t.getNodeValue();
255 if (c != null)
256 {
257 c = c.replaceAll("\\s+", " ");
258 t.setNodeValue(c);
259 }
260 }
261 }
262
263 }