# HG changeset patch # User Mike Becker # Date 1704621763 -3600 # Node ID afe11f0ad62b20e7dcdfb4d7219fa5c0af475368 # Parent 002497e6c27d085ef01ff96aaa2d6fa353b15ba8 trim indents by default - fixes #317 diff -r 002497e6c27d -r afe11f0ad62b src/main/java/de/unixwork/uwproj/Util.java --- 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 "";