1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 // $Id: StAXSource.java 670394 2008-06-22 18:50:36Z mrglavas $ 19 20 package javax.xml.transform.stax; 21 22 import javax.xml.stream.XMLEventReader; 23 import javax.xml.stream.XMLStreamConstants; 24 import javax.xml.stream.XMLStreamException; 25 import javax.xml.stream.XMLStreamReader; 26 import javax.xml.stream.events.XMLEvent; 27 import javax.xml.transform.Source; 28 29 public class StAXSource implements Source { 30 31 public static final String FEATURE = "http://javax.xml.transform.stax.StAXSource/feature"; 32 33 private final XMLStreamReader xmlStreamReader; 34 private final XMLEventReader xmlEventReader; 35 private final String systemId; 36 37 public StAXSource(XMLStreamReader xmlStreamReader) { 38 if (xmlStreamReader == null) { 39 throw new IllegalArgumentException("XMLStreamReader cannot be null."); 40 } 41 final int event = xmlStreamReader.getEventType(); 42 if (event != XMLStreamConstants.START_DOCUMENT && 43 event != XMLStreamConstants.START_ELEMENT) { 44 throw new IllegalStateException("The state of the XMLStreamReader must be START_DOCUMENT or START_ELEMENT"); 45 } 46 this.xmlStreamReader = xmlStreamReader; 47 this.xmlEventReader = null; 48 this.systemId = xmlStreamReader.getLocation().getSystemId(); 49 } 50 51 public StAXSource(XMLEventReader xmlEventReader) 52 throws XMLStreamException { 53 if (xmlEventReader == null) { 54 throw new IllegalArgumentException("XMLEventReader cannot be null."); 55 } 56 final XMLEvent event = xmlEventReader.peek(); 57 if (!event.isStartDocument() && 58 !event.isStartElement()) { 59 throw new IllegalStateException("The state of the XMLEventReader must be START_DOCUMENT or START_ELEMENT"); 60 } 61 this.xmlStreamReader = null; 62 this.xmlEventReader = xmlEventReader; 63 this.systemId = event.getLocation().getSystemId(); 64 } 65 66 public XMLStreamReader getXMLStreamReader() { 67 return xmlStreamReader; 68 } 69 70 public XMLEventReader getXMLEventReader() { 71 return xmlEventReader; 72 } 73 74 public String getSystemId() { 75 return systemId; 76 } 77 78 public void setSystemId(String systemId) { 79 throw new UnsupportedOperationException("Setting systemId is not supported."); 80 } 81 }