KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > CommentStripper


1 package net.sf.saxon.event;
2
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.om.FastStringBuffer;
5
6 /**
7   * The CommentStripper class is a filter that removes all comments and processing instructions.
8   * It also concatenates text nodes that are split by comments and PIs
9   * @author Michael H. Kay
10   */

11
12   
13 public class CommentStripper extends ProxyReceiver {
14
15     private FastStringBuffer buffer = new FastStringBuffer(200);
16
17     /**
18     * Default constructor for use in subclasses
19     */

20     
21     public CommentStripper() {}
22     
23
24     public void startElement (int nameCode, int typeCode, int locationId, int properties)
25     throws XPathException {
26         flush();
27         super.startElement(nameCode, typeCode, locationId, properties);
28     }
29
30     /**
31     * Callback interface for SAX: not for application use
32     */

33
34     public void endElement () throws XPathException {
35         flush();
36         super.endElement();
37     }
38
39     /**
40     * Callback interface for SAX: not for application use
41     */

42
43     public void characters (CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
44         buffer.append(chars);
45     }
46
47     /**
48     * Remove comments
49     */

50     
51     public void comment (CharSequence JavaDoc chars, int locationId, int properties) {}
52     
53     /**
54     * Remove processing instructions
55     */

56     
57     public void processingInstruction(String JavaDoc name, CharSequence JavaDoc data, int locationId, int properties) {}
58
59     /**
60     * Flush the character buffer
61     */

62     
63     private void flush() throws XPathException {
64         super.characters(buffer, 0, 0);
65         buffer.setLength(0);
66     }
67
68 }
69
70 //
71
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
72
// you may not use this file except in compliance with the License. You may obtain a copy of the
73
// License at http://www.mozilla.org/MPL/
74
//
75
// Software distributed under the License is distributed on an "AS IS" basis,
76
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
77
// See the License for the specific language governing rights and limitations under the License.
78
//
79
// The Original Code is: all this file.
80
//
81
// The Initial Developer of the Original Code is Michael H. Kay.
82
//
83
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
84
//
85
// Contributor(s): none.
86
//
87
Popular Tags