Sun, 07 Jan 2024 11:02:43 +0100
trim indents by default - fixes #317
src/main/java/de/unixwork/uwproj/Util.java | file | annotate | diff | comparison | revisions |
--- a/src/main/java/de/unixwork/uwproj/Util.java Fri Jan 05 21:04:57 2024 +0100 +++ b/src/main/java/de/unixwork/uwproj/Util.java Sun Jan 07 11:02:43 2024 +0100 @@ -5,19 +5,37 @@ import org.w3c.dom.NodeList; public class Util { + /** - * Returns the text content of the given element. + * Returns the text content of the given element with indentation automatically trimmed. * * @param elm the element * @return the contents or empty string if there are no contents */ public static String getContent(Element elm) { + return getContent(elm, true); + } + + /** + * Returns the text content of the given element. + * + * @param elm the element + * @param trimIndent indicates whether indentation shall be trimmed + * @return the contents or empty string if there are no contents + */ + public static String getContent(Element elm, boolean trimIndent) { NodeList nodes = elm.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { var node = nodes.item(i); if (node.getNodeType() == Node.TEXT_NODE) { final var v = node.getNodeValue(); - return v == null ? "" : v; + if (v == null) { + return ""; + } else if (trimIndent) { + return v.replaceAll("\\n[ \\t]+", "\n"); + } else { + return v; + } } } return "";