View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.util;
21  
22  import java.io.IOException;
23  import java.io.LineNumberReader;
24  import java.io.PrintWriter;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  import java.util.ArrayList;
28  
29  /**
30   * Utility class for working with xml data
31   *
32   * Implementation is heavily based on org.apache.log4j.helpers.Transform
33   * 
34   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
35   */
36  public class Transform {
37  
38      private static final String CDATA_START = "<![CDATA[";
39  
40      private static final String CDATA_END = "]]>";
41  
42      private static final String CDATA_PSEUDO_END = "]]&gt;";
43  
44      private static final String CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END + CDATA_START;
45  
46      private static final int CDATA_END_LEN = CDATA_END.length();
47  
48      /**
49       * This method takes a string which may contain HTML tags (ie,
50       * &lt;b&gt;, &lt;table&gt;, etc) and replaces any
51       * '&lt;',  '&gt;' , '&amp;' or '"'
52       * characters with respective predefined entity references.
53       *
54       * @param input The text to be converted.
55       * @return The input string with the special characters replaced.
56       * */
57      public static String escapeTags(String input) {
58          // Check if the string is null, zero length or devoid of special characters
59          // if so, return what was sent in.
60  
61          if (input == null || input.length() == 0) {
62              return input;
63          }
64  
65          StringBuilder buf = new StringBuilder(input.length() + 6);
66          char ch;
67  
68          int len = input.length();
69          
70          for (int i = 0; i < len; i++) {
71              ch = input.charAt(i);
72              
73              switch ( ch )
74              {
75                  case '<' :
76                      buf.append("&lt;");
77                      break;
78                      
79                  case '>' :
80                      buf.append("&gt;");
81                      break;
82                      
83                  case '&' :
84                      buf.append("&amp;");
85                      break;
86                      
87                  case '"' :
88                      buf.append("&quot;");
89                      break;
90  
91                  default :
92                      buf.append(ch);
93              }
94          }
95          
96          return buf.toString();
97      }
98  
99      /**
100      * Ensures that embeded CDEnd strings (]]&gt;) are handled properly
101      * within message, NDC and throwable tag text.
102      *
103      * @param buf StringBuffer holding the XML data to this point.  The
104      * initial CDStart (&lt;![CDATA[) and final CDEnd (]]&gt;) of the CDATA
105      * section are the responsibility of the calling method.
106      * @param str The String that is inserted into an existing CDATA Section within buf.
107      * */
108     public static void appendEscapingCDATA(final StringBuilder buf, final String str) {
109         if (str != null) {
110             int end = str.indexOf(CDATA_END);
111             
112             if (end < 0) {
113                 buf.append(str);
114             } else {
115                 int start = 0;
116                 
117                 while (end > -1) {
118                     buf.append(str.substring(start, end));
119                     buf.append(CDATA_EMBEDED_END);
120                     start = end + CDATA_END_LEN;
121                     
122                     if (start < str.length()) {
123                         end = str.indexOf(CDATA_END, start);
124                     } else {
125                         return;
126                     }
127                 }
128                 
129                 buf.append(str.substring(start));
130             }
131         }
132     }
133 
134     /**
135      * Converts a Throwable into an array of Strings
136      * @param throwable The Throwable to convert 
137      * @return string representation of the throwable
138      */
139     public static String[] getThrowableStrRep(Throwable throwable) {
140         StringWriter sw = new StringWriter();
141         PrintWriter pw = new PrintWriter(sw);
142         throwable.printStackTrace(pw);
143         pw.flush();
144         LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
145         ArrayList<String> lines = new ArrayList<>();
146         
147         try {
148             String line = reader.readLine();
149             
150             while (line != null) {
151                 lines.add(line);
152                 line = reader.readLine();
153             }
154         } catch (IOException ex) {
155             lines.add(ex.toString());
156         }
157         
158         String[] rep = new String[lines.size()];
159         lines.toArray(rep);
160         
161         return rep;
162     }
163 }