1 56 57 package org.jdom.transform; 58 59 import java.io.*; 60 import java.util.*; 61 62 import javax.xml.transform.sax.*; 63 64 import org.jdom.*; 65 import org.jdom.output.*; 66 import org.xml.sax.*; 67 68 101 public class JDOMSource extends SAXSource { 102 103 private static final String CVS_ID = 104 "@(#) $RCSfile: JDOMSource.java,v $ $Revision: 1.18 $ $Date: 2004/08/31 04:43:48 $ $Name: $"; 105 106 117 public final static String JDOM_FEATURE = 118 "http://org.jdom.transform.JDOMSource/feature"; 119 120 126 private XMLReader xmlReader = null; 127 128 137 public JDOMSource(Document source) { 138 setDocument(source); 139 } 140 141 150 public JDOMSource(List source) { 151 setNodes(source); 152 } 153 154 163 public JDOMSource(Element source) { 164 List nodes = new ArrayList(); 165 nodes.add(source); 166 167 setNodes(nodes); 168 } 169 170 181 public void setDocument(Document source) { 182 super.setInputSource(new JDOMInputSource(source)); 183 } 184 185 193 public Document getDocument() { 194 Object src = ((JDOMInputSource)getInputSource()).getSource(); 195 Document doc = null; 196 197 if (src instanceof Document) { 198 doc = (Document)src; 199 } 200 return doc; 201 } 202 203 214 public void setNodes(List source) { 215 super.setInputSource(new JDOMInputSource(source)); 216 } 217 218 226 public List getNodes() { 227 Object src = ((JDOMInputSource)getInputSource()).getSource(); 228 List nodes = null; 229 230 if (src instanceof List) { 231 nodes = (List)src; 232 } 233 return nodes; 234 } 235 236 237 241 253 public void setInputSource(InputSource inputSource) 254 throws UnsupportedOperationException { 255 throw new UnsupportedOperationException (); 256 } 257 258 275 public void setXMLReader(XMLReader reader) 276 throws UnsupportedOperationException { 277 if (reader instanceof XMLFilter) { 278 XMLFilter filter = (XMLFilter)reader; 280 while (filter.getParent() instanceof XMLFilter) { 281 filter = (XMLFilter)(filter.getParent()); 282 } 283 filter.setParent(new DocumentReader()); 284 285 this.xmlReader = reader; 287 } 288 else { 289 throw new UnsupportedOperationException (); 290 } 291 } 292 293 303 public XMLReader getXMLReader() { 304 if (this.xmlReader == null) { 305 this.xmlReader = new DocumentReader(); 306 } 307 return this.xmlReader; 308 } 309 310 314 324 private static class JDOMInputSource extends InputSource { 325 328 private Object source = null; 329 330 335 public JDOMInputSource(Document document) { 336 this.source = document; 337 } 338 339 344 public JDOMInputSource(List nodes) { 345 this.source = nodes; 346 } 347 348 353 public Object getSource() { 354 return source; 355 } 356 357 361 374 public void setCharacterStream(Reader characterStream) 375 throws UnsupportedOperationException { 376 throw new UnsupportedOperationException (); 377 } 378 379 392 public Reader getCharacterStream() { 393 Object src = this.getSource(); 394 Reader reader = null; 395 396 if (src instanceof Document) { 397 reader = new StringReader( 400 new XMLOutputter().outputString((Document)src)); 401 } 402 else { 403 if (src instanceof List) { 404 reader = new StringReader( 405 new XMLOutputter().outputString((List)src)); 406 } 407 } 409 return reader; 410 } 411 } 412 413 417 425 private static class DocumentReader extends SAXOutputter 426 implements XMLReader { 427 430 public DocumentReader() { 431 super(); 432 } 433 434 438 450 public void parse(String systemId) throws SAXNotSupportedException { 451 throw new SAXNotSupportedException( 452 "Only JDOM Documents are supported as input"); 453 } 454 455 471 public void parse(InputSource input) throws SAXException { 472 if (input instanceof JDOMInputSource) { 473 try { 474 Object source = ((JDOMInputSource)input).getSource(); 475 if (source instanceof Document) { 476 this.output((Document)source); 477 } 478 else { 479 this.output((List)source); 480 } 481 } 482 catch (JDOMException e) { 483 throw new SAXException(e.getMessage(), e); 484 } 485 } 486 else { 487 throw new SAXNotSupportedException( 488 "Only JDOM Documents are supported as input"); 489 } 490 } 491 } 492 } 493 494 | Popular Tags |