1 20 21 27 28 package javax.xml.transform.stax; 29 30 import javax.xml.stream.XMLEventReader; 31 import javax.xml.stream.XMLStreamConstants; 32 import javax.xml.stream.XMLStreamException; 33 import javax.xml.stream.XMLStreamReader; 34 import javax.xml.stream.events.XMLEvent; 35 import javax.xml.transform.Source ; 36 37 60 public class StAXSource implements Source { 61 62 66 public static final String FEATURE = 67 "http://javax.xml.transform.stax.StAXSource/feature"; 68 69 70 private XMLEventReader xmlEventReader = null; 71 72 73 private XMLStreamReader xmlStreamReader = null; 74 75 76 private String systemId = null; 77 78 100 public StAXSource(final XMLEventReader xmlEventReader) 101 throws XMLStreamException { 102 103 if (xmlEventReader == null) { 104 throw new IllegalArgumentException ( 105 "StAXSource(XMLEventReader) with XMLEventReader == null"); 106 } 107 108 XMLEvent event = xmlEventReader.peek(); 115 int eventType = event.getEventType(); 116 if (eventType != XMLStreamConstants.START_DOCUMENT 117 && eventType != XMLStreamConstants.START_ELEMENT) { 118 throw new IllegalStateException ( 119 "StAXSource(XMLEventReader) with XMLEventReader " 120 + "not in XMLStreamConstants.START_DOCUMENT or " 121 + "XMLStreamConstants.START_ELEMENT state"); 122 } 123 124 this.xmlEventReader = xmlEventReader; 125 systemId = event.getLocation().getSystemId(); 126 } 127 128 148 public StAXSource(final XMLStreamReader xmlStreamReader) { 149 150 if (xmlStreamReader == null) { 151 throw new IllegalArgumentException ( 152 "StAXSource(XMLStreamReader) with XMLStreamReader == null"); 153 } 154 155 int eventType = xmlStreamReader.getEventType(); 156 if (eventType != XMLStreamConstants.START_DOCUMENT 157 && eventType != XMLStreamConstants.START_ELEMENT) { 158 throw new IllegalStateException ( 159 "StAXSource(XMLStreamReader) with XMLStreamReader" 160 + "not in XMLStreamConstants.START_DOCUMENT or " 161 + "XMLStreamConstants.START_ELEMENT state"); 162 } 163 164 this.xmlStreamReader = xmlStreamReader; 165 systemId = xmlStreamReader.getLocation().getSystemId(); 166 } 167 168 179 public XMLEventReader getXMLEventReader() { 180 181 return xmlEventReader; 182 } 183 184 195 public XMLStreamReader getXMLStreamReader() { 196 197 return xmlStreamReader; 198 } 199 200 215 public void setSystemId(final String systemId) { 216 217 throw new UnsupportedOperationException ( 218 "StAXSource#setSystemId(systemId) cannot set the " 219 + "system identifier for a StAXSource"); 220 } 221 222 235 public String getSystemId() { 236 237 return systemId; 238 } 239 } 240 | Popular Tags |