KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > WhitespaceFilter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.xml;
17
18 import org.apache.cocoon.xml.AbstractXMLPipe;
19
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /**
25  * A SAX filter to remove whitespace character, which disturb the
26  * XML matching process.
27  *
28  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
29  * @version CVS $Id: WhitespaceFilter.java 30932 2004-07-29 17:35:38Z vgritsenko $
30  */

31 public class WhitespaceFilter extends AbstractXMLPipe {
32     private StringBuffer JavaDoc buffer = null;
33
34     /**
35      * Create a new WhitespaceFilter.
36      *
37      * @param handler Content handler.
38      */

39     public WhitespaceFilter(ContentHandler JavaDoc handler) {
40         setContentHandler(handler);
41     }
42
43     /**
44      * Receive notification of character data.
45      */

46     public void characters(char c[], int start, int len) throws SAXException JavaDoc {
47         if (contentHandler==null) {
48             return;
49         }
50
51         if (buffer==null) {
52             buffer = new StringBuffer JavaDoc();
53         }
54
55         buffer.append(c, start, len);
56     }
57
58     /**
59      * Receive notification of ignorable whitespace in element content.
60      */

61     public void ignorableWhitespace(char c[], int start,
62                                     int len) throws SAXException JavaDoc {
63         // ignore
64
}
65
66     /**
67      * Receive notification of the beginning of an element.
68      */

69     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
70                              String JavaDoc qName,
71                              Attributes JavaDoc atts) throws SAXException JavaDoc {
72
73         pushText();
74         contentHandler.startElement(namespaceURI, localName, qName, atts);
75     }
76
77     /**
78      * Receive notification of the end of an element.
79      */

80     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
81         throws SAXException JavaDoc {
82
83         pushText();
84         contentHandler.endElement(uri, loc, raw);
85     }
86
87     /**
88      * Receive notification of a processing instruction.
89      */

90     public void processingInstruction(String JavaDoc target, String JavaDoc data)
91         throws SAXException JavaDoc {
92
93         pushText();
94         contentHandler.processingInstruction(target, data);
95     }
96
97     /**
98      * Report an XML comment anywhere in the document.
99      *
100      * @param ch An array holding the characters in the comment.
101      * @param start The starting position in the array.
102      * @param len The number of characters to use from the array.
103      */

104     public void comment(char ch[], int start, int len)
105         throws SAXException JavaDoc {
106   
107         pushText();
108         super.comment(ch, start, len);
109     }
110
111
112     public void pushText() throws SAXException JavaDoc {
113
114         if (buffer!=null) {
115             String JavaDoc text = buffer.toString();
116
117             StringBuffer JavaDoc normalized = new StringBuffer JavaDoc();
118
119             for(int i=0; i<text.length(); i++) {
120                 if (Character.isWhitespace(text.charAt(i))) {
121                     normalized.append(' ');
122                     while (((i+1)<text.length()) && (Character.isWhitespace(text.charAt(i+1))))
123                         i++;
124                 } else {
125                     normalized.append(text.charAt(i));
126                 }
127             }
128
129             text = normalized.toString().trim();
130
131             if (text.length()>0) {
132                 contentHandler.characters(text.toCharArray(), 0,
133                                           text.length());
134             }
135
136             buffer = null;
137         }
138     }
139 }
140
Popular Tags