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("&lt;");
074                                break;
075                            case '>':
076                                sb.append("&gt;");
077                                break;
078                            case '"':
079                                sb.append("&quot;");
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("&amp;");
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                            "&quot;",   "&#34;",
104                            "&apos;",   "&#39;",
105                            "&&amp;",       "&#38;",
106                            "&lt;",     "&#60;",
107                            "&gt;",     "&#62;",
108                            "&nbsp;",   "&#160;",
109                            "&iexcl;",  "&#161;",
110                            "&curren;", "&#164;",
111                            "&cent;",   "&#162;",
112                            "&pound;",  "&#163;",
113                            "&yen;",    "&#165;",
114                            "&brvbar;", "&#166;",
115                            "&sect;",   "&#167;",
116                            "&uml;",    "&#168;",
117                            "&copy;",   "&#169;",
118                            "&ordf;",   "&#170;",
119                            "&laquo;",  "&#171;",
120                            "&not;",    "&#172;",
121                            "&shy;",    "&#173;",
122                            "&reg;",    "&#174;",
123                            "&macr;",   "&#175;",
124                            "&deg;",    "&#176;",
125                            "&plusmn;", "&#177;",
126                            "&sup2;",   "&#178;",
127                            "&sup3;",   "&#179;",
128                            "&acute;",  "&#180;",
129                            "&micro;",  "&#181;",
130                            "&para;",   "&#182;",
131                            "&middot;", "&#183;",
132                            "&cedil;",  "&#184;",
133                            "&sup1;",   "&#185;",
134                            "&ordm;",   "&#186;",
135                            "&raquo;",  "&#187;",
136                            "&frac14;", "&#188;",
137                            "&frac12;", "&#189;",
138                            "&frac34;", "&#190;",
139                            "&iquest;", "&#191;",
140                            "&times;",  "&#215;",
141                            "&divide;", "&#247;",
142                            "&Agrave;", "&#192;",
143                            "&Aacute;", "&#193;",
144                            "&Acirc;",  "&#194;",
145                            "&Atilde;", "&#195;",
146                            "&Auml;",   "&#196;",
147                            "&Aring;",  "&#197;",
148                            "&AElig;",  "&#198;",
149                            "&Ccedil;", "&#199;",
150                            "&Egrave;", "&#200;",
151                            "&Eacute;", "&#201;",
152                            "&Ecirc;",  "&#202;",
153                            "&Euml;",   "&#203;",
154                            "&Igrave;", "&#204;",
155                            "&Iacute;", "&#205;",
156                            "&Icirc;",  "&#206;",
157                            "&Iuml;",   "&#207;",
158                            "&ETH;",    "&#208;",
159                            "&Ntilde;", "&#209;",
160                            "&Ograve;", "&#210;",
161                            "&Oacute;", "&#211;",
162                            "&Ocirc;",  "&#212;",
163                            "&Otilde;", "&#213;",
164                            "&Ouml;",   "&#214;",
165                            "&Oslash;", "&#216;",
166                            "&Ugrave;", "&#217;",
167                            "&Uacute;", "&#218;",
168                            "&Ucirc;",  "&#219;",
169                            "&Uuml;",   "&#220;",
170                            "&Yacute;", "&#221;",
171                            "&THORN;",  "&#222;",
172                            "&szlig;",  "&#223;",
173                            "&agrave;", "&#224;",
174                            "&aacute;", "&#225;",
175                            "&acirc;",  "&#226;",
176                            "&atilde;", "&#227;",
177                            "&auml;",   "&#228;",
178                            "&aring;",  "&#229;",
179                            "&aelig;",  "&#230;",
180                            "&ccedil;", "&#231;",
181                            "&egrave;", "&#232;",
182                            "&eacute;", "&#233;",
183                            "&ecirc;",  "&#234;",
184                            "&euml;",   "&#235;",
185                            "&igrave;", "&#236;",
186                            "&iacute;", "&#237;",
187                            "&icirc;",  "&#238;",
188                            "&iuml;",   "&#239;",
189                            "&eth;",    "&#240;",
190                            "&ntilde;", "&#241;",
191                            "&ograve;", "&#242;",
192                            "&oacute;", "&#243;",
193                            "&ocirc;",  "&#244;",
194                            "&otilde;", "&#245;",
195                            "&ouml;",   "&#246;",
196                            "&oslash;", "&#248;",
197                            "&ugrave;", "&#249;",
198                            "&uacute;", "&#250;",
199                            "&ucirc;",  "&#251;",
200                            "&uuml;",   "&#252;",
201                            "&yacute;", "&#253;",
202                            "&thorn;",  "&#254;",
203                            "&yuml;",   "&#255;",
204                            "&OElig;",  "&#338;",
205                            "&oelig;",  "&#339;",
206                            "&Scaron;", "&#352;",
207                            "&scaron;", "&#353;",
208                            "&Yuml;",   "&#376;",
209                            "&circ;",   "&#710;",
210                            "&tilde;",  "&#732;",
211                            "&ensp;",   "&#8194;",
212                            "&emsp;",   "&#8195;",
213                            "&thinsp;", "&#8201;",
214                            "&zwnj;",   "&#8204;",
215                            "&zwj;",    "&#8205;",
216                            "&lrm;",    "&#8206;",
217                            "&rlm;",    "&#8207;",
218                            "&ndash;",  "&#8211;",
219                            "&mdash;",  "&#8212;",
220                            "&lsquo;",  "&#8216;",
221                            "&rsquo;",  "&#8217;",
222                            "&sbquo;",  "&#8218;",
223                            "&ldquo;",  "&#8220;",
224                            "&rdquo;",  "&#8221;",
225                            "&bdquo;",  "&#8222;",
226                            "&dagger;", "&#8224;",
227                            "&Dagger;", "&#8225;",
228                            "&hellip;", "&#8230;",
229                            "&permil;", "&#8240;",
230                            "&lsaquo;", "&#8249;",
231                            "&rsaquo;", "&#8250;",
232                            "&euro;",   "&#8364;",
233                            "&trade;",  "&#8482;",
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    }