source: trunk/java-utils/src/main/java/de/ugoe/cs/util/StringTools.java @ 776

Last change on this file since 776 was 350, checked in by sherbold, 13 years ago
File size: 1.3 KB
Line 
1package 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 */
11final 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 &amp;, &lt;, &gt;, &apos;, and &quot;}
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);)", "&amp;");
46                        result = result.replaceAll("<", "&lt;");
47                        result = result.replaceAll(">", "&gt;");
48                        result = result.replaceAll("'", "&apos;");
49                        result = result.replaceAll("\"", "&quot;");
50                }
51                return result;
52        }
53}
Note: See TracBrowser for help on using the repository browser.