1 19 package org.openide.util; 20 21 import java.util.*; 22 23 24 28 final class RE13 implements Utilities.RE { 29 30 private Object [] root; 31 32 33 private String [] results; 34 35 37 public String [] readPair(String 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 [] { line.substring(0, indx).trim(), line.substring(indx + 1).trim() }; 55 } 56 57 public String convert(String pattern) { 58 Object [] 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 Object last = item[item.length - 1]; 72 73 if (last instanceof Integer ) { 74 resIndex = ((Integer ) last).intValue(); 76 resLength += lenOfPattern; 77 lenOfPattern = 0; 78 } 79 80 if (indx >= pattern.length()) { 81 break; 83 } 84 85 char f = pattern.charAt(indx++); 86 87 for (int i = 0; i < item.length; i++) { 89 if (item[i] instanceof String && (((String ) item[i]).charAt(0) == f)) { 90 String s = (String ) 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 break ALL; 98 } 99 } 100 101 item = (Object []) item[i + 1]; 103 lenOfPattern += s.length(); 104 105 continue ALL; 106 } 107 } 108 109 break; 112 } 113 114 if (resIndex != -1) { 115 return results[resIndex] + pattern.substring(resLength); 117 } else { 118 return pattern; 120 } 121 } 122 123 124 public void init(String [] original, String [] newversion) { 125 ArrayList<Object > root = new ArrayList<Object >(); 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 140 private static void placeString(List<Object > item, String s, int indx) { 141 if (s.length() == 0) { 142 item.add(new Integer (indx)); 143 144 return; 145 } 146 147 char f = s.charAt(0); 148 149 ListIterator<Object > it = item.listIterator(); 150 151 while (it.hasNext()) { 152 Object o = it.next(); 153 154 if (o instanceof String ) { 155 String pref = (String ) o; 157 158 if (f == pref.charAt(0)) { 159 for (int i = 1; i < pref.length(); i++) { 161 if ((i >= s.length()) || (s.charAt(i) != pref.charAt(i))) { 162 it.set(pref.substring(0, i)); 164 165 List listForPref = (List) it.next(); 167 168 ArrayList<Object > switchList = new ArrayList<Object >(); 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 (indx)); 176 } else { 177 ArrayList<Object > terminalList = new ArrayList<Object >(); 178 terminalList.add(new Integer (indx)); 179 180 switchList.add(s.substring(i)); 181 switchList.add(terminalList); 182 } 183 184 return; 185 } 186 } 187 188 List<Object > switchList = nextList(it); 192 placeString(switchList, s.substring(pref.length()), indx); 193 194 return; 195 } 196 } 197 } 198 199 ArrayList<Object > id = new ArrayList<Object >(); 203 id.add(new Integer (indx)); 204 205 item.add(s); 206 item.add(id); 207 } 208 209 @SuppressWarnings ("unchecked") 210 private static List<Object > nextList(final ListIterator<Object > it) { 211 List<Object > switchList = (List<Object >) it.next(); 212 return switchList; 213 } 214 215 217 private static Object [] compress(List item) { 218 Object [] arr = new Object [item.size()]; 219 220 Integer last = null; 221 222 Iterator it = item.iterator(); 223 int i = 0; 224 225 while (it.hasNext()) { 226 Object o = it.next(); 227 228 if (o instanceof Integer ) { 229 if (last != null) { 230 throw new IllegalStateException (); 231 } 232 233 last = (Integer ) o; 234 235 continue; 236 } 237 238 if (o instanceof String ) { 239 arr[i++] = ((String ) 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 (); 251 } 252 253 if (last != null) { 254 arr[arr.length - 1] = last; 256 } 257 258 return arr; 259 } 260 } 261 | Popular Tags |