KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > util > CharNormalizeFilter


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.util;
24
25 import org.xml.sax.Attributes JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.XMLReader JavaDoc;
28
29 /**
30  * An XMLFilter implementation used to perform empty character events removal
31  * and consecutive character events aggregation.
32  */

33 public class CharNormalizeFilter extends DefaultXMLFilter
34 {
35 private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
36 private static final String JavaDoc RCSName = "$Name: $";
37     private boolean lastEventIsChar = false;
38     private StringBuffer JavaDoc chars = new StringBuffer JavaDoc();
39     private boolean removeWhitespace = false; // if true removes all character data containing only whitespaces
40

41     /** Constructor
42      * @param reader The parent XMLReader that is filtered.
43      * @param removeWhitespace If true, packets of characters containing only
44      * whitespace characters are removed. On the other
45      * hand, aggregation is always active.
46      */

47     public CharNormalizeFilter(XMLReader JavaDoc reader,boolean removeWhitespace) {
48         super(reader);
49         this.removeWhitespace = removeWhitespace;
50     }
51
52     ////////////////////////////////////////////////////////////////////
53
// Implementation of LexicalHandler interface.
54
////////////////////////////////////////////////////////////////////
55
public void comment (char ch[], int start, int length)
56       throws SAXException JavaDoc
57     {
58         saveWaitingChars();
59         super.comment(ch, start, length);
60     }
61
62     ////////////////////////////////////////////////////////////////////
63
// Implementation of ContentHandler interface.
64
////////////////////////////////////////////////////////////////////
65
public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
66     throws SAXException JavaDoc
67     {
68         saveWaitingChars();
69         super.startElement(namespaceURI, localName, qName, atts);
70     }
71     
72     public void endElement (String JavaDoc uri, String JavaDoc localName, String JavaDoc rawName)
73     throws SAXException JavaDoc
74     {
75         saveWaitingChars();
76         super.endElement(uri, localName, rawName);
77     }
78
79     public void processingInstruction (String JavaDoc target, String JavaDoc data)
80     throws SAXException JavaDoc
81     {
82         saveWaitingChars();
83         super.processingInstruction(target, data);
84     }
85     
86     public void characters (char ch[], int start, int length)
87     throws SAXException JavaDoc
88     {
89         lastEventIsChar = true;
90         
91         // removes (if option set) white space data passed by the parser because there is no DTD
92
if (removeWhitespace && isWhitespace(ch, start, length))
93             return;
94         
95         // Store the characters in the StringBuffer
96
chars.append(ch, start, length);
97     }
98     
99     ////////////////////////
100
// Private Utilities
101
////////////////////////
102

103     private void saveWaitingChars() throws SAXException JavaDoc
104     {
105       if (chars.length() > 0) {
106         char array[] = chars.toString().toCharArray();
107         super.characters(array, 0, array.length);
108         
109         lastEventIsChar = false;
110         chars.setLength(0);
111       }
112     }
113     
114     /**
115      * Test if the character array is whitespace according to XML 1.0 specifications.
116      *
117      * @return the result of the test.
118      */

119     private boolean isWhitespace(char ch[], int start, int length)
120     {
121         int i, max = start+length;
122         for (i = start; i < max; i++)
123         {
124             switch (ch[i])
125             {
126                 case 0x20 : case 0x9 : case 0xD : case 0xA :
127                     break;
128                 default :
129                     return false;
130             }
131         }
132         return true; // Only found WS : text ignored
133
}
134     
135 }
136
Popular Tags