KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > runtime > impl > ISO88591StreamWriter


1 /*
2 Copyright (c) 2004, Dennis M. Sosnoski.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.runtime.impl;
30
31 import java.io.IOException JavaDoc;
32
33 /**
34  * Handler for marshalling text document to a UTF-8 output stream.
35  *
36  * @author Dennis M. Sosnoski
37  * @version 1.0
38  */

39
40 public class ISO88591StreamWriter extends StreamWriterBase
41 {
42     /**
43      * Constructor.
44      *
45      * @param uris ordered array of URIs for namespaces used in document (must
46      * be constant; the value in position 0 must always be the empty string "",
47      * and the value in position 1 must always be the XML namespace
48      * "http://www.w3.org/XML/1998/namespace")
49      */

50     
51     public ISO88591StreamWriter(String JavaDoc[] uris) {
52         super("ISO-8859-1", uris);
53         m_prefixBytes = new byte[uris.length][];
54         try {
55             defineNamespace(0, "");
56             defineNamespace(1, "xml");
57         } catch (IOException JavaDoc e) {
58             throw new RuntimeException JavaDoc(e.getMessage());
59         }
60     }
61     
62     /**
63      * Write markup text to output. Markup text can be written directly to the
64      * output without the need for any escaping, but still needs to be properly
65      * encoded.
66      *
67      * @param text markup text to be written
68      * @throws IOException if error writing to document
69      */

70     
71     protected void writeMarkup(String JavaDoc text) throws IOException JavaDoc {
72         int length = text.length();
73         makeSpace(length);
74         int fill = m_fillOffset;
75         for (int i = 0; i < length; i++) {
76             char chr = text.charAt(i);
77             if (chr > 0xFF) {
78                 throw new IOException JavaDoc("Unable to write character code 0x" +
79                     Integer.toHexString(chr) + " in encoding ISO-8859-1");
80             } else {
81                 m_buffer[fill++] = (byte)chr;
82             }
83         }
84         m_fillOffset = fill;
85     }
86     
87     /**
88      * Write markup character to output. Markup text can be written directly to
89      * the output without the need for any escaping, but still needs to be
90      * properly encoded.
91      *
92      * @param chr markup character to be written
93      * @throws IOException if error writing to document
94      */

95     
96     protected void writeMarkup(char chr) throws IOException JavaDoc {
97         makeSpace(1);
98         if (chr > 0xFF) {
99             throw new IOException JavaDoc("Unable to write character code 0x" +
100                 Integer.toHexString(chr) + " in encoding ISO-8859-1");
101         } else {
102             m_buffer[m_fillOffset++] = (byte)chr;
103         }
104     }
105     
106     /**
107      * Report that namespace has been defined.
108      *
109      * @param index namespace URI index number
110      * @param prefix prefix used for namespace
111      * @throws IOException if error writing to document
112      */

113     
114     protected void defineNamespace(int index, String JavaDoc prefix)
115         throws IOException JavaDoc {
116         byte[] buff = new byte[prefix.length()];
117         for (int i = 0; i < buff.length; i++) {
118             char chr = prefix.charAt(i);
119             if (chr > 0xFF) {
120                 throw new IOException JavaDoc("Unable to write character code 0x" +
121                     Integer.toHexString(chr) + " in encoding ISO-8859-1");
122             } else {
123                 buff[i] = (byte)chr;
124             }
125         }
126         if (index < m_prefixBytes.length) {
127             m_prefixBytes[index] = buff;
128         } else if (m_extensionBytes != null) {
129             index -= m_prefixBytes.length;
130             for (int i = 0; i < m_extensionBytes.length; i++) {
131                 int length = m_extensionBytes[i].length;
132                 if (index < length) {
133                     m_extensionBytes[i][index] = buff;
134                 } else {
135                     index -= length;
136                 }
137             }
138         } else {
139             throw new IllegalArgumentException JavaDoc("Index out of range");
140         }
141     }
142     
143     /**
144      * Write attribute text to output. This needs to write the text with any
145      * appropriate escaping.
146      *
147      * @param text attribute value text to be written
148      * @throws IOException if error writing to document
149      */

150     
151     protected void writeAttributeText(String JavaDoc text) throws IOException JavaDoc {
152         int length = text.length();
153         makeSpace(length);
154         int fill = m_fillOffset;
155         for (int i = 0; i < length; i++) {
156             char chr = text.charAt(i);
157             if (chr == '"') {
158                 fill = writeEntity(QUOT_ENTITY, fill);
159             } else if (chr == '&') {
160                 fill = writeEntity(AMP_ENTITY, fill);
161             } else if (chr == '<') {
162                 fill = writeEntity(LT_ENTITY, fill);
163             } else if (chr == '>' && i > 2 && text.charAt(i-1) == ']' &&
164                 text.charAt(i-2) == ']') {
165                 m_buffer[fill++] = (byte)']';
166                 m_buffer[fill++] = (byte)']';
167                 fill = writeEntity(GT_ENTITY, fill);
168             } else if (chr < 0x20) {
169                 if (chr != 0x9 && chr != 0xA && chr != 0xD) {
170                     throw new IOException JavaDoc("Illegal character code 0x" +
171                         Integer.toHexString(chr) + " in attribute value text");
172                 } else {
173                     m_buffer[fill++] = (byte)chr;
174                 }
175             } else {
176                 if (chr > 0xFF) {
177                     if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE ||
178                         chr == 0xFFFF || chr > 0x10FFFF)) {
179                         throw new IOException JavaDoc("Illegal character code 0x" +
180                             Integer.toHexString(chr) +
181                             " in attribute value text");
182                     } else {
183                         m_fillOffset = fill;
184                         makeSpace(length - i + 8);
185                         fill = m_fillOffset;
186                         m_buffer[fill++] = (byte)'&';
187                         m_buffer[fill++] = (byte)'#';
188                         m_buffer[fill++] = (byte)'x';
189                         for (int j = 12; j >= 0; j -= 4) {
190                             int nib = (chr >> j) & 0xF;
191                             if (nib < 10) {
192                                 m_buffer[fill++] = (byte)('0' + nib);
193                             } else {
194                                 m_buffer[fill++] = (byte)('A' + nib);
195                             }
196                         }
197                         m_buffer[fill++] = (byte)';';
198                     }
199                 } else {
200                     m_buffer[fill++] = (byte)chr;
201                 }
202             }
203         }
204         m_fillOffset = fill;
205     }
206     
207     /**
208      * Write ordinary character data text content to document.
209      *
210      * @param text content value text
211      * @throws IOException on error writing to document
212      */

213
214     public void writeTextContent(String JavaDoc text) throws IOException JavaDoc {
215         int length = text.length();
216         makeSpace(length);
217         int fill = m_fillOffset;
218         for (int i = 0; i < length; i++) {
219             char chr = text.charAt(i);
220             if (chr == '&') {
221                 fill = writeEntity(AMP_ENTITY, fill);
222             } else if (chr == '<') {
223                 fill = writeEntity(LT_ENTITY, fill);
224             } else if (chr == '>' && i > 2 && text.charAt(i-1) == ']' &&
225                 text.charAt(i-2) == ']') {
226                 m_buffer[fill++] = (byte)']';
227                 m_buffer[fill++] = (byte)']';
228                 fill = writeEntity(GT_ENTITY, fill);
229             } else if (chr < 0x20) {
230                 if (chr != 0x9 && chr != 0xA && chr != 0xD) {
231                     throw new IOException JavaDoc("Illegal character code 0x" +
232                         Integer.toHexString(chr) + " in content text");
233                 } else {
234                     m_buffer[fill++] = (byte)chr;
235                 }
236             } else {
237                 if (chr > 0xFF) {
238                     if (chr > 0xD7FF && (chr < 0xE000 || chr == 0xFFFE ||
239                         chr == 0xFFFF || chr > 0x10FFFF)) {
240                         throw new IOException JavaDoc("Illegal character code 0x" +
241                             Integer.toHexString(chr) +
242                             " in character data text");
243                     } else {
244                         m_fillOffset = fill;
245                         makeSpace(length - i + 8);
246                         fill = m_fillOffset;
247                         m_buffer[fill++] = (byte)'&';
248                         m_buffer[fill++] = (byte)'#';
249                         m_buffer[fill++] = (byte)'x';
250                         for (int j = 12; j >= 0; j -= 4) {
251                             int nib = (chr >> j) & 0xF;
252                             if (nib < 10) {
253                                 m_buffer[fill++] = (byte)('0' + nib);
254                             } else {
255                                 m_buffer[fill++] = (byte)('A' + nib);
256                             }
257                         }
258                         m_buffer[fill++] = (byte)';';
259                     }
260                 } else {
261                     m_buffer[fill++] = (byte)chr;
262                 }
263             }
264         }
265         m_fillOffset = fill;
266         m_textSeen = m_contentSeen = true;
267     }
268     
269     /**
270      * Write CDATA text to document.
271      *
272      * @param text content value text
273      * @throws IOException on error writing to document
274      */

275
276     public void writeCData(String JavaDoc text) throws IOException JavaDoc {
277         int length = text.length();
278         makeSpace(length + 12);
279         int fill = m_fillOffset;
280         fill = writeEntity(LT_CDATASTART, fill);
281         for (int i = 0; i < length; i++) {
282             char chr = text.charAt(i);
283             if (chr == '>' && i > 2 && text.charAt(i-1) == ']' &&
284                 text.charAt(i-2) == ']') {
285                 throw new IOException JavaDoc("Sequence \"]]>\" is not allowed " +
286                     "within CDATA section text");
287             } else if (chr < 0x20) {
288                 if (chr != 0x9 && chr != 0xA && chr != 0xD) {
289                     throw new IOException JavaDoc("Illegal character code 0x" +
290                         Integer.toHexString(chr) + " in content text");
291                 } else {
292                     m_buffer[fill++] = (byte)chr;
293                 }
294             } else {
295                 if (chr > 0xFF) {
296                     throw new IOException JavaDoc("Character code 0x" +
297                         Integer.toHexString(chr) +
298                         " not allowed by encoding in CDATA section text");
299                 } else {
300                     m_buffer[fill++] = (byte)chr;
301                 }
302             }
303         }
304         m_fillOffset = writeEntity(LT_CDATAEND, fill);
305         m_textSeen = m_contentSeen = true;
306     }
307 }
Popular Tags