1 16 package org.outerj.daisy.xmlutil; 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 24 import java.util.List ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.Collections ; 28 import java.io.Serializable ; 29 import java.io.IOException ; 30 import java.io.Writer ; 31 32 37 public class SaxBuffer implements ContentHandler , LexicalHandler , Serializable { 38 39 42 protected List saxbits = new ArrayList (); 43 44 47 public SaxBuffer() { 48 } 49 50 53 public SaxBuffer(SaxBuffer saxBuffer) { 54 this.saxbits.addAll(saxBuffer.saxbits); 55 } 56 57 58 public void skippedEntity(String name) throws SAXException { 59 saxbits.add(new SkippedEntity(name)); 60 } 61 62 public void setDocumentLocator(Locator locator) { 63 } 65 66 public void ignorableWhitespace(char ch[], int start, int length) throws SAXException { 67 saxbits.add(new IgnorableWhitespace(ch, start, length)); 68 } 69 70 public void processingInstruction(String target, String data) throws SAXException { 71 saxbits.add(new PI(target, data)); 72 } 73 74 public void startDocument() throws SAXException { 75 saxbits.add(StartDocument.SINGLETON); 76 } 77 78 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { 79 saxbits.add(new StartElement(namespaceURI, localName, qName, atts)); 80 } 81 82 public void endPrefixMapping(String prefix) throws SAXException { 83 saxbits.add(new EndPrefixMapping(prefix)); 84 } 85 86 public void characters(char ch[], int start, int length) throws SAXException { 87 saxbits.add(new Characters(ch, start, length)); 88 } 89 90 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 91 saxbits.add(new EndElement(namespaceURI, localName, qName)); 92 } 93 94 public void endDocument() throws SAXException { 95 saxbits.add(EndDocument.SINGLETON); 96 } 97 98 public void startPrefixMapping(String prefix, String uri) throws SAXException { 99 saxbits.add(new StartPrefixMapping(prefix, uri)); 100 } 101 102 public void endCDATA() throws SAXException { 103 saxbits.add(EndCDATA.SINGLETON); 104 } 105 106 public void comment(char ch[], int start, int length) throws SAXException { 107 saxbits.add(new Comment(ch, start, length)); 108 } 109 110 public void startEntity(String name) throws SAXException { 111 saxbits.add(new StartEntity(name)); 112 } 113 114 public void endDTD() throws SAXException { 115 saxbits.add(EndDTD.SINGLETON); 116 } 117 118 public void startDTD(String name, String publicId, String systemId) throws SAXException { 119 saxbits.add(new StartDTD(name, publicId, systemId)); 120 } 121 122 public void startCDATA() throws SAXException { 123 saxbits.add(StartCDATA.SINGLETON); 124 } 125 126 public void endEntity(String name) throws SAXException { 127 saxbits.add(new EndEntity(name)); 128 } 129 130 131 134 protected final void addBit(SaxBit bit) { 135 saxbits.add(bit); 136 } 137 138 141 protected final Iterator bits() { 142 return saxbits.iterator(); 143 } 144 145 public boolean isEmpty() { 146 return saxbits.isEmpty(); 147 } 148 149 public List getBits() { 150 return Collections.unmodifiableList(saxbits); 151 } 152 153 public void toSAX(ContentHandler contentHandler) throws SAXException { 154 for (Iterator i = saxbits.iterator(); i.hasNext();) { 155 SaxBit saxbit = (SaxBit)i.next(); 156 saxbit.send(contentHandler); 157 } 158 } 159 160 163 public String toString() { 164 StringBuffer value = new StringBuffer (); 165 for (Iterator i = saxbits.iterator(); i.hasNext();) { 166 SaxBit saxbit = (SaxBit)i.next(); 167 if (saxbit instanceof Characters) { 168 ((Characters)saxbit).toString(value); 169 } 170 } 171 172 return value.toString(); 173 } 174 175 public void recycle() { 176 saxbits.clear(); 177 } 178 179 public void dump(Writer writer) throws IOException { 180 Iterator saxbitIt = saxbits.iterator(); 181 while (saxbitIt.hasNext()) { 182 SaxBit saxbit = (SaxBit)saxbitIt.next(); 183 saxbit.dump(writer); 184 } 185 writer.flush(); 186 } 187 188 191 interface SaxBit { 192 public void send(ContentHandler contentHandler) throws SAXException ; 193 public void dump(Writer writer) throws IOException ; 194 } 195 196 public static final class StartDocument implements SaxBit, Serializable { 197 public static final StartDocument SINGLETON = new StartDocument(); 198 public void send(ContentHandler contentHandler) throws SAXException { 199 contentHandler.startDocument(); 200 } 201 202 public void dump(Writer writer) throws IOException { 203 writer.write("[StartDocument]\n"); 204 } 205 } 206 207 public static final class EndDocument implements SaxBit, Serializable { 208 public static final EndDocument SINGLETON = new EndDocument(); 209 public void send(ContentHandler contentHandler) throws SAXException { 210 contentHandler.endDocument(); 211 } 212 213 public void dump(Writer writer) throws IOException { 214 writer.write("[EndDocument]\n"); 215 } 216 } 217 218 public static final class PI implements SaxBit, Serializable { 219 public final String target; 220 public final String data; 221 222 public PI(String target, String data) { 223 this.target = target; 224 this.data = data; 225 } 226 227 public void send(ContentHandler contentHandler) throws SAXException { 228 contentHandler.processingInstruction(target, data); 229 } 230 231 public void dump(Writer writer) throws IOException { 232 writer.write("[ProcessingInstruction] target=" + target + ",data=" + data + "\n"); 233 } 234 } 235 236 public static final class StartDTD implements SaxBit, Serializable { 237 public final String name; 238 public final String publicId; 239 public final String systemId; 240 241 public StartDTD(String name, String publicId, String systemId) { 242 this.name = name; 243 this.publicId = publicId; 244 this.systemId = systemId; 245 } 246 247 public void send(ContentHandler contentHandler) throws SAXException { 248 if (contentHandler instanceof LexicalHandler ) 249 ((LexicalHandler )contentHandler).startDTD(name, publicId, systemId); 250 } 251 252 public void dump(Writer writer) throws IOException { 253 writer.write("[StartDTD] name=" + name + ",publicId=" + publicId + ",systemId=" + systemId + "\n"); 254 } 255 } 256 257 public static final class EndDTD implements SaxBit, Serializable { 258 public static final EndDTD SINGLETON = new EndDTD(); 259 public void send(ContentHandler contentHandler) throws SAXException { 260 if (contentHandler instanceof LexicalHandler ) 261 ((LexicalHandler )contentHandler).endDTD(); 262 } 263 264 public void dump(Writer writer) throws IOException { 265 writer.write("[EndDTD]\n"); 266 } 267 } 268 269 public static final class StartEntity implements SaxBit, Serializable { 270 public final String name; 271 272 public StartEntity(String name) { 273 this.name = name; 274 } 275 276 public void send(ContentHandler contentHandler) throws SAXException { 277 if (contentHandler instanceof LexicalHandler ) 278 ((LexicalHandler )contentHandler).startEntity(name); 279 } 280 281 public void dump(Writer writer) throws IOException { 282 writer.write("[StartEntity] name=" + name + "\n"); 283 } 284 } 285 286 public static final class EndEntity implements SaxBit, Serializable { 287 public final String name; 288 289 public EndEntity(String name) { 290 this.name = name; 291 } 292 293 public void send(ContentHandler contentHandler) throws SAXException { 294 if (contentHandler instanceof LexicalHandler ) 295 ((LexicalHandler )contentHandler).endEntity(name); 296 } 297 298 public void dump(Writer writer) throws IOException { 299 writer.write("[EndEntity] name=" + name + "\n"); 300 } 301 } 302 303 public static final class SkippedEntity implements SaxBit, Serializable { 304 public final String name; 305 306 public SkippedEntity(String name) { 307 this.name = name; 308 } 309 310 public void send(ContentHandler contentHandler) throws SAXException { 311 contentHandler.skippedEntity(name); 312 } 313 314 public void dump(Writer writer) throws IOException { 315 writer.write("[SkippedEntity] name=" + name + "\n"); 316 } 317 } 318 319 public static final class StartPrefixMapping implements SaxBit, Serializable { 320 public final String prefix; 321 public final String uri; 322 323 public StartPrefixMapping(String prefix, String uri) { 324 this.prefix = prefix; 325 this.uri = uri; 326 } 327 328 public void send(ContentHandler contentHandler) throws SAXException { 329 contentHandler.startPrefixMapping(prefix, uri); 330 } 331 332 public void dump(Writer writer) throws IOException { 333 writer.write("[StartPrefixMapping] prefix=" + prefix + ",uri=" + uri + "\n"); 334 } 335 } 336 337 public static final class EndPrefixMapping implements SaxBit, Serializable { 338 public final String prefix; 339 340 public EndPrefixMapping(String prefix) { 341 this.prefix = prefix; 342 } 343 344 public void send(ContentHandler contentHandler) throws SAXException { 345 contentHandler.endPrefixMapping(prefix); 346 } 347 348 public void dump(Writer writer) throws IOException { 349 writer.write("[EndPrefixMapping] prefix=" + prefix + "\n"); 350 } 351 } 352 353 public static final class StartElement implements SaxBit, Serializable { 354 public final String namespaceURI; 355 public final String localName; 356 public final String qName; 357 public final Attributes attrs; 358 359 public StartElement(String namespaceURI, String localName, String qName, Attributes attrs) { 360 this.namespaceURI = namespaceURI; 361 this.localName = localName; 362 this.qName = qName; 363 this.attrs = new org.xml.sax.helpers.AttributesImpl (attrs); 364 } 365 366 public void send(ContentHandler contentHandler) throws SAXException { 367 contentHandler.startElement(namespaceURI, localName, qName, attrs); 368 } 369 370 public void dump(Writer writer) throws IOException { 371 writer.write("[StartElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n"); 372 for (int i = 0; i < attrs.getLength(); i++) { 373 writer.write(" [Attribute] namespaceURI=" + attrs.getURI(i) + ",localName=" + attrs.getLocalName(i) + ",qName=" + attrs.getQName(i) + ",type=" + attrs.getType(i) + ",value=" + attrs.getValue(i) + "\n"); 374 } 375 } 376 } 377 378 public static final class EndElement implements SaxBit, Serializable { 379 public final String namespaceURI; 380 public final String localName; 381 public final String qName; 382 383 public EndElement(String namespaceURI, String localName, String qName) { 384 this.namespaceURI = namespaceURI; 385 this.localName = localName; 386 this.qName = qName; 387 } 388 389 public void send(ContentHandler contentHandler) throws SAXException { 390 contentHandler.endElement(namespaceURI, localName, qName); 391 } 392 393 public void dump(Writer writer) throws IOException { 394 writer.write("[EndElement] namespaceURI=" + namespaceURI + ",localName=" + localName + ",qName=" + qName + "\n"); 395 } 396 } 397 398 public static final class Characters implements SaxBit, Serializable { 399 public final char[] ch; 400 401 public Characters(char[] ch, int start, int length) { 402 this.ch = new char[length]; 404 System.arraycopy(ch, start, this.ch, 0, length); 405 } 406 407 public void send(ContentHandler contentHandler) throws SAXException { 408 contentHandler.characters(ch, 0, ch.length); 409 } 410 411 public void toString(StringBuffer value) { 412 value.append(ch); 413 } 414 415 public void dump(Writer writer) throws IOException { 416 writer.write("[Characters] ch=" + new String (ch) + "\n"); 417 } 418 } 419 420 public static final class Comment implements SaxBit, Serializable { 421 public final char[] ch; 422 423 public Comment(char[] ch, int start, int length) { 424 this.ch = new char[length]; 426 System.arraycopy(ch, start, this.ch, 0, length); 427 } 428 429 public void send(ContentHandler contentHandler) throws SAXException { 430 if (contentHandler instanceof LexicalHandler ) 431 ((LexicalHandler )contentHandler).comment(ch, 0, ch.length); 432 } 433 434 public void dump(Writer writer) throws IOException { 435 writer.write("[Comment] ch=" + new String (ch) + "\n"); 436 } 437 } 438 439 public static final class StartCDATA implements SaxBit, Serializable { 440 public static final StartCDATA SINGLETON = new StartCDATA(); 441 public void send(ContentHandler contentHandler) throws SAXException { 442 if (contentHandler instanceof LexicalHandler ) 443 ((LexicalHandler )contentHandler).startCDATA(); 444 } 445 446 public void dump(Writer writer) throws IOException { 447 writer.write("[StartCDATA]\n"); 448 } 449 } 450 451 public static final class EndCDATA implements SaxBit, Serializable { 452 public static final EndCDATA SINGLETON = new EndCDATA(); 453 public void send(ContentHandler contentHandler) throws SAXException { 454 if (contentHandler instanceof LexicalHandler ) 455 ((LexicalHandler )contentHandler).endCDATA(); 456 } 457 458 public void dump(Writer writer) throws IOException { 459 writer.write("[EndCDATA]\n"); 460 } 461 } 462 463 public static final class IgnorableWhitespace implements SaxBit, Serializable { 464 public final char[] ch; 465 466 public IgnorableWhitespace(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.ignorableWhitespace(ch, 0, ch.length); 474 } 475 476 public void dump(Writer writer) throws IOException { 477 writer.write("IgnorableWhitespace] ch=" + new String (ch) + "\n"); 478 } 479 } 480 } 481 | Popular Tags |