01: package net.sf.saxon.event;
02:
03: import net.sf.saxon.trans.XPathException;
04: import net.sf.saxon.om.FastStringBuffer;
05:
06: /**
07: * The CommentStripper class is a filter that removes all comments and processing instructions.
08: * It also concatenates text nodes that are split by comments and PIs
09: * @author Michael H. Kay
10: */
11:
12: public class CommentStripper extends ProxyReceiver {
13:
14: private FastStringBuffer buffer = new FastStringBuffer(200);
15:
16: /**
17: * Default constructor for use in subclasses
18: */
19:
20: public CommentStripper() {
21: }
22:
23: public void startElement(int nameCode, int typeCode,
24: int locationId, int properties) throws XPathException {
25: flush();
26: super .startElement(nameCode, typeCode, locationId, properties);
27: }
28:
29: /**
30: * Callback interface for SAX: not for application use
31: */
32:
33: public void endElement() throws XPathException {
34: flush();
35: super .endElement();
36: }
37:
38: /**
39: * Callback interface for SAX: not for application use
40: */
41:
42: public void characters(CharSequence chars, int locationId,
43: int properties) throws XPathException {
44: buffer.append(chars);
45: }
46:
47: /**
48: * Remove comments
49: */
50:
51: public void comment(CharSequence chars, int locationId,
52: int properties) {
53: }
54:
55: /**
56: * Remove processing instructions
57: */
58:
59: public void processingInstruction(String name, CharSequence data,
60: int locationId, int properties) {
61: }
62:
63: /**
64: * Flush the character buffer
65: */
66:
67: private void flush() throws XPathException {
68: super .characters(buffer, 0, 0);
69: buffer.setLength(0);
70: }
71:
72: }
73:
74: //
75: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
76: // you may not use this file except in compliance with the License. You may obtain a copy of the
77: // License at http://www.mozilla.org/MPL/
78: //
79: // Software distributed under the License is distributed on an "AS IS" basis,
80: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
81: // See the License for the specific language governing rights and limitations under the License.
82: //
83: // The Original Code is: all this file.
84: //
85: // The Initial Developer of the Original Code is Michael H. Kay.
86: //
87: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
88: //
89: // Contributor(s): none.
90: //
|