src/main/java/de/unixwork/uwproj/Util.java

changeset 95
afe11f0ad62b
parent 64
00586d892855
child 96
eb9152e5f1b6
equal deleted inserted replaced
94:002497e6c27d 95:afe11f0ad62b
3 import org.w3c.dom.Element; 3 import org.w3c.dom.Element;
4 import org.w3c.dom.Node; 4 import org.w3c.dom.Node;
5 import org.w3c.dom.NodeList; 5 import org.w3c.dom.NodeList;
6 6
7 public class Util { 7 public class Util {
8
8 /** 9 /**
9 * Returns the text content of the given element. 10 * Returns the text content of the given element with indentation automatically trimmed.
10 * 11 *
11 * @param elm the element 12 * @param elm the element
12 * @return the contents or empty string if there are no contents 13 * @return the contents or empty string if there are no contents
13 */ 14 */
14 public static String getContent(Element elm) { 15 public static String getContent(Element elm) {
16 return getContent(elm, true);
17 }
18
19 /**
20 * Returns the text content of the given element.
21 *
22 * @param elm the element
23 * @param trimIndent indicates whether indentation shall be trimmed
24 * @return the contents or empty string if there are no contents
25 */
26 public static String getContent(Element elm, boolean trimIndent) {
15 NodeList nodes = elm.getChildNodes(); 27 NodeList nodes = elm.getChildNodes();
16 for (int i = 0; i < nodes.getLength(); i++) { 28 for (int i = 0; i < nodes.getLength(); i++) {
17 var node = nodes.item(i); 29 var node = nodes.item(i);
18 if (node.getNodeType() == Node.TEXT_NODE) { 30 if (node.getNodeType() == Node.TEXT_NODE) {
19 final var v = node.getNodeValue(); 31 final var v = node.getNodeValue();
20 return v == null ? "" : v; 32 if (v == null) {
33 return "";
34 } else if (trimIndent) {
35 return v.replaceAll("\\n[ \\t]+", "\n");
36 } else {
37 return v;
38 }
21 } 39 }
22 } 40 }
23 return ""; 41 return "";
24 } 42 }
25 43

mercurial