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

Last change on this file since 1242 was 1241, checked in by pharms, 13 years ago
  • applied code formating
File size: 2.1 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 respective XML entities
46     * {@literal &amp;, &lt;, &gt;, &apos;, and &quot;} without destroying already existing
47     * 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 literals
53     */
54    public static String xmlEntityReplacement(String str) {
55        String result = str;
56        if (result != null && !"".equals(result)) {
57            result = result.replaceAll("&(?!(?:lt|gt|apos|quot|amp);)", "&amp;");
58            result = result.replaceAll("<", "&lt;");
59            result = result.replaceAll(">", "&gt;");
60            result = result.replaceAll("'", "&apos;");
61            result = result.replaceAll("\"", "&quot;");
62        }
63        return result;
64    }
65}
Note: See TracBrowser for help on using the repository browser.