KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > VisualFormatter


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common;
25
26 import java.net.URLEncoder JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Locale JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.infoglue.cms.util.CmsPropertyHandler;
33
34 public class VisualFormatter
35 {
36     private final static Logger logger = Logger.getLogger(VisualFormatter.class.getName());
37     
38     public VisualFormatter()
39     {
40     }
41     
42     public String JavaDoc formatDate(Date JavaDoc date, String JavaDoc pattern)
43     {
44         if(date == null)
45             return "";
46             
47         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
48         String JavaDoc dateString = formatter.format(date);
49
50         return dateString;
51     }
52
53     public String JavaDoc formatDate(Date JavaDoc date, Locale JavaDoc locale, String JavaDoc pattern)
54     {
55         if(date == null)
56             return "";
57             
58         // Format the current time.
59
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern, locale);
60         String JavaDoc dateString = formatter.format(date);
61
62         return dateString;
63     }
64     
65     public Date JavaDoc parseDate(String JavaDoc dateString, String JavaDoc pattern)
66     {
67         if(dateString == null)
68             return new Date JavaDoc();
69         
70         Date JavaDoc date = new Date JavaDoc();
71         
72         try
73         {
74             // Format the current time.
75
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern);
76             date = formatter.parse(dateString);
77         }
78         catch(Exception JavaDoc e)
79         {
80             logger.info("Error parsing date:" + dateString);
81         }
82         
83         return date;
84     }
85
86     public Date JavaDoc parseDate(String JavaDoc dateString, Locale JavaDoc locale, String JavaDoc pattern)
87      {
88          if(dateString == null)
89              return new Date JavaDoc();
90         
91          Date JavaDoc date = new Date JavaDoc();
92         
93          try
94          {
95              // Format the current time.
96
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(pattern, locale);
97              date = formatter.parse(dateString);
98          }
99          catch(Exception JavaDoc e)
100          {
101              logger.info("Error parsing date:" + dateString);
102          }
103         
104          return date;
105      }
106     
107     /**
108      * This method converts all non-standard characters to html-equivalents.
109      */

110     
111     public final String JavaDoc escapeHTML(String JavaDoc s)
112     {
113         if(s == null)
114             return null;
115             
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
117         int n = s.length();
118         for (int i = 0; i < n; i++)
119         {
120             char c = s.charAt(i);
121             switch (c)
122             {
123                 case '<': sb.append("&lt;"); break;
124                 case '>': sb.append("&gt;"); break;
125                 case '&': sb.append("&amp;"); break;
126                 case '"': sb.append("&quot;"); break;
127                 /*
128                 case 'à': sb.append("&agrave;");break;
129                 case 'À': sb.append("&Agrave;");break;
130                 case 'â': sb.append("&acirc;");break;
131                 case 'Â': sb.append("&Acirc;");break;
132                 case 'ä': sb.append("&auml;");break;
133                 case 'Ä': sb.append("&Auml;");break;
134                 case 'å': sb.append("&aring;");break;
135                 case 'Å': sb.append("&Aring;");break;
136                 case 'æ': sb.append("&aelig;");break;
137                 case 'Æ': sb.append("&AElig;");break;
138                 case 'ç': sb.append("&ccedil;");break;
139                 case 'Ç': sb.append("&Ccedil;");break;
140                 case 'é': sb.append("&eacute;");break;
141                 case 'É': sb.append("&Eacute;");break;
142                 case 'è': sb.append("&egrave;");break;
143                 case 'È': sb.append("&Egrave;");break;
144                 case 'ê': sb.append("&ecirc;");break;
145                 case 'Ê': sb.append("&Ecirc;");break;
146                 case 'ë': sb.append("&euml;");break;
147                 case 'Ë': sb.append("&Euml;");break;
148                 case 'ï': sb.append("&iuml;");break;
149                 case 'Ï': sb.append("&Iuml;");break;
150                 case 'ô': sb.append("&ocirc;");break;
151                 case 'Ô': sb.append("&Ocirc;");break;
152                 case 'ö': sb.append("&ouml;");break;
153                 case 'Ö': sb.append("&Ouml;");break;
154                 case 'ø': sb.append("&oslash;");break;
155                 case 'Ø': sb.append("&Oslash;");break;
156                 case 'ß': sb.append("&szlig;");break;
157                 case 'ù': sb.append("&ugrave;");break;
158                 case 'Ù': sb.append("&Ugrave;");break;
159                 case 'û': sb.append("&ucirc;");break;
160                 case 'Û': sb.append("&Ucirc;");break;
161                 case 'ü': sb.append("&uuml;");break;
162                 case 'Ü': sb.append("&Uuml;");break;
163                 case '®': sb.append("&reg;");break;
164                 case '©': sb.append("&copy;");break;
165                 case '€': sb.append("&euro;"); break;
166                 */

167
168                 default: sb.append(c); break;
169             }
170         }
171         return sb.toString();
172     }
173     
174
175     /**
176      *
177      * Temporary method, please do not use. (SS, 2004-12-13)
178      * @deprecated
179      */

180     
181     public final String JavaDoc escapeHTMLforXMLService(String JavaDoc s)
182     {
183         if(s == null)
184             return null;
185             
186         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
187         int n = s.length();
188         for (int i = 0; i < n; i++)
189         {
190             char c = s.charAt(i);
191             switch (c)
192             {
193                 case '<': sb.append("&lt;"); break;
194                 case '>': sb.append("&gt;"); break;
195                 case '&': sb.append("&amp;"); break;
196                 case '"': sb.append("&quot;"); break;
197                 case 'à': sb.append("&agrave;");break;
198                 case 'À': sb.append("&Agrave;");break;
199                 case 'â': sb.append("&acirc;");break;
200                 case 'Â': sb.append("&Acirc;");break;
201                 case 'ä': sb.append("&auml;");break;
202                 case 'Ä': sb.append("&Auml;");break;
203                 case 'å': sb.append("&aring;");break;
204                 case 'Å': sb.append("&Aring;");break;
205                 case 'æ': sb.append("&aelig;");break;
206                 case 'Æ': sb.append("&AElig;");break;
207                 case 'ç': sb.append("&ccedil;");break;
208                 case 'Ç': sb.append("&Ccedil;");break;
209                 case 'é': sb.append("&eacute;");break;
210                 case 'É': sb.append("&Eacute;");break;
211                 case 'è': sb.append("&egrave;");break;
212                 case 'ò': sb.append("&ograve;");break;
213                 case 'È': sb.append("&Egrave;");break;
214                 case 'ê': sb.append("&ecirc;");break;
215                 case 'Ê': sb.append("&Ecirc;");break;
216                 case 'ë': sb.append("&euml;");break;
217                 case 'Ë': sb.append("&Euml;");break;
218                 case 'ï': sb.append("&iuml;");break;
219                 case 'Ï': sb.append("&Iuml;");break;
220                 case 'ô': sb.append("&ocirc;");break;
221                 case 'Ô': sb.append("&Ocirc;");break;
222                 case 'ö': sb.append("&ouml;");break;
223                 case 'Ö': sb.append("&Ouml;");break;
224                 case 'ø': sb.append("&oslash;");break;
225                 case 'Ø': sb.append("&Oslash;");break;
226                 case 'ß': sb.append("&szlig;");break;
227                 case 'ù': sb.append("&ugrave;");break;
228                 case 'Ù': sb.append("&Ugrave;");break;
229                 case 'û': sb.append("&ucirc;");break;
230                 case 'Û': sb.append("&Ucirc;");break;
231                 case 'ü': sb.append("&uuml;");break;
232                 case 'Ü': sb.append("&Uuml;");break;
233                 case '®': sb.append("&reg;");break;
234                 case '©': sb.append("&copy;");break;
235                 case '€': sb.append("&euro;"); break;
236                 case '\'': sb.append("&#146;"); break;
237                 
238                 default: sb.append(c); break;
239             }
240         }
241         return sb.toString();
242     }
243     
244     public final String JavaDoc escapeExtendedHTML(String JavaDoc s)
245     {
246         if(s == null)
247             return null;
248             
249         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
250         int n = s.length();
251         for (int i = 0; i < n; i++)
252         {
253             char c = s.charAt(i);
254             switch (c)
255             {
256                 case '<': sb.append("&lt;"); break;
257                 case '>': sb.append("&gt;"); break;
258                 case '&': sb.append("&amp;"); break;
259                 case '"': sb.append("&quot;"); break;
260                 /*
261                 case 'à': sb.append("&agrave;");break;
262                 case 'À': sb.append("&Agrave;");break;
263                 case 'â': sb.append("&acirc;");break;
264                 case 'Â': sb.append("&Acirc;");break;
265                 case 'ä': sb.append("&auml;");break;
266                 case 'Ä': sb.append("&Auml;");break;
267                 case 'å': sb.append("&aring;");break;
268                 case 'Å': sb.append("&Aring;");break;
269                 case 'æ': sb.append("&aelig;");break;
270                 case 'Æ': sb.append("&AElig;");break;
271                 case 'ç': sb.append("&ccedil;");break;
272                 case 'Ç': sb.append("&Ccedil;");break;
273                 case 'é': sb.append("&eacute;");break;
274                 case 'É': sb.append("&Eacute;");break;
275                 case 'è': sb.append("&egrave;");break;
276                 case 'È': sb.append("&Egrave;");break;
277                 case 'ê': sb.append("&ecirc;");break;
278                 case 'Ê': sb.append("&Ecirc;");break;
279                 case 'ë': sb.append("&euml;");break;
280                 case 'Ë': sb.append("&Euml;");break;
281                 case 'ï': sb.append("&iuml;");break;
282                 case 'Ï': sb.append("&Iuml;");break;
283                 case 'ô': sb.append("&ocirc;");break;
284                 case 'Ô': sb.append("&Ocirc;");break;
285                 case 'ö': sb.append("&ouml;");break;
286                 case 'Ö': sb.append("&Ouml;");break;
287                 case 'ø': sb.append("&oslash;");break;
288                 case 'Ø': sb.append("&Oslash;");break;
289                 case 'ß': sb.append("&szlig;");break;
290                 case 'ù': sb.append("&ugrave;");break;
291                 case 'Ù': sb.append("&Ugrave;");break;
292                 case 'û': sb.append("&ucirc;");break;
293                 case 'Û': sb.append("&Ucirc;");break;
294                 case 'ü': sb.append("&uuml;");break;
295                 case 'Ü': sb.append("&Uuml;");break;
296                 case '®': sb.append("&reg;");break;
297                 case '©': sb.append("&copy;");break;
298                 case '€': sb.append("&euro;"); break;
299                 */

300                 case '\'': sb.append("&#146;"); break;
301
302                 default: sb.append(c); break;
303             }
304         }
305         return sb.toString();
306     }
307     
308     /**
309      * This method converts all non-standard characters to html-equivalents.
310      */

311     
312     public final String JavaDoc escapeForJavascripts(String JavaDoc s)
313     {
314         if(s == null)
315             return null;
316             
317         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
318         int n = s.length();
319         for (int i = 0; i < n; i++)
320         {
321             char c = s.charAt(i);
322             if(c == '\'') sb.append("\\'");
323             else sb.append(c);
324         }
325         
326         return sb.toString();
327     }
328
329     /**
330      * This method converts all non-standard characters to html-equivalents.
331      */

332     
333     public final String JavaDoc cleanForJavascriptStrings(String JavaDoc s)
334     {
335         if(s == null)
336             return null;
337
338         String JavaDoc lineSep = System.getProperty("line.separator");
339         s = s.replaceAll(lineSep, "<br/>");
340         s = s.replaceAll("\n", "<br/>");
341         s = s.replaceAll("\r", "<br/>");
342              
343         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
344         int n = s.length();
345         for (int i = 0; i < n; i++)
346         {
347             char c = s.charAt(i);
348             switch (c)
349             {
350                 case '\'': sb.append("\\'"); break;
351                 case '"': sb.append("&quot;"); break;
352                 
353                 default: sb.append(c); break;
354             }
355         }
356
357         return sb.toString();
358     }
359
360     /**
361      * This method converts all non-standard characters to html-equivalents.
362      */

363     
364     public final String JavaDoc escapeForAdvancedJavascripts(String JavaDoc s)
365     {
366         if(s == null)
367             return null;
368             
369         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
370         int n = s.length();
371         for (int i = 0; i < n; i++)
372         {
373             char c = s.charAt(i);
374             switch (c)
375             {
376                 case '\'': sb.append("\\'"); break;
377                 case '"': sb.append("&quot;"); break;
378                 default: sb.append(c); break;
379             }
380         }
381         
382         return sb.toString();
383     }
384     
385     public final String JavaDoc replaceNonAscii(String JavaDoc s, char character)
386     {
387         if(s == null)
388             return null;
389             
390         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
391         int n = s.length();
392         for (int i = 0; i < n; i++)
393         {
394             char c = s.charAt(i);
395             if(c < 128 && c > 32)
396             {
397                 if(Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.')
398                     sb.append(c);
399                 else
400                     sb.append(character);
401             }
402             else
403             {
404                 sb.append(character);
405             }
406         }
407         return sb.toString();
408     }
409     
410     /**
411      * This method converts all non-standard characters to html-equivalents.
412      */

413     
414     public final String JavaDoc encode(String JavaDoc s) throws Exception JavaDoc
415     {
416         if(s == null)
417             return null;
418         
419         return URLEncoder.encode(s, "UTF-8");
420     }
421
422     /**
423      * This method converts all non-standard characters to html-equivalents.
424      */

425     
426     public final String JavaDoc encodeURI(String JavaDoc s) throws Exception JavaDoc
427     {
428         if(s == null)
429             return null;
430         
431         String JavaDoc encoding = CmsPropertyHandler.getURIEncoding();
432         
433         return URLEncoder.encode(s, encoding);
434     }
435
436     
437     public final String JavaDoc encodeURI(String JavaDoc s, String JavaDoc encoding) throws Exception JavaDoc
438     {
439         if(s == null)
440             return null;
441         
442         return URLEncoder.encode(s, encoding);
443     }
444
445 }
Popular Tags