1 7 8 package org.dom4j.io; 9 10 import javax.xml.transform.sax.SAXSource ; 11 12 import org.dom4j.Document; 13 import org.dom4j.Node; 14 15 import org.xml.sax.InputSource ; 16 import org.xml.sax.XMLFilter ; 17 import org.xml.sax.XMLReader ; 18 19 28 public class DocumentSource extends SAXSource { 29 34 public static final String DOM4J_FEATURE 35 = "http://org.dom4j.io.DoucmentSource/feature"; 36 37 38 private XMLReader xmlReader = new SAXWriter(); 39 40 46 public DocumentSource(Node node) { 47 setDocument(node.getDocument()); 48 } 49 50 56 public DocumentSource(Document document) { 57 setDocument(document); 58 } 59 60 63 68 public Document getDocument() { 69 DocumentInputSource source = (DocumentInputSource) getInputSource(); 70 return source.getDocument(); 71 } 72 73 79 public void setDocument(Document document) { 80 super.setInputSource(new DocumentInputSource(document)); 81 } 82 83 86 91 public XMLReader getXMLReader() { 92 return xmlReader; 93 } 94 95 105 public void setInputSource(InputSource inputSource) 106 throws UnsupportedOperationException { 107 if (inputSource instanceof DocumentInputSource) { 108 super.setInputSource((DocumentInputSource) inputSource); 109 } else { 110 throw new UnsupportedOperationException (); 111 } 112 } 113 114 123 public void setXMLReader(XMLReader reader) 124 throws UnsupportedOperationException { 125 if (reader instanceof SAXWriter) { 126 this.xmlReader = (SAXWriter) reader; 127 } else if (reader instanceof XMLFilter ) { 128 XMLFilter filter = (XMLFilter ) reader; 129 130 while (true) { 131 XMLReader parent = filter.getParent(); 132 133 if (parent instanceof XMLFilter ) { 134 filter = (XMLFilter ) parent; 135 } else { 136 break; 137 } 138 } 139 140 filter.setParent(xmlReader); 142 xmlReader = filter; 143 } else { 144 throw new UnsupportedOperationException (); 145 } 146 } 147 } 148 149 185 | Popular Tags |