KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > helpers > Transform


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.log4j.helpers;
18
19 import org.apache.log4j.spi.LoggingEvent;
20 import org.apache.log4j.spi.LocationInfo;
21
22 /**
23    Utility class for transforming strings.
24
25    @author Ceki Gülcü
26    @author Michael A. McAngus
27  */

28 public class Transform {
29
30    private static final String JavaDoc CDATA_START = "<![CDATA[";
31    private static final String JavaDoc CDATA_END = "]]>";
32    private static final String JavaDoc CDATA_PSEUDO_END = "]]&gt;";
33    private static final String JavaDoc CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END + CDATA_START;
34    private static final int CDATA_END_LEN = CDATA_END.length();
35
36   /**
37    * This method takes a string which may contain HTML tags (ie,
38    * &lt;b&gt;, &lt;table&gt;, etc) and replaces any '<' and '>'
39    * characters with respective predefined entity references.
40    *
41    * @param input The text to be converted.
42    * @return The input string with the characters '<' and '>' replaced with
43    * &amp;lt; and &amp;gt; respectively.
44    * */

45   static public String JavaDoc escapeTags(String JavaDoc input) {
46     //Check if the string is null or zero length -- if so, return
47
//what was sent in.
48

49     if( input == null || input.length() == 0 ) {
50       return input;
51     }
52
53     //Use a StringBuffer in lieu of String concatenation -- it is
54
//much more efficient this way.
55

56     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(input.length() + 6);
57     char ch = ' ';
58
59     int len = input.length();
60     for(int i=0; i < len; i++) {
61       ch = input.charAt(i);
62       if(ch == '<') {
63     buf.append("&lt;");
64       } else if(ch == '>') {
65     buf.append("&gt;");
66       } else {
67     buf.append(ch);
68       }
69     }
70     return buf.toString();
71   }
72
73   /**
74   * Ensures that embeded CDEnd strings (]]>) are handled properly
75   * within message, NDC and throwable tag text.
76   *
77   * @param buf StringBuffer holding the XML data to this point. The
78   * initial CDStart (<![CDATA[) and final CDEnd (]]>) of the CDATA
79   * section are the responsibility of the calling method.
80   * @param str The String that is inserted into an existing CDATA Section within buf.
81   * */

82   static public void appendEscapingCDATA(StringBuffer JavaDoc buf, String JavaDoc str) {
83     if(str == null) {
84       buf.append("");
85       return;
86     }
87     
88     int end = str.indexOf(CDATA_END);
89     if (end < 0) {
90       buf.append(str);
91       return;
92     }
93     
94     int start = 0;
95     while (end > -1) {
96       buf.append(str.substring(start,end));
97       buf.append(CDATA_EMBEDED_END);
98       start = end + CDATA_END_LEN;
99       if (start < str.length()) {
100     end = str.indexOf(CDATA_END, start);
101       } else {
102     return;
103       }
104     }
105     
106     buf.append(str.substring(start));
107   }
108 }
109
Popular Tags