1 19 20 package org.netbeans.modules.web.jspcompiler; 21 22 import java.util.*; 23 import java.io.IOException ; 24 25 27 31 public class SmapResolver { 32 33 35 private static final String SMAP_HEADER = "SMAP"; 37 39 private static final String DEFAULT_STRATUM = "JSP"; 41 43 private static final String STRATUM_SECTION = "*S JSP"; 45 47 private static final String LINE_SECTION = "*L"; 49 51 private static final String FILE_SECTION = "*F"; 53 55 private static final String END_SECTION = "*E"; 57 59 private static final String FID_DELIM = "#"; 61 63 private SmapReader reader = null; 64 65 67 private boolean resolved = false; 68 69 71 private String defaultStratum = null; 72 73 75 private String outputFileName = null; 76 77 79 private Hashtable fsection = new Hashtable(3); 80 81 83 private Map jsp2java = new TreeMap(); 84 85 87 private Map java2jsp = new TreeMap(); 88 89 92 public SmapResolver(SmapReader reader) { 93 this.resolved = resolve(reader.readSmap()); 94 this.reader = reader; 95 } 96 97 public String toString() { 98 return reader.toString(); 99 } 100 101 106 private boolean resolve(String smap) { 107 108 String currentSection = ""; 109 if (smap == null) return false; 110 111 StringTokenizer st = new StringTokenizer(smap, "\n", false); 113 114 boolean beginning = true; 115 int sectionCounter = 0; 117 119 String fileIndex = null; 120 121 while (st.hasMoreTokens()) { 122 String token = st.nextToken(); 123 124 if (beginning) { if (!SMAP_HEADER.equals(token)) { 127 return false; 128 } 129 beginning = false; 130 currentSection = SMAP_HEADER; 131 continue; 132 } else if (STRATUM_SECTION.equals(token)) { 133 currentSection = STRATUM_SECTION; 134 continue; 135 } else if (FILE_SECTION.equals(token)) { 136 currentSection = FILE_SECTION; 137 sectionCounter = 0; 138 continue; 139 } else if (LINE_SECTION.equals(token)) { 140 currentSection = LINE_SECTION; 141 sectionCounter = 0; 142 fileIndex = "0"; 143 continue; 144 } else if (END_SECTION.equals(token)) { 145 currentSection = END_SECTION; 146 break; 147 } 148 149 if (SMAP_HEADER.equals(currentSection)) { 151 if (sectionCounter == 0) { outputFileName = token; 153 } 154 if (sectionCounter == 1) { defaultStratum = token; 156 } 157 } 158 159 if (FILE_SECTION.equals(currentSection)) { 161 if (token.startsWith("+")) { 162 sectionCounter++; 163 storeFile(token, token = st.nextToken()); 164 } else { 165 storeFile(token, null); 166 } 167 } 168 169 if (LINE_SECTION.equals(currentSection)) { 170 int hashPresent = token.indexOf(FID_DELIM); 171 if (hashPresent > -1) { fileIndex = token.substring(hashPresent + 1, token.indexOf(':')); 173 if ((fileIndex != null) && (fileIndex.indexOf(',') > -1)) { 174 fileIndex = fileIndex.substring(0,fileIndex.indexOf(',')); 175 } 176 177 } 178 storeLine(token, fileIndex); 179 } 180 sectionCounter++; 181 } 182 183 this.resolved = sanityCheck(); 185 return this.resolved; 186 } 187 188 190 private void storeFile(String token, String token2) { 191 String id = ""; 192 String filename = ""; 193 int spaceIndex = 0; 194 if ((token != null) && (token.startsWith("+"))) { 195 token = token.substring(2); 196 spaceIndex = token.indexOf(" "); 197 id = token.substring(0, spaceIndex); 198 filename = token2; 199 } else { 200 spaceIndex = token.indexOf(" "); 201 id = token.substring(0, spaceIndex); 202 filename = token.substring(spaceIndex+1); 203 } 204 fsection.put(id, filename); 205 } 206 207 209 private void storeLine(String token, String fileIndex) { 210 int delimIndex = token.indexOf(":"); 212 213 String jspLine = token.substring(0, delimIndex); 214 String javaLine = token.substring(delimIndex+1); 215 216 int hashPresent = jspLine.indexOf(FID_DELIM); 217 int commaPresent = jspLine.indexOf(','); 218 219 int jspIndex = 0; 220 int repeatCount = 0; 221 222 if (commaPresent != -1) { 223 repeatCount = Integer.parseInt(jspLine.substring(commaPresent+1)); 224 if (hashPresent == -1) { 225 jspIndex = Integer.parseInt(jspLine.substring(0, commaPresent)); 226 } else { 227 jspIndex = Integer.parseInt(jspLine.substring(0, hashPresent)); 228 } 229 } else { 230 if (hashPresent == -1) { 231 jspIndex = Integer.parseInt(jspLine); 232 } else { 233 jspIndex = Integer.parseInt(jspLine.substring(0, hashPresent)); 234 } 235 repeatCount = 1; 236 } 237 238 commaPresent = javaLine.indexOf(','); 239 240 int outputIncrement; 241 int javaIndex; 242 if (commaPresent != -1) { 243 outputIncrement = Integer.parseInt(javaLine.substring(commaPresent+1)); 244 javaIndex = Integer.parseInt(javaLine.substring(0, commaPresent)); 245 } else { 246 outputIncrement = 1; 247 javaIndex = Integer.parseInt(javaLine); 248 } 249 250 for (int i=0; i < repeatCount; i++) { 251 int jspL = jspIndex + i; 252 int javaL = javaIndex + (i * outputIncrement); 253 254 jspLine = Integer.toString(jspL).concat(FID_DELIM).concat(fileIndex); 256 javaLine = Integer.toString(javaL); 257 if (!jsp2java.containsKey(jspLine)) { jsp2java.put(jspLine, javaLine); 259 } 260 261 jspLine = Integer.toString(jspL).concat("#").concat(fileIndex); 263 javaLine = Integer.toString(javaL); 264 if (!java2jsp.containsKey(javaLine)) { java2jsp.put(javaLine, jspLine); 266 } 267 } 268 } 269 270 272 private boolean sanityCheck() { 273 if (!DEFAULT_STRATUM.equals(defaultStratum)) return false; 274 if (!(outputFileName.endsWith(".java"))) return false; 275 if (fsection.isEmpty()) return false; 276 if (jsp2java.isEmpty()) return false; if (java2jsp.isEmpty()) return false; return true; 279 } 280 281 285 private String getFileNameByIndex(String index) { 286 return (String )fsection.get(index); 287 } 288 289 293 private String getIndexByFileName(String fname) { 294 Set s = fsection.entrySet(); 295 Iterator i = s.iterator(); 296 while (i.hasNext()) { 297 Map.Entry mentry = (Map.Entry)i.next(); 298 String value = (String )mentry.getValue(); 299 if (value.equalsIgnoreCase(fname)) { 300 return mentry.getKey().toString(); 301 } 302 } 303 return null; 304 } 305 306 309 public boolean isResolved() { 310 return this.resolved; 311 } 312 313 316 public Map getFileNames() { 317 Hashtable h = new Hashtable(fsection.size()); 318 Collection c = fsection.values(); 319 Iterator i = c.iterator(); 320 int counter = 0; 321 while (i.hasNext()) { 322 h.put(new Integer (counter++), i.next()); 323 } 324 return h; 325 } 326 327 330 public String getPrimaryJspFileName() { 331 TreeMap tm = new TreeMap(fsection); 332 Object o = tm.firstKey(); 333 String s = (String )fsection.get(o); 334 return s; 335 } 336 337 339 public boolean hasIncludedFiles() { 340 return (fsection.size() > 1); 341 } 342 343 public String getJavaLineType(int line, int col) { 344 return null; 346 } 347 348 350 public boolean isEmpty() { 351 return jsp2java.isEmpty(); } 353 354 356 public String getJspFileName(int line, int col) throws IOException { 357 String key = Integer.toString(line); 358 String value = (String )java2jsp.get(key); 359 if (value == null) return null; 360 String index = value.substring(value.indexOf(FID_DELIM)+1); 361 return getFileNameByIndex(index); 362 } 363 364 public int mangle(String jspFileName, int line, int col) { 365 String fileIndex = getIndexByFileName(jspFileName); 366 if (fileIndex == null) return -1; 367 String key = "".concat(Integer.toString(line)).concat("#").concat(fileIndex); 368 String value = (String )jsp2java.get(key); 369 if (value == null) return -1; 370 return Integer.parseInt(value); 371 } 372 373 public int unmangle(int line, int col) { 374 String key = Integer.toString(line); 375 String value = (String )java2jsp.get(key); 376 if (value == null) return -1; 377 int jspline = Integer.parseInt(value.substring(0, value.indexOf("#"))); 378 return jspline; 379 } 380 381 } 393 | Popular Tags |