1 | package de.ugoe.cs.util;
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * <p>
|
---|
5 | * Helper class that provides methods to simplify working with {@link String}s.
|
---|
6 | * </p>
|
---|
7 | *
|
---|
8 | * @author Steffen Herbold
|
---|
9 | * @version 1.0
|
---|
10 | */
|
---|
11 | final public class StringTools {
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * <p>
|
---|
15 | * Private constructor to prevent initializing of the class.
|
---|
16 | * </p>
|
---|
17 | */
|
---|
18 | private StringTools() {
|
---|
19 |
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * <p>
|
---|
24 | * Simplifies use of operation system specific line separators.
|
---|
25 | * </p>
|
---|
26 | */
|
---|
27 | public final static String ENDLINE = System.getProperty("line.separator");
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * <p>
|
---|
31 | * Replaces all occurrences of {@literal &, <, >, ', and "} with their
|
---|
32 | * respective XML entities {@literal &, <, >, ', and "}
|
---|
33 | * without destroying already existing entities.
|
---|
34 | * </p>
|
---|
35 | *
|
---|
36 | * @param str
|
---|
37 | * String where the XML entities are to be replaced
|
---|
38 | * @return new String, where the XML entities are used instead of the
|
---|
39 | * literals
|
---|
40 | */
|
---|
41 | public static String xmlEntityReplacement(String str) {
|
---|
42 | String result = str;
|
---|
43 | if (result != null && !"".equals(result)) {
|
---|
44 | result = result
|
---|
45 | .replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&");
|
---|
46 | result = result.replaceAll("<", "<");
|
---|
47 | result = result.replaceAll(">", ">");
|
---|
48 | result = result.replaceAll("'", "'");
|
---|
49 | result = result.replaceAll("\"", """);
|
---|
50 | }
|
---|
51 | return result;
|
---|
52 | }
|
---|
53 | }
|
---|