Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
CustomSeparator |
|
| 1.5555555555555556;1.556 |
1 | /* $Id: CustomSeparator.java 17887 2010-01-12 21:17:18Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2006 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.util; | |
40 | ||
41 | /** | |
42 | * Base class for custom separators. | |
43 | * | |
44 | * <p>It can be instantiated directly, and then works like a separator in the | |
45 | * delimiter string. For that purpose you should use the delimiter string | |
46 | * in MyTokenizer, unless your token is wider than 32 characters. | |
47 | * | |
48 | * <p>You can also subclass this class to provide for more intricate recogition | |
49 | * of the tokens. It is known that this class has been subclassed to recognize | |
50 | * quoted strings, and balanced parentheses. | |
51 | * | |
52 | * <p>You should have this mental image of the tokenizing process:<nl> | |
53 | * <li>Reset is called. | |
54 | * <li>For each character, c, in the sequence being tokenized:<ul> | |
55 | * <li>addChar(c) is called for each separator in the tokenizer. | |
56 | * <li>if addChar returns true, break.</ul> | |
57 | * <li>hasFreePart is checked to see if something follows. If true:<ul> | |
58 | * <li>endChar(c) is called for each following character. | |
59 | * <li>if/when endChar returns true, break.</ul> | |
60 | * <li>tokenLength is checked to see how far back in the sequence the token | |
61 | * begun. If there are characters before that but after the last token, | |
62 | * then they are made a token and this token is saved and returned next. | |
63 | * </nl> | |
64 | * | |
65 | * @author Michael Stockman | |
66 | * @since 0.11.2 | |
67 | * @see MyTokenizer | |
68 | */ | |
69 | public class CustomSeparator { | |
70 | private char pattern[]; | |
71 | private char match[]; | |
72 | ||
73 | /** | |
74 | * This constructor is only availible to subclasses of this class. | |
75 | * If you use it you should also override {@link #addChar addChar} | |
76 | * to recognize when your separator should that control. If you don't, | |
77 | * then you may block all other separators. | |
78 | */ | |
79 | 900 | protected CustomSeparator() { |
80 | 900 | pattern = new char[0]; |
81 | 900 | match = pattern; |
82 | 900 | } |
83 | ||
84 | /** | |
85 | * This constructor creates a new custom separator that matches the | |
86 | * character start. Unless you override {@link #addChar addChar}, the | |
87 | * default behaviour is to return false in addChar until start is | |
88 | * encountered and then hasFreePart returns false. | |
89 | * | |
90 | * @param start The start character. | |
91 | */ | |
92 | 3600 | public CustomSeparator(char start) { |
93 | 3600 | pattern = new char[1]; |
94 | 3600 | pattern[0] = start; |
95 | 3600 | match = new char[pattern.length]; |
96 | 3600 | } |
97 | ||
98 | /** | |
99 | * This constructor creates a new custom separator that matches the | |
100 | * string start. Unless you override {@link #addChar addChar}, the | |
101 | * default behaviour is to return false in addChar until start is | |
102 | * encountered and then hasFreePart returns false. | |
103 | * | |
104 | * @param start The start String. | |
105 | */ | |
106 | 0 | public CustomSeparator(String start) { |
107 | 0 | pattern = start.toCharArray(); |
108 | 0 | match = new char[pattern.length]; |
109 | 0 | } |
110 | ||
111 | /** | |
112 | * Called to reset the separator before staring to look for a new | |
113 | * token. | |
114 | */ | |
115 | public void reset() { | |
116 | int i; | |
117 | 0 | for (i = 0; i < match.length; i++) |
118 | 0 | match[i] = 0; |
119 | 0 | } |
120 | ||
121 | /** | |
122 | * Returns the length of the matched token. It is not required to be | |
123 | * meaningful unless addChar has returned true and hasFreePart | |
124 | * returned false or endChar returned true. | |
125 | * | |
126 | * @return The length of the matched token. | |
127 | */ | |
128 | public int tokenLength() { | |
129 | 0 | return pattern.length; |
130 | } | |
131 | ||
132 | /** | |
133 | * Called to allow you to decide if you want to capure control of | |
134 | * the matching process. If you return true, then | |
135 | * {@link #hasFreePart hasFreePart} will be checked to see if you | |
136 | * expect more things to follow. | |
137 | * | |
138 | * <p>The default behaviour is to return false until the character | |
139 | * or String given as parameter to the constructor has been matched. | |
140 | * | |
141 | * @param c The next character in the sequence being tokenized. | |
142 | * @return true to gain control of the matching, false to continue | |
143 | * matching. | |
144 | */ | |
145 | public boolean addChar(char c) { | |
146 | int i; | |
147 | 0 | for (i = 0; i < match.length - 1; i++) |
148 | 0 | match[i] = match[i + 1]; |
149 | 0 | match[match.length - 1] = c; |
150 | 0 | for (i = 0; i < match.length; i++) |
151 | 0 | if (match[i] != pattern[i]) |
152 | 0 | return false; |
153 | 0 | return true; |
154 | } | |
155 | ||
156 | /** | |
157 | * Called to check if more characters are expected to follow after | |
158 | * addChar has returned true. If true, then any following characters | |
159 | * will be fed to endChar until endChar returns true. | |
160 | * | |
161 | * <p>The default behaviour is to return false. | |
162 | * | |
163 | * @return true to continue feeding characters to endChar or false. | |
164 | */ | |
165 | public boolean hasFreePart() { | |
166 | 0 | return false; |
167 | } | |
168 | ||
169 | /** | |
170 | * Called to check if more characters are expected in the free part of | |
171 | * the token. | |
172 | * | |
173 | * @param c The next character in the sequence being tokenized. | |
174 | * @return true to indicate that the token is complete, or false to | |
175 | * continue feeding characters through endChar. | |
176 | */ | |
177 | public boolean endChar(char c) { | |
178 | 0 | return true; |
179 | } | |
180 | ||
181 | /** | |
182 | * Called to how many characters the CustomSeparator read after | |
183 | * the end of the separator. This allows them to see beyond the | |
184 | * end, but these characters will be fed to the separators again | |
185 | * when looking for the next token so be careful. | |
186 | * | |
187 | * @return the number of characters that were read after the end | |
188 | * of the token had been read. | |
189 | */ | |
190 | public int getPeekCount() { | |
191 | 0 | return 0; |
192 | } | |
193 | } | |
194 |