1 16 package org.apache.cocoon.xml; 17 18 import org.xml.sax.ContentHandler ; 19 import org.xml.sax.SAXException ; 20 import org.xml.sax.Locator ; 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.ext.LexicalHandler ; 23 import org.apache.excalibur.xml.sax.XMLizable; 24 import org.apache.avalon.excalibur.pool.Recyclable; 25 26 import java.io.Serializable ; 27 import java.io.Writer ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Collections ; 33 34 51 public class SaxBuffer extends AbstractSAXFragment 52 implements XMLConsumer, Recyclable, Serializable { 53 54 57 protected List saxbits; 58 59 62 public SaxBuffer() { 63 this.saxbits = new ArrayList (); 64 } 65 66 69 public SaxBuffer(List bits) { 70 this.saxbits = bits; 71 } 72 73 76 public SaxBuffer(SaxBuffer saxBuffer) { 77 this.saxbits = new ArrayList (saxBuffer.saxbits); 78 } 79 80 84 public void skippedEntity(String name) throws SAXException { 85 saxbits.add(new SkippedEntity(name)); 86 } 87 88 public void setDocumentLocator(Locator locator) { 89 } 91 92 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 93 saxbits.add(new IgnorableWhitespace(ch, start, length)); 94 } 95 96 public void processingInstruction(String target, String data) throws SAXException { 97 saxbits.add(new PI(target, data)); 98 } 99 100 public void startDocument() throws SAXException { 101 saxbits.add(StartDocument.SINGLETON); 102 } 103 104 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 105 saxbits.add(new StartElement(namespaceURI, localName, qName, atts)); 106 } 107 108 public void endPrefixMapping(String prefix) throws SAXException { 109 saxbits.add(new EndPrefixMapping(prefix)); 110 } 111 112 public void characters(char ch[], int start, int length) throws SAXException { 113 saxbits.add(new Characters(ch, start, length)); 114 } 115 116 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 117 saxbits.add(new EndElement(namespaceURI, localName, qName)); 118 } 119 120 public void endDocument() throws SAXException { 121 saxbits.add(EndDocument.SINGLETON); 122 } 123 124 public void startPrefixMapping(String prefix, String uri) throws SAXException { 125 saxbits.add(new StartPrefixMapping(prefix, uri)); 126 } 127 128 132 public void endCDATA() throws SAXException { 133 saxbits.add(EndCDATA.SINGLETON); 134 } 135 136 public void comment(char ch[], int start, int length) throws SAXException { 137 saxbits.add(new Comment(ch, start, length)); 138 } 139 140 public void startEntity(String name) throws SAXException { 141 saxbits.add(new StartEntity(name)); 142 } 143 144 public void endDTD() throws SAXException { 145 saxbits.add(EndDTD.SINGLETON); 146 } 147 148 public void startDTD(String name, String publicId, String systemId) throws SAXException { 149 saxbits.add(new StartDTD(name, publicId, systemId)); 150 } 151 152 public void startCDATA() throws SAXException { 153 saxbits.add(StartCDATA.SINGLETON); 154 } 155 156 public void endEntity(String name) throws SAXException { 157 saxbits.add(new EndEntity(name)); 158 } 159 160 164 167 public void xmlizable(XMLizable xml) { 168 saxbits.add(new XMLizableBit(xml)); 169 } 170 171 174 public boolean isEmpty() { 175 return saxbits.isEmpty(); 176 } 177 178 181 public List getBits() { 182 return Collections.unmodifiableList(saxbits); 183 } 184 185 190 public void toSAX(ContentHandler contentHandler) throws SAXException { 191 for (Iterator i = saxbits.iterator(); i.hasNext();) { 192 SaxBit saxbit = (SaxBit)i.next(); 193 saxbit.send(contentHandler); 194 } 195 } 196 197 200 public String toString() { 201 final StringBuffer value = new StringBuffer (); 203 for (Iterator i = saxbits.iterator(); i.hasNext();) { 204 final SaxBit saxbit = (SaxBit) i.next(); 205 if (saxbit instanceof Characters) { 206 ((Characters) saxbit).toString(value); 207 } 208 } 209 210 return value.toString(); 211 } 212 213 216 public void recycle() { 217 saxbits.clear(); 218 } 219 220 223 public void dump(Writer writer) throws IOException { 224 Iterator i = saxbits.iterator(); 225 while (i.hasNext()) { 226 final SaxBit saxbit = (SaxBit) i.next(); 227 saxbit.dump(writer); 228 } 229 writer.flush(); 230 } 231 232 236 239 protected final void addBit(SaxBit bit) { 240 saxbits.add(bit); 241 } 242 243 246 protected final Iterator bits() { 247 return saxbits.iterator(); 248 } 249 250 253 interface SaxBit { 254 public void send(ContentHandler contentHandler) throws SAXException ; 255 public void dump(Writer writer) throws IOException ; 256 } 257 258 public final static class StartDocument implements SaxBit, Serializable { 259 public static final StartDocument SINGLETON = new StartDocument(); 260 261 public void send(ContentHandler contentHandler) throws SAXException { 262 contentHandler.startDocument(); 263 } 264 265 public void dump(Writer writer) throws IOException { 266 writer.write("[StartDocument]\n"); 267 } 268 } 269 270 public final static class EndDocument implements SaxBit, Serializable { 271 public static final EndDocument SINGLETON = new EndDocument(); 272 273 public void send(ContentHandler contentHandler) throws SAXException { 274 contentHandler.endDocument(); 275 } 276 277 public void dump(Writer writer) throws IOException { 278 writer.write("[EndDocument]\n"); 279 } 280 } 281 282 public final static class PI implements SaxBit, Serializable { 283 public final String target; 284 public final String data; 285 286 public PI(String target, String data) { 287 this.target = target; 288 this.data = data; 289 } 290 291 public void send(ContentHandler contentHandler) throws SAXException { 292 contentHandler.processingInstruction(target, data); 293 } 294 295 public void dump(Writer writer) throws IOException { 296 writer.write("[ProcessingInstruction] target=" + target + ",data=" + data + "\n"); 297 } 298 } 299 300 public final static class StartDTD implements SaxBit, Serializable { 301 public final String name; 302 public final String publicId; 303 public final String systemId; 304 305 public StartDTD(String name, String publicId, String systemId) { 306 this.name = name; 307 this.publicId = publicId; 308 this.systemId = systemId; 309 } 310 311 public void send(ContentHandler contentHandler) throws SAXException { 312 if (contentHandler instanceof LexicalHandler ) 313 ((LexicalHandler )contentHandler).startDTD(name, publicId, systemId); 314 } 315 316 public void dump(Writer writer) throws IOException { 317 writer.write("[StartDTD] name=" + name + ",publicId=" + publicId + ",systemId=" + systemId + "\n"); 318 } 319 } 320 321 public final static class EndDTD implements SaxBit, Serializable { 322 public static final EndDTD SINGLETON = new EndDTD(); 323 324 public void send(ContentHandler contentHandler) throws SAXException { 325 if (contentHandler instanceof LexicalHandler ) 326 ((LexicalHandler )contentHandler).endDTD(); 327 } 328 329 public void dump(Writer writer) throws IOException { 330 writer.write("[EndDTD]\n"); 331 } 332 } 333 334 public final static class StartEntity implements SaxBit, Serializable { 335 public final String name; 336 337 public StartEntity(String name) { 338 this.name = name; 339 } 340 341 public void send(ContentHandler contentHandler) throws SAXException { 342 if (contentHandler instanceof LexicalHandler ) 343 ((LexicalHandler )contentHandler).startEntity(name); 344 } 345 346 public void dump(Writer writer) throws IOException { 347 writer.write("[StartEntity] name=" + name + "\n"); 348 } 349 } 350 351 public final static class EndEntity implements SaxBit, Serializable { 352 public final String name; 353 354 public EndEntity(String name) { 355 this.name = name; 356 } 357 358 public void send(ContentHandler contentHandler) throws SAXException { 359 if (contentHandler instanceof LexicalHandler ) 360 ((LexicalHandler )contentHandler).endEntity(name); 361 } 362 363 public void dump(Writer writer) throws IOException { 364 writer.write("[EndEntity] name=" + name + "\n"); 365 } 366 } 367 368 public final static class SkippedEntity implements SaxBit, Serializable { 369 public final String name; 370 371 public SkippedEntity(String name) { 372 this.name = name; 373 } 374 375 public void send(ContentHandler contentHandler) throws SAXException { 376 contentHandler.skippedEntity(name); 377 } 378 379 public void dump(Writer writer) throws IOException { 380 writer.write("[SkippedEntity] name=" + name + "\n"); 381 } 382 } 383 384 public final static class StartPrefixMapping implements SaxBit, Serializable { 385 public final String prefix; 386 public final String uri; 387 388 public StartPrefixMapping(String prefix, String uri) { 389 this.prefix = prefix; 390 this.uri = uri; 391 } 392 393 public void send(ContentHandler contentHandler) throws SAXException { 394 contentHandler.startPrefixMapping(prefix, uri); 395 } 396 397 public void dump(Writer writer) throws IOException { 398 writer.write("[StartPrefixMapping] prefix=" + prefix + ",uri=" + uri + "\n"); 399 } 400 } 401 402 public final static class EndPrefixMapping implements SaxBit, Serializable { 403 public final String prefix; 404 405 public EndPrefixMapping(String prefix) { 406 this.prefix = prefix; 407 } 408 409 public void send(ContentHandler contentHandler) throws SAXException { 410 contentHandler.endPrefixMapping(prefix); 411 } 412 413 public void dump(Writer writer) throws IOException { 414 writer.write("[EndPrefixMapping] prefix=" + prefix + "\n"); 415 } 416 } 417 418 public final static class StartElement implements SaxBit, Serializable { 419 public final String namespaceURI; 420 public final String localName; 421 public final String qName; 422 public final Attributes attrs; 423 424 public StartElement(String namespaceURI, String localName, String qName, Attributes attrs) { 425 this.namespaceURI = namespaceURI; 426 this.localName = localName; 427 this.qName = qName; 428 this.attrs = new org.xml.sax.helpers.AttributesImpl (attrs); 429 } 430 431 public void send(ContentHandler contentHandler) throws SAXException { 432 contentHandler.startElement(namespaceURI, localName, qName, attrs); 433 } 434 435 public void dump(Writer writer) throws IOException { 436 writer.write("[StartElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n"); 437 for (int i = 0; i < attrs.getLength(); i++) { 438 writer.write(" [Attribute] namespaceURI=" + attrs.getURI(i) + ",localName=" + attrs.getLocalName(i) + ",qName=" + attrs.getQName(i) + ",type=" + attrs.getType(i) + ",value=" + attrs.getValue(i) + "\n"); 439 } 440 } 441 } 442 443 public final static class EndElement implements SaxBit, Serializable { 444 public final String namespaceURI; 445 public final String localName; 446 public final String qName; 447 448 public EndElement(String namespaceURI, String localName, String qName) { 449 this.namespaceURI = namespaceURI; 450 this.localName = localName; 451 this.qName = qName; 452 } 453 454 public void send(ContentHandler contentHandler) throws SAXException { 455 contentHandler.endElement(namespaceURI, localName, qName); 456 } 457 458 public void dump(Writer writer) throws IOException { 459 writer.write("[EndElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n"); 460 } 461 } 462 463 public final static class Characters implements SaxBit, Serializable { 464 public final char[] ch; 465 466 public Characters(char[] ch, int start, int length) { 467 this.ch = new char[length]; 469 System.arraycopy(ch, start, this.ch, 0, length); 470 } 471 472 public void send(ContentHandler contentHandler) throws SAXException { 473 contentHandler.characters(ch, 0, ch.length); 474 } 475 476 public void toString(StringBuffer value) { 477 value.append(ch); 478 } 479 480 public void dump(Writer writer) throws IOException { 481 writer.write("[Characters] ch=" + new String (ch) + "\n"); 482 } 483 } 484 485 public final static class Comment implements SaxBit, Serializable { 486 public final char[] ch; 487 488 public Comment(char[] ch, int start, int length) { 489 this.ch = new char[length]; 491 System.arraycopy(ch, start, this.ch, 0, length); 492 } 493 494 public void send(ContentHandler contentHandler) throws SAXException { 495 if (contentHandler instanceof LexicalHandler ) 496 ((LexicalHandler )contentHandler).comment(ch, 0, ch.length); 497 } 498 499 public void dump(Writer writer) throws IOException { 500 writer.write("[Comment] ch=" + new String (ch) + "\n"); 501 } 502 } 503 504 public final static class StartCDATA implements SaxBit, Serializable { 505 public static final StartCDATA SINGLETON = new StartCDATA(); 506 507 public void send(ContentHandler contentHandler) throws SAXException { 508 if (contentHandler instanceof LexicalHandler ) 509 ((LexicalHandler )contentHandler).startCDATA(); 510 } 511 512 public void dump(Writer writer) throws IOException { 513 writer.write("[StartCDATA]\n"); 514 } 515 } 516 517 public final static class EndCDATA implements SaxBit, Serializable { 518 public static final EndCDATA SINGLETON = new EndCDATA(); 519 520 public void send(ContentHandler contentHandler) throws SAXException { 521 if (contentHandler instanceof LexicalHandler ) 522 ((LexicalHandler )contentHandler).endCDATA(); 523 } 524 525 public void dump(Writer writer) throws IOException { 526 writer.write("[EndCDATA]\n"); 527 } 528 } 529 530 public final static class IgnorableWhitespace implements SaxBit, Serializable { 531 public final char[] ch; 532 533 public IgnorableWhitespace(char[] ch, int start, int length) { 534 this.ch = new char[length]; 536 System.arraycopy(ch, start, this.ch, 0, length); 537 } 538 539 public void send(ContentHandler contentHandler) throws SAXException { 540 contentHandler.ignorableWhitespace(ch, 0, ch.length); 541 } 542 543 public void dump(Writer writer) throws IOException { 544 writer.write("[IgnorableWhitespace] ch=" + new String (ch) + "\n"); 545 } 546 } 547 548 public final static class XMLizableBit implements SaxBit, Serializable { 549 public final XMLizable xml; 550 551 public XMLizableBit(XMLizable xml) { 552 this.xml = xml; 553 } 554 555 public void send(ContentHandler contentHandler) throws SAXException { 556 this.xml.toSAX(new EmbeddedXMLPipe(contentHandler)); 557 } 558 559 public void dump(Writer writer) throws IOException { 560 if (xml instanceof SaxBuffer) { 561 writer.write("[XMLizable] Begin nested SaxBuffer\n"); 562 ((SaxBuffer)xml).dump(writer); 563 writer.write("[XMLizable] End nested SaxBuffer\n"); 564 } else { 565 writer.write("[XMLizable] xml=" + xml + "\n"); 566 } 567 } 568 } 569 } 570 | Popular Tags |