KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cyberneko > html > filters > DefaultFilter


1 /*
2  * (C) Copyright 2002-2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7
8 package org.cyberneko.html.filters;
9
10 import java.lang.reflect.InvocationTargetException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12
13 import org.cyberneko.html.HTMLComponent;
14
15 import org.apache.xerces.xni.Augmentations;
16 import org.apache.xerces.xni.NamespaceContext;
17 import org.apache.xerces.xni.QName;
18 import org.apache.xerces.xni.XMLAttributes;
19 import org.apache.xerces.xni.XMLDocumentHandler;
20 import org.apache.xerces.xni.XMLLocator;
21 import org.apache.xerces.xni.XMLResourceIdentifier;
22 import org.apache.xerces.xni.XMLString;
23 import org.apache.xerces.xni.XNIException;
24 import org.apache.xerces.xni.parser.XMLComponentManager;
25 import org.apache.xerces.xni.parser.XMLConfigurationException;
26 import org.apache.xerces.xni.parser.XMLDocumentFilter;
27 import org.apache.xerces.xni.parser.XMLDocumentSource;
28
29 /**
30  * This class implements a filter that simply passes document
31  * events to the next handler. It can be used as a base class to
32  * simplify the development of new document filters.
33  *
34  * @author Andy Clark
35  *
36  * @version $Id: DefaultFilter.java,v 1.7 2005/02/14 03:56:54 andyc Exp $
37  */

38 public class DefaultFilter
39     implements XMLDocumentFilter, HTMLComponent {
40
41     //
42
// Data
43
//
44

45     /** Document handler. */
46     protected XMLDocumentHandler fDocumentHandler;
47
48     /** Document source. */
49     protected XMLDocumentSource fDocumentSource;
50
51     //
52
// XMLDocumentSource methods
53
//
54

55     /** Sets the document handler. */
56     public void setDocumentHandler(XMLDocumentHandler handler) {
57         fDocumentHandler = handler;
58     } // setDocumentHandler(XMLDocumentHandler)
59

60     // @since Xerces 2.1.0
61

62     /** Returns the document handler. */
63     public XMLDocumentHandler getDocumentHandler() {
64         return fDocumentHandler;
65     } // getDocumentHandler():XMLDocumentHandler
66

67     /** Sets the document source. */
68     public void setDocumentSource(XMLDocumentSource source) {
69         fDocumentSource = source;
70     } // setDocumentSource(XMLDocumentSource)
71

72     /** Returns the document source. */
73     public XMLDocumentSource getDocumentSource() {
74         return fDocumentSource;
75     } // getDocumentSource():XMLDocumentSource
76

77     //
78
// XMLDocumentHandler methods
79
//
80

81     // since Xerces-J 2.2.0
82

83     /** Start document. */
84     public void startDocument(XMLLocator locator, String JavaDoc encoding,
85                               NamespaceContext nscontext, Augmentations augs)
86         throws XNIException {
87         if (fDocumentHandler != null) {
88             try {
89                 // NOTE: Hack to allow the default filter to work with
90
// old and new versions of the XNI document handler
91
// interface. -Ac
92
Class JavaDoc cls = fDocumentHandler.getClass();
93                 Class JavaDoc[] types = {
94                     XMLLocator.class, String JavaDoc.class,
95                     NamespaceContext.class, Augmentations.class
96                 };
97                 Method JavaDoc method = cls.getMethod("startDocument", types);
98                 Object JavaDoc[] params = {
99                     locator, encoding,
100                     nscontext, augs
101                 };
102                 method.invoke(fDocumentHandler, params);
103             }
104             catch (IllegalAccessException JavaDoc e) {
105                 throw new XNIException(e);
106             }
107             catch (InvocationTargetException JavaDoc e) {
108                 throw new XNIException(e);
109             }
110             catch (NoSuchMethodException JavaDoc e) {
111                 try {
112                     // NOTE: Hack to allow the default filter to work with
113
// old and new versions of the XNI document handler
114
// interface. -Ac
115
Class JavaDoc cls = fDocumentHandler.getClass();
116                     Class JavaDoc[] types = {
117                         XMLLocator.class, String JavaDoc.class, Augmentations.class
118                     };
119                     Method JavaDoc method = cls.getMethod("startDocument", types);
120                     Object JavaDoc[] params = {
121                         locator, encoding, augs
122                     };
123                     method.invoke(fDocumentHandler, params);
124                 }
125                 catch (NoSuchMethodException JavaDoc ex) {
126                     // NOTE: Should not happen!
127
throw new XNIException(ex);
128                 }
129                 catch (IllegalAccessException JavaDoc ex) {
130                     // NOTE: Should not happen!
131
throw new XNIException(ex);
132                 }
133                 catch (InvocationTargetException JavaDoc ex) {
134                     // NOTE: Should not happen!
135
throw new XNIException(ex);
136                 }
137             }
138         }
139     } // startDocument(XMLLocator,String,Augmentations)
140

141     // old methods
142

143     /** XML declaration. */
144     public void xmlDecl(String JavaDoc version, String JavaDoc encoding, String JavaDoc standalone, Augmentations augs)
145         throws XNIException {
146         if (fDocumentHandler != null) {
147             fDocumentHandler.xmlDecl(version, encoding, standalone, augs);
148         }
149     } // xmlDecl(String,String,String,Augmentations)
150

151     /** Doctype declaration. */
152     public void doctypeDecl(String JavaDoc root, String JavaDoc publicId, String JavaDoc systemId, Augmentations augs)
153         throws XNIException {
154         if (fDocumentHandler != null) {
155             fDocumentHandler.doctypeDecl(root, publicId, systemId, augs);
156         }
157     } // doctypeDecl(String,String,String,Augmentations)
158

159     /** Comment. */
160     public void comment(XMLString text, Augmentations augs)
161         throws XNIException {
162         if (fDocumentHandler != null) {
163             fDocumentHandler.comment(text, augs);
164         }
165     } // comment(XMLString,Augmentations)
166

167     /** Processing instruction. */
168     public void processingInstruction(String JavaDoc target, XMLString data, Augmentations augs)
169         throws XNIException {
170         if (fDocumentHandler != null) {
171             fDocumentHandler.processingInstruction(target, data, augs);
172         }
173     } // processingInstruction(String,XMLString,Augmentations)
174

175     /** Start element. */
176     public void startElement(QName element, XMLAttributes attributes, Augmentations augs)
177         throws XNIException {
178         if (fDocumentHandler != null) {
179             fDocumentHandler.startElement(element, attributes, augs);
180         }
181     } // startElement(QName,XMLAttributes,Augmentations)
182

183     /** Empty element. */
184     public void emptyElement(QName element, XMLAttributes attributes, Augmentations augs)
185         throws XNIException {
186         if (fDocumentHandler != null) {
187             fDocumentHandler.emptyElement(element, attributes, augs);
188         }
189     } // emptyElement(QName,XMLAttributes,Augmentations)
190

191     /** Characters. */
192     public void characters(XMLString text, Augmentations augs)
193         throws XNIException {
194         if (fDocumentHandler != null) {
195             fDocumentHandler.characters(text, augs);
196         }
197     } // characters(XMLString,Augmentations)
198

199     /** Ignorable whitespace. */
200     public void ignorableWhitespace(XMLString text, Augmentations augs)
201         throws XNIException {
202         if (fDocumentHandler != null) {
203             fDocumentHandler.ignorableWhitespace(text, augs);
204         }
205     } // ignorableWhitespace(XMLString,Augmentations)
206

207     /** Start general entity. */
208     public void startGeneralEntity(String JavaDoc name, XMLResourceIdentifier id, String JavaDoc encoding, Augmentations augs)
209         throws XNIException {
210         if (fDocumentHandler != null) {
211             fDocumentHandler.startGeneralEntity(name, id, encoding, augs);
212         }
213     } // startGeneralEntity(String,XMLResourceIdentifier,String,Augmentations)
214

215     /** Text declaration. */
216     public void textDecl(String JavaDoc version, String JavaDoc encoding, Augmentations augs)
217         throws XNIException {
218         if (fDocumentHandler != null) {
219             fDocumentHandler.textDecl(version, encoding, augs);
220         }
221     } // textDecl(String,String,Augmentations)
222

223     /** End general entity. */
224     public void endGeneralEntity(String JavaDoc name, Augmentations augs)
225         throws XNIException {
226         if (fDocumentHandler != null) {
227             fDocumentHandler.endGeneralEntity(name, augs);
228         }
229     } // endGeneralEntity(String,Augmentations)
230

231     /** Start CDATA section. */
232     public void startCDATA(Augmentations augs) throws XNIException {
233         if (fDocumentHandler != null) {
234             fDocumentHandler.startCDATA(augs);
235         }
236     } // startCDATA(Augmentations)
237

238     /** End CDATA section. */
239     public void endCDATA(Augmentations augs) throws XNIException {
240         if (fDocumentHandler != null) {
241             fDocumentHandler.endCDATA(augs);
242         }
243     } // endCDATA(Augmentations)
244

245     /** End element. */
246     public void endElement(QName element, Augmentations augs)
247         throws XNIException {
248         if (fDocumentHandler != null) {
249             fDocumentHandler.endElement(element, augs);
250         }
251     } // endElement(QName,Augmentations)
252

253     /** End document. */
254     public void endDocument(Augmentations augs) throws XNIException {
255         if (fDocumentHandler != null) {
256             fDocumentHandler.endDocument(augs);
257         }
258     } // endDocument(Augmentations)
259

260     // removed since Xerces-J 2.3.0
261

262     /** Start document. */
263     public void startDocument(XMLLocator locator, String JavaDoc encoding, Augmentations augs)
264         throws XNIException {
265         startDocument(locator, encoding, null, augs);
266     } // startDocument(XMLLocator,String,Augmentations)
267

268     /** Start prefix mapping. */
269     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri, Augmentations augs)
270         throws XNIException {
271         if (fDocumentHandler != null) {
272             Class JavaDoc cls = fDocumentHandler.getClass();
273             Class JavaDoc[] types = { String JavaDoc.class, String JavaDoc.class, Augmentations.class };
274             try {
275                 Method JavaDoc method = cls.getMethod("startPrefixMapping", types);
276                 Object JavaDoc[] args = { prefix, uri, augs };
277                 method.invoke(fDocumentHandler, args);
278             }
279             catch (NoSuchMethodException JavaDoc e) {
280                 // ignore
281
}
282             catch (IllegalAccessException JavaDoc e) {
283                 // ignore
284
}
285             catch (InvocationTargetException JavaDoc e) {
286                 // ignore
287
}
288         }
289     } // startPrefixMapping(String,String,Augmentations)
290

291     /** End prefix mapping. */
292     public void endPrefixMapping(String JavaDoc prefix, Augmentations augs)
293         throws XNIException {
294         if (fDocumentHandler != null) {
295             Class JavaDoc cls = fDocumentHandler.getClass();
296             Class JavaDoc[] types = { String JavaDoc.class, Augmentations.class };
297             try {
298                 Method JavaDoc method = cls.getMethod("endPrefixMapping", types);
299                 Object JavaDoc[] args = { prefix, augs };
300                 method.invoke(fDocumentHandler, args);
301             }
302             catch (NoSuchMethodException JavaDoc e) {
303                 // ignore
304
}
305             catch (IllegalAccessException JavaDoc e) {
306                 // ignore
307
}
308             catch (InvocationTargetException JavaDoc e) {
309                 // ignore
310
}
311         }
312     } // endPrefixMapping(String,Augmentations)
313

314     //
315
// HTMLComponent methods
316
//
317

318     /**
319      * Returns a list of feature identifiers that are recognized by
320      * this component. This method may return null if no features
321      * are recognized by this component.
322      */

323     public String JavaDoc[] getRecognizedFeatures() {
324         return null;
325     } // getRecognizedFeatures():String[]
326

327     /**
328      * Returns the default state for a feature, or null if this
329      * component does not want to report a default value for this
330      * feature.
331      */

332     public Boolean JavaDoc getFeatureDefault(String JavaDoc featureId) {
333         return null;
334     } // getFeatureDefault(String):Boolean
335

336     /**
337      * Returns a list of property identifiers that are recognized by
338      * this component. This method may return null if no properties
339      * are recognized by this component.
340      */

341     public String JavaDoc[] getRecognizedProperties() {
342         return null;
343     } // getRecognizedProperties():String[]
344

345     /**
346      * Returns the default state for a property, or null if this
347      * component does not want to report a default value for this
348      * property.
349      */

350     public Object JavaDoc getPropertyDefault(String JavaDoc propertyId) {
351         return null;
352     } // getPropertyDefault(String):Object
353

354     /**
355      * Resets the component. The component can query the component manager
356      * about any features and properties that affect the operation of the
357      * component.
358      *
359      * @param componentManager The component manager.
360      *
361      * @throws XNIException Thrown by component on initialization error.
362      */

363     public void reset(XMLComponentManager componentManager)
364         throws XMLConfigurationException {
365     } // reset(XMLComponentManager)
366

367     /**
368      * Sets the state of a feature. This method is called by the component
369      * manager any time after reset when a feature changes state.
370      * <p>
371      * <strong>Note:</strong> Components should silently ignore features
372      * that do not affect the operation of the component.
373      *
374      * @param featureId The feature identifier.
375      * @param state The state of the feature.
376      *
377      * @throws XMLConfigurationException Thrown for configuration error.
378      * In general, components should
379      * only throw this exception if
380      * it is <strong>really</strong>
381      * a critical error.
382      */

383     public void setFeature(String JavaDoc featureId, boolean state)
384         throws XMLConfigurationException {
385     } // setFeature(String,boolean)
386

387     /**
388      * Sets the value of a property. This method is called by the component
389      * manager any time after reset when a property changes value.
390      * <p>
391      * <strong>Note:</strong> Components should silently ignore properties
392      * that do not affect the operation of the component.
393      *
394      * @param propertyId The property identifier.
395      * @param value The value of the property.
396      *
397      * @throws XMLConfigurationException Thrown for configuration error.
398      * In general, components should
399      * only throw this exception if
400      * it is <strong>really</strong>
401      * a critical error.
402      */

403     public void setProperty(String JavaDoc propertyId, Object JavaDoc value)
404         throws XMLConfigurationException {
405     } // setProperty(String,Object)
406

407     //
408
// Protected static methods
409
//
410

411     /**
412      * Utility method for merging string arrays for recognized features
413      * and recognized properties.
414      */

415     protected static String JavaDoc[] merge(String JavaDoc[] array1, String JavaDoc[] array2) {
416
417         // shortcut merge
418
if (array1 == array2) {
419             return array1;
420         }
421         if (array1 == null) {
422             return array2;
423         }
424         if (array2 == null) {
425             return array1;
426         }
427
428         // full merge
429
String JavaDoc[] array3 = new String JavaDoc[array1.length + array2.length];
430         System.arraycopy(array1, 0, array3, 0, array1.length);
431         System.arraycopy(array2, 0, array3, array1.length, array2.length);
432
433         return array3;
434
435     } // merge(String[],String[]):String[]
436

437 } // class DefaultFilter
438
Popular Tags