View Javadoc

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