source: trunk/JavaHelperLib/src/de/ugoe/cs/util/StringTools.java @ 1

Last change on this file since 1 was 1, checked in by sherbold, 13 years ago
File size: 915 bytes
Line 
1package de.ugoe.cs.util;
2
3final public class StringTools {
4
5        public final static String ENDLINE = System.getProperty("line.separator");
6       
7        /**
8         * <p>
9         * Replaces all occurences of {@literal &, <, >, ', and "} with their
10         * respective XML entites {@literal &amp;, &lt;, &gt;, &apos;, and &quot;}
11         * without destroying already existing entities.
12         * </p>
13         *
14         * @param str
15         *            String where the XML entites are to be replaced
16         * @return new String, where the XML entites are used instead of the
17         *         literals
18         */
19        public static String xmlEntityReplacement(String str) {
20                String result = str;
21                result = result.replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&amp;");
22                result = result.replaceAll("<", "&lt;");
23                result = result.replaceAll(">", "&gt;");
24                result = result.replaceAll("'", "&apos;");
25                result = result.replaceAll("\"", "&quot;");
26                return result;
27        }
28}
Note: See TracBrowser for help on using the repository browser.