KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > test > WhitespaceFilter


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.test;
10
11 import org.apache.commons.logging.Log;
12
13 import org.xml.sax.*;
14
15 /**
16  * A SAX filter to remove whitespace character, which disturb the XML matching process.
17  *
18  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
19  * @version CVS $Id: WhitespaceFilter.java,v 1.6 2004/01/08 11:30:53 benedikta Exp $
20  */

21 public class WhitespaceFilter implements ContentHandler
22 {
23   private ContentHandler handler = null;
24   private StringBuffer JavaDoc buffer = null;
25   private Log log = null;
26
27   /**
28    * Create a new WhitespaceFilter.
29    *
30    * @param handler Content handler.
31    */

32   public WhitespaceFilter(ContentHandler handler)
33   {
34     this.handler = handler;
35   }
36
37   public WhitespaceFilter(ContentHandler handler, Log log)
38   {
39     this.handler = handler;
40     this.log = log;
41   }
42
43   public void setLog(Log log)
44   {
45     this.log = log;
46   }
47
48   /**
49    * Receive an object for locating the origin of SAX document events.
50    */

51   public void setDocumentLocator(Locator locator)
52   {
53     if (log!=null)
54       log.debug("[SAX] DocumentLocator "+locator);
55
56     handler.setDocumentLocator(locator);
57   }
58
59   /**
60    * Receive notification of the beginning of a document.
61    */

62   public void startDocument() throws SAXException
63   {
64     if (log!=null)
65       log.debug("[SAX] StartDocument");
66
67     handler.startDocument();
68   }
69
70   /**
71    * Receive notification of the end of a document.
72    */

73   public void endDocument() throws SAXException
74   {
75     if (log!=null)
76       log.debug("[SAX] EndDocument");
77
78     handler.endDocument();
79   }
80
81   /**
82    * Receive notification of character data.
83    */

84   public void characters(char[] c, int start, int len)
85   {
86     if (log!=null)
87       log.debug("[SAX] Characters "+new String JavaDoc(c, start, len));
88
89     if (buffer==null)
90       buffer = new StringBuffer JavaDoc();
91
92     buffer.append(c, start, len);
93   }
94
95   /**
96    * Receive notification of ignorable whitespace in element content.
97    */

98   public void ignorableWhitespace(char[] c, int start, int len)
99   {
100     if (log!=null)
101       log.debug("[SAX] Ignorable Whitespace "+new String JavaDoc(c, start, len));
102
103     // ignore
104
}
105
106   /**
107    * Receive notification of the beginning of an element.
108    */

109   public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts)
110     throws SAXException
111   {
112     if (log!=null)
113       log.debug("[SAX] Start Element "+localName+" Namespace "+namespaceURI);
114
115     pushText();
116     handler.startElement(namespaceURI, localName, qName, atts);
117   }
118
119   /**
120    * Receive notification of the end of an element.
121    */

122   public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc raw)
123     throws SAXException
124   {
125     if (log!=null)
126       log.debug("[SAX] End Element "+localName+" Namespace "+namespaceURI);
127
128     pushText();
129     handler.endElement(namespaceURI, localName, raw);
130   }
131
132   /**
133    * Receive notification of a processing instruction.
134    */

135   public void processingInstruction(String JavaDoc target, String JavaDoc data)
136     throws SAXException
137   {
138     if (log!=null)
139       log.debug("[SAX] Processing Instruction Target "+target+" Data "+data);
140
141     pushText();
142     handler.processingInstruction(target, data);
143   }
144
145   /**
146    * Begin the scope of a prefix-URI Namespace mapping.
147    */

148   public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
149     throws SAXException
150   {
151     if (log!=null)
152       log.debug("[SAX] Start Prefix Mapping "+prefix+" Namespace "+uri);
153
154     handler.startPrefixMapping(prefix, uri);
155   }
156
157   /**
158    * End the scope of a prefix-URI mapping.
159    */

160   public void endPrefixMapping(String JavaDoc prefix) throws SAXException
161   {
162     if (log!=null)
163       log.debug("[SAX] Start Prefix Mapping "+prefix);
164
165     handler.endPrefixMapping(prefix);
166   }
167
168   public void skippedEntity(String JavaDoc name) throws SAXException
169   {
170     if (log!=null)
171       log.debug("[SAX] Skipped Entity "+name);
172
173     handler.skippedEntity(name);
174   }
175
176   public void pushText() throws SAXException
177   {
178     if (buffer!=null)
179     {
180       String JavaDoc text = buffer.toString();
181
182       StringBuffer JavaDoc normalized = new StringBuffer JavaDoc();
183
184       for (int i = 0; i<text.length(); i++)
185       {
186         if (Character.isWhitespace(text.charAt(i)))
187         {
188           normalized.append(' ');
189           while (((i+1)<text.length()) && (Character.isWhitespace(text.charAt(i+1))))
190             i++;
191         }
192         else
193           normalized.append(text.charAt(i));
194       }
195
196       text = normalized.toString().trim();
197
198       if (text.length()>0)
199         handler.characters(text.toCharArray(), 0, text.length());
200
201       buffer = null;
202     }
203   }
204 }
205
Popular Tags