KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > Tools


1 package org.jahia.tools;
2
3 import java.text.*;
4 import java.util.*;
5
6 /**
7  * @author Jerome Tamiotti <a HREF="mailto:tamiotti@xo3.com">tamiotti@xo3.com</a>
8  */

9
10
11 /**
12  * Class Tools:
13  * provides tools like debug printing, getting current date
14  *
15  */

16 public class Tools
17 {
18     // debug mode
19
private static final boolean debug = false;
20
21
22
23     public static String JavaDoc getCurrentDate()
24     // get the current date under a default format
25
{
26     return getCurrentDate("dd/MM/yyyy");
27     }
28     /**
29       * Replaces a token in a string with another given String
30       *
31       * @param str the String to be parsed
32       * @param token the token to be found and replaced
33       * @param replaceValue the value to insert where token is found
34       * @return the new String
35       */

36      public static String JavaDoc replace(String JavaDoc str, String JavaDoc token,
37                                   String JavaDoc replaceValue) {
38
39          String JavaDoc result = "";
40          int i = str.indexOf(token);
41          while (i != -1) {
42              result += str.substring(0,i) + replaceValue;
43              str = str.substring(i + token.length(), str.length());
44              i = str.indexOf(token);
45          }
46          return result + str;
47      }
48
49
50     public static String JavaDoc getCurrentDate(String JavaDoc format)
51     // get the current date under the specified format
52
{
53     Date today = new Date();
54     SimpleDateFormat formatter = new SimpleDateFormat(format);
55     String JavaDoc datenewformat = formatter.format(today);
56     return datenewformat;
57     }
58
59     static public String JavaDoc toHtml(String JavaDoc input) {
60         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(input);
61         for (int i = 0; i < sb.length(); i++) {
62
63             char c = sb.charAt(i);
64             if (c == '\''){
65                 sb.deleteCharAt(i);
66                 sb.insert(i,"&#39;");
67             }
68             if (c == '"') {
69                 sb.deleteCharAt(i);
70                sb.insert(i,"&#34;");
71             }
72             if (c == '<') {
73                 sb.deleteCharAt(i);
74                sb.insert(i,"&lt;");
75             }
76             if (c == '>') {
77                 sb.deleteCharAt(i);
78                sb.insert(i,"&gt;");
79             }
80             if (c == 'à') {
81                 sb.deleteCharAt(i);
82                sb.insert(i,"&agrave;");
83             }
84             if (c == 'â') {
85                 sb.deleteCharAt(i);
86                sb.insert(i,"&acirc;");
87             }
88             if (c == 'é') {
89                 sb.deleteCharAt(i);
90                sb.insert(i,"&eacute;");
91             }
92             if (c == 'è') {
93                 sb.deleteCharAt(i);
94                sb.insert(i,"&egrave;");
95             }
96             if (c == 'ê') {
97                 sb.deleteCharAt(i);
98                sb.insert(i,"&ecirc;");
99             }
100             if (c == 'ë') {
101                 sb.deleteCharAt(i);
102                sb.insert(i,"&euml;");
103             }
104             if (c == 'ä') {
105                 sb.deleteCharAt(i);
106                sb.insert(i,"&auml;");
107             }
108             if (c == 'ù') {
109                 sb.deleteCharAt(i);
110                sb.insert(i,"&ugrave;");
111             }
112             if (c == 'û') {
113                 sb.deleteCharAt(i);
114                sb.insert(i,"&ucirc;");
115             }
116             if (c == 'ü') {
117                 sb.deleteCharAt(i);
118                sb.insert(i,"&uuml;");
119             }
120             if (c == 'ö') {
121                 sb.deleteCharAt(i);
122                sb.insert(i,"&ouml;");
123             }
124             if (c == 'ï') {
125                 sb.deleteCharAt(i);
126                sb.insert(i,"&iuml;");
127             }
128             if (c == 'î') {
129                 sb.deleteCharAt(i);
130                sb.insert(i,"&icirc;");
131             }
132             if (c == 'ç') {
133                 sb.deleteCharAt(i);
134                sb.insert(i,"&ccedil;");
135             }
136         }
137         return sb.toString();
138     }
139
140
141
142 }
143
Popular Tags