View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package com.sun.org.apache.xerces.internal.util;
23  
24  import javax.xml.stream.XMLEventReader;
25  import javax.xml.stream.XMLStreamException;
26  import javax.xml.stream.XMLStreamReader;
27  
28  import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
29  
30  /**
31   * <p>An <code>XMLInputSource</code> analogue to <code>javax.xml.transform.stax.StAXSource</code>.</p>
32   *
33   * @version $Id: StAXInputSource.java,v 1.2 2010-10-26 23:01:17 joehw Exp $
34   */
35  public final class StAXInputSource extends XMLInputSource {
36  
37      private final XMLStreamReader fStreamReader;
38      private final XMLEventReader fEventReader;
39      private final boolean fConsumeRemainingContent;
40  
41      public StAXInputSource(XMLStreamReader source) {
42          this(source, false);
43      }
44  
45      public StAXInputSource(XMLStreamReader source, boolean consumeRemainingContent) {
46          super(null, source.getLocation().getSystemId(), null);
47          if (source == null) {
48              throw new IllegalArgumentException("XMLStreamReader parameter cannot be null.");
49          }
50          fStreamReader = source;
51          fEventReader = null;
52          fConsumeRemainingContent = consumeRemainingContent;
53      }
54  
55      public StAXInputSource(XMLEventReader source) {
56          this(source, false);
57      }
58  
59      public StAXInputSource(XMLEventReader source, boolean consumeRemainingContent) {
60          super(null, getEventReaderSystemId(source), null);
61          if (source == null) {
62              throw new IllegalArgumentException("XMLEventReader parameter cannot be null.");
63          }
64          fStreamReader = null;
65          fEventReader = source;
66          fConsumeRemainingContent = consumeRemainingContent;
67      }
68  
69      public XMLStreamReader getXMLStreamReader() {
70          return fStreamReader;
71      }
72  
73      public XMLEventReader getXMLEventReader() {
74          return fEventReader;
75      }
76  
77      public boolean shouldConsumeRemainingContent() {
78          return fConsumeRemainingContent;
79      }
80  
81      public void setSystemId(String systemId){
82          throw new UnsupportedOperationException("Cannot set the system ID on a StAXInputSource");
83      }
84  
85      private static String getEventReaderSystemId(XMLEventReader reader) {
86          try {
87              if (reader != null) {
88                  return reader.peek().getLocation().getSystemId();
89              }
90          }
91          catch (XMLStreamException e) {}
92          return null;
93      }
94  
95  } // StAXInputSource