KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > lib > HtmlModule


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.quercus.lib;
31
32 import com.caucho.quercus.annotation.Optional;
33 import com.caucho.quercus.env.ArrayValue;
34 import com.caucho.quercus.env.ConstArrayValue;
35 import com.caucho.quercus.env.Env;
36 import com.caucho.quercus.env.StringValue;
37 import com.caucho.quercus.env.StringValueImpl;
38 import com.caucho.quercus.env.Value;
39 import com.caucho.quercus.module.AbstractQuercusModule;
40 import com.caucho.util.L10N;
41
42 import java.util.HashMap JavaDoc;
43
44 /**
45  * PHP functions implementing html code.
46  */

47 public class HtmlModule extends AbstractQuercusModule {
48   private static final L10N L = new L10N(HtmlModule.class);
49
50   public static final int HTML_SPECIALCHARS = 0;
51   public static final int HTML_ENTITIES = 1;
52
53   public static final int ENT_HTML_QUOTE_NONE = 0;
54   public static final int ENT_HTML_QUOTE_SINGLE = 1;
55   public static final int ENT_HTML_QUOTE_DOUBLE = 2;
56
57   public static final int ENT_COMPAT = ENT_HTML_QUOTE_DOUBLE;
58   public static final int ENT_QUOTES = ENT_HTML_QUOTE_SINGLE|ENT_HTML_QUOTE_DOUBLE;
59   public static final int ENT_NOQUOTES = ENT_HTML_QUOTE_NONE;
60
61   private final static ArrayValue HTML_SPECIALCHARS_ARRAY
62     = new ConstArrayValue();
63   private final static ArrayValue HTML_ENTITIES_ARRAY
64     = new ConstArrayValue();
65
66   private final static HashMap JavaDoc<String JavaDoc,String JavaDoc> HTML_DECODE
67     = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
68
69   private final ArrayValue _htmlQuotesArray = new ConstArrayValue();
70   private final ArrayValue _htmlNoQuotesArray = new ConstArrayValue();
71
72   public HtmlModule()
73   {
74   }
75
76   /**
77    * Returns HTML translation tables.
78    */

79   public Value get_html_translation_table(@Optional("HTML_SPECIALCHARS") int table,
80                                           @Optional("ENT_COMPAT") int quoteStyle)
81   {
82     Value result;
83
84     if (table == HTML_ENTITIES)
85       result = HTML_ENTITIES_ARRAY.copy();
86     else
87       result = HTML_SPECIALCHARS_ARRAY.copy();
88
89     if ((quoteStyle & ENT_HTML_QUOTE_SINGLE) != 0)
90       result.put(new StringValueImpl("'"), new StringValueImpl("&apos;"));
91
92     if ((quoteStyle & ENT_HTML_QUOTE_DOUBLE) != 0)
93       result.put(new StringValueImpl("\""), new StringValueImpl("&quot;"));
94
95     return result;
96   }
97
98   /**
99    * Escapes HTML
100    *
101    * @param env the calling environment
102    * @param stringV the string to be trimmed
103    * @param quoteStyleV optional quote style
104    * @param charsetV optional charset style
105    * @return the trimmed string
106    */

107   public static Value htmlspecialchars(Env env,
108                        Value stringV,
109                                        @Optional Value quoteStyleV,
110                                        @Optional Value charsetV)
111   {
112       // XXX: quotestyle and charset
113
String JavaDoc string = stringV.toString();
114     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
115
116     int len = string.length();
117     for (int i = 0; i < len; i++) {
118       char ch = string.charAt(i);
119
120       switch (ch) {
121       case '&':
122         sb.append("&amp;");
123         break;
124       case '"':
125         sb.append("&quot;");
126         break;
127       case '\'':
128         sb.append("'");
129         break;
130       case '<':
131         sb.append("&lt;");
132         break;
133       case '>':
134         sb.append("&gt;");
135         break;
136       default:
137         sb.append(ch);
138         break;
139       }
140     }
141
142     return new StringValueImpl(sb.toString());
143   }
144
145   /**
146    * Escapes HTML
147    *
148    * @param env the calling environment
149    * @param stringV the string to be trimmed
150    * @param quoteStyleV optional quote style
151    * @param charsetV optional charset style
152    * @return the trimmed string
153    */

154   public static Value htmlentities(Env env,
155                    Value stringV,
156                                    @Optional Value quoteStyleV,
157                                    @Optional Value charsetV)
158   {
159     // XXX: other entities
160
return htmlspecialchars(env, stringV, quoteStyleV, charsetV);
161   }
162
163   /**
164    * Escapes HTML
165    *
166    * @param string the string to be trimmed
167    * @param quoteStyle optional quote style
168    * @param charset optional charset style
169    * @return the trimmed string
170    */

171   public static StringValue html_entity_decode(Env env,
172                            StringValue string,
173                            @Optional int quoteStyle,
174                            @Optional String JavaDoc charset)
175   {
176     if (string.length() == 0)
177       return StringValue.EMPTY;
178
179    // StringBuilder result = new StringBuilder();
180

181    // int i = 0;
182
//int length = string.length();
183

184     // generate keys & values for preg_replace
185
// ArrayValue decodedArray = new ArrayValueImpl();
186
//ArrayValue encodedArray = new ArrayValueImpl();
187

188     Value[] keys = HTML_SPECIALCHARS_ARRAY.getKeyArray();
189     Value[] values = HTML_SPECIALCHARS_ARRAY.getValueArray(env);
190     int length = keys.length;
191     for (int i = 0; i < length; i++) {
192       string = RegexpModule.ereg_replace(env,
193                      values[i].toStringValue(),
194                      keys[i].toStringValue(),
195                      string).toStringValue();
196     }
197     /*
198     Value value = QuercusRegexpModule.preg_replace(env,
199                                                    encodedArray,
200                                                    decodedArray,
201                                                    new StringValueImpl(string),
202                                                    -1,
203                                                    null);*/

204     return string;
205   }
206
207   /**
208    * Replaces newlines with HTML breaks.
209    *
210    * @param env the calling environment
211    */

212   public static Value nl2br(Env env, Value stringV)
213   {
214     String JavaDoc string = stringV.toString();
215
216     int strLen = string.length();
217
218     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
219
220     for (int i = 0; i < strLen; i++) {
221       char ch = string.charAt(i);
222
223       if (ch == '\n') {
224         sb.append("<br />\n");
225       }
226       else if (ch == '\r') {
227         if (i + 1 < strLen && string.charAt(i + 1) == '\n') {
228           sb.append("<br />\r\n");
229           i++;
230         }
231         else {
232           sb.append("<br />\r");
233         }
234       }
235       else {
236         sb.append(ch);
237       }
238     }
239
240     return new StringValueImpl(sb.toString());
241   }
242
243   private static void entity(int ch, String JavaDoc entity)
244   {
245     HTML_ENTITIES_ARRAY.put("&" + (char) ch + ";", entity);
246     HTML_DECODE.put(entity, String.valueOf((char) ch));
247   }
248
249   static {
250     HTML_SPECIALCHARS_ARRAY.put("<", "&lt;");
251     HTML_SPECIALCHARS_ARRAY.put(">", "&gt;");
252     HTML_SPECIALCHARS_ARRAY.put("&", "&amp;");
253
254     entity('<', "lt");
255     entity('>', "gt");
256     entity('&', "amp");
257
258     entity(160, "nbsp");
259     entity(161, "iexcl");
260     entity(162, "cent");
261     entity(163, "pound");
262     entity(164, "curren");
263     entity(165, "yen");
264     entity(166, "brvbar");
265     entity(167, "sect");
266     entity(168, "uml");
267     entity(169, "copy");
268     entity(170, "ordf");
269     entity(171, "laquo");
270     entity(172, "not");
271     entity(173, "shy");
272     entity(174, "reg");
273     entity(175, "macr");
274     entity(176, "deg");
275     entity(177, "plusmn");
276     entity(178, "sup2");
277     entity(179, "sup3");
278     entity(180, "acute");
279     entity(181, "micro");
280     entity(182, "para");
281     entity(183, "middot");
282     entity(184, "cedil");
283     entity(185, "sup1");
284     entity(186, "ordm");
285     entity(187, "raquo");
286     entity(188, "frac14");
287     entity(189, "frac12");
288     entity(190, "frac34");
289     entity(191, "iquest");
290     entity(192, "Agrave");
291     entity(193, "Aacute");
292     entity(194, "Acirc");
293     entity(195, "Atilde");
294     entity(196, "Auml");
295     entity(197, "Aring");
296     entity(198, "AElig");
297     entity(199, "Ccedil");
298     entity(200, "Egrave");
299     entity(201, "Eacute");
300     entity(202, "Ecirc");
301     entity(203, "Euml");
302     entity(204, "Igrave");
303     entity(205, "Iacute");
304     entity(206, "Icirc");
305     entity(207, "Iuml");
306     entity(208, "ETH");
307     entity(209, "Ntilde");
308     entity(210, "Ograve");
309     entity(211, "Oacute");
310     entity(212, "Ocirc");
311     entity(213, "Otilde");
312     entity(214, "Ouml");
313     entity(215, "times");
314     entity(216, "Oslash");
315     entity(217, "Ugrave");
316     entity(218, "Uacute");
317     entity(219, "Ucirc");
318     entity(220, "Uuml");
319     entity(221, "Yacute");
320     entity(222, "THORN");
321     entity(223, "szlig");
322     entity(224, "agrave");
323     entity(225, "aacute");
324     entity(226, "acirc");
325     entity(227, "atilde");
326     entity(228, "auml");
327     entity(229, "aring");
328     entity(230, "aelig");
329     entity(231, "ccedil");
330     entity(232, "egrave");
331     entity(233, "eacute");
332     entity(234, "ecirc");
333     entity(235, "euml");
334     entity(236, "igrave");
335     entity(237, "iacute");
336     entity(238, "icirc");
337     entity(239, "iuml");
338     entity(240, "eth");
339     entity(241, "ntilde");
340     entity(242, "ograve");
341     entity(243, "oacute");
342     entity(244, "ocirc");
343     entity(245, "otilde");
344     entity(246, "ouml");
345     entity(247, "divide");
346     entity(248, "oslash");
347     entity(249, "ugrave");
348     entity(250, "uacute");
349     entity(251, "ucirc");
350     entity(252, "uuml");
351     entity(253, "yacute");
352     entity(254, "thorn");
353     entity(255, "yuml");
354   }
355 }
356
357
Popular Tags