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

Sat, 20 Jan 2024 15:37:16 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 20 Jan 2024 15:37:16 +0100
changeset 104
ea36fd802446
parent 96
eb9152e5f1b6
child 113
24f32dbd88cd
permissions
-rw-r--r--

adds documentation for new toolchain attr to XSD

relates to #346

0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 package de.unixwork.uwproj;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 import org.w3c.dom.Element;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 import org.w3c.dom.Node;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 import org.w3c.dom.NodeList;
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
7 public class Util {
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
8
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
9 /**
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
10 * Returns the text content of the given element with indentation automatically trimmed.
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
11 *
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
12 * @param elm the element
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
13 * @return the contents or empty string if there are no contents
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
14 */
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 public static String getContent(Element elm) {
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
16 return getContent(elm, true);
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
17 }
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
18
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
19 /**
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
20 * Returns the text content of the given element.
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
21 *
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
22 * @param elm the element
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
23 * @param trimIndent indicates whether indentation shall be trimmed
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
24 * @return the contents or empty string if there are no contents
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
25 */
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
26 public static String getContent(Element elm, boolean trimIndent) {
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 NodeList nodes = elm.getChildNodes();
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
28 for (int i = 0; i < nodes.getLength(); i++) {
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
29 var node = nodes.item(i);
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 if (node.getNodeType() == Node.TEXT_NODE) {
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
31 final var v = node.getNodeValue();
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
32 if (v == null) {
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
33 return "";
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
34 } else if (trimIndent) {
96
eb9152e5f1b6 trim all contents by default - relates to #317
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
35 return v.trim().replaceAll("\\n[ \\t]+", "\n");
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
36 } else {
96
eb9152e5f1b6 trim all contents by default - relates to #317
Mike Becker <universe@uap-core.de>
parents: 95
diff changeset
37 return v.trim();
95
afe11f0ad62b trim indents by default - fixes #317
Mike Becker <universe@uap-core.de>
parents: 64
diff changeset
38 }
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 }
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 }
40
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
41 return "";
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
42 }
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
43
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
44 public static boolean isNullOrBlank(String s) {
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
45 return s == null || s.isBlank();
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
46 }
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
47
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
48 public static boolean isNotNullOrBlank(String s) {
b42bfc9e9983 first minor code improvements
Mike Becker <universe@uap-core.de>
parents: 0
diff changeset
49 return s != null && !s.isBlank();
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
51
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
52 /**
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
53 * Creates an identifier from the given string that can be used as an identifier in a POSIX shell script.
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
54 * <p>
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
55 * If the given string does not contain any character that is illegal in a POSIX shell variable or function name,
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
56 * the string is returned unmodified.
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
57 *
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
58 * @param s the input string
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
59 * @return the sanitized string
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
60 */
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
61 public static String shId(String s) {
64
00586d892855 basic implementation of shId()
Mike Becker <universe@uap-core.de>
parents: 41
diff changeset
62 return s.replaceAll("[^A-Za-z0-9_]", "_");
41
75ee588d5d9e some more code cleanup
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
63 }
0
38775db5fdf5 add existing source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }

mercurial