001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.stream;
018:
019: import javax.xml.stream.Location;
020: import javax.xml.stream.XMLStreamException;
021:
022: import org.springframework.xml.sax.AbstractXmlReader;
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.InputSource;
025: import org.xml.sax.Locator;
026: import org.xml.sax.SAXException;
027: import org.xml.sax.SAXParseException;
028:
029: /**
030: * Abstract base class for SAX <code>XMLReader</code> implementations that use StAX as a basis.
031: *
032: * @author Arjen Poutsma
033: * @see #setContentHandler(org.xml.sax.ContentHandler)
034: * @see #setDTDHandler(org.xml.sax.DTDHandler)
035: * @see #setEntityResolver(org.xml.sax.EntityResolver)
036: * @see #setErrorHandler(org.xml.sax.ErrorHandler)
037: * @since 1.0.0
038: */
039: public abstract class AbstractStaxXmlReader extends AbstractXmlReader {
040:
041: /**
042: * Parses the StAX XML reader passed at construction-time.
043: * <p/>
044: * <strong>Note</strong> that the given <code>InputSource</code> is not read, but ignored.
045: *
046: * @param ignored is ignored
047: * @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
048: */
049: public final void parse(InputSource ignored) throws SAXException {
050: parse();
051: }
052:
053: /**
054: * Parses the StAX XML reader passed at construction-time.
055: * <p/>
056: * <strong>Note</strong> that the given system identifier is not read, but ignored.
057: *
058: * @param ignored is ignored
059: * @throws SAXException A SAX exception, possibly wrapping a <code>XMLStreamException</code>
060: */
061: public final void parse(String ignored) throws SAXException {
062: parse();
063: }
064:
065: private void parse() throws SAXException {
066: try {
067: parseInternal();
068: } catch (XMLStreamException ex) {
069: SAXParseException saxException = new SAXParseException(ex
070: .getMessage(), null, null, ex.getLocation()
071: .getLineNumber(), ex.getLocation()
072: .getColumnNumber(), ex);
073: if (getErrorHandler() != null) {
074: getErrorHandler().fatalError(saxException);
075: } else {
076: throw saxException;
077: }
078: }
079: }
080:
081: /**
082: * Sets the SAX <code>Locator</code> based on the given StAX <code>Location</code>.
083: *
084: * @param location the location
085: * @see ContentHandler#setDocumentLocator(org.xml.sax.Locator)
086: */
087: protected void setLocator(Location location) {
088: if (getContentHandler() != null) {
089: getContentHandler().setDocumentLocator(
090: new StaxLocator(location));
091: }
092: }
093:
094: /** Template-method that parses the StAX reader passed at construction-time. */
095: protected abstract void parseInternal() throws SAXException,
096: XMLStreamException;
097:
098: /**
099: * Implementation of the <code>Locator</code> interface that is based on a StAX <code>Location</code>.
100: *
101: * @see Locator
102: * @see Location
103: */
104: private static class StaxLocator implements Locator {
105:
106: private Location location;
107:
108: protected StaxLocator(Location location) {
109: this .location = location;
110: }
111:
112: public String getPublicId() {
113: return location.getPublicId();
114: }
115:
116: public String getSystemId() {
117: return location.getSystemId();
118: }
119:
120: public int getLineNumber() {
121: return location.getLineNumber();
122: }
123:
124: public int getColumnNumber() {
125: return location.getColumnNumber();
126: }
127: }
128: }
|