KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > Tools


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20 /*
21  * @(#)Tools.java 1.0 17/Sept/2001
22  *
23  * @author : Wendel D. de Witte
24  * @version 1.0
25  */

26
27 import java.util.Collection JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.awt.Point JavaDoc;
31 import java.io.*;
32
33 public class Tools {
34
35    // Encoding maps for the decoding/encoding methods.
36
private static HashMap JavaDoc decodeMap;
37    private static HashMap JavaDoc encodeMap;
38
39
40    static {
41       encodeMap = new HashMap JavaDoc();
42       encodeMap.put("&", "&");
43       encodeMap.put(" ", " ");
44       encodeMap.put("\"", """);
45       encodeMap.put("'", "'");
46       encodeMap.put("<", "&lt;");
47       encodeMap.put(">", "&gt;");
48       encodeMap.put("", "&copy;");
49       encodeMap.put("", "&reg;");
50       encodeMap.put("", "&acute;");
51       encodeMap.put("", "&laquo;");
52       encodeMap.put("", "&raquo;");
53       encodeMap.put("", "&iexcl;");
54       encodeMap.put("", "&iquest;");
55       encodeMap.put("", "&Agrave;");
56       encodeMap.put("", "&agrave;");
57       encodeMap.put("", "&Aacute;");
58       encodeMap.put("", "&aacute;");
59       encodeMap.put("", "&Acirc;");
60       encodeMap.put("", "&acirc;");
61       encodeMap.put("", "&Atilde;");
62       encodeMap.put("", "&atilde;");
63       encodeMap.put("", "&Auml;");
64       encodeMap.put("", "&auml;");
65       encodeMap.put("", "&Aring;");
66       encodeMap.put("", "&aring;");
67       encodeMap.put("", "&AElig;");
68       encodeMap.put("", "&aelig;");
69       encodeMap.put("", "&Ccedil;");
70       encodeMap.put("", "&ccedil;");
71       encodeMap.put("", "&ETH;");
72       encodeMap.put("", "&eth;");
73       encodeMap.put("", "&Egrave;");
74       encodeMap.put("", "&egrave;");
75       encodeMap.put("", "&Eacute;");
76       encodeMap.put("", "&eacute;");
77       encodeMap.put("", "&Ecirc;");
78       encodeMap.put("", "&ecirc;");
79       encodeMap.put("", "&Euml;");
80       encodeMap.put("", "&euml;");
81       encodeMap.put("", "&Igrave;");
82       encodeMap.put("", "&igrave;");
83       encodeMap.put("", "&Iacute;");
84       encodeMap.put("", "&iacute;");
85       encodeMap.put("", "&Icirc;");
86       encodeMap.put("", "&icirc;");
87       encodeMap.put("", "&Iuml;");
88       encodeMap.put("", "&iuml;");
89       encodeMap.put("", "&Ntilde;");
90       encodeMap.put("", "&ntilde;");
91       encodeMap.put("", "&Ograve;");
92       encodeMap.put("", "&ograve;");
93       encodeMap.put("", "&Oacute;");
94       encodeMap.put("", "&oacute;");
95       encodeMap.put("", "&Ocirc;");
96       encodeMap.put("", "&ocirc;");
97       encodeMap.put("", "&Otilde;");
98       encodeMap.put("", "&otilde;");
99       encodeMap.put("", "&Ouml;");
100       encodeMap.put("", "&ouml;");
101       encodeMap.put("", "&Oslash;");
102       encodeMap.put("", "&oslash;");
103       encodeMap.put("", "&Ugrave;");
104       encodeMap.put("", "&ugrave;");
105       encodeMap.put("", "&Uacute;");
106       encodeMap.put("", "&uacute;");
107       encodeMap.put("", "&Ucirc;");
108       encodeMap.put("", "&ucirc;");
109       encodeMap.put("", "&Uuml;");
110       encodeMap.put("", "&uuml;");
111       encodeMap.put("", "&Yacute;");
112       encodeMap.put("", "&yacute;");
113       encodeMap.put("", "&yuml;");
114       encodeMap.put("", "&THORN;");
115       encodeMap.put("", "&thorn;");
116       encodeMap.put("", "&szlig;");
117       encodeMap.put("", "&sect;");
118       encodeMap.put("", "&para;");
119       encodeMap.put("", "&micro;");
120       encodeMap.put("", "&brvbar;");
121       encodeMap.put("", "&plusmn;");
122       encodeMap.put("", "&middot;");
123       encodeMap.put("", "&uml;");
124       encodeMap.put("", "&cedil;");
125       encodeMap.put("", "&ordf;");
126       encodeMap.put("", "&ordm;");
127       encodeMap.put("", "&not;");
128       encodeMap.put("", "&shy;");
129       encodeMap.put("", "&macr;");
130       encodeMap.put("", "&deg;");
131       encodeMap.put("", "&sup1;");
132       encodeMap.put("", "&sup2;");
133       encodeMap.put("", "&sup3;");
134       encodeMap.put("", "&frac14;");
135       encodeMap.put("", "&frac12;");
136       encodeMap.put("", "&frac34;");
137       encodeMap.put("", "&times;");
138       encodeMap.put("", "&divide;");
139       encodeMap.put("", "&cent;");
140       encodeMap.put("", "&pound;");
141       encodeMap.put("", "&curren;");
142       encodeMap.put("", "&yen;");
143       java.util.Set JavaDoc keys = encodeMap.keySet();
144       java.util.Iterator JavaDoc iterator = keys.iterator();
145       decodeMap = new HashMap JavaDoc();
146       while (iterator.hasNext()) {
147          String JavaDoc key = (String JavaDoc) iterator.next();
148          String JavaDoc value = (String JavaDoc) encodeMap.get(key);
149          decodeMap.put(value, key);
150       }
151    }
152
153
154    /** General-purpose utility function for removing
155     * characters from the front and back of string
156     * @param src The string to process
157     * @param head exact string to strip from head
158     * @param tail exact string to strip from tail
159     * @return The resulting string
160     */

161    public static String JavaDoc stripFrontBack(String JavaDoc src, String JavaDoc head, String JavaDoc tail) {
162       int h = src.indexOf(head);
163       int t = src.lastIndexOf(tail);
164       if (h == -1 || t == -1) return src;
165       return src.substring(h + 1, t);
166    }
167
168
169    /** General-purpose utility function for encoding the string
170     * @param text The string to process
171     * @return The resulting string
172     */

173    public static String JavaDoc encode(String JavaDoc text) {
174       char c;
175       StringBuffer JavaDoc n = new StringBuffer JavaDoc();
176       for (int i = 0; i < text.length(); i++) {
177          c = text.charAt(i);
178          String JavaDoc code = (String JavaDoc) encodeMap.get("" + c);
179          if (code == null)
180             n.append(c);
181          else
182             n.append(code);
183       }
184       return new String JavaDoc(n);
185    }
186
187
188    /** General-purpose utility function for decoding the string!
189     * @param text The string to process
190     * @return The resulting string
191     */

192    public static String JavaDoc decode(String JavaDoc text) {
193       StringBuffer JavaDoc n = new StringBuffer JavaDoc();
194       for (int i = 0; i < text.length(); i++) {
195          char c = text.charAt(i);
196          if (c == '&') {
197             StringBuffer JavaDoc code = new StringBuffer JavaDoc();
198             int j = i;
199             for (; j < text.length(); j++) {
200                code.append(text.charAt(j));
201                if (text.charAt(j) == ';')
202                   break;
203             }
204
205             String JavaDoc ch = (String JavaDoc) decodeMap.get(new String JavaDoc(code));
206             if (ch != null) {
207                n.append(ch);
208                i = j;
209             }
210          }
211          else {
212             n.append(c);
213          }
214       }
215       return new String JavaDoc(n);
216    }
217 }
218
Popular Tags