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

Last change on this file since 1101 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 2.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.util;
16
17/**
18 * <p>
19 * Helper class that provides methods to simplify working with {@link String}s.
20 * </p>
21 *
22 * @author Steffen Herbold
23 * @version 1.0
24 */
25final public class StringTools {
26
27        /**
28         * <p>
29         * Private constructor to prevent initializing of the class.
30         * </p>
31         */
32        private StringTools() {
33
34        }
35
36        /**
37         * <p>
38         * Simplifies use of operation system specific line separators.
39         * </p>
40         */
41        public final static String ENDLINE = System.getProperty("line.separator");
42
43        /**
44         * <p>
45         * Replaces all occurrences of {@literal &, <, >, ', and "} with their
46         * respective XML entities {@literal &amp;, &lt;, &gt;, &apos;, and &quot;}
47         * without destroying already existing entities.
48         * </p>
49         *
50         * @param str
51         *            String where the XML entities are to be replaced
52         * @return new String, where the XML entities are used instead of the
53         *         literals
54         */
55        public static String xmlEntityReplacement(String str) {
56                String result = str;
57                if (result != null && !"".equals(result)) {
58                        result = result
59                                        .replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&amp;");
60                        result = result.replaceAll("<", "&lt;");
61                        result = result.replaceAll(">", "&gt;");
62                        result = result.replaceAll("'", "&apos;");
63                        result = result.replaceAll("\"", "&quot;");
64                }
65                return result;
66        }
67}
Note: See TracBrowser for help on using the repository browser.