KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > xml > Normalize


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: Normalize.java 42598 2004-03-01 16:18:28Z gregor $ */
19
20 package org.apache.lenya.xml;
21
22
23 /**
24  * @deprecated
25  */

26 public class Normalize {
27     /**
28      * DOCUMENT ME!
29      *
30      * @param args DOCUMENT ME!
31      */

32     public static void main(String JavaDoc[] args) {
33         System.out.println(Normalize.normalize("&"));
34         System.out.println(Normalize.denormalize("Zürich"));
35         System.out.println(Normalize.denormalize("Z&252;rich & Region&#787djŤ"));
36     }
37
38     /**
39      *
40      */

41     public static String JavaDoc normalize(String JavaDoc s) {
42         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
43
44         for (int i = 0; i < s.length(); i++) {
45             char ch = s.charAt(i);
46
47             switch (ch) {
48             case '&': // 38
49
{
50                 sb.append("&#38;");
51
52                 break;
53             }
54
55             case 60: // <
56
{
57                 sb.append("&#60;");
58
59                 break;
60             }
61
62             case 62: // >
63
{
64                 sb.append("&#62;");
65
66                 break;
67             }
68
69             case 139: // <
70
{
71                 sb.append("&#139;");
72
73                 break;
74             }
75
76             case 155: // >
77
{
78                 sb.append("&#155;");
79
80                 break;
81             }
82
83             case 160: // nbsp
84
{
85                 sb.append("&#160;");
86
87                 break;
88             }
89
90             case 171: // <<
91
{
92                 sb.append("&#171;");
93
94                 break;
95             }
96
97             case 183: // .
98
{
99                 sb.append("&#183;");
100
101                 break;
102             }
103
104             case 187: // >>
105
{
106                 sb.append("&#187;");
107
108                 break;
109             }
110
111             case 196: // Ae
112
{
113                 sb.append("&#196;");
114
115                 break;
116             }
117
118             case 214: // Oe
119
{
120                 sb.append("&#214;");
121
122                 break;
123             }
124
125             case 220: // Ue
126
{
127                 sb.append("&#220;");
128
129                 break;
130             }
131
132             case 223: // Scharfes S
133
{
134                 sb.append("&#223;");
135
136                 break;
137             }
138
139             case 225: // aacute
140
{
141                 sb.append("&#225;");
142
143                 break;
144             }
145
146             case 228: // ae
147
{
148                 sb.append("&#228;");
149
150                 break;
151             }
152
153             case 232: // egrave
154
{
155                 sb.append("&#232;");
156
157                 break;
158             }
159
160             case 233: // eacute
161
{
162                 sb.append("&#233;");
163
164                 break;
165             }
166
167             case 234: // ecirc
168
{
169                 sb.append("&#234;");
170
171                 break;
172             }
173
174             case 244: // ocirc
175
{
176                 sb.append("&#244;");
177
178                 break;
179             }
180
181             case 246: // oe
182
{
183                 sb.append("&#246;");
184
185                 break;
186             }
187
188             case 252: // ue
189
{
190                 sb.append("&#252;");
191
192                 break;
193             }
194
195             default:sb.append(ch);
196             }
197         }
198
199         return sb.toString();
200     }
201
202     /**
203      *
204      */

205     public static String JavaDoc denormalize(String JavaDoc s) {
206         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
207
208         for (int i = 0; i < s.length(); i++) {
209             char ch = s.charAt(i);
210
211             if (ch == '&') {
212                 StringBuffer JavaDoc substring = new StringBuffer JavaDoc();
213                 int length = i + 6;
214
215                 if (length > s.length()) {
216                     length = s.length();
217                 }
218
219                 for (int k = i; k < length; k++) {
220                     substring.append(s.charAt(k));
221
222                     if (s.charAt(k) == ';') {
223                         break;
224                     }
225                 }
226
227                 if (substring.length() > 3) {
228                     if ((substring.charAt(1) != '#') ||
229                             (substring.charAt(substring.length() - 1) != ';')) {
230                         sb.append(ch);
231                     } else {
232                         int ascii = 0;
233                         int power = 1;
234
235                         for (int j = substring.length() - 2; j >= 2; j--) {
236                             try {
237                                 Integer JavaDoc integer = new Integer JavaDoc("" + substring.charAt(j));
238                                 ascii = ascii + (power * integer.intValue());
239                             } catch (Exception JavaDoc e) {
240                                 ascii = -1;
241
242                                 break;
243                             }
244
245                             power = power * 10;
246                         }
247
248                         if (ascii >= 0) {
249                             char character = (char) ascii;
250                             sb.append(character);
251                             i = (i + substring.length()) - 1;
252                         } else {
253                             sb.append(ch);
254                         }
255                     }
256                 } else {
257                     sb.append(ch);
258                 }
259             } else {
260                 sb.append(ch);
261             }
262         }
263
264         return sb.toString();
265     }
266 }
267
Popular Tags