1 | /*
|
---|
2 | * Copyright 2003-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 | package org.apache.tools.ant.filters;
|
---|
18 |
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.io.Reader;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This method converts non-latin characters to unicode escapes.
|
---|
24 | * Useful to load properties containing non latin
|
---|
25 | * Example:
|
---|
26 | *
|
---|
27 | * <pre><escapeunicode></pre>
|
---|
28 | *
|
---|
29 | * Or:
|
---|
30 | *
|
---|
31 | * <pre><filterreader
|
---|
32 | classname="org.apache.tools.ant.filters.EscapeUnicode"/>
|
---|
33 | * </pre>
|
---|
34 | *
|
---|
35 | * @since Ant 1.6
|
---|
36 | */
|
---|
37 | public class EscapeUnicode
|
---|
38 | extends BaseParamFilterReader
|
---|
39 | implements ChainableReader {
|
---|
40 | //this field will hold unnnn right after reading a non latin character
|
---|
41 | //afterwards it will be truncated of one char every call to read
|
---|
42 | private StringBuffer unicodeBuf;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructor for "dummy" instances.
|
---|
46 | *
|
---|
47 | * @see BaseFilterReader#BaseFilterReader()
|
---|
48 | */
|
---|
49 | public EscapeUnicode() {
|
---|
50 | super();
|
---|
51 | unicodeBuf = new StringBuffer();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Creates a new filtered reader.
|
---|
56 | *
|
---|
57 | * @param in A Reader object providing the underlying stream.
|
---|
58 | * Must not be <code>null</code>.
|
---|
59 | */
|
---|
60 | public EscapeUnicode(final Reader in) {
|
---|
61 | super(in);
|
---|
62 | unicodeBuf = new StringBuffer();
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Returns the next character in the filtered stream, converting non latin
|
---|
67 | * characters to unicode escapes.
|
---|
68 | *
|
---|
69 | * @return the next character in the resulting stream, or -1
|
---|
70 | * if the end of the resulting stream has been reached
|
---|
71 | *
|
---|
72 | * @exception IOException if the underlying stream throws
|
---|
73 | * an IOException during reading
|
---|
74 | */
|
---|
75 | public final int read() throws IOException {
|
---|
76 | if (!getInitialized()) {
|
---|
77 | initialize();
|
---|
78 | setInitialized(true);
|
---|
79 | }
|
---|
80 |
|
---|
81 | int ch = -1;
|
---|
82 | if (unicodeBuf.length() == 0) {
|
---|
83 | ch = in.read();
|
---|
84 | if (ch != -1) {
|
---|
85 | char achar = (char) ch;
|
---|
86 | if (achar >= '\u0080') {
|
---|
87 | unicodeBuf = new StringBuffer("u0000");
|
---|
88 | String s = Integer.toHexString(ch);
|
---|
89 | //replace the last 0s by the chars contained in s
|
---|
90 | for (int i = 0; i < s.length(); i++) {
|
---|
91 | unicodeBuf.setCharAt(unicodeBuf.length()
|
---|
92 | - s.length() + i,
|
---|
93 | s.charAt(i));
|
---|
94 | }
|
---|
95 | ch = '\\';
|
---|
96 | }
|
---|
97 | }
|
---|
98 | } else {
|
---|
99 | ch = (int) unicodeBuf.charAt(0);
|
---|
100 | unicodeBuf.deleteCharAt(0);
|
---|
101 | }
|
---|
102 | return ch;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Creates a new EscapeUnicode using the passed in
|
---|
107 | * Reader for instantiation.
|
---|
108 | *
|
---|
109 | * @param rdr A Reader object providing the underlying stream.
|
---|
110 | * Must not be <code>null</code>.
|
---|
111 | *
|
---|
112 | * @return a new filter based on this configuration, but filtering
|
---|
113 | * the specified reader
|
---|
114 | */
|
---|
115 | public final Reader chain(final Reader rdr) {
|
---|
116 | EscapeUnicode newFilter = new EscapeUnicode(rdr);
|
---|
117 | newFilter.setInitialized(true);
|
---|
118 | return newFilter;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Parses the parameters (currently unused)
|
---|
123 | */
|
---|
124 | private final void initialize() {
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|