View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.helpers;
15  
16  /**
17   * Utility class for transforming strings.
18   * 
19   * @author Ceki Gülcü
20   * @author Michael A. McAngus
21   */
22  public class Transform {
23    private static final String CDATA_START = "<![CDATA[";
24    private static final String CDATA_END = "]]>";
25    private static final String CDATA_PSEUDO_END = "]]&gt;";
26    private static final String CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END
27        + CDATA_START;
28    private static final int CDATA_END_LEN = CDATA_END.length();
29  
30    /**
31     * This method takes a string which may contain HTML tags (ie, &lt;b&gt;,
32     * &lt;table&gt;, etc) and replaces any '<' and '>' characters with
33     * respective predefined entity references.
34     * 
35     * @param input
36     *          The text to be converted.
37     */
38    public static String escapeTags(final String input) {
39      // Check if the string is null or zero length -- if so, return
40      // what was sent in.
41      if ((input == null) || (input.length() == 0)
42          || (input.indexOf("<") == -1 && input.indexOf(">") == -1)) {
43        return input;
44      }
45  
46      StringBuffer buf = new StringBuffer(input);
47      return escapeTags(buf);
48    }
49    
50  
51    /**
52     * This method takes a StringBuilder which may contain HTML tags (ie, &lt;b&gt;,
53     * &lt;table&gt;, etc) and replaces any '<' and '>' characters with
54     * respective predefined entity references.
55     * @param buf
56     * @return
57     */
58    public static String escapeTags(final StringBuffer buf) {
59      for (int i = 0; i < buf.length(); i++) {
60        char ch = buf.charAt(i);
61        if (ch == '<') {
62          buf.replace(i, i + 1, "&lt;");
63        } else if (ch == '>') {
64          buf.replace(i, i + 1, "&gt;");
65        }
66      }
67      return buf.toString();
68    }
69    
70  
71    /**
72     * Ensures that embedded CDEnd strings (]]>) are handled properly within
73     * message, NDC and throwable tag text.
74     * 
75     * @param output
76     *          Writer. The initial CDSutart (<![CDATA[) and final CDEnd (]]>) of
77     *          the CDATA section are the responsibility of the calling method.
78     * 
79     * @param str
80     *          The String that is inserted into an existing CDATA Section.
81     */
82    public static void appendEscapingCDATA(StringBuilder output, String str) {
83      if (str == null) {
84        return;
85      }
86  
87      int end = str.indexOf(CDATA_END);
88  
89      if (end < 0) {
90        output.append(str);
91  
92        return;
93      }
94  
95      int start = 0;
96  
97      while (end > -1) {
98        output.append(str.substring(start, end));
99        output.append(CDATA_EMBEDED_END);
100       start = end + CDATA_END_LEN;
101 
102       if (start < str.length()) {
103         end = str.indexOf(CDATA_END, start);
104       } else {
105         return;
106       }
107     }
108 
109     output.append(str.substring(start));
110   }
111 }