KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > RE13


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util;
20
21 import java.util.*;
22
23
24 /** Implementation of translate regular expression methods.
25  * Works on 1.3 though we could use 1.4's java.util.regex - this is faster anyway.
26  * @author Jaroslav Tulach
27  */

28 final class RE13 implements Utilities.RE {
29     /** root of the match automata */
30     private Object JavaDoc[] root;
31
32     /** list of strings to convert to */
33     private String JavaDoc[] results;
34
35     /** Parses line of text to two parts: the key and the rest
36      */

37     public String JavaDoc[] readPair(String JavaDoc line) {
38         int indx = line.indexOf('#');
39
40         if (indx != -1) {
41             line = line.substring(0, indx).trim();
42         }
43
44         indx = line.indexOf('=');
45
46         if (indx == -1) {
47             indx = line.indexOf(' ');
48         }
49
50         if (indx == -1) {
51             return null;
52         }
53
54         return new String JavaDoc[] { line.substring(0, indx).trim(), line.substring(indx + 1).trim() };
55     }
56
57     public String JavaDoc convert(String JavaDoc pattern) {
58         Object JavaDoc[] item = root;
59         int resIndex = -1;
60         int resLength = 0;
61
62         int lenOfPattern = 0;
63         int indx = 0;
64 ALL:
65         for (;;) {
66             if (item.length == 0) {
67                 break;
68             }
69
70             // update the result if this item represents a result
71
Object JavaDoc last = item[item.length - 1];
72
73             if (last instanceof Integer JavaDoc) {
74                 // remember the number
75
resIndex = ((Integer JavaDoc) last).intValue();
76                 resLength += lenOfPattern;
77                 lenOfPattern = 0;
78             }
79
80             if (indx >= pattern.length()) {
81                 // no next text to compare, stop the search
82
break;
83             }
84
85             char f = pattern.charAt(indx++);
86
87             // find next suitable item
88
for (int i = 0; i < item.length; i++) {
89                 if (item[i] instanceof String JavaDoc && (((String JavaDoc) item[i]).charAt(0) == f)) {
90                     // we have found the branch to possibly follow, now check
91
String JavaDoc s = (String JavaDoc) item[i];
92
93                     for (int j = 1; j < s.length(); j++, indx++) {
94                         if ((pattern.length() <= indx) || (pattern.charAt(indx) != s.charAt(j))) {
95                             // well this is not the right path and there is
96
// no better => evaluate in this node
97
break ALL;
98                         }
99                     }
100
101                     // ok, correct convert path
102
item = (Object JavaDoc[]) item[i + 1];
103                     lenOfPattern += s.length();
104
105                     continue ALL;
106                 }
107             }
108
109             // no suitable continuation found, if this is end tree item
110
// do convertion
111
break;
112         }
113
114         if (resIndex != -1) {
115             // do the conversion
116
return results[resIndex] + pattern.substring(resLength);
117         } else {
118             // no conversion
119
return pattern;
120         }
121     }
122
123     /** Data structure to needed to store the */
124     public void init(String JavaDoc[] original, String JavaDoc[] newversion) {
125         ArrayList<Object JavaDoc> root = new ArrayList<Object JavaDoc>();
126
127         for (int i = 0; i < original.length; i++) {
128             placeString(root, original[i], i);
129         }
130
131         this.root = compress(root);
132         this.results = newversion;
133     }
134
135     /** Places a string to the graph of other strings.
136      * @param item list to add the string to
137      * @param s string to place there
138      * @param indx index to put at the end node
139      */

140     private static void placeString(List<Object JavaDoc> item, String JavaDoc s, int indx) {
141         if (s.length() == 0) {
142             item.add(new Integer JavaDoc(indx));
143
144             return;
145         }
146
147         char f = s.charAt(0);
148
149         ListIterator<Object JavaDoc> it = item.listIterator();
150
151         while (it.hasNext()) {
152             Object JavaDoc o = it.next();
153
154             if (o instanceof String JavaDoc) {
155                 // could be also Integer or array
156
String JavaDoc pref = (String JavaDoc) o;
157
158                 if (f == pref.charAt(0)) {
159                     // find the first difference
160
for (int i = 1; i < pref.length(); i++) {
161                         if ((i >= s.length()) || (s.charAt(i) != pref.charAt(i))) {
162                             // split in the i-th index
163
it.set(pref.substring(0, i));
164
165                             // next is the list or null
166
List listForPref = (List) it.next();
167
168                             ArrayList<Object JavaDoc> switchList = new ArrayList<Object JavaDoc>();
169                             it.set(switchList);
170
171                             switchList.add(pref.substring(i));
172                             switchList.add(listForPref);
173
174                             if (i >= s.length()) {
175                                 switchList.add(new Integer JavaDoc(indx));
176                             } else {
177                                 ArrayList<Object JavaDoc> terminalList = new ArrayList<Object JavaDoc>();
178                                 terminalList.add(new Integer JavaDoc(indx));
179
180                                 switchList.add(s.substring(i));
181                                 switchList.add(terminalList);
182                             }
183
184                             return;
185                         }
186                     }
187
188                     //
189
// the new string is longer than the existing recursive add
190
//
191
List<Object JavaDoc> switchList = nextList(it);
192                     placeString(switchList, s.substring(pref.length()), indx);
193
194                     return;
195                 }
196             }
197         }
198
199         //
200
// ok new prefix in this item
201
//
202
ArrayList<Object JavaDoc> id = new ArrayList<Object JavaDoc>();
203         id.add(new Integer JavaDoc(indx));
204
205         item.add(s);
206         item.add(id);
207     }
208
209     @SuppressWarnings JavaDoc("unchecked")
210     private static List<Object JavaDoc> nextList(final ListIterator<Object JavaDoc> it) {
211         List<Object JavaDoc> switchList = (List<Object JavaDoc>) it.next();
212         return switchList;
213     }
214
215     /** Compress tree of Lists into tree of Objects.
216      */

217     private static Object JavaDoc[] compress(List item) {
218         Object JavaDoc[] arr = new Object JavaDoc[item.size()];
219
220         Integer JavaDoc last = null;
221
222         Iterator it = item.iterator();
223         int i = 0;
224
225         while (it.hasNext()) {
226             Object JavaDoc o = it.next();
227
228             if (o instanceof Integer JavaDoc) {
229                 if (last != null) {
230                     throw new IllegalStateException JavaDoc();
231                 }
232
233                 last = (Integer JavaDoc) o;
234
235                 continue;
236             }
237
238             if (o instanceof String JavaDoc) {
239                 arr[i++] = ((String JavaDoc) o).intern();
240
241                 continue;
242             }
243
244             if (o instanceof List) {
245                 arr[i++] = compress((List) o);
246
247                 continue;
248             }
249
250             throw new IllegalStateException JavaDoc();
251         }
252
253         if (last != null) {
254             // assigned integer to this object
255
arr[arr.length - 1] = last;
256         }
257
258         return arr;
259     }
260 }
261
Popular Tags