View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   */
19  package org.apache.rat.report.xml.writer.impl.base;
20  
21  import org.apache.commons.collections.ArrayStack;
22  import org.apache.rat.report.xml.writer.IXmlWriter;
23  import org.apache.rat.report.xml.writer.InvalidXmlException;
24  import org.apache.rat.report.xml.writer.OperationNotAllowedException;
25  
26  import java.io.IOException;
27  import java.io.Writer;
28  import java.util.Arrays;
29  import java.util.HashSet;
30  import java.util.Set;
31  
32  /**
33   * <p>Lightweight {@link IXmlWriter} implementation.</p>
34   * <p>
35   * Requires a wrapper to be used safely in a multithreaded
36   * environment.</p>
37   * <p>
38   * Not intended to be subclassed. Please copy and hack!</p>
39   */
40  public final class XmlWriter implements IXmlWriter {
41  
42      private static final byte NAME_START_MASK = 1 << 1;
43      private static final byte NAME_MASK = 1 << 2;
44      private static final byte NAME_BODY_CHAR = NAME_MASK;
45      private static final byte NAME_START_OR_BODY_CHAR = NAME_MASK | NAME_START_MASK;
46  
47      private final static boolean[] ALLOWED_CHARACTERS = new boolean[1 << 16];
48  
49      static {
50          Arrays.fill(ALLOWED_CHARACTERS, false);
51          ALLOWED_CHARACTERS[0x9] = true;
52          ALLOWED_CHARACTERS[0xA] = true;
53          ALLOWED_CHARACTERS[0xD] = true;
54          Arrays.fill(ALLOWED_CHARACTERS, 0x20, 0xD7FF, true);
55          Arrays.fill(ALLOWED_CHARACTERS, 0xE000, 0xFFFD, true);
56      }
57  
58      private final static byte[] CHARACTER_CODES = new byte[1 << 16];
59  
60      static {
61          // Name ::= (Letter | '_' | ':') (NameChar)*
62          CHARACTER_CODES['_'] = NAME_START_OR_BODY_CHAR;
63          CHARACTER_CODES[':'] = NAME_START_OR_BODY_CHAR;
64          // Letter ::= BaseChar | Ideographic
65          // BaseChar
66          Arrays.fill(CHARACTER_CODES, 0x0041, 0x005A, NAME_START_OR_BODY_CHAR);
67          Arrays.fill(CHARACTER_CODES, 0x0061, 0x007A, NAME_START_OR_BODY_CHAR);
68          Arrays.fill(CHARACTER_CODES, 0x00C0, 0x00D6, NAME_START_OR_BODY_CHAR);
69          Arrays.fill(CHARACTER_CODES, 0x00D8, 0x00F6, NAME_START_OR_BODY_CHAR);
70          Arrays.fill(CHARACTER_CODES, 0x00F8, 0x00FF, NAME_START_OR_BODY_CHAR);
71          Arrays.fill(CHARACTER_CODES, 0x0100, 0x0131, NAME_START_OR_BODY_CHAR);
72          Arrays.fill(CHARACTER_CODES, 0x0134, 0x013E, NAME_START_OR_BODY_CHAR);
73          Arrays.fill(CHARACTER_CODES, 0x0141, 0x0148, NAME_START_OR_BODY_CHAR);
74          Arrays.fill(CHARACTER_CODES, 0x014A, 0x017E, NAME_START_OR_BODY_CHAR);
75          Arrays.fill(CHARACTER_CODES, 0x0180, 0x01C3, NAME_START_OR_BODY_CHAR);
76          Arrays.fill(CHARACTER_CODES, 0x01CD, 0x01F0, NAME_START_OR_BODY_CHAR);
77          Arrays.fill(CHARACTER_CODES, 0x01F4, 0x01F5, NAME_START_OR_BODY_CHAR);
78          Arrays.fill(CHARACTER_CODES, 0x01FA, 0x0217, NAME_START_OR_BODY_CHAR);
79          Arrays.fill(CHARACTER_CODES, 0x0250, 0x02A8, NAME_START_OR_BODY_CHAR);
80          Arrays.fill(CHARACTER_CODES, 0x02BB, 0x02C1, NAME_START_OR_BODY_CHAR);
81          CHARACTER_CODES[0x0386] = NAME_START_OR_BODY_CHAR;
82          Arrays.fill(CHARACTER_CODES, 0x0388, 0x038A, NAME_START_OR_BODY_CHAR);
83          CHARACTER_CODES[0x038C] = NAME_START_OR_BODY_CHAR;
84          Arrays.fill(CHARACTER_CODES, 0x038E, 0x03A1, NAME_START_OR_BODY_CHAR);
85          Arrays.fill(CHARACTER_CODES, 0x03A3, 0x03CE, NAME_START_OR_BODY_CHAR);
86          Arrays.fill(CHARACTER_CODES, 0x03D0, 0x03D6, NAME_START_OR_BODY_CHAR);
87          CHARACTER_CODES[0x03DA] = NAME_START_OR_BODY_CHAR;
88          CHARACTER_CODES[0x03DC] = NAME_START_OR_BODY_CHAR;
89          CHARACTER_CODES[0x03DE] = NAME_START_OR_BODY_CHAR;
90          CHARACTER_CODES[0x03E0] = NAME_START_OR_BODY_CHAR;
91          Arrays.fill(CHARACTER_CODES, 0x03E2, 0x03F3, NAME_START_OR_BODY_CHAR);
92          Arrays.fill(CHARACTER_CODES, 0x0401, 0x040C, NAME_START_OR_BODY_CHAR);
93          Arrays.fill(CHARACTER_CODES, 0x040E, 0x044F, NAME_START_OR_BODY_CHAR);
94          Arrays.fill(CHARACTER_CODES, 0x0451, 0x045C, NAME_START_OR_BODY_CHAR);
95          Arrays.fill(CHARACTER_CODES, 0x045E, 0x0481, NAME_START_OR_BODY_CHAR);
96          Arrays.fill(CHARACTER_CODES, 0x0490, 0x04C4, NAME_START_OR_BODY_CHAR);
97          Arrays.fill(CHARACTER_CODES, 0x04C7, 0x04C8, NAME_START_OR_BODY_CHAR);
98          Arrays.fill(CHARACTER_CODES, 0x04CB, 0x04CC, NAME_START_OR_BODY_CHAR);
99          Arrays.fill(CHARACTER_CODES, 0x04D0, 0x04EB, NAME_START_OR_BODY_CHAR);
100         Arrays.fill(CHARACTER_CODES, 0x04EE, 0x04F5, NAME_START_OR_BODY_CHAR);
101         Arrays.fill(CHARACTER_CODES, 0x04F8, 0x04F9, NAME_START_OR_BODY_CHAR);
102         Arrays.fill(CHARACTER_CODES, 0x0531, 0x0556, NAME_START_OR_BODY_CHAR);
103         CHARACTER_CODES[0x0559] = NAME_START_OR_BODY_CHAR;
104         Arrays.fill(CHARACTER_CODES, 0x0561, 0x0586, NAME_START_OR_BODY_CHAR);
105         Arrays.fill(CHARACTER_CODES, 0x05D0, 0x05EA, NAME_START_OR_BODY_CHAR);
106         Arrays.fill(CHARACTER_CODES, 0x05F0, 0x05F2, NAME_START_OR_BODY_CHAR);
107         Arrays.fill(CHARACTER_CODES, 0x0621, 0x063A, NAME_START_OR_BODY_CHAR);
108         Arrays.fill(CHARACTER_CODES, 0x0641, 0x064A, NAME_START_OR_BODY_CHAR);
109         Arrays.fill(CHARACTER_CODES, 0x0671, 0x06B7, NAME_START_OR_BODY_CHAR);
110         Arrays.fill(CHARACTER_CODES, 0x06BA, 0x06BE, NAME_START_OR_BODY_CHAR);
111         Arrays.fill(CHARACTER_CODES, 0x06C0, 0x06CE, NAME_START_OR_BODY_CHAR);
112         Arrays.fill(CHARACTER_CODES, 0x06D0, 0x06D3, NAME_START_OR_BODY_CHAR);
113         CHARACTER_CODES[0x06D5] = NAME_START_OR_BODY_CHAR;
114         Arrays.fill(CHARACTER_CODES, 0x06E5, 0x06E6, NAME_START_OR_BODY_CHAR);
115         Arrays.fill(CHARACTER_CODES, 0x0905, 0x0939, NAME_START_OR_BODY_CHAR);
116         CHARACTER_CODES[0x093D] = NAME_START_OR_BODY_CHAR;
117         Arrays.fill(CHARACTER_CODES, 0x0958, 0x0961, NAME_START_OR_BODY_CHAR);
118         Arrays.fill(CHARACTER_CODES, 0x0985, 0x098C, NAME_START_OR_BODY_CHAR);
119         Arrays.fill(CHARACTER_CODES, 0x098F, 0x0990, NAME_START_OR_BODY_CHAR);
120         Arrays.fill(CHARACTER_CODES, 0x0993, 0x09A8, NAME_START_OR_BODY_CHAR);
121         Arrays.fill(CHARACTER_CODES, 0x09AA, 0x09B0, NAME_START_OR_BODY_CHAR);
122         CHARACTER_CODES[0x09B2] = NAME_START_OR_BODY_CHAR;
123         Arrays.fill(CHARACTER_CODES, 0x09B6, 0x09B9, NAME_START_OR_BODY_CHAR);
124         Arrays.fill(CHARACTER_CODES, 0x09DC, 0x09DD, NAME_START_OR_BODY_CHAR);
125         Arrays.fill(CHARACTER_CODES, 0x09DF, 0x09E1, NAME_START_OR_BODY_CHAR);
126         Arrays.fill(CHARACTER_CODES, 0x09F0, 0x09F1, NAME_START_OR_BODY_CHAR);
127         Arrays.fill(CHARACTER_CODES, 0x0A05, 0x0A0A, NAME_START_OR_BODY_CHAR);
128         Arrays.fill(CHARACTER_CODES, 0x0A0F, 0x0A10, NAME_START_OR_BODY_CHAR);
129         Arrays.fill(CHARACTER_CODES, 0x0A13, 0x0A28, NAME_START_OR_BODY_CHAR);
130         Arrays.fill(CHARACTER_CODES, 0x0A2A, 0x0A30, NAME_START_OR_BODY_CHAR);
131         Arrays.fill(CHARACTER_CODES, 0x0A32, 0x0A33, NAME_START_OR_BODY_CHAR);
132         Arrays.fill(CHARACTER_CODES, 0x0A35, 0x0A36, NAME_START_OR_BODY_CHAR);
133         Arrays.fill(CHARACTER_CODES, 0x0A38, 0x0A39, NAME_START_OR_BODY_CHAR);
134         Arrays.fill(CHARACTER_CODES, 0x0A59, 0x0A5C, NAME_START_OR_BODY_CHAR);
135         CHARACTER_CODES[0x0A5E] = NAME_START_OR_BODY_CHAR;
136         Arrays.fill(CHARACTER_CODES, 0x0A72, 0x0A74, NAME_START_OR_BODY_CHAR);
137         Arrays.fill(CHARACTER_CODES, 0x0A85, 0x0A8B, NAME_START_OR_BODY_CHAR);
138         CHARACTER_CODES[0x0A8D] = NAME_START_OR_BODY_CHAR;
139         Arrays.fill(CHARACTER_CODES, 0x0A8F, 0x0A91, NAME_START_OR_BODY_CHAR);
140         Arrays.fill(CHARACTER_CODES, 0x0A93, 0x0AA8, NAME_START_OR_BODY_CHAR);
141         Arrays.fill(CHARACTER_CODES, 0x0AAA, 0x0AB0, NAME_START_OR_BODY_CHAR);
142         Arrays.fill(CHARACTER_CODES, 0x0AB2, 0x0AB3, NAME_START_OR_BODY_CHAR);
143         Arrays.fill(CHARACTER_CODES, 0x0AB5, 0x0AB9, NAME_START_OR_BODY_CHAR);
144         CHARACTER_CODES[0x0ABD] = NAME_START_OR_BODY_CHAR;
145         CHARACTER_CODES[0x0AE0] = NAME_START_OR_BODY_CHAR;
146         Arrays.fill(CHARACTER_CODES, 0x0B05, 0x0B0C, NAME_START_OR_BODY_CHAR);
147         Arrays.fill(CHARACTER_CODES, 0x0B0F, 0x0B10, NAME_START_OR_BODY_CHAR);
148         Arrays.fill(CHARACTER_CODES, 0x0B13, 0x0B28, NAME_START_OR_BODY_CHAR);
149         Arrays.fill(CHARACTER_CODES, 0x0B2A, 0x0B30, NAME_START_OR_BODY_CHAR);
150         Arrays.fill(CHARACTER_CODES, 0x0B32, 0x0B33, NAME_START_OR_BODY_CHAR);
151         Arrays.fill(CHARACTER_CODES, 0x0B36, 0x0B39, NAME_START_OR_BODY_CHAR);
152         CHARACTER_CODES[0x0B3D] = NAME_START_OR_BODY_CHAR;
153         Arrays.fill(CHARACTER_CODES, 0x0B5C, 0x0B5D, NAME_START_OR_BODY_CHAR);
154         Arrays.fill(CHARACTER_CODES, 0x0B5F, 0x0B61, NAME_START_OR_BODY_CHAR);
155         Arrays.fill(CHARACTER_CODES, 0x0B85, 0x0B8A, NAME_START_OR_BODY_CHAR);
156         Arrays.fill(CHARACTER_CODES, 0x0B8E, 0x0B90, NAME_START_OR_BODY_CHAR);
157         Arrays.fill(CHARACTER_CODES, 0x0B92, 0x0B95, NAME_START_OR_BODY_CHAR);
158         Arrays.fill(CHARACTER_CODES, 0x0B99, 0x0B9A, NAME_START_OR_BODY_CHAR);
159         CHARACTER_CODES[0x0B9C] = NAME_START_OR_BODY_CHAR;
160         Arrays.fill(CHARACTER_CODES, 0x0B9E, 0x0B9F, NAME_START_OR_BODY_CHAR);
161         Arrays.fill(CHARACTER_CODES, 0x0BA3, 0x0BA4, NAME_START_OR_BODY_CHAR);
162         Arrays.fill(CHARACTER_CODES, 0x0BA8, 0x0BAA, NAME_START_OR_BODY_CHAR);
163         Arrays.fill(CHARACTER_CODES, 0x0BAE, 0x0BB5, NAME_START_OR_BODY_CHAR);
164         Arrays.fill(CHARACTER_CODES, 0x0BB7, 0x0BB9, NAME_START_OR_BODY_CHAR);
165         Arrays.fill(CHARACTER_CODES, 0x0C05, 0x0C0C, NAME_START_OR_BODY_CHAR);
166         Arrays.fill(CHARACTER_CODES, 0x0C0E, 0x0C10, NAME_START_OR_BODY_CHAR);
167         Arrays.fill(CHARACTER_CODES, 0x0C12, 0x0C28, NAME_START_OR_BODY_CHAR);
168         Arrays.fill(CHARACTER_CODES, 0x0C2A, 0x0C33, NAME_START_OR_BODY_CHAR);
169         Arrays.fill(CHARACTER_CODES, 0x0C35, 0x0C39, NAME_START_OR_BODY_CHAR);
170         Arrays.fill(CHARACTER_CODES, 0x0C60, 0x0C61, NAME_START_OR_BODY_CHAR);
171         Arrays.fill(CHARACTER_CODES, 0x0C85, 0x0C8C, NAME_START_OR_BODY_CHAR);
172         Arrays.fill(CHARACTER_CODES, 0x0C8E, 0x0C90, NAME_START_OR_BODY_CHAR);
173         Arrays.fill(CHARACTER_CODES, 0x0C92, 0x0CA8, NAME_START_OR_BODY_CHAR);
174         Arrays.fill(CHARACTER_CODES, 0x0CAA, 0x0CB3, NAME_START_OR_BODY_CHAR);
175         Arrays.fill(CHARACTER_CODES, 0x0CB5, 0x0CB9, NAME_START_OR_BODY_CHAR);
176         CHARACTER_CODES[0x0CDE] = NAME_START_OR_BODY_CHAR;
177         Arrays.fill(CHARACTER_CODES, 0x0CE0, 0x0CE1, NAME_START_OR_BODY_CHAR);
178         Arrays.fill(CHARACTER_CODES, 0x0D05, 0x0D0C, NAME_START_OR_BODY_CHAR);
179         Arrays.fill(CHARACTER_CODES, 0x0D0E, 0x0D10, NAME_START_OR_BODY_CHAR);
180         Arrays.fill(CHARACTER_CODES, 0x0D12, 0x0D28, NAME_START_OR_BODY_CHAR);
181         Arrays.fill(CHARACTER_CODES, 0x0D2A, 0x0D39, NAME_START_OR_BODY_CHAR);
182         Arrays.fill(CHARACTER_CODES, 0x0D60, 0x0D61, NAME_START_OR_BODY_CHAR);
183         Arrays.fill(CHARACTER_CODES, 0x0E01, 0x0E2E, NAME_START_OR_BODY_CHAR);
184         CHARACTER_CODES[0x0E30] = NAME_START_OR_BODY_CHAR;
185         Arrays.fill(CHARACTER_CODES, 0x0E32, 0x0E33, NAME_START_OR_BODY_CHAR);
186         Arrays.fill(CHARACTER_CODES, 0x0E40, 0x0E45, NAME_START_OR_BODY_CHAR);
187         Arrays.fill(CHARACTER_CODES, 0x0E81, 0x0E82, NAME_START_OR_BODY_CHAR);
188         CHARACTER_CODES[0x0E84] = NAME_START_OR_BODY_CHAR;
189         Arrays.fill(CHARACTER_CODES, 0x0E87, 0x0E88, NAME_START_OR_BODY_CHAR);
190         CHARACTER_CODES[0x0E8A] = NAME_START_OR_BODY_CHAR;
191         CHARACTER_CODES[0x0E8D] = NAME_START_OR_BODY_CHAR;
192         Arrays.fill(CHARACTER_CODES, 0x0E94, 0x0E97, NAME_START_OR_BODY_CHAR);
193         Arrays.fill(CHARACTER_CODES, 0x0E99, 0x0E9F, NAME_START_OR_BODY_CHAR);
194         Arrays.fill(CHARACTER_CODES, 0x0EA1, 0x0EA3, NAME_START_OR_BODY_CHAR);
195         CHARACTER_CODES[0x0EA5] = NAME_START_OR_BODY_CHAR;
196         CHARACTER_CODES[0x0EA7] = NAME_START_OR_BODY_CHAR;
197         Arrays.fill(CHARACTER_CODES, 0x0EAA, 0x0EAB, NAME_START_OR_BODY_CHAR);
198         Arrays.fill(CHARACTER_CODES, 0x0EAD, 0x0EAE, NAME_START_OR_BODY_CHAR);
199         CHARACTER_CODES[0x0EB0] = NAME_START_OR_BODY_CHAR;
200         Arrays.fill(CHARACTER_CODES, 0x0EB2, 0x0EB3, NAME_START_OR_BODY_CHAR);
201         CHARACTER_CODES[0x0EBD] = NAME_START_OR_BODY_CHAR;
202         Arrays.fill(CHARACTER_CODES, 0x0EC0, 0x0EC4, NAME_START_OR_BODY_CHAR);
203         Arrays.fill(CHARACTER_CODES, 0x0F40, 0x0F47, NAME_START_OR_BODY_CHAR);
204         Arrays.fill(CHARACTER_CODES, 0x0F49, 0x0F69, NAME_START_OR_BODY_CHAR);
205         Arrays.fill(CHARACTER_CODES, 0x10A0, 0x10C5, NAME_START_OR_BODY_CHAR);
206         Arrays.fill(CHARACTER_CODES, 0x10D0, 0x10F6, NAME_START_OR_BODY_CHAR);
207         CHARACTER_CODES[0x1100] = NAME_START_OR_BODY_CHAR;
208         Arrays.fill(CHARACTER_CODES, 0x1102, 0x1103, NAME_START_OR_BODY_CHAR);
209         Arrays.fill(CHARACTER_CODES, 0x1105, 0x1107, NAME_START_OR_BODY_CHAR);
210         CHARACTER_CODES[0x1109] = NAME_START_OR_BODY_CHAR;
211         Arrays.fill(CHARACTER_CODES, 0x110B, 0x110C, NAME_START_OR_BODY_CHAR);
212         Arrays.fill(CHARACTER_CODES, 0x110E, 0x1112, NAME_START_OR_BODY_CHAR);
213         CHARACTER_CODES[0x113C] = NAME_START_OR_BODY_CHAR;
214         CHARACTER_CODES[0x113E] = NAME_START_OR_BODY_CHAR;
215         CHARACTER_CODES[0x1140] = NAME_START_OR_BODY_CHAR;
216         CHARACTER_CODES[0x114C] = NAME_START_OR_BODY_CHAR;
217         CHARACTER_CODES[0x114E] = NAME_START_OR_BODY_CHAR;
218         CHARACTER_CODES[0x1150] = NAME_START_OR_BODY_CHAR;
219         Arrays.fill(CHARACTER_CODES, 0x1154, 0x1155, NAME_START_OR_BODY_CHAR);
220         CHARACTER_CODES[0x1159] = NAME_START_OR_BODY_CHAR;
221         Arrays.fill(CHARACTER_CODES, 0x115F, 0x1161, NAME_START_OR_BODY_CHAR);
222         CHARACTER_CODES[0x1163] = NAME_START_OR_BODY_CHAR;
223         CHARACTER_CODES[0x1165] = NAME_START_OR_BODY_CHAR;
224         CHARACTER_CODES[0x1167] = NAME_START_OR_BODY_CHAR;
225         CHARACTER_CODES[0x1169] = NAME_START_OR_BODY_CHAR;
226         Arrays.fill(CHARACTER_CODES, 0x116D, 0x116E, NAME_START_OR_BODY_CHAR);
227         Arrays.fill(CHARACTER_CODES, 0x1172, 0x1173, NAME_START_OR_BODY_CHAR);
228         CHARACTER_CODES[0x1175] = NAME_START_OR_BODY_CHAR;
229         CHARACTER_CODES[0x119E] = NAME_START_OR_BODY_CHAR;
230         CHARACTER_CODES[0x11A8] = NAME_START_OR_BODY_CHAR;
231         CHARACTER_CODES[0x11AB] = NAME_START_OR_BODY_CHAR;
232         Arrays.fill(CHARACTER_CODES, 0x11AE, 0x11AF, NAME_START_OR_BODY_CHAR);
233         Arrays.fill(CHARACTER_CODES, 0x11B7, 0x11B8, NAME_START_OR_BODY_CHAR);
234         CHARACTER_CODES[0x11BA] = NAME_START_OR_BODY_CHAR;
235         Arrays.fill(CHARACTER_CODES, 0x11BC, 0x11C2, NAME_START_OR_BODY_CHAR);
236         CHARACTER_CODES[0x11EB] = NAME_START_OR_BODY_CHAR;
237         CHARACTER_CODES[0x11F0] = NAME_START_OR_BODY_CHAR;
238         CHARACTER_CODES[0x11F9] = NAME_START_OR_BODY_CHAR;
239         Arrays.fill(CHARACTER_CODES, 0x1E00, 0x1E9B, NAME_START_OR_BODY_CHAR);
240         Arrays.fill(CHARACTER_CODES, 0x1EA0, 0x1EF9, NAME_START_OR_BODY_CHAR);
241         Arrays.fill(CHARACTER_CODES, 0x1F00, 0x1F15, NAME_START_OR_BODY_CHAR);
242         Arrays.fill(CHARACTER_CODES, 0x1F18, 0x1F1D, NAME_START_OR_BODY_CHAR);
243         Arrays.fill(CHARACTER_CODES, 0x1F20, 0x1F45, NAME_START_OR_BODY_CHAR);
244         Arrays.fill(CHARACTER_CODES, 0x1F48, 0x1F4D, NAME_START_OR_BODY_CHAR);
245         Arrays.fill(CHARACTER_CODES, 0x1F50, 0x1F57, NAME_START_OR_BODY_CHAR);
246         CHARACTER_CODES[0x1F59] = NAME_START_OR_BODY_CHAR;
247         CHARACTER_CODES[0x1F5B] = NAME_START_OR_BODY_CHAR;
248         CHARACTER_CODES[0x1F5D] = NAME_START_OR_BODY_CHAR;
249         Arrays.fill(CHARACTER_CODES, 0x1F5F, 0x1F7D, NAME_START_OR_BODY_CHAR);
250         Arrays.fill(CHARACTER_CODES, 0x1F80, 0x1FB4, NAME_START_OR_BODY_CHAR);
251         Arrays.fill(CHARACTER_CODES, 0x1FB6, 0x1FBC, NAME_START_OR_BODY_CHAR);
252         CHARACTER_CODES[0x1FBE] = NAME_START_OR_BODY_CHAR;
253         Arrays.fill(CHARACTER_CODES, 0x1FC2, 0x1FC4, NAME_START_OR_BODY_CHAR);
254         Arrays.fill(CHARACTER_CODES, 0x1FC6, 0x1FCC, NAME_START_OR_BODY_CHAR);
255         Arrays.fill(CHARACTER_CODES, 0x1FD0, 0x1FD3, NAME_START_OR_BODY_CHAR);
256         Arrays.fill(CHARACTER_CODES, 0x1FD6, 0x1FDB, NAME_START_OR_BODY_CHAR);
257         Arrays.fill(CHARACTER_CODES, 0x1FE0, 0x1FEC, NAME_START_OR_BODY_CHAR);
258         Arrays.fill(CHARACTER_CODES, 0x1FF2, 0x1FF4, NAME_START_OR_BODY_CHAR);
259         Arrays.fill(CHARACTER_CODES, 0x1FF6, 0x1FFC, NAME_START_OR_BODY_CHAR);
260         CHARACTER_CODES[0x2126] = NAME_START_OR_BODY_CHAR;
261         Arrays.fill(CHARACTER_CODES, 0x212A, 0x212B, NAME_START_OR_BODY_CHAR);
262         CHARACTER_CODES[0x212E] = NAME_START_OR_BODY_CHAR;
263         Arrays.fill(CHARACTER_CODES, 0x2180, 0x2182, NAME_START_OR_BODY_CHAR);
264         Arrays.fill(CHARACTER_CODES, 0x3041, 0x3094, NAME_START_OR_BODY_CHAR);
265         Arrays.fill(CHARACTER_CODES, 0x30A1, 0x30FA, NAME_START_OR_BODY_CHAR);
266         Arrays.fill(CHARACTER_CODES, 0x3105, 0x312C, NAME_START_OR_BODY_CHAR);
267         Arrays.fill(CHARACTER_CODES, 0xAC00, 0xD7A3, NAME_START_OR_BODY_CHAR);
268         // Ideographic
269         Arrays.fill(CHARACTER_CODES, 0x4E00, 0x9FA5, NAME_START_OR_BODY_CHAR);
270         CHARACTER_CODES[0x3007] = NAME_START_OR_BODY_CHAR;
271         Arrays.fill(CHARACTER_CODES, 0x3021, 0x3029, NAME_START_OR_BODY_CHAR);
272         // NameChar ::= Letter | Digit | '.' | '-' | '_' | ':' | CombiningChar | Extender
273         CHARACTER_CODES['.'] = NAME_BODY_CHAR;
274         CHARACTER_CODES['-'] = NAME_BODY_CHAR;
275         // CombiningChar 
276         Arrays.fill(CHARACTER_CODES, 0x0300, 0x0345, NAME_BODY_CHAR);
277         Arrays.fill(CHARACTER_CODES, 0x0360, 0x0361, NAME_BODY_CHAR);
278         Arrays.fill(CHARACTER_CODES, 0x0483, 0x0486, NAME_BODY_CHAR);
279         Arrays.fill(CHARACTER_CODES, 0x0591, 0x05A1, NAME_BODY_CHAR);
280         Arrays.fill(CHARACTER_CODES, 0x05A3, 0x05B9, NAME_BODY_CHAR);
281         Arrays.fill(CHARACTER_CODES, 0x05BB, 0x05BD, NAME_BODY_CHAR);
282         CHARACTER_CODES[0x05BF] = NAME_BODY_CHAR;
283         Arrays.fill(CHARACTER_CODES, 0x05C1, 0x05C2, NAME_BODY_CHAR);
284         CHARACTER_CODES[0x05C4] = NAME_BODY_CHAR;
285         Arrays.fill(CHARACTER_CODES, 0x064B, 0x0652, NAME_BODY_CHAR);
286         CHARACTER_CODES[0x0670] = NAME_BODY_CHAR;
287         Arrays.fill(CHARACTER_CODES, 0x06D6, 0x06DC, NAME_BODY_CHAR);
288         Arrays.fill(CHARACTER_CODES, 0x06DD, 0x06DF, NAME_BODY_CHAR);
289         Arrays.fill(CHARACTER_CODES, 0x06E0, 0x06E4, NAME_BODY_CHAR);
290         Arrays.fill(CHARACTER_CODES, 0x06E7, 0x06E8, NAME_BODY_CHAR);
291         Arrays.fill(CHARACTER_CODES, 0x06EA, 0x06ED, NAME_BODY_CHAR);
292         Arrays.fill(CHARACTER_CODES, 0x0901, 0x0903, NAME_BODY_CHAR);
293         CHARACTER_CODES[0x093C] = NAME_BODY_CHAR;
294         Arrays.fill(CHARACTER_CODES, 0x093E, 0x094C, NAME_BODY_CHAR);
295         CHARACTER_CODES[0x094D] = NAME_BODY_CHAR;
296         Arrays.fill(CHARACTER_CODES, 0x0951, 0x0954, NAME_BODY_CHAR);
297         Arrays.fill(CHARACTER_CODES, 0x0962, 0x0963, NAME_BODY_CHAR);
298         Arrays.fill(CHARACTER_CODES, 0x0981, 0x0983, NAME_BODY_CHAR);
299         CHARACTER_CODES[0x09BC] = NAME_BODY_CHAR;
300         CHARACTER_CODES[0x09BE] = NAME_BODY_CHAR;
301         CHARACTER_CODES[0x09BF] = NAME_BODY_CHAR;
302         Arrays.fill(CHARACTER_CODES, 0x09C0, 0x09C4, NAME_BODY_CHAR);
303         Arrays.fill(CHARACTER_CODES, 0x09C7, 0x09C8, NAME_BODY_CHAR);
304         Arrays.fill(CHARACTER_CODES, 0x09CB, 0x09CD, NAME_BODY_CHAR);
305         CHARACTER_CODES[0x09D7] = NAME_BODY_CHAR;
306         Arrays.fill(CHARACTER_CODES, 0x09E2, 0x09E3, NAME_BODY_CHAR);
307         CHARACTER_CODES[0x0A02] = NAME_BODY_CHAR;
308         CHARACTER_CODES[0x0A3C] = NAME_BODY_CHAR;
309         CHARACTER_CODES[0x0A3E] = NAME_BODY_CHAR;
310         CHARACTER_CODES[0x0A3F] = NAME_BODY_CHAR;
311         Arrays.fill(CHARACTER_CODES, 0x0A40, 0x0A42, NAME_BODY_CHAR);
312         Arrays.fill(CHARACTER_CODES, 0x0A47, 0x0A48, NAME_BODY_CHAR);
313         Arrays.fill(CHARACTER_CODES, 0x0A4B, 0x0A4D, NAME_BODY_CHAR);
314         Arrays.fill(CHARACTER_CODES, 0x0A70, 0x0A71, NAME_BODY_CHAR);
315         Arrays.fill(CHARACTER_CODES, 0x0A81, 0x0A83, NAME_BODY_CHAR);
316         CHARACTER_CODES[0x0ABC] = NAME_BODY_CHAR;
317         Arrays.fill(CHARACTER_CODES, 0x0ABE, 0x0AC5, NAME_BODY_CHAR);
318         Arrays.fill(CHARACTER_CODES, 0x0AC7, 0x0AC9, NAME_BODY_CHAR);
319         Arrays.fill(CHARACTER_CODES, 0x0ACB, 0x0ACD, NAME_BODY_CHAR);
320         Arrays.fill(CHARACTER_CODES, 0x0B01, 0x0B03, NAME_BODY_CHAR);
321         CHARACTER_CODES[0x0B3C] = NAME_BODY_CHAR;
322         Arrays.fill(CHARACTER_CODES, 0x0B3E, 0x0B43, NAME_BODY_CHAR);
323         Arrays.fill(CHARACTER_CODES, 0x0B47, 0x0B48, NAME_BODY_CHAR);
324         Arrays.fill(CHARACTER_CODES, 0x0B4B, 0x0B4D, NAME_BODY_CHAR);
325         Arrays.fill(CHARACTER_CODES, 0x0B56, 0x0B57, NAME_BODY_CHAR);
326         Arrays.fill(CHARACTER_CODES, 0x0B82, 0x0B83, NAME_BODY_CHAR);
327         Arrays.fill(CHARACTER_CODES, 0x0BBE, 0x0BC2, NAME_BODY_CHAR);
328         Arrays.fill(CHARACTER_CODES, 0x0BC6, 0x0BC8, NAME_BODY_CHAR);
329         Arrays.fill(CHARACTER_CODES, 0x0BCA, 0x0BCD, NAME_BODY_CHAR);
330         CHARACTER_CODES[0x0BD7] = NAME_BODY_CHAR;
331         Arrays.fill(CHARACTER_CODES, 0x0C01, 0x0C03, NAME_BODY_CHAR);
332         Arrays.fill(CHARACTER_CODES, 0x0C3E, 0x0C44, NAME_BODY_CHAR);
333         Arrays.fill(CHARACTER_CODES, 0x0C46, 0x0C48, NAME_BODY_CHAR);
334         Arrays.fill(CHARACTER_CODES, 0x0C4A, 0x0C4D, NAME_BODY_CHAR);
335         Arrays.fill(CHARACTER_CODES, 0x0C55, 0x0C56, NAME_BODY_CHAR);
336         Arrays.fill(CHARACTER_CODES, 0x0C82, 0x0C83, NAME_BODY_CHAR);
337         Arrays.fill(CHARACTER_CODES, 0x0CBE, 0x0CC4, NAME_BODY_CHAR);
338         Arrays.fill(CHARACTER_CODES, 0x0CC6, 0x0CC8, NAME_BODY_CHAR);
339         Arrays.fill(CHARACTER_CODES, 0x0CCA, 0x0CCD, NAME_BODY_CHAR);
340         Arrays.fill(CHARACTER_CODES, 0x0CD5, 0x0CD6, NAME_BODY_CHAR);
341         Arrays.fill(CHARACTER_CODES, 0x0D02, 0x0D03, NAME_BODY_CHAR);
342         Arrays.fill(CHARACTER_CODES, 0x0D3E, 0x0D43, NAME_BODY_CHAR);
343         Arrays.fill(CHARACTER_CODES, 0x0D46, 0x0D48, NAME_BODY_CHAR);
344         Arrays.fill(CHARACTER_CODES, 0x0D4A, 0x0D4D, NAME_BODY_CHAR);
345         CHARACTER_CODES[0x0D57] = NAME_BODY_CHAR;
346         CHARACTER_CODES[0x0E31] = NAME_BODY_CHAR;
347         Arrays.fill(CHARACTER_CODES, 0x0E34, 0x0E3A, NAME_BODY_CHAR);
348         Arrays.fill(CHARACTER_CODES, 0x0E47, 0x0E4E, NAME_BODY_CHAR);
349         CHARACTER_CODES[0x0EB1] = NAME_BODY_CHAR;
350         Arrays.fill(CHARACTER_CODES, 0x0EB4, 0x0EB9, NAME_BODY_CHAR);
351         Arrays.fill(CHARACTER_CODES, 0x0EBB, 0x0EBC, NAME_BODY_CHAR);
352         Arrays.fill(CHARACTER_CODES, 0x0EC8, 0x0ECD, NAME_BODY_CHAR);
353         Arrays.fill(CHARACTER_CODES, 0x0F18, 0x0F19, NAME_BODY_CHAR);
354         CHARACTER_CODES[0x0F35] = NAME_BODY_CHAR;
355         CHARACTER_CODES[0x0F37] = NAME_BODY_CHAR;
356         CHARACTER_CODES[0x0F39] = NAME_BODY_CHAR;
357         CHARACTER_CODES[0x0F3E] = NAME_BODY_CHAR;
358         CHARACTER_CODES[0x0F3F] = NAME_BODY_CHAR;
359         Arrays.fill(CHARACTER_CODES, 0x0F71, 0x0F84, NAME_BODY_CHAR);
360         Arrays.fill(CHARACTER_CODES, 0x0F86, 0x0F8B, NAME_BODY_CHAR);
361         Arrays.fill(CHARACTER_CODES, 0x0F90, 0x0F95, NAME_BODY_CHAR);
362         CHARACTER_CODES[0x0F97] = NAME_BODY_CHAR;
363         Arrays.fill(CHARACTER_CODES, 0x0F99, 0x0FAD, NAME_BODY_CHAR);
364         Arrays.fill(CHARACTER_CODES, 0x0FB1, 0x0FB7, NAME_BODY_CHAR);
365         CHARACTER_CODES[0x0FB9] = NAME_BODY_CHAR;
366         Arrays.fill(CHARACTER_CODES, 0x20D0, 0x20DC, NAME_BODY_CHAR);
367         CHARACTER_CODES[0x20E1] = NAME_BODY_CHAR;
368         Arrays.fill(CHARACTER_CODES, 0x302A, 0x302F, NAME_BODY_CHAR);
369         CHARACTER_CODES[0x3099] = NAME_BODY_CHAR;
370         CHARACTER_CODES[0x309A] = NAME_BODY_CHAR;
371         // Digit 
372         Arrays.fill(CHARACTER_CODES, 0x0030, 0x0039, NAME_BODY_CHAR);
373         Arrays.fill(CHARACTER_CODES, 0x0660, 0x0669, NAME_BODY_CHAR);
374         Arrays.fill(CHARACTER_CODES, 0x06F0, 0x06F9, NAME_BODY_CHAR);
375         Arrays.fill(CHARACTER_CODES, 0x0966, 0x096F, NAME_BODY_CHAR);
376         Arrays.fill(CHARACTER_CODES, 0x09E6, 0x09EF, NAME_BODY_CHAR);
377         Arrays.fill(CHARACTER_CODES, 0x0A66, 0x0A6F, NAME_BODY_CHAR);
378         Arrays.fill(CHARACTER_CODES, 0x0AE6, 0x0AEF, NAME_BODY_CHAR);
379         Arrays.fill(CHARACTER_CODES, 0x0B66, 0x0B6F, NAME_BODY_CHAR);
380         Arrays.fill(CHARACTER_CODES, 0x0BE7, 0x0BEF, NAME_BODY_CHAR);
381         Arrays.fill(CHARACTER_CODES, 0x0C66, 0x0C6F, NAME_BODY_CHAR);
382         Arrays.fill(CHARACTER_CODES, 0x0CE6, 0x0CEF, NAME_BODY_CHAR);
383         Arrays.fill(CHARACTER_CODES, 0x0D66, 0x0D6F, NAME_BODY_CHAR);
384         Arrays.fill(CHARACTER_CODES, 0x0E50, 0x0E59, NAME_BODY_CHAR);
385         Arrays.fill(CHARACTER_CODES, 0x0ED0, 0x0ED9, NAME_BODY_CHAR);
386         Arrays.fill(CHARACTER_CODES, 0x0F20, 0x0F29, NAME_BODY_CHAR);
387         // Extender 
388         CHARACTER_CODES[0x00B7] = NAME_BODY_CHAR;
389         CHARACTER_CODES[0x02D0] = NAME_BODY_CHAR;
390         CHARACTER_CODES[0x02D1] = NAME_BODY_CHAR;
391         CHARACTER_CODES[0x0387] = NAME_BODY_CHAR;
392         CHARACTER_CODES[0x0640] = NAME_BODY_CHAR;
393         CHARACTER_CODES[0x0E46] = NAME_BODY_CHAR;
394         CHARACTER_CODES[0x0EC6] = NAME_BODY_CHAR;
395         CHARACTER_CODES[0x3005] = NAME_BODY_CHAR;
396         Arrays.fill(CHARACTER_CODES, 0x3031, 0x3035, NAME_BODY_CHAR);
397         Arrays.fill(CHARACTER_CODES, 0x309D, 0x309E, NAME_BODY_CHAR);
398         Arrays.fill(CHARACTER_CODES, 0x30FC, 0x30FE, NAME_BODY_CHAR);
399 
400     }
401 
402     private final Writer writer;
403     private final ArrayStack elementNames;
404     private final Set<CharSequence> currentAttributes = new HashSet<CharSequence>();
405 
406     boolean elementsWritten = false;
407     boolean inElement = false;
408     boolean prologWritten = false;
409 
410     public XmlWriter(final Writer writer) {
411         this.writer = writer;
412         this.elementNames = new ArrayStack();
413     }
414 
415     /**
416      * Starts a document by writing a prolog.
417      * Calling this method is optional.
418      * When writing a document fragment, it should <em>not</em> be called.
419      *
420      * @return this object
421      * @throws OperationNotAllowedException if called after the first element has been written
422      *                                      or once a prolog has already been written
423      */
424     public IXmlWriter startDocument() throws IOException {
425         if (elementsWritten) {
426             throw new OperationNotAllowedException("Document already started");
427         }
428         if (prologWritten) {
429             throw new OperationNotAllowedException("Only one prolog allowed");
430         }
431         writer.write("<?xml version='1.0'?>");
432         prologWritten = true;
433         return this;
434     }
435 
436     /**
437      * Writes the start of an element.
438      *
439      * @param elementName the name of the element, not null
440      * @return this object
441      * @throws InvalidXmlException          if the name is not valid for an xml element
442      * @throws OperationNotAllowedException if called after the first element has been closed
443      */
444     public IXmlWriter openElement(final CharSequence elementName) throws IOException {
445         if (elementsWritten && elementNames.isEmpty()) {
446             throw new OperationNotAllowedException("Root element already closed. Cannot open new element.");
447         }
448         if (!isValidName(elementName)) {
449             throw new InvalidXmlException("'" + elementName + "' is not a valid element name");
450         }
451         elementsWritten = true;
452         if (inElement) {
453             writer.write('>');
454         }
455         writer.write('<');
456         rawWrite(elementName);
457         inElement = true;
458         elementNames.push(elementName);
459         currentAttributes.clear();
460         return this;
461     }
462 
463     /**
464      * Writes an attribute of an element.
465      * Note that this is only allowed directly after {@link #openElement(CharSequence)}
466      * or {@link #attribute}.
467      *
468      * @param name  the attribute name, not null
469      * @param value the attribute value, not null
470      * @return this object
471      * @throws InvalidXmlException          if the name is not valid for an xml attribute
472      *                                      or if a value for the attribute has already been written
473      * @throws OperationNotAllowedException if called after {@link #content(CharSequence)}
474      *                                      or {@link #closeElement()} or before any call to {@link #openElement(CharSequence)}
475      */
476     public IXmlWriter attribute(CharSequence name, CharSequence value) throws IOException {
477         if (elementNames.isEmpty()) {
478             if (elementsWritten) {
479                 throw new OperationNotAllowedException("Root element has already been closed.");
480             } else {
481                 throw new OperationNotAllowedException("Close called before an element has been opened.");
482             }
483         }
484         if (!isValidName(name)) {
485             throw new InvalidXmlException("'" + name + "' is not a valid attribute name.");
486         }
487         if (!inElement) {
488             throw new InvalidXmlException("Attributes can only be written in elements");
489         }
490         if (currentAttributes.contains(name)) {
491             throw new InvalidXmlException("Each attribute can only be written once");
492         }
493         writer.write(' ');
494         rawWrite(name);
495         writer.write('=');
496         writer.write('\'');
497         writeAttributeContent(value);
498         writer.write('\'');
499         currentAttributes.add(name);
500         return this;
501     }
502 
503     private void writeAttributeContent(CharSequence content) throws IOException {
504         writeEscaped(content, true);
505     }
506 
507     /**
508      * Writes content.
509      * Calling this method will automatically
510      * Note that this method does not use CDATA.
511      *
512      * @param content the content to write
513      * @return this object
514      * @throws OperationNotAllowedException if called before any call to {@link #openElement}
515      *                                      or after the first element has been closed
516      */
517     public IXmlWriter content(CharSequence content) throws IOException {
518         if (elementNames.isEmpty()) {
519             if (elementsWritten) {
520                 throw new OperationNotAllowedException("Root element has already been closed.");
521             } else {
522                 throw new OperationNotAllowedException("An element must be opened before content can be written.");
523             }
524         }
525         if (inElement) {
526             writer.write('>');
527         }
528         writeBodyContent(content);
529         inElement = false;
530         return this;
531     }
532 
533     private void writeBodyContent(final CharSequence content) throws IOException {
534         writeEscaped(content, false);
535     }
536 
537     private void writeEscaped(final CharSequence content, boolean isAttributeContent) throws IOException {
538         final int length = content.length();
539         for (int i = 0; i < length; i++) {
540             char character = content.charAt(i);
541             if (character == '&') {
542                 writer.write("&amp;");
543             } else if (character == '<') {
544                 writer.write("&lt;");
545             } else if (character == '>') {
546                 writer.write("&gt;");
547             } else if (isAttributeContent && character == '\'') {
548                 writer.write("&apos;");
549             } else if (isAttributeContent && character == '\"') {
550                 writer.write("&quot;");
551             } else if (isOutOfRange(character)) {
552                 writer.write('?');
553             } else {
554                 writer.write(character);
555             }
556         }
557     }
558 
559     private boolean isOutOfRange(final char character) {
560         return !ALLOWED_CHARACTERS[character];
561     }
562 
563     /**
564      * Closes the last element written.
565      *
566      * @return this object
567      * @throws OperationNotAllowedException if called before any call to {@link #openElement}
568      *                                      or after the first element has been closed
569      */
570     public IXmlWriter closeElement() throws IOException {
571         if (elementNames.isEmpty()) {
572             if (elementsWritten) {
573                 throw new OperationNotAllowedException("Root element has already been closed.");
574             } else {
575                 throw new OperationNotAllowedException("Close called before an element has been opened.");
576             }
577         }
578         final CharSequence elementName = (CharSequence) elementNames.pop();
579         if (inElement) {
580             writer.write('/');
581             writer.write('>');
582         } else {
583             writer.write('<');
584             writer.write('/');
585             rawWrite(elementName);
586             writer.write('>');
587         }
588         writer.flush();
589         inElement = false;
590         return this;
591     }
592 
593 
594     /**
595      * Closes all pending elements.
596      * When appropriate, resources are also flushed and closed.
597      * No exception is raised when called upon a document whose
598      * root element has already been closed.
599      *
600      * @return this object
601      * @throws OperationNotAllowedException if called before any call to {@link #openElement}
602      */
603     public IXmlWriter closeDocument() throws IOException {
604         if (elementNames.isEmpty() && !elementsWritten) {
605             throw new OperationNotAllowedException("Close called before an element has been opened.");
606         }
607         while (!elementNames.isEmpty()) {
608             closeElement();
609         }
610         writer.flush();
611         return this;
612     }
613 
614     private void rawWrite(final CharSequence sequence) throws IOException {
615         for (int i = 0; i < sequence.length(); i++) {
616             final char charAt = sequence.charAt(i);
617             writer.write(charAt);
618         }
619     }
620 
621     private boolean isValidName(final CharSequence sequence) {
622         boolean result = true;
623         final int length = sequence.length();
624         for (int i = 0; i < length; i++) {
625             char character = sequence.charAt(i);
626             if (i == 0) {
627                 if (!isValidNameStart(character)) {
628                     result = false;
629                     break;
630                 }
631             } else {
632                 if (!isValidNameBody(character)) {
633                     result = false;
634                     break;
635                 }
636             }
637         }
638         return result;
639     }
640 
641     private boolean isValidNameStart(final char character) {
642         final byte code = CHARACTER_CODES[character];
643         return (code & NAME_START_MASK) > 0;
644     }
645 
646     private boolean isValidNameBody(final char character) {
647         final byte code = CHARACTER_CODES[character];
648         return (code & NAME_MASK) > 0;
649     }
650 }