1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.services.util;
20
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.StringTokenizer;
24
25 import org.apache.log4j.Logger;
26 import org.apache.oro.text.regex.MalformedPatternException;
27 import org.apache.oro.text.regex.Pattern;
28 import org.apache.oro.text.regex.PatternCompiler;
29 import org.apache.oro.text.regex.Perl5Compiler;
30 import org.apache.oro.text.regex.Perl5Matcher;
31 import org.apache.oro.text.regex.Perl5Substitution;
32 import org.apache.oro.text.regex.Util;
33
34 public class HTMLUtilities {
35
36 private static final Logger logger = Logger.getLogger(HTMLUtilities.class);
37 private static Perl5Matcher matcher = new Perl5Matcher();
38 private static PatternCompiler compiler = new Perl5Compiler();
39 private static Pattern pattern = null;
40
41 static {
42 try {
43 pattern = compiler.compile("<[\\w/].*?>", Perl5Compiler.CASE_INSENSITIVE_MASK);
44 } catch(MalformedPatternException mpe) {
45 logger.error("Invalid pattern in HTMLUtilities. " + mpe.getMessage());
46 }
47 }
48
49 public static String removeQuotes(String input) {
50 StringBuffer sb = new StringBuffer(input.length());
51 int len = input.length();
52 char c;
53
54 for(int i = 0; i < len; i++) {
55 c = input.charAt(i);
56 if (c == '\'') {
57 sb.append("''");
58 } else {
59 sb.append(c);
60 }
61 }
62 return sb.toString();
63 }
64
65 public static String handleQuotes(String input) {
66 if(input == null || "".equals(input) || input.indexOf('"') == -1) {
67 return input;
68 }
69
70 StringBuffer buf = new StringBuffer();
71
72 char[] chars = input.toCharArray();
73 for(int i = 0; i < chars.length; i++) {
74 if(chars[i] == '"') {
75 buf.append(""");
76 } else {
77 buf.append(chars[i]);
78 }
79 }
80
81 return buf.toString();
82 }
83
84 public static String escapeNewlines(String input) {
85 if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
86 return input;
87 }
88
89 StringBuffer buf = new StringBuffer();
90 char[] chars = input.toCharArray();
91 for(int i = 0; i < chars.length; i++) {
92 if(chars[i] == '\r') {
93 continue;
94 } else if(chars[i] == '\n') {
95 buf.append("\\n");
96 } else {
97 buf.append(chars[i]);
98 }
99 }
100 return buf.toString();
101 }
102
103 public static String newlinesToBreaks(String input) {
104 if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
105 return input;
106 }
107
108 StringBuffer buf = new StringBuffer();
109 char[] chars = input.toCharArray();
110 for(int i = 0; i < chars.length; i++) {
111 if(chars[i] == '\r') {
112 continue;
113 } else if(chars[i] == '\n') {
114 buf.append("<br>");
115 } else {
116 buf.append(chars[i]);
117 }
118 }
119 return buf.toString();
120 }
121
122 public static String removeMarkup(String input) {
123 String output = (input == null ? "" : input);
124
125 if(pattern != null && matcher != null && output != null && ! output.equals("")) {
126 output = Util.substitute(matcher, pattern, new Perl5Substitution(), output, Util.SUBSTITUTE_ALL);
127 } else {
128 logger.debug("Failed removing markup. Pattern = " + pattern + " Output = " + output);
129 }
130
131 return output;
132 }
133
134 public static String escapeTags(String input) {
135 StringBuffer sb = new StringBuffer(input.length());
136 int len = input.length();
137 char c;
138
139 for(int i = 0; i < len; i++) {
140 c = input.charAt(i);
141 if (c == '"') {
142 sb.append(""");
143 } else if (c == '&') {
144 sb.append("&");
145 } else if (c == '<') {
146 sb.append("<");
147 } else if (c == '>') {
148 sb.append(">");
149 } else {
150 int ci = 0xffff & c;
151 if (ci < 160 ) {
152
153 sb.append(c);
154 } else {
155
156 sb.append("&#");
157 sb.append(new Integer(ci).toString());
158 sb.append(';');
159 }
160 }
161 }
162
163 return sb.toString();
164 }
165
166
167
168
169
170
171
172
173
174 public static final String getJSDateFormat(String format) {
175
176
177 String f = format.replace('m', 'n');
178
179
180 f = format.toLowerCase();
181
182 return f;
183
184
185 }
186
187 }
188
189