View Javadoc

1   package net.sf.tomp.pp;
2   
3   import java.io.BufferedReader;
4   import java.io.File;
5   import java.io.FileInputStream;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.io.InputStreamReader;
9   import java.io.Reader;
10  import java.util.HashMap;
11  import java.util.Map;
12  
13  import net.sf.tomp.xml.sax.ParametrizedXMLReader;
14  
15  import org.xml.sax.ContentHandler;
16  import org.xml.sax.DTDHandler;
17  import org.xml.sax.EntityResolver;
18  import org.xml.sax.ErrorHandler;
19  import org.xml.sax.InputSource;
20  import org.xml.sax.SAXException;
21  import org.xml.sax.SAXNotRecognizedException;
22  import org.xml.sax.SAXNotSupportedException;
23  
24  /***
25   * DOCUMENT ME!
26   * 
27   * @author $author$
28   * @version $Revision$
29   */
30  public abstract class AbstractXMLReader implements ParametrizedXMLReader {
31      /*** DOCUMENT ME! */
32      protected Map featureMap = new HashMap();
33  
34      /*** DOCUMENT ME! */
35      protected Map propertyMap = new HashMap();
36  
37      /*** DOCUMENT ME! */
38      protected EntityResolver entityResolver;
39  
40      /*** DOCUMENT ME! */
41      protected ContentHandler contentHandler;
42  
43      /*** DOCUMENT ME! */
44      protected ErrorHandler errorHandler;
45  
46      /*** DOCUMENT ME! */
47      protected DTDHandler dtdHandler;
48  
49      /***
50       * DOCUMENT ME!
51       * 
52       * @param k DOCUMENT ME!
53       * @param v DOCUMENT ME!
54       */
55      public abstract void setParameter(String k, Object v);
56  
57      /***
58       * DOCUMENT ME!
59       * 
60       * @param name DOCUMENT ME!
61       * @return DOCUMENT ME!
62       * @throws SAXNotRecognizedException DOCUMENT ME!
63       * @throws SAXNotSupportedException DOCUMENT ME!
64       */
65      public boolean getFeature(String name) throws SAXNotRecognizedException,
66              SAXNotSupportedException {
67          if (featureMap.containsKey(name)) {
68              return ((Boolean) featureMap.get(name)).booleanValue();
69          }
70  
71          return false;
72      }
73  
74      /***
75       * DOCUMENT ME!
76       * 
77       * @param name DOCUMENT ME!
78       * @param value DOCUMENT ME!
79       * @throws SAXNotRecognizedException DOCUMENT ME!
80       * @throws SAXNotSupportedException DOCUMENT ME!
81       */
82      public void setFeature(String name, boolean value)
83              throws SAXNotRecognizedException, SAXNotSupportedException {
84          featureMap.put(name, new Boolean(value));
85      }
86  
87      /***
88       * DOCUMENT ME!
89       * 
90       * @param name DOCUMENT ME!
91       * @return DOCUMENT ME!
92       * @throws SAXNotRecognizedException DOCUMENT ME!
93       * @throws SAXNotSupportedException DOCUMENT ME!
94       */
95      public Object getProperty(String name) throws SAXNotRecognizedException,
96              SAXNotSupportedException {
97          return propertyMap.get(name);
98      }
99  
100     /***
101      * DOCUMENT ME!
102      * 
103      * @param name DOCUMENT ME!
104      * @param value DOCUMENT ME!
105      * @throws SAXNotRecognizedException DOCUMENT ME!
106      * @throws SAXNotSupportedException DOCUMENT ME!
107      */
108     public void setProperty(String name, Object value)
109             throws SAXNotRecognizedException, SAXNotSupportedException {
110         propertyMap.put(name, value);
111     }
112 
113     /***
114      * DOCUMENT ME!
115      * 
116      * @param resolver DOCUMENT ME!
117      */
118     public void setEntityResolver(EntityResolver resolver) {
119         entityResolver = resolver;
120     }
121 
122     /***
123      * DOCUMENT ME!
124      * 
125      * @return DOCUMENT ME!
126      */
127     public EntityResolver getEntityResolver() {
128         return entityResolver;
129     }
130 
131     /***
132      * DOCUMENT ME!
133      * 
134      * @param handler DOCUMENT ME!
135      */
136     public void setContentHandler(ContentHandler handler) {
137         contentHandler = handler;
138     }
139 
140     /***
141      * DOCUMENT ME!
142      * 
143      * @return DOCUMENT ME!
144      */
145     public ContentHandler getContentHandler() {
146         return contentHandler;
147     }
148 
149     /***
150      * DOCUMENT ME!
151      * 
152      * @param handler DOCUMENT ME!
153      */
154     public void setErrorHandler(ErrorHandler handler) {
155         errorHandler = handler;
156     }
157 
158     /***
159      * DOCUMENT ME!
160      * 
161      * @return DOCUMENT ME!
162      */
163     public ErrorHandler getErrorHandler() {
164         return errorHandler;
165     }
166 
167     /***
168      * DOCUMENT ME!
169      * 
170      * @param handler DOCUMENT ME!
171      */
172     public void setDTDHandler(DTDHandler handler) {
173         dtdHandler = handler;
174     }
175 
176     /***
177      * DOCUMENT ME!
178      * 
179      * @return DOCUMENT ME!
180      */
181     public DTDHandler getDTDHandler() {
182         return dtdHandler;
183     }
184 
185     /***
186      * DOCUMENT ME!
187      * 
188      * @param systemId DOCUMENT ME!
189      * @throws IOException DOCUMENT ME!
190      * @throws SAXException DOCUMENT ME!
191      */
192     public void parse(String systemId) throws IOException, SAXException {
193         parse(new InputSource(systemId));
194     }
195 
196     /***
197      * DOCUMENT ME!
198      * 
199      * @param source DOCUMENT ME!
200      * @throws IOException DOCUMENT ME!
201      * @throws SAXException DOCUMENT ME!
202      */
203     public abstract void parse(InputSource source) throws IOException,
204             SAXException;
205 
206     /***
207      * DOCUMENT ME!
208      * 
209      * @param source DOCUMENT ME!
210      * @return DOCUMENT ME!
211      * @throws IOException DOCUMENT ME!
212      */
213     protected File getFile(InputSource source) throws IOException {
214         String systemid = source.getSystemId();
215 
216         if (systemid != null) {
217             return new File(systemid);
218         }
219 
220         return null;
221     }
222 
223     /***
224      * DOCUMENT ME!
225      * 
226      * @param source DOCUMENT ME!
227      * @return DOCUMENT ME!
228      * @throws IOException DOCUMENT ME!
229      */
230     protected InputStream getInputStream(InputSource source) throws IOException {
231         InputStream input = source.getByteStream();
232 
233         if (input != null) {
234             return input;
235         }
236 
237         String systemid = source.getSystemId();
238 
239         if (systemid != null) {
240             return new FileInputStream(systemid);
241         }
242 
243         return null;
244     }
245 
246     /***
247      * DOCUMENT ME!
248      * 
249      * @param source DOCUMENT ME!
250      * @return DOCUMENT ME!
251      * @throws IOException DOCUMENT ME!
252      */
253     protected BufferedReader getBufferedReader(InputSource source)
254             throws IOException {
255         Reader reader = source.getCharacterStream();
256 
257         if (reader != null) {
258             return new BufferedReader(reader);
259         }
260 
261         InputStream input = getInputStream(source);
262 
263         if (input != null) {
264             reader = new InputStreamReader(input);
265 
266             return new BufferedReader(reader);
267         }
268 
269         return null;
270     }
271 }
272 /*
273  * The contents of this file are subject to the Mozilla Public License Version
274  * 1.1 (the "License"); you may not use this file except in compliance with the
275  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
276  * Software distributed under the License is distributed on an "AS IS" basis,
277  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
278  * the specific language governing rights and limitations under the License. The
279  * Original Code is: all this file. The Initial Developer of the Original Code
280  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
281  */