KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > text > PatternEntry


1 /*
2  * @(#)PatternEntry.java 1.25 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
10  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
11  *
12  * The original version of this source code and documentation is copyrighted
13  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
14  * materials are provided under terms of a License Agreement between Taligent
15  * and Sun. This technology is protected by multiple US and International
16  * patents. This notice and attribution to Taligent may not be removed.
17  * Taligent is a registered trademark of Taligent, Inc.
18  *
19  */

20
21 package java.text;
22
23 import java.lang.Character JavaDoc;
24
25 /**
26  * Utility class for normalizing and merging patterns for collation.
27  * This is to be used with MergeCollation for adding patterns to an
28  * existing rule table.
29  * @see MergeCollation
30  * @version 1.25 12/19/03
31  * @author Mark Davis, Helena Shih
32  */

33
34 class PatternEntry {
35     /**
36      * Gets the current extension, quoted
37      */

38     public void appendQuotedExtension(StringBuffer JavaDoc toAddTo) {
39         appendQuoted(extension,toAddTo);
40     }
41
42     /**
43      * Gets the current chars, quoted
44      */

45     public void appendQuotedChars(StringBuffer JavaDoc toAddTo) {
46         appendQuoted(chars,toAddTo);
47     }
48
49     /**
50      * WARNING this is used for searching in a Vector.
51      * Because Vector.indexOf doesn't take a comparator,
52      * this method is ill-defined and ignores strength.
53      */

54     public boolean equals(Object JavaDoc obj) {
55         if (obj == null) return false;
56         PatternEntry JavaDoc other = (PatternEntry JavaDoc) obj;
57         boolean result = chars.equals(other.chars);
58         return result;
59     }
60
61     public int hashCode() {
62         return chars.hashCode();
63     }
64
65     /**
66      * For debugging.
67      */

68     public String JavaDoc toString() {
69         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
70         addToBuffer(result, true, false, null);
71         return result.toString();
72     }
73
74     /**
75      * Gets the strength of the entry.
76      */

77     final int getStrength() {
78         return strength;
79     }
80
81     /**
82      * Gets the expanding characters of the entry.
83      */

84     final String JavaDoc getExtension() {
85         return extension;
86     }
87
88     /**
89      * Gets the core characters of the entry.
90      */

91     final String JavaDoc getChars() {
92         return chars;
93     }
94
95     // ===== privates =====
96

97     void addToBuffer(StringBuffer JavaDoc toAddTo,
98                      boolean showExtension,
99                      boolean showWhiteSpace,
100                      PatternEntry JavaDoc lastEntry)
101     {
102         if (showWhiteSpace && toAddTo.length() > 0)
103             if (strength == Collator.PRIMARY || lastEntry != null)
104                 toAddTo.append('\n');
105             else
106                 toAddTo.append(' ');
107         if (lastEntry != null) {
108             toAddTo.append('&');
109             if (showWhiteSpace)
110                 toAddTo.append(' ');
111             lastEntry.appendQuotedChars(toAddTo);
112             appendQuotedExtension(toAddTo);
113             if (showWhiteSpace)
114                 toAddTo.append(' ');
115         }
116         switch (strength) {
117         case Collator.IDENTICAL: toAddTo.append('='); break;
118         case Collator.TERTIARY: toAddTo.append(','); break;
119         case Collator.SECONDARY: toAddTo.append(';'); break;
120         case Collator.PRIMARY: toAddTo.append('<'); break;
121         case RESET: toAddTo.append('&'); break;
122         case UNSET: toAddTo.append('?'); break;
123         }
124         if (showWhiteSpace)
125             toAddTo.append(' ');
126         appendQuoted(chars,toAddTo);
127         if (showExtension && extension.length() != 0) {
128             toAddTo.append('/');
129             appendQuoted(extension,toAddTo);
130         }
131     }
132
133     static void appendQuoted(String JavaDoc chars, StringBuffer JavaDoc toAddTo) {
134         boolean inQuote = false;
135         char ch = chars.charAt(0);
136         if (Character.isSpaceChar(ch)) {
137             inQuote = true;
138             toAddTo.append('\'');
139         } else {
140           if (PatternEntry.isSpecialChar(ch)) {
141                 inQuote = true;
142                 toAddTo.append('\'');
143             } else {
144                 switch (ch) {
145                     case 0x0010: case '\f': case '\r':
146                     case '\t': case '\n': case '@':
147                     inQuote = true;
148                     toAddTo.append('\'');
149                     break;
150                 case '\'':
151                     inQuote = true;
152                     toAddTo.append('\'');
153                     break;
154                 default:
155                     if (inQuote) {
156                         inQuote = false; toAddTo.append('\'');
157                     }
158                     break;
159                 }
160            }
161         }
162         toAddTo.append(chars);
163         if (inQuote)
164             toAddTo.append('\'');
165     }
166
167     //========================================================================
168
// Parsing a pattern into a list of PatternEntries....
169
//========================================================================
170

171     PatternEntry(int strength,
172                  StringBuffer JavaDoc chars,
173                  StringBuffer JavaDoc extension)
174     {
175         this.strength = strength;
176         this.chars = chars.toString();
177         this.extension = (extension.length() > 0) ? extension.toString()
178                                                   : "";
179     }
180
181     static class Parser {
182         private String JavaDoc pattern;
183         private int i;
184         
185         public Parser(String JavaDoc pattern) {
186             this.pattern = pattern;
187             this.i = 0;
188         }
189         
190         public PatternEntry JavaDoc next() throws ParseException JavaDoc {
191             int newStrength = UNSET;
192             
193             newChars.setLength(0);
194             newExtension.setLength(0);
195             
196             boolean inChars = true;
197             boolean inQuote = false;
198         mainLoop:
199             while (i < pattern.length()) {
200                 char ch = pattern.charAt(i);
201                 if (inQuote) {
202                     if (ch == '\'') {
203                         inQuote = false;
204                     } else {
205                         if (newChars.length() == 0) newChars.append(ch);
206                         else if (inChars) newChars.append(ch);
207                         else newExtension.append(ch);
208                     }
209                 } else switch (ch) {
210                 case '=': if (newStrength != UNSET) break mainLoop;
211                     newStrength = Collator.IDENTICAL; break;
212                 case ',': if (newStrength != UNSET) break mainLoop;
213                     newStrength = Collator.TERTIARY; break;
214                 case ';': if (newStrength != UNSET) break mainLoop;
215                     newStrength = Collator.SECONDARY; break;
216                 case '<': if (newStrength != UNSET) break mainLoop;
217                     newStrength = Collator.PRIMARY; break;
218                 case '&': if (newStrength != UNSET) break mainLoop;
219                     newStrength = RESET; break;
220                 case '\t':
221                 case '\n':
222                 case '\f':
223                 case '\r':
224                 case ' ': break; // skip whitespace TODO use Character
225
case '/': inChars = false; break;
226                 case '\'':
227                     inQuote = true;
228                     ch = pattern.charAt(++i);
229                     if (newChars.length() == 0) newChars.append(ch);
230                     else if (inChars) newChars.append(ch);
231                     else newExtension.append(ch);
232                     break;
233                 default:
234                     if (newStrength == UNSET) {
235                         throw new ParseException JavaDoc
236                             ("missing char (=,;<&) : " +
237                              pattern.substring(i,
238                                 (i+10 < pattern.length()) ?
239                                  i+10 : pattern.length()),
240                              i);
241                     }
242                     if (PatternEntry.isSpecialChar(ch) && (inQuote == false))
243                         throw new ParseException JavaDoc
244                             ("Unquoted punctuation character : " + Integer.toString(ch, 16), i);
245                     if (inChars) {
246                         newChars.append(ch);
247                     } else {
248                         newExtension.append(ch);
249                     }
250                     break;
251                 }
252                 i++;
253             }
254             if (newStrength == UNSET)
255                 return null;
256             if (newChars.length() == 0) {
257                 throw new ParseException JavaDoc
258                     ("missing chars (=,;<&): " +
259                       pattern.substring(i,
260                           (i+10 < pattern.length()) ?
261                            i+10 : pattern.length()),
262                      i);
263             }
264             
265             return new PatternEntry JavaDoc(newStrength, newChars, newExtension);
266         }
267
268         // We re-use these objects in order to improve performance
269
private StringBuffer JavaDoc newChars = new StringBuffer JavaDoc();
270         private StringBuffer JavaDoc newExtension = new StringBuffer JavaDoc();
271         
272     }
273         
274     static boolean isSpecialChar(char ch) {
275         return ((ch == '\u0020') ||
276         ((ch <= '\u002F') && (ch >= '\u0022')) ||
277                 ((ch <= '\u003F') && (ch >= '\u003A')) ||
278                 ((ch <= '\u0060') && (ch >= '\u005B')) ||
279                 ((ch <= '\u007E') && (ch >= '\u007B')));
280     }
281     
282
283     static final int RESET = -2;
284     static final int UNSET = -1;
285
286     int strength = UNSET;
287     String JavaDoc chars = "";
288     String JavaDoc extension = "";
289 }
290
Popular Tags