KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > util > HTMLUtilities


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.util;
20
21 import org.apache.oro.text.regex.*;
22 import org.apache.struts.util.*;
23
24 import java.util.*;
25
26 public class HTMLUtilities {
27     private static Perl5Matcher matcher = new Perl5Matcher();
28     private static PatternCompiler compiler = new Perl5Compiler();
29     private static Pattern pattern = null;
30
31     static {
32         try {
33             pattern = compiler.compile("<[\\w/].*?>", Perl5Compiler.CASE_INSENSITIVE_MASK);
34         } catch(MalformedPatternException mpe) {
35             Logger.logError("Invalid pattern in HTMLUtilities. " + mpe.getMessage());
36         }
37     }
38
39     public static String JavaDoc removeQuotes(String JavaDoc input) {
40         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(input.length());
41         int len = input.length();
42         char c;
43
44         for(int i = 0; i < len; i++) {
45             c = input.charAt(i);
46             if (c == '\'') {
47                 sb.append("''");
48             } else {
49                 sb.append(c);
50             }
51         }
52         return sb.toString();
53     }
54
55     public static String JavaDoc handleQuotes(String JavaDoc input) {
56         if(input == null || "".equals(input) || input.indexOf('"') == -1) {
57             return input;
58         }
59
60         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
61
62         char[] chars = input.toCharArray();
63         for(int i = 0; i < chars.length; i++) {
64             if(chars[i] == '"') {
65                 buf.append("&quot;");
66             } else {
67                 buf.append(chars[i]);
68             }
69         }
70
71         return buf.toString();
72     }
73
74      public static String JavaDoc escapeNewlines(String JavaDoc input) {
75         if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
76             return input;
77         }
78
79         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
80         char[] chars = input.toCharArray();
81         for(int i = 0; i < chars.length; i++) {
82             if(chars[i] == '\r') {
83                 continue;
84             } else if(chars[i] == '\n') {
85                 buf.append("\\n");
86             } else {
87                 buf.append(chars[i]);
88             }
89         }
90         return buf.toString();
91     }
92
93      public static String JavaDoc newlinesToBreaks(String JavaDoc input) {
94         if(input == null || "".equals(input) || input.indexOf('\n') == -1) {
95             return input;
96         }
97
98         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
99         char[] chars = input.toCharArray();
100         for(int i = 0; i < chars.length; i++) {
101             if(chars[i] == '\r') {
102                 continue;
103             } else if(chars[i] == '\n') {
104                 buf.append("<br>");
105             } else {
106                 buf.append(chars[i]);
107             }
108         }
109         return buf.toString();
110     }
111
112     public static String JavaDoc removeMarkup(String JavaDoc input) {
113         String JavaDoc output = (input == null ? "" : input);
114
115         if(pattern != null && matcher != null && output != null && ! output.equals("")) {
116             output = Util.substitute(matcher, pattern, new Perl5Substitution(), output, Util.SUBSTITUTE_ALL);
117         } else {
118             Logger.logDebug("Failed removing markup. Pattern = " + pattern + " Output = " + output);
119         }
120
121         return output;
122     }
123
124     public static String JavaDoc escapeTags(String JavaDoc input) {
125         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(input.length());
126         int len = input.length();
127         char c;
128
129         for(int i = 0; i < len; i++) {
130             c = input.charAt(i);
131             if (c == '"') {
132                 sb.append("&quot;");
133             } else if (c == '&') {
134                 sb.append("&amp;");
135             } else if (c == '<') {
136                 sb.append("&lt;");
137             } else if (c == '>') {
138                 sb.append("&gt;");
139             } else {
140                 int ci = 0xffff & c;
141                 if (ci < 160 ) {
142                     // nothing special only 7 Bit
143
sb.append(c);
144                 } else {
145                     // Not 7 Bit use the unicode system
146
sb.append("&#");
147                     sb.append(new Integer JavaDoc(ci).toString());
148                     sb.append(';');
149                 }
150             }
151         }
152
153         return sb.toString();
154     }
155 }
156
157  
Popular Tags