KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > xml > XMLUtil


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 package org.apache.cocoon.webapps.session.xml;
17
18
19
20 /**
21  * A utility class which will soon be removed...
22  *
23  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
24  * @version CVS $Id: XMLUtil.java 30932 2004-07-29 17:35:38Z vgritsenko $
25 */

26 public final class XMLUtil {
27
28     /**
29      * Convert umlaute to entities
30      */

31     public static String JavaDoc encode(String JavaDoc value) {
32         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value);
33         for(int i = 0; i < buffer.length(); i++) {
34             if (buffer.charAt(i) > 127) {
35                 buffer.replace(i, i+1, "__"+((int)buffer.charAt(i))+";");
36             }
37         }
38         return buffer.toString();
39     }
40
41     /**
42      * Convert entities to umlaute
43      */

44     public static String JavaDoc decode(String JavaDoc value) {
45         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(value);
46         int pos;
47         boolean found;
48         for(int i = 0; i < buffer.length(); i++) {
49             if (buffer.charAt(i) == '_' &&
50                 buffer.charAt(i+1) == '_') {
51                 pos = i + 2;
52                 found = false;
53                 while (buffer.charAt(pos) >= '0'
54                        && buffer.charAt(pos) <= '9') {
55                     found = true;
56                     pos++;
57                 }
58                 if (found == true
59                     && pos > i + 2
60                     && buffer.charAt(pos) == ';') {
61                     int ent = new Integer JavaDoc(buffer.substring(i+2, pos)).intValue();
62                     buffer.replace(i, pos+1, ""+ (char)ent);
63                 }
64             }
65         }
66         return buffer.toString();
67     }
68
69 }
70
Popular Tags