]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/impl/duration/impl/XMLRecordReader.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / impl / duration / impl / XMLRecordReader.java
1 /*
2 ******************************************************************************
3 * Copyright (C) 2007-2010, International Business Machines Corporation and   *
4 * others. All Rights Reserved.                                               *
5 ******************************************************************************
6 */
7
8 package com.ibm.icu.impl.duration.impl;
9
10 import java.io.IOException;
11 import java.io.Reader;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import com.ibm.icu.lang.UCharacter;
16
17 public class XMLRecordReader implements RecordReader {
18     private Reader r;
19
20     private List<String> nameStack;
21
22     private boolean atTag;
23
24     private String tag; // cache
25
26     public XMLRecordReader(Reader r) {
27         this.r = r;
28         this.nameStack = new ArrayList<String>();
29
30         // skip XML prologue
31         if (getTag().startsWith("?xml")) {
32             advance();
33         }
34
35         // skip FIRST comment
36         if (getTag().startsWith("!--")) {
37             advance();
38         }
39     }
40
41     public boolean open(String title) {
42         if (getTag().equals(title)) {
43             nameStack.add(title);
44             advance();
45             return true;
46         }
47         return false;
48     }
49
50     public boolean close() {
51         int ix = nameStack.size() - 1;
52         String name = nameStack.get(ix);
53         if (getTag().equals("/" + name)) {
54             nameStack.remove(ix);
55             advance();
56             return true;
57         }
58         return false;
59     }
60
61     public boolean bool(String name) {
62         String s = string(name);
63         if (s != null) {
64             return "true".equals(s);
65         }
66         return false;
67     }
68
69     public boolean[] boolArray(String name) {
70         String[] sa = stringArray(name);
71         if (sa != null) {
72             boolean[] result = new boolean[sa.length];
73             for (int i = 0; i < sa.length; ++i) {
74                 result[i] = "true".equals(sa[i]);
75             }
76             return result;
77         }
78         return null;
79     }
80
81     public char character(String name) {
82         String s = string(name);
83         if (s != null) {
84             return s.charAt(0);
85         }
86         return '\uffff';
87     }
88
89     public char[] characterArray(String name) {
90         String[] sa = stringArray(name);
91         if (sa != null) {
92             char[] result = new char[sa.length];
93             for (int i = 0; i < sa.length; ++i) {
94                 result[i] = sa[i].charAt(0);
95             }
96             return result;
97         }
98         return null;
99     }
100
101     public byte namedIndex(String name, String[] names) {
102         String sa = string(name);
103         if (sa != null) {
104             for (int i = 0; i < names.length; ++i) {
105                 if (sa.equals(names[i])) {
106                     return (byte) i;
107                 }
108             }
109         }
110         return (byte) -1;
111     }
112
113     public byte[] namedIndexArray(String name, String[] names) {
114         String[] sa = stringArray(name);
115         if (sa != null) {
116             byte[] result = new byte[sa.length];
117             loop: for (int i = 0; i < sa.length; ++i) {
118                 String s = sa[i];
119                 for (int j = 0; j < names.length; ++j) {
120                     if (names[j].equals(s)) {
121                         result[i] = (byte) j;
122                         continue loop;
123                     }
124                 }
125                 result[i] = (byte) -1;
126             }
127             return result;
128         }
129         return null;
130     }
131
132     public String string(String name) {
133         if (match(name)) {
134             String result = readData();
135             if (match("/" + name)) {
136                 return result;
137             }
138         }
139         return null;
140     }
141
142     public String[] stringArray(String name) {
143         if (match(name + "List")) {
144             List<String> list = new ArrayList<String>();
145             String s;
146             while (null != (s = string(name))) {
147                 if ("Null".equals(s)) {
148                     s = null;
149                 }
150                 list.add(s);
151             }
152             if (match("/" + name + "List")) {
153                 return list.toArray(new String[list.size()]);
154             }
155         }
156         return null;
157     }
158
159     public String[][] stringTable(String name) {
160         if (match(name + "Table")) {
161             List<String[]> list = new ArrayList<String[]>();
162             String[] sa;
163             while (null != (sa = stringArray(name))) {
164                 list.add(sa);
165             }
166             if (match("/" + name + "Table")) {
167                 return list.toArray(new String[list.size()][]);
168             }
169         }
170         return null;
171     }
172
173     private boolean match(String target) {
174         if (getTag().equals(target)) {
175             // System.out.println("match '" + target + "'");
176             advance();
177             return true;
178         }
179         return false;
180     }
181
182     private String getTag() {
183         if (tag == null) {
184             tag = readNextTag();
185         }
186         return tag;
187     }
188
189     private void advance() {
190         tag = null;
191     }
192
193     private String readData() {
194         StringBuilder sb = new StringBuilder();
195         boolean inWhitespace = false;
196         // boolean inAmp = false;
197         while (true) {
198             int c = readChar();
199             if (c == -1 || c == '<') {
200                 atTag = c == '<';
201                 break;
202             }
203             if (c == '&') {
204                 c = readChar();
205                 if (c == '#') {
206                     StringBuilder numBuf = new StringBuilder();
207                     int radix = 10;
208                     c = readChar();
209                     if (c == 'x') {
210                         radix = 16;
211                         c = readChar();
212                     }
213                     while (c != ';' && c != -1) {
214                         numBuf.append((char) c);
215                         c = readChar();
216                     }
217                     try {
218                         int num = Integer.parseInt(numBuf.toString(), radix);
219                         c = (char) num;
220                     } catch (NumberFormatException ex) {
221                         System.err.println("numbuf: " + numBuf.toString()
222                                 + " radix: " + radix);
223                         throw ex;
224                     }
225                 } else {
226                     StringBuilder charBuf = new StringBuilder();
227                     while (c != ';' && c != -1) {
228                         charBuf.append((char) c);
229                         c = readChar();
230                     }
231                     String charName = charBuf.toString();
232                     if (charName.equals("lt")) {
233                         c = '<';
234                     } else if (charName.equals("gt")) {
235                         c = '>';
236                     } else if (charName.equals("quot")) {
237                         c = '"';
238                     } else if (charName.equals("apos")) {
239                         c = '\'';
240                     } else if (charName.equals("amp")) {
241                         c = '&';
242                     } else {
243                         System.err.println("unrecognized character entity: '"
244                                 + charName + "'");
245                         continue;
246                     }
247                 }
248             }
249
250             if (UCharacter.isWhitespace(c)) {
251                 if (inWhitespace) {
252                     continue;
253                 }
254                 c = ' ';
255                 inWhitespace = true;
256             } else {
257                 inWhitespace = false;
258             }
259             sb.append((char) c);
260         }
261         //System.err.println("read data: '" + sb.toString() + "'");
262         return sb.toString();
263     }
264
265     private String readNextTag() {
266         int c = '\0';
267         while (!atTag) {
268             c = readChar();
269             if (c == '<' || c == -1) {
270                 if (c == '<') {
271                     atTag = true;
272                 }
273                 break;
274             }
275             if (!UCharacter.isWhitespace(c)) {
276                 System.err.println("Unexpected non-whitespace character "
277                         + Integer.toHexString(c));
278                 break;
279             }
280         }
281
282         if (atTag) {
283             atTag = false;
284             StringBuilder sb = new StringBuilder();
285             while (true) {
286                 c = readChar();
287                 if (c == '>' || c == -1) {
288                     break;
289                 }
290                 sb.append((char) c);
291             }
292             // System.err.println("read tag: '" + sb.toString() + "'");
293             return sb.toString();
294         }
295         return null;
296     }
297
298     int readChar() {
299         try {
300             return r.read();
301         } catch (IOException e) {
302             // assume end of input
303         }
304         return -1;
305     }
306 }