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.sax;
18  
19  import org.xml.sax.ContentHandler;
20  import org.xml.sax.DTDHandler;
21  import org.xml.sax.EntityResolver;
22  import org.xml.sax.ErrorHandler;
23  import org.xml.sax.SAXNotRecognizedException;
24  import org.xml.sax.SAXNotSupportedException;
25  import org.xml.sax.XMLReader;
26  import org.xml.sax.ext.LexicalHandler;
27  
28  /**
29   * Abstract base class for SAX <code>XMLReader</code> implementations. Contains properties as defined in [email protected]
30   * XMLReader}, and does not recognize any features
31   *
32   * @author Arjen Poutsma
33   * @see #setContentHandler(org.xml.sax.ContentHandler)
34   * @see #setDTDHandler(org.xml.sax.DTDHandler)
35   * @see #setEntityResolver(org.xml.sax.EntityResolver)
36   * @see #setErrorHandler(org.xml.sax.ErrorHandler)
37   * @since 1.0.0
38   */
39  public abstract class AbstractXmlReader implements XMLReader {
40  
41      private DTDHandler dtdHandler;
42  
43      private ContentHandler contentHandler;
44  
45      private EntityResolver entityResolver;
46  
47      private ErrorHandler errorHandler;
48  
49      private LexicalHandler lexicalHandler;
50  
51      public ContentHandler getContentHandler() {
52          return contentHandler;
53      }
54  
55      public void setContentHandler(ContentHandler contentHandler) {
56          this.contentHandler = contentHandler;
57      }
58  
59      public void setDTDHandler(DTDHandler dtdHandler) {
60          this.dtdHandler = dtdHandler;
61      }
62  
63      public DTDHandler getDTDHandler() {
64          return dtdHandler;
65      }
66  
67      public EntityResolver getEntityResolver() {
68          return entityResolver;
69      }
70  
71      public void setEntityResolver(EntityResolver entityResolver) {
72          this.entityResolver = entityResolver;
73      }
74  
75      public ErrorHandler getErrorHandler() {
76          return errorHandler;
77      }
78  
79      public void setErrorHandler(ErrorHandler errorHandler) {
80          this.errorHandler = errorHandler;
81      }
82  
83      protected LexicalHandler getLexicalHandler() {
84          return lexicalHandler;
85      }
86  
87      /**
88       * Throws a <code>SAXNotRecognizedException</code> exception.
89       *
90       * @throws org.xml.sax.SAXNotRecognizedException
91       *          always
92       */
93      public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
94          throw new SAXNotRecognizedException(name);
95      }
96  
97      /**
98       * Throws a <code>SAXNotRecognizedException</code> exception.
99       *
100      * @throws SAXNotRecognizedException always
101      */
102     public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
103         throw new SAXNotRecognizedException(name);
104     }
105 
106     /**
107      * Throws a <code>SAXNotRecognizedException</code> exception when the given property does not signify a lexical
108      * handler. The property name for a lexical handler is <code>http://xml.org/sax/properties/lexical-handler</code>.
109      */
110     public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
111         if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
112             return lexicalHandler;
113         }
114         else {
115             throw new SAXNotRecognizedException(name);
116         }
117     }
118 
119     /**
120      * Throws a <code>SAXNotRecognizedException</code> exception when the given property does not signify a lexical
121      * handler. The property name for a lexical handler is <code>http://xml.org/sax/properties/lexical-handler</code>.
122      */
123     public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
124         if ("http://xml.org/sax/properties/lexical-handler".equals(name)) {
125             lexicalHandler = (LexicalHandler) value;
126         }
127         else {
128             throw new SAXNotRecognizedException(name);
129         }
130     }
131 }