001 package echopoint.util;
002
003 /*
004 * This file is part of the Echo Point Project. This project is a collection
005 * of Components that have extended the Echo Web Application Framework.
006 *
007 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
008 *
009 * The contents of this file are subject to the Mozilla Public License Version
010 * 1.1 (the "License"); you may not use this file except in compliance with
011 * the License. You may obtain a copy of the License at
012 * http://www.mozilla.org/MPL/
013 *
014 * Software distributed under the License is distributed on an "AS IS" basis,
015 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
016 * for the specific language governing rights and limitations under the
017 * License.
018 *
019 * Alternatively, the contents of this file may be used under the terms of
020 * either the GNU General Public License Version 2 or later (the "GPL"), or
021 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
022 * in which case the provisions of the GPL or the LGPL are applicable instead
023 * of those above. If you wish to allow use of your version of this file only
024 * under the terms of either the GPL or the LGPL, and not to allow others to
025 * use your version of this file under the terms of the MPL, indicate your
026 * decision by deleting the provisions above and replace them with the notice
027 * and other provisions required by the GPL or the LGPL. If you do not delete
028 * the provisions above, a recipient may use your version of this file under
029 * the terms of any one of the MPL, the GPL or the LGPL.
030 */
031
032 /**
033 * This class will quote string data
034 */
035 public class QuoterKit {
036
037 /** not instantiable */
038 private QuoterKit() {
039 }
040
041 /**
042 * This method will apply a "Java quote" (ie double slashes)
043 * to a string with the given quote char
044 */
045 public static String quoteJ(String s, char quoteChar) {
046 StringBuffer sb = new StringBuffer(s.length());
047 for (int i = 0; i < s.length(); i++) {
048 char c = s.charAt(i);
049 if (c == quoteChar) {
050 sb.append("\\");
051 sb.append(quoteChar);
052 } else {
053 sb.append(c);
054 }
055 }
056 return sb.toString();
057 }
058
059 /**
060 * Quortes a string into HTML characters.
061 * @param s - the string to quote into safe HTML
062 * @return - safe HTML or null if s is null
063 */
064 public static String quoteHTML(String s) {
065 if (s == null)
066 return s;
067 int slen = s.length();
068 StringBuffer sb = new StringBuffer((int) (slen*1.5));
069 for (int i = 0; i < s.length(); i++) {
070 char c = s.charAt(i);
071 switch (c) {
072 case '<':
073 sb.append("<");
074 break;
075 case '>':
076 sb.append(">");
077 break;
078 case '"':
079 sb.append(""");
080 break;
081 case '&': {
082 // if the next N chars are &xxx; then dont replace it
083 if (checkForEntities(i,slen,s)) {
084 sb.append(c);
085 continue;
086 }
087 sb.append("&");
088 break;
089 }
090 default :
091 sb.append(c);
092 break;
093 }
094 }
095 return sb.toString();
096 }
097
098 /**
099 * All the valid HTML entities according to W3CSchools :
100 * http://www.w3schools.com/html/html_entitiesref.asp
101 */
102 private static String[] htmlEntities = {
103 """, """,
104 "'", "'",
105 "&&", "&",
106 "<", "<",
107 ">", ">",
108 " ", " ",
109 "¡", "¡",
110 "¤", "¤",
111 "¢", "¢",
112 "£", "£",
113 "¥", "¥",
114 "¦", "¦",
115 "§", "§",
116 "¨", "¨",
117 "©", "©",
118 "ª", "ª",
119 "«", "«",
120 "¬", "¬",
121 "­", "­",
122 "®", "®",
123 "¯", "¯",
124 "°", "°",
125 "±", "±",
126 "²", "²",
127 "³", "³",
128 "´", "´",
129 "µ", "µ",
130 "¶", "¶",
131 "·", "·",
132 "¸", "¸",
133 "¹", "¹",
134 "º", "º",
135 "»", "»",
136 "¼", "¼",
137 "½", "½",
138 "¾", "¾",
139 "¿", "¿",
140 "×", "×",
141 "÷", "÷",
142 "À", "À",
143 "Á", "Á",
144 "Â", "Â",
145 "Ã", "Ã",
146 "Ä", "Ä",
147 "Å", "Å",
148 "Æ", "Æ",
149 "Ç", "Ç",
150 "È", "È",
151 "É", "É",
152 "Ê", "Ê",
153 "Ë", "Ë",
154 "Ì", "Ì",
155 "Í", "Í",
156 "Î", "Î",
157 "Ï", "Ï",
158 "Ð", "Ð",
159 "Ñ", "Ñ",
160 "Ò", "Ò",
161 "Ó", "Ó",
162 "Ô", "Ô",
163 "Õ", "Õ",
164 "Ö", "Ö",
165 "Ø", "Ø",
166 "Ù", "Ù",
167 "Ú", "Ú",
168 "Û", "Û",
169 "Ü", "Ü",
170 "Ý", "Ý",
171 "Þ", "Þ",
172 "ß", "ß",
173 "à", "à",
174 "á", "á",
175 "â", "â",
176 "ã", "ã",
177 "ä", "ä",
178 "å", "å",
179 "æ", "æ",
180 "ç", "ç",
181 "è", "è",
182 "é", "é",
183 "ê", "ê",
184 "ë", "ë",
185 "ì", "ì",
186 "í", "í",
187 "î", "î",
188 "ï", "ï",
189 "ð", "ð",
190 "ñ", "ñ",
191 "ò", "ò",
192 "ó", "ó",
193 "ô", "ô",
194 "õ", "õ",
195 "ö", "ö",
196 "ø", "ø",
197 "ù", "ù",
198 "ú", "ú",
199 "û", "û",
200 "ü", "ü",
201 "ý", "ý",
202 "þ", "þ",
203 "ÿ", "ÿ",
204 "Œ", "Œ",
205 "œ", "œ",
206 "Š", "Š",
207 "š", "š",
208 "Ÿ", "Ÿ",
209 "ˆ", "ˆ",
210 "˜", "˜",
211 " ", " ",
212 " ", " ",
213 " ", " ",
214 "‌", "‌",
215 "‍", "‍",
216 "‎", "‎",
217 "‏", "‏",
218 "–", "–",
219 "—", "—",
220 "‘", "‘",
221 "’", "’",
222 "‚", "‚",
223 "“", "“",
224 "”", "”",
225 "„", "„",
226 "†", "†",
227 "‡", "‡",
228 "…", "…",
229 "‰", "‰",
230 "‹", "‹",
231 "›", "›",
232 "€", "€",
233 "™", "™",
234 };
235
236 /*
237 * Check that the string does have any HTML entities
238 */
239 private static boolean checkForEntities(int i, int slen, String s) {
240 for (int j = 0; j < htmlEntities.length; j++) {
241 if (checkFor(i,slen,s,htmlEntities[j]))
242 return true;
243 }
244 return false;
245 }
246
247 /*
248 * Check that the string does have the HTML entity
249 */
250 private static boolean checkFor(int i, int slen, String s, String checkFor) {
251 int cflen = checkFor.length();
252 if (i <= (slen - cflen)) {
253 if (s.substring(i,i+cflen).equalsIgnoreCase(checkFor)) {
254 return true;
255 }
256 }
257 return false;
258 }
259 }