KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > EscapeUnicode


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

18 package org.apache.tools.ant.filters;
19
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22
23 /**
24  * This method converts non-latin characters to unicode escapes.
25  * Useful to load properties containing non latin
26  * Example:
27  *
28  * <pre>&lt;escapeunicode&gt;</pre>
29  *
30  * Or:
31  *
32  * <pre>&lt;filterreader
33         classname=&quot;org.apache.tools.ant.filters.EscapeUnicode&quot;/&gt;
34  * </pre>
35  *
36  * @since Ant 1.6
37  */

38 public class EscapeUnicode
39     extends BaseParamFilterReader
40     implements ChainableReader {
41     //this field will hold unnnn right after reading a non latin character
42
//afterwards it will be truncated of one char every call to read
43
private StringBuffer JavaDoc unicodeBuf;
44
45     /**
46      * Constructor for "dummy" instances.
47      *
48      * @see BaseFilterReader#BaseFilterReader()
49      */

50     public EscapeUnicode() {
51         super();
52         unicodeBuf = new StringBuffer JavaDoc();
53     }
54
55     /**
56      * Creates a new filtered reader.
57      *
58      * @param in A Reader object providing the underlying stream.
59      * Must not be <code>null</code>.
60      */

61     public EscapeUnicode(final Reader JavaDoc in) {
62         super(in);
63         unicodeBuf = new StringBuffer JavaDoc();
64     }
65
66     /**
67      * Returns the next character in the filtered stream, converting non latin
68      * characters to unicode escapes.
69      *
70      * @return the next character in the resulting stream, or -1
71      * if the end of the resulting stream has been reached
72      *
73      * @exception IOException if the underlying stream throws
74      * an IOException during reading
75      */

76     public final int read() throws IOException JavaDoc {
77         if (!getInitialized()) {
78             initialize();
79             setInitialized(true);
80         }
81
82         int ch = -1;
83         if (unicodeBuf.length() == 0) {
84             ch = in.read();
85             if (ch != -1) {
86                 char achar = (char) ch;
87                 if (achar >= '\u0080') {
88                     unicodeBuf = new StringBuffer JavaDoc("u0000");
89                     String JavaDoc s = Integer.toHexString(ch);
90                     //replace the last 0s by the chars contained in s
91
for (int i = 0; i < s.length(); i++) {
92                         unicodeBuf.setCharAt(unicodeBuf.length()
93                                              - s.length() + i,
94                                              s.charAt(i));
95                     }
96                     ch = '\\';
97                 }
98             }
99         } else {
100             ch = (int) unicodeBuf.charAt(0);
101             unicodeBuf.deleteCharAt(0);
102         }
103         return ch;
104     }
105
106     /**
107      * Creates a new EscapeUnicode using the passed in
108      * Reader for instantiation.
109      *
110      * @param rdr A Reader object providing the underlying stream.
111      * Must not be <code>null</code>.
112      *
113      * @return a new filter based on this configuration, but filtering
114      * the specified reader
115      */

116     public final Reader JavaDoc chain(final Reader JavaDoc rdr) {
117         EscapeUnicode newFilter = new EscapeUnicode(rdr);
118         newFilter.setInitialized(true);
119         return newFilter;
120     }
121
122     /**
123      * Parses the parameters (currently unused)
124      */

125     private void initialize() {
126     }
127 }
128
129
Popular Tags