1 56 57 package org.jdom.transform; 58 59 import java.util.*; 60 61 import javax.xml.transform.sax.*; 62 63 import org.jdom.*; 64 import org.jdom.input.*; 65 import org.xml.sax.*; 66 import org.xml.sax.ext.*; 67 import org.xml.sax.helpers.*; 68 69 103 public class JDOMResult extends SAXResult { 104 105 private static final String CVS_ID = 106 "@(#) $RCSfile: JDOMResult.java,v $ $Revision: 1.23 $ $Date: 2004/08/31 06:10:38 $ $Name: $"; 107 108 118 public final static String JDOM_FEATURE = 119 "http://org.jdom.transform.JDOMResult/feature"; 120 121 126 private Object result = null; 127 128 132 private boolean queried = false; 133 134 138 private JDOMFactory factory = null; 139 140 143 public JDOMResult() { 144 DocumentBuilder builder = new DocumentBuilder(); 146 147 super.setHandler(builder); 149 super.setLexicalHandler(builder); 150 } 151 152 168 public void setResult(List result) { 169 this.result = result; 170 this.queried = false; 171 } 172 173 185 public List getResult() { 186 List nodes = Collections.EMPTY_LIST; 187 188 this.retrieveResult(); 190 191 if (result instanceof List) { 192 nodes = (List)result; 193 } 194 else { 195 if ((result instanceof Document) && (queried == false)) { 196 List content = ((Document)result).getContent(); 197 nodes = new ArrayList(content.size()); 198 199 while (content.size() != 0) 200 { 201 Object o = content.remove(0); 202 nodes.add(o); 203 } 204 result = nodes; 205 } 206 } 207 queried = true; 208 209 return (nodes); 210 } 211 212 227 public void setDocument(Document document) { 228 this.result = document; 229 this.queried = false; 230 } 231 232 252 public Document getDocument() { 253 Document doc = null; 254 255 this.retrieveResult(); 257 258 if (result instanceof Document) { 259 doc = (Document)result; 260 } 261 else { 262 if ((result instanceof List) && (queried == false)) { 263 try { 265 JDOMFactory f = this.getFactory(); 266 if (f == null) { f = new DefaultJDOMFactory(); } 267 268 doc = f.document(null); 269 doc.setContent((List)result); 270 271 result = doc; 272 } 273 catch (RuntimeException ex1) { 274 } 277 } 278 } 279 queried = true; 280 281 return (doc); 282 } 283 284 295 public void setFactory(JDOMFactory factory) { 296 this.factory = factory; 297 } 298 299 309 public JDOMFactory getFactory() { 310 return this.factory; 311 } 312 313 317 private void retrieveResult() { 318 if (result == null) { 319 this.setResult(((DocumentBuilder)this.getHandler()).getResult()); 320 } 321 } 322 323 327 332 public void setHandler(ContentHandler handler) { } 333 334 344 public void setLexicalHandler(LexicalHandler handler) { } 345 346 347 351 private static class FragmentHandler extends SAXHandler { 352 356 private Element dummyRoot = new Element("root", null, null); 357 358 361 public FragmentHandler(JDOMFactory factory) { 362 super(factory); 363 364 this.pushElement(dummyRoot); 368 } 369 370 376 public List getResult() { 377 try { 380 this.flushCharacters(); 381 } 382 catch (SAXException e) { } 383 return this.getDetachedContent(dummyRoot); 384 } 385 386 394 private List getDetachedContent(Element elt) { 395 List content = elt.getContent(); 396 List nodes = new ArrayList(content.size()); 397 398 while (content.size() != 0) 399 { 400 Object o = content.remove(0); 401 nodes.add(o); 402 } 403 return (nodes); 404 } 405 } 406 407 411 private class DocumentBuilder extends XMLFilterImpl 412 implements LexicalHandler { 413 416 private FragmentHandler saxHandler = null; 417 418 422 private boolean startDocumentReceived = false; 423 424 427 public DocumentBuilder() { } 428 429 437 public List getResult() { 438 List result = null; 439 440 if (this.saxHandler != null) { 441 result = this.saxHandler.getResult(); 443 444 this.saxHandler = null; 446 447 this.startDocumentReceived = false; 449 } 450 return result; 451 } 452 453 private void ensureInitialization() throws SAXException { 454 if (this.startDocumentReceived == false) { 457 this.startDocument(); 458 } 459 } 460 461 465 475 public void startDocument() throws SAXException { 476 this.startDocumentReceived = true; 477 478 setResult(null); 480 481 this.saxHandler = new FragmentHandler(getFactory()); 486 super.setContentHandler(this.saxHandler); 487 488 super.startDocument(); 490 } 491 492 515 public void startElement(String nsURI, String localName, String qName, 516 Attributes atts) throws SAXException 517 { 518 this.ensureInitialization(); 519 super.startElement(nsURI, localName, qName, atts); 520 } 521 522 526 public void startPrefixMapping(String prefix, String uri) 527 throws SAXException { 528 this.ensureInitialization(); 529 super.startPrefixMapping(prefix, uri); 530 } 531 532 536 public void characters(char ch[], int start, int length) 537 throws SAXException { 538 this.ensureInitialization(); 539 super.characters(ch, start, length); 540 } 541 542 546 public void ignorableWhitespace(char ch[], int start, int length) 547 throws SAXException { 548 this.ensureInitialization(); 549 super.ignorableWhitespace(ch, start, length); 550 } 551 552 556 public void processingInstruction(String target, String data) 557 throws SAXException { 558 this.ensureInitialization(); 559 super.processingInstruction(target, data); 560 } 561 562 566 public void skippedEntity(String name) throws SAXException { 567 this.ensureInitialization(); 568 super.skippedEntity(name); 569 } 570 571 575 589 public void startDTD(String name, String publicId, String systemId) 590 throws SAXException { 591 this.ensureInitialization(); 592 this.saxHandler.startDTD(name, publicId, systemId); 593 } 594 595 601 public void endDTD() throws SAXException { 602 this.saxHandler.endDTD(); 603 } 604 605 615 public void startEntity(String name) throws SAXException { 616 this.ensureInitialization(); 617 this.saxHandler.startEntity(name); 618 } 619 620 628 public void endEntity(String name) throws SAXException { 629 this.saxHandler.endEntity(name); 630 } 631 632 638 public void startCDATA() throws SAXException { 639 this.ensureInitialization(); 640 this.saxHandler.startCDATA(); 641 } 642 643 649 public void endCDATA() throws SAXException { 650 this.saxHandler.endCDATA(); 651 } 652 653 663 public void comment(char ch[], int start, int length) 664 throws SAXException { 665 this.ensureInitialization(); 666 this.saxHandler.comment(ch, start, length); 667 } 668 } 669 } 670 671 | Popular Tags |