1 38 39 40 package com.sun.xml.fastinfoset.sax; 41 42 import com.sun.xml.fastinfoset.Encoder; 43 import com.sun.xml.fastinfoset.EncodingConstants; 44 import com.sun.xml.fastinfoset.QualifiedName; 45 import org.jvnet.fastinfoset.sax.FastInfosetWriter; 46 import com.sun.xml.fastinfoset.util.LocalNameQualifiedNamesMap; 47 import java.io.IOException ; 48 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes; 49 import org.jvnet.fastinfoset.FastInfosetException; 50 import org.jvnet.fastinfoset.RestrictedAlphabet; 51 import org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes; 52 import org.xml.sax.Attributes ; 53 import org.xml.sax.SAXException ; 54 import com.sun.xml.fastinfoset.CommonResourceBundle; 55 56 57 public class SAXDocumentSerializer extends Encoder implements FastInfosetWriter { 58 protected boolean _elementHasNamespaces = false; 59 60 protected boolean _charactersAsCDATA = false; 61 62 public SAXDocumentSerializer() { 63 } 64 65 66 public void reset() { 67 super.reset(); 68 69 _elementHasNamespaces = false; 70 _charactersAsCDATA = false; 71 } 72 73 75 public final void startDocument() throws SAXException { 76 try { 77 reset(); 78 encodeHeader(false); 79 encodeInitialVocabulary(); 80 } catch (IOException e) { 81 throw new SAXException ("startDocument", e); 82 } 83 } 84 85 public final void endDocument() throws SAXException { 86 try { 87 encodeDocumentTermination(); 88 } catch (IOException e) { 89 throw new SAXException ("endDocument", e); 90 } 91 } 92 93 public final void startPrefixMapping(String prefix, String uri) throws SAXException { 94 try { 95 if (_elementHasNamespaces == false) { 96 encodeTermination(); 97 98 mark(); 100 _elementHasNamespaces = true; 101 102 write(EncodingConstants.ELEMENT | EncodingConstants.ELEMENT_NAMESPACES_FLAG); 104 } 105 106 encodeNamespaceAttribute(prefix, uri); 107 } catch (IOException e) { 108 throw new SAXException ("startElement", e); 109 } 110 } 111 112 public final void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 113 final int attributeCount = (atts.getLength() > 0) ? countAttributes(atts) : 0; 115 try { 116 if (_elementHasNamespaces) { 117 _elementHasNamespaces = false; 118 119 if (attributeCount > 0) { 120 _octetBuffer[_markIndex] |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG; 122 } 123 resetMark(); 124 125 write(EncodingConstants.TERMINATOR); 126 127 _b = 0; 128 } else { 129 encodeTermination(); 130 131 _b = EncodingConstants.ELEMENT; 132 if (attributeCount > 0) { 133 _b |= EncodingConstants.ELEMENT_ATTRIBUTE_FLAG; 134 } 135 } 136 137 encodeElement(namespaceURI, qName, localName); 138 139 if (attributeCount > 0) { 140 boolean addToTable; 141 String value; 142 if (atts instanceof EncodingAlgorithmAttributes) { 143 final EncodingAlgorithmAttributes eAtts = (EncodingAlgorithmAttributes)atts; 144 for (int i = 0; i < eAtts.getLength(); i++) { 145 if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) { 146 value = eAtts.getValue(i); 147 if (value != null) { 148 addToTable = (value.length() < attributeValueSizeConstraint) ? true : false; 149 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable); 150 } else { 151 encodeNonIdentifyingStringOnFirstBit(eAtts.getAlgorithmURI(i), 152 eAtts.getAlgorithmIndex(i), eAtts.getAlgorithmData(i)); 153 } 154 } 155 } 156 } else { 157 for (int i = 0; i < atts.getLength(); i++) { 158 if (encodeAttribute(atts.getURI(i), atts.getQName(i), atts.getLocalName(i))) { 159 value = atts.getValue(i); 160 addToTable = (value.length() < attributeValueSizeConstraint) ? true : false; 161 encodeNonIdentifyingStringOnFirstBit(value, _v.attributeValue, addToTable); 162 } 163 } 164 } 165 _b = EncodingConstants.TERMINATOR; 166 _terminate = true; 167 } 168 } catch (IOException e) { 169 throw new SAXException ("startElement", e); 170 } catch (FastInfosetException e) { 171 throw new SAXException ("startElement", e); 172 } 173 } 174 175 public final int countAttributes(Attributes atts) { 176 int count = 0; 179 for (int i = 0; i < atts.getLength(); i++) { 180 final String uri = atts.getURI(i); 181 if (uri == "http://www.w3.org/2000/xmlns/" || uri.equals("http://www.w3.org/2000/xmlns/")) { 182 continue; 183 } 184 count++; 185 } 186 return count; 187 } 188 189 public final void endElement(String namespaceURI, String localName, String qName) throws SAXException { 190 try { 191 encodeElementTermination(); 192 } catch (IOException e) { 193 throw new SAXException ("startElement", e); 194 } 195 } 196 197 public final void characters(char[] ch, int start, int length) throws SAXException { 198 if (length <= 0) { 199 return; 200 } 201 202 try { 203 encodeTermination(); 204 205 if (!_charactersAsCDATA) { 206 encodeCharacters(ch, start, length); 207 } else { 208 encodeCIIBuiltInAlgorithmDataAsCDATA(ch, start, length); 209 } 210 } catch (IOException e) { 211 throw new SAXException (e); 212 } catch (FastInfosetException e) { 213 throw new SAXException (e); 214 } 215 } 216 217 public final void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 218 characters(ch, start, length); 219 } 220 221 public final void processingInstruction(String target, String data) throws SAXException { 222 try { 223 if (target == "") { 224 throw new SAXException (CommonResourceBundle.getInstance().getString("message.processingInstructionTargetIsEmpty")); 225 } 226 encodeTermination(); 227 228 encodeProcessingInstruction(target, data); 229 } catch (IOException e) { 230 throw new SAXException ("processingInstruction", e); 231 } 232 } 233 234 public final void setDocumentLocator(org.xml.sax.Locator locator) { 235 } 236 237 public final void skippedEntity(String name) throws SAXException { 238 } 239 240 241 242 244 public final void comment(char[] ch, int start, int length) throws SAXException { 245 try { 246 encodeTermination(); 247 248 encodeComment(ch, start, length); 249 } catch (IOException e) { 250 throw new SAXException ("startElement", e); 251 } 252 } 253 254 public final void startCDATA() throws SAXException { 255 _charactersAsCDATA = true; 256 } 257 258 public final void endCDATA() throws SAXException { 259 _charactersAsCDATA = false; 260 } 261 262 public final void startDTD(String name, String publicId, String systemId) throws SAXException { 263 } 264 265 public final void endDTD() throws SAXException { 266 } 267 268 public final void startEntity(String name) throws SAXException { 269 } 270 271 public final void endEntity(String name) throws SAXException { 272 } 273 274 275 277 public final void octets(String URI, int id, byte[] b, int start, int length) throws SAXException { 278 if (length <= 0) { 279 return; 280 } 281 282 try { 283 encodeTermination(); 284 285 encodeNonIdentifyingStringOnThirdBit(URI, id, b, start, length); 286 } catch (IOException e) { 287 throw new SAXException (e); 288 } catch (FastInfosetException e) { 289 throw new SAXException (e); 290 } 291 } 292 293 public final void object(String URI, int id, Object data) throws SAXException { 294 try { 295 encodeTermination(); 296 297 encodeNonIdentifyingStringOnThirdBit(URI, id, data); 298 } catch (IOException e) { 299 throw new SAXException (e); 300 } catch (FastInfosetException e) { 301 throw new SAXException (e); 302 } 303 } 304 305 306 308 public final void bytes(byte[] b, int start, int length) throws SAXException { 309 if (length <= 0) { 310 return; 311 } 312 313 try { 314 encodeTermination(); 315 316 encodeCIIOctetAlgorithmData(EncodingAlgorithmIndexes.BASE64, b, start, length); 317 } catch (IOException e) { 318 throw new SAXException (e); 319 } 320 } 321 322 public final void shorts(short[] s, int start, int length) throws SAXException { 323 if (length <= 0) { 324 return; 325 } 326 327 try { 328 encodeTermination(); 329 330 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.SHORT, s, start, length); 331 } catch (IOException e) { 332 throw new SAXException (e); 333 } catch (FastInfosetException e) { 334 throw new SAXException (e); 335 } 336 } 337 338 public final void ints(int[] i, int start, int length) throws SAXException { 339 if (length <= 0) { 340 return; 341 } 342 343 try { 344 encodeTermination(); 345 346 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.INT, i, start, length); 347 } catch (IOException e) { 348 throw new SAXException (e); 349 } catch (FastInfosetException e) { 350 throw new SAXException (e); 351 } 352 } 353 354 public final void longs(long[] l, int start, int length) throws SAXException { 355 if (length <= 0) { 356 return; 357 } 358 359 try { 360 encodeTermination(); 361 362 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.LONG, l, start, length); 363 } catch (IOException e) { 364 throw new SAXException (e); 365 } catch (FastInfosetException e) { 366 throw new SAXException (e); 367 } 368 } 369 370 public final void booleans(boolean[] b, int start, int length) throws SAXException { 371 if (length <= 0) { 372 return; 373 } 374 375 try { 376 encodeTermination(); 377 378 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.BOOLEAN, b, start, length); 379 } catch (IOException e) { 380 throw new SAXException (e); 381 } catch (FastInfosetException e) { 382 throw new SAXException (e); 383 } 384 } 385 386 public final void floats(float[] f, int start, int length) throws SAXException { 387 if (length <= 0) { 388 return; 389 } 390 391 try { 392 encodeTermination(); 393 394 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.FLOAT, f, start, length); 395 } catch (IOException e) { 396 throw new SAXException (e); 397 } catch (FastInfosetException e) { 398 throw new SAXException (e); 399 } 400 } 401 402 public final void doubles(double[] d, int start, int length) throws SAXException { 403 if (length <= 0) { 404 return; 405 } 406 407 try { 408 encodeTermination(); 409 410 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.DOUBLE, d, start, length); 411 } catch (IOException e) { 412 throw new SAXException (e); 413 } catch (FastInfosetException e) { 414 throw new SAXException (e); 415 } 416 } 417 418 public void uuids(long[] msblsb, int start, int length) throws SAXException { 419 if (length <= 0) { 420 return; 421 } 422 423 try { 424 encodeTermination(); 425 426 encodeCIIBuiltInAlgorithmData(EncodingAlgorithmIndexes.UUID, msblsb, start, length); 427 } catch (IOException e) { 428 throw new SAXException (e); 429 } catch (FastInfosetException e) { 430 throw new SAXException (e); 431 } 432 } 433 434 435 437 public void numericCharacters(char ch[], int start, int length) throws SAXException { 438 if (length <= 0) { 439 return; 440 } 441 442 try { 443 encodeTermination(); 444 445 encodeFourBitCharacters(RestrictedAlphabet.NUMERIC_CHARACTERS_INDEX, EncodingConstants.NUMERIC_CHARACTERS_TABLE, ch, start, length); 446 } catch (IOException e) { 447 throw new SAXException (e); 448 } catch (FastInfosetException e) { 449 throw new SAXException (e); 450 } 451 } 452 453 public void dateTimeCharacters(char ch[], int start, int length) throws SAXException { 454 if (length <= 0) { 455 return; 456 } 457 458 try { 459 encodeTermination(); 460 461 encodeFourBitCharacters(RestrictedAlphabet.DATE_TIME_CHARACTERS_INDEX, EncodingConstants.DATE_TIME_CHARACTERS_TABLE, ch, start, length); 462 } catch (IOException e) { 463 throw new SAXException (e); 464 } catch (FastInfosetException e) { 465 throw new SAXException (e); 466 } 467 } 468 469 public void alphabetCharacters(String alphabet, char ch[], int start, int length) throws SAXException { 470 if (length <= 0) { 471 return; 472 } 473 474 try { 475 encodeTermination(); 476 477 encodeAlphabetCharacters(alphabet, ch, start, length); 478 } catch (IOException e) { 479 throw new SAXException (e); 480 } catch (FastInfosetException e) { 481 throw new SAXException (e); 482 } 483 } 484 485 486 487 protected final void encodeElement(String namespaceURI, String qName, String localName) throws IOException { 488 LocalNameQualifiedNamesMap.Entry entry = _v.elementName.obtainEntry(qName); 489 if (entry._valueIndex > 0) { 490 QualifiedName[] names = entry._value; 491 for (int i = 0; i < entry._valueIndex; i++) { 492 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 493 encodeNonZeroIntegerOnThirdBit(names[i].index); 494 return; 495 } 496 } 497 } 498 499 encodeLiteralElementQualifiedNameOnThirdBit(namespaceURI, getPrefixFromQualifiedName(qName), 500 localName, entry); 501 } 502 503 protected final boolean encodeAttribute(String namespaceURI, String qName, String localName) throws IOException { 504 LocalNameQualifiedNamesMap.Entry entry = _v.attributeName.obtainEntry(qName); 505 if (entry._valueIndex > 0) { 506 QualifiedName[] names = entry._value; 507 for (int i = 0; i < entry._valueIndex; i++) { 508 if ((namespaceURI == names[i].namespaceName || namespaceURI.equals(names[i].namespaceName))) { 509 encodeNonZeroIntegerOnSecondBitFirstBitZero(names[i].index); 510 return true; 511 } 512 } 513 } 514 515 return encodeLiteralAttributeQualifiedNameOnSecondBit(namespaceURI, getPrefixFromQualifiedName(qName), 516 localName, entry); 517 } 518 } 519 | Popular Tags |