1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33
34
35
36 public class Transform {
37
38 private static final String CDATA_START = "<![CDATA[";
39 private static final String CDATA_END = "]]>";
40 private static final String CDATA_PSEUDO_END = "]]>";
41 private static final String CDATA_EMBEDED_END = CDATA_END + CDATA_PSEUDO_END + CDATA_START;
42 private static final int CDATA_END_LEN = CDATA_END.length();
43
44
45
46
47
48
49
50
51
52
53 static public String escapeTags(final String input) {
54
55
56
57 if(input == null
58 || input.length() == 0
59 || (input.indexOf('"') == -1 &&
60 input.indexOf('&') == -1 &&
61 input.indexOf('<') == -1 &&
62 input.indexOf('>') == -1)) {
63 return input;
64 }
65
66 StringBuilder buf = new StringBuilder(input.length() + 6);
67 char ch;
68
69 int len = input.length();
70 for(int i=0; i < len; i++) {
71 ch = input.charAt(i);
72 if (ch > '>') {
73 buf.append(ch);
74 } else if(ch == '<') {
75 buf.append("<");
76 } else if(ch == '>') {
77 buf.append(">");
78 } else if(ch == '&') {
79 buf.append("&");
80 } else if(ch == '"') {
81 buf.append(""");
82 } else {
83 buf.append(ch);
84 }
85 }
86 return buf.toString();
87 }
88
89
90
91
92
93
94
95
96
97
98 static public void appendEscapingCDATA(final StringBuffer buf,
99 final String str) {
100 if (str != null) {
101 int end = str.indexOf(CDATA_END);
102 if (end < 0) {
103 buf.append(str);
104 } else {
105 int start = 0;
106 while (end > -1) {
107 buf.append(str.substring(start, end));
108 buf.append(CDATA_EMBEDED_END);
109 start = end + CDATA_END_LEN;
110 if (start < str.length()) {
111 end = str.indexOf(CDATA_END, start);
112 } else {
113 return;
114 }
115 }
116 buf.append(str.substring(start));
117 }
118 }
119 }
120
121
122
123
124
125
126 public static String[] getThrowableStrRep(Throwable throwable) {
127 StringWriter sw = new StringWriter();
128 PrintWriter pw = new PrintWriter(sw);
129 throwable.printStackTrace(pw);
130 pw.flush();
131 LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
132 ArrayList<String> lines = new ArrayList<String>();
133 try {
134 String line = reader.readLine();
135 while(line != null) {
136 lines.add(line);
137 line = reader.readLine();
138 }
139 } catch(IOException ex) {
140 lines.add(ex.toString());
141 }
142 String[] rep = new String[lines.size()];
143 lines.toArray(rep);
144 return rep;
145 }
146
147 }