1 28 29 package org.jibx.runtime.impl; 30 31 import java.io.IOException ; 32 33 39 40 public class ISO88591StreamWriter extends StreamWriterBase 41 { 42 50 51 public ISO88591StreamWriter(String [] 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 e) { 58 throw new RuntimeException (e.getMessage()); 59 } 60 } 61 62 70 71 protected void writeMarkup(String text) throws IOException { 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 ("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 95 96 protected void writeMarkup(char chr) throws IOException { 97 makeSpace(1); 98 if (chr > 0xFF) { 99 throw new IOException ("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 113 114 protected void defineNamespace(int index, String prefix) 115 throws IOException { 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 ("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 ("Index out of range"); 140 } 141 } 142 143 150 151 protected void writeAttributeText(String text) throws IOException { 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 ("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 ("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 213 214 public void writeTextContent(String text) throws IOException { 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 ("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 ("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 275 276 public void writeCData(String text) throws IOException { 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 ("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 ("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 ("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 |