View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.stream;
18  
19  import javax.xml.stream.Location;
20  import javax.xml.stream.XMLStreamException;
21  
22  import org.xml.sax.ContentHandler;
23  import org.xml.sax.InputSource;
24  import org.xml.sax.Locator;
25  import org.xml.sax.SAXException;
26  import org.xml.sax.SAXParseException;
27  
28  import org.springframework.xml.sax.AbstractXmlReader;
29  
30  /**
31   * Abstract base class for SAX <code>XMLReader</code> implementations that use StAX as a basis.
32   *
33   * @author Arjen Poutsma
34   * @see #setContentHandler(org.xml.sax.ContentHandler)
35   * @see #setDTDHandler(org.xml.sax.DTDHandler)
36   * @see #setEntityResolver(org.xml.sax.EntityResolver)
37   * @see #setErrorHandler(org.xml.sax.ErrorHandler)
38   * @since 1.0.0
39   */
40  public abstract class AbstractStaxXmlReader extends AbstractXmlReader {
41  
42      /**
43       * Parses the StAX XML reader passed at construction-time.
44       * <p/>
45       * <strong>Note</strong> that the given <code>InputSource</code> is not read, but ignored.
46       *
47       * @param ignored is ignored
48       * @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
49       */
50      public final void parse(InputSource ignored) throws SAXException {
51          parse();
52      }
53  
54      /**
55       * Parses the StAX XML reader passed at construction-time.
56       * <p/>
57       * <strong>Note</strong> that the given system identifier is not read, but ignored.
58       *
59       * @param ignored is ignored
60       * @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
61       */
62      public final void parse(String ignored) throws SAXException {
63          parse();
64      }
65  
66      private void parse() throws SAXException {
67          try {
68              parseInternal();
69          }
70          catch (XMLStreamException ex) {
71              Locator locator = null;
72              if (ex.getLocation() != null) {
73                  locator = new StaxLocator(ex.getLocation());
74              }
75              SAXParseException saxException = new SAXParseException(ex.getMessage(), locator, ex);
76              if (getErrorHandler() != null) {
77                  getErrorHandler().fatalError(saxException);
78              }
79              else {
80                  throw saxException;
81              }
82          }
83      }
84  
85      /**
86       * Sets the SAX <code>Locator</code> based on the given StAX <code>Location</code>.
87       *
88       * @param location the location
89       * @see ContentHandler#setDocumentLocator(org.xml.sax.Locator)
90       */
91      protected void setLocator(Location location) {
92          if (getContentHandler() != null) {
93              getContentHandler().setDocumentLocator(new StaxLocator(location));
94          }
95      }
96  
97      /** Template-method that parses the StAX reader passed at construction-time. */
98      protected abstract void parseInternal() throws SAXException, XMLStreamException;
99  
100     /**
101      * Implementation of the <code>Locator</code> interface that is based on a StAX <code>Location</code>.
102      *
103      * @see Locator
104      * @see Location
105      */
106     private static class StaxLocator implements Locator {
107 
108         private Location location;
109 
110         protected StaxLocator(Location location) {
111             this.location = location;
112         }
113 
114         public String getPublicId() {
115             return location.getPublicId();
116         }
117 
118         public String getSystemId() {
119             return location.getSystemId();
120         }
121 
122         public int getLineNumber() {
123             return location.getLineNumber();
124         }
125 
126         public int getColumnNumber() {
127             return location.getColumnNumber();
128         }
129     }
130 }