KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > susebox > jtopas > spi > StandardWhitespaceHandler


1 /*
2  * StandardWhitespaceHandler.java: default implementation of WhitespaceHandler
3  *
4  * Copyright (C) 2002 Heiko Blau
5  *
6  * This file belongs to the JTopas Library.
7  * JTopas is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or (at your
10  * option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License along
18  * with JTopas. If not, write to the
19  *
20  * Free Software Foundation, Inc.
21  * 59 Temple Place, Suite 330,
22  * Boston, MA 02111-1307
23  * USA
24  *
25  * or check the Internet: http://www.fsf.org
26  *
27  * Contact:
28  * email: heiko@susebox.de
29  */

30
31 package de.susebox.jtopas.spi;
32
33
34 //-----------------------------------------------------------------------------
35
// Imports
36
//
37
import de.susebox.jtopas.TokenizerProperties;
38 import de.susebox.jtopas.TokenizerException;
39 import de.susebox.jtopas.Flags;
40
41
42 //-----------------------------------------------------------------------------
43
// class StandardWhitespaceHandler
44
//
45

46 /**<p>
47  * Simple implementation of the {@link WhitespaceHandler} interface. This class
48  * works only with the {@link de.susebox.jtopas.TokenizerProperties} interface
49  * methods and is aware of changes in these properties. It does not cache any
50  * information and is therefore a more or less slow way to handle whitespaces.
51  *</p><p>
52  * This class is a bridge between arbitrary {@link de.susebox.jtopas.Tokenizer}
53  * implementations using the SPI interface {@link WhitespaceHandler} and any
54  * {@link de.susebox.jtopas.TokenizerProperties} implementation that does not
55  * implement the <code>WhitespaceHandler</code> interface itself.
56  *</p>
57  *
58  * @see WhitespaceHandler
59  * @see de.susebox.jtopas.Tokenizer
60  * @see de.susebox.jtopas.TokenizerProperties
61  * @author Heiko Blau
62  */

63 public class StandardWhitespaceHandler implements WhitespaceHandler {
64   
65   /**
66    * The constructor takes the {@link de.susebox.jtopas.TokenizerProperties}
67    * that provide the whitespaces.
68    *
69    * @param props the {@link de.susebox.jtopas.TokenizerProperties} to take the
70    * whitespaces from
71    */

72   public StandardWhitespaceHandler(TokenizerProperties props) {
73     _properties = props;
74   }
75   
76   /**
77    * This method checks if the given character is a whitespace.
78    *
79    * @param testChar check this character
80    * @return <code>true</code> if the given character is a whitespace,
81    * <code>false</code> otherwise
82    */

83   public boolean isWhitespace(char testChar) {
84     String JavaDoc whitespaces;
85     
86     if (_properties != null && (whitespaces = _properties.getWhitespaces()) != null) {
87       if (_properties.isFlagSet(Flags.F_NO_CASE)) {
88         return whitespaces.toLowerCase().indexOf(Character.toLowerCase(testChar)) >= 0;
89       } else {
90         return whitespaces.indexOf(testChar) >= 0;
91       }
92     } else {
93       return false;
94     }
95   }
96      
97   /**
98    * This method detects the number of whitespace characters the data range given
99    * through the {@link DataProvider} parameter starts with.
100    *
101    * @param dataProvider the source to get the data range from
102    * @return number of whitespace characters starting from the given offset
103    * @throws NullPointerException if no {@link DataProvider} is given
104    * @see DataProvider
105    */

106   public int countLeadingWhitespaces(DataProvider dataProvider) throws NullPointerException JavaDoc {
107     int len = 0;
108     int maxChars = dataProvider.getLength();
109     
110     while (len < maxChars && isWhitespace(dataProvider.getCharAt(len))) {
111       len++;
112     }
113     return len;
114   }
115
116   /**
117    * If a {@link de.susebox.jtopas.Tokenizer} performs line counting, it is often
118    * nessecary to know if newline characters is considered to be a whitespace.
119    * See {@link WhitespaceHandler} for details.
120    *
121    * @return <code>true</code> if newline characters are in the current whitespace set,
122    * <code>false</code> otherwise
123    *
124    */

125   public boolean newlineIsWhitespace() {
126     String JavaDoc whitespaces;
127     boolean isWhitespace;
128     
129     if (_properties != null && (whitespaces = _properties.getWhitespaces()) != null) {
130       return newlineIsWhitespace(whitespaces);
131     } else {
132       return false;
133     }
134   }
135   
136   //---------------------------------------------------------------------------
137
// Implementation
138
//
139

140   /**
141    * Check a set that may contain ranges
142    *
143    * @param set the whitespace set
144    */

145   private boolean newlineIsWhitespace(String JavaDoc set) {
146     int len = (set != null) ? set.length() : 0;
147     char start, end, setChar;
148     boolean crFound = false;
149     boolean lfFound = false;
150     
151     for (int ii = 0; ii < len; ++ii) {
152       switch (setChar = set.charAt(ii)) {
153       case '-':
154         start = (ii > 0) ? set.charAt(ii - 1) : 0;
155         end = (ii < len - 1) ? set.charAt(ii + 1) : 0xFFFF;
156         if ('\n' >= start && '\n' <= end) {
157           lfFound = true;
158         }
159         if ('\r' >= start && '\r' <= end) {
160           crFound = true;
161         }
162         ii += 2;
163         break;
164         
165       case '\r':
166         crFound = true;
167         break;
168         
169       case '\n':
170         lfFound = true;
171         break;
172         
173       case '\\':
174         ii++;
175         break;
176       }
177       
178       // both characters found ?
179
if (crFound && lfFound) {
180         return true;
181       }
182     }
183     
184     // not found
185
return false;
186   }
187   
188   //---------------------------------------------------------------------------
189
// Members
190
//
191

192   /**
193    * The {@link TokenizerProperties} that provide the whitespaces and the
194    * control flags.
195    */

196   private TokenizerProperties _properties = null;
197 }
198
Popular Tags