1 17 18 package org.apache.jasper.compiler; 19 20 import java.util.List ; 21 import java.util.ArrayList ; 22 23 30 public class SmapStratum { 31 32 35 39 public static class LineInfo { 40 private int inputStartLine = -1; 41 private int outputStartLine = -1; 42 private int lineFileID = 0; 43 private int inputLineCount = 1; 44 private int outputLineIncrement = 1; 45 private boolean lineFileIDSet = false; 46 47 48 public void setInputStartLine(int inputStartLine) { 49 if (inputStartLine < 0) 50 throw new IllegalArgumentException ("" + inputStartLine); 51 this.inputStartLine = inputStartLine; 52 } 53 54 55 public void setOutputStartLine(int outputStartLine) { 56 if (outputStartLine < 0) 57 throw new IllegalArgumentException ("" + outputStartLine); 58 this.outputStartLine = outputStartLine; 59 } 60 61 67 public void setLineFileID(int lineFileID) { 68 if (lineFileID < 0) 69 throw new IllegalArgumentException ("" + lineFileID); 70 this.lineFileID = lineFileID; 71 this.lineFileIDSet = true; 72 } 73 74 75 public void setInputLineCount(int inputLineCount) { 76 if (inputLineCount < 0) 77 throw new IllegalArgumentException ("" + inputLineCount); 78 this.inputLineCount = inputLineCount; 79 } 80 81 82 public void setOutputLineIncrement(int outputLineIncrement) { 83 if (outputLineIncrement < 0) 84 throw new IllegalArgumentException ("" + outputLineIncrement); 85 this.outputLineIncrement = outputLineIncrement; 86 } 87 88 93 public String getString() { 94 if (inputStartLine == -1 || outputStartLine == -1) 95 throw new IllegalStateException (); 96 StringBuffer out = new StringBuffer (); 97 out.append(inputStartLine); 98 if (lineFileIDSet) 99 out.append("#" + lineFileID); 100 if (inputLineCount != 1) 101 out.append("," + inputLineCount); 102 out.append(":" + outputStartLine); 103 if (outputLineIncrement != 1) 104 out.append("," + outputLineIncrement); 105 out.append('\n'); 106 return out.toString(); 107 } 108 109 public String toString() { 110 return getString(); 111 } 112 } 113 114 117 private String stratumName; 118 private List fileNameList; 119 private List filePathList; 120 private List lineData; 121 private int lastFileID; 122 123 126 132 public SmapStratum(String stratumName) { 133 this.stratumName = stratumName; 134 fileNameList = new ArrayList (); 135 filePathList = new ArrayList (); 136 lineData = new ArrayList (); 137 lastFileID = 0; 138 } 139 140 143 148 public void addFile(String filename) { 149 addFile(filename, filename); 150 } 151 152 160 public void addFile(String filename, String filePath) { 161 int pathIndex = filePathList.indexOf(filePath); 162 if (pathIndex == -1) { 163 fileNameList.add(filename); 164 filePathList.add(filePath); 165 } 166 } 167 168 171 public void optimizeLineSection() { 172 173 179 int i = 0; 182 while (i < lineData.size() - 1) { 183 LineInfo li = (LineInfo)lineData.get(i); 184 LineInfo liNext = (LineInfo)lineData.get(i + 1); 185 if (!liNext.lineFileIDSet 186 && liNext.inputStartLine == li.inputStartLine 187 && liNext.inputLineCount == 1 188 && li.inputLineCount == 1 189 && liNext.outputStartLine 190 == li.outputStartLine 191 + li.inputLineCount * li.outputLineIncrement) { 192 li.setOutputLineIncrement( 193 liNext.outputStartLine 194 - li.outputStartLine 195 + liNext.outputLineIncrement); 196 lineData.remove(i + 1); 197 } else { 198 i++; 199 } 200 } 201 202 i = 0; 205 while (i < lineData.size() - 1) { 206 LineInfo li = (LineInfo)lineData.get(i); 207 LineInfo liNext = (LineInfo)lineData.get(i + 1); 208 if (!liNext.lineFileIDSet 209 && liNext.inputStartLine == li.inputStartLine + li.inputLineCount 210 && liNext.outputLineIncrement == li.outputLineIncrement 211 && liNext.outputStartLine 212 == li.outputStartLine 213 + li.inputLineCount * li.outputLineIncrement) { 214 li.setInputLineCount(li.inputLineCount + liNext.inputLineCount); 215 lineData.remove(i + 1); 216 } else { 217 i++; 218 } 219 } 220 } 221 222 245 public void addLineData( 246 int inputStartLine, 247 String inputFileName, 248 int inputLineCount, 249 int outputStartLine, 250 int outputLineIncrement) { 251 int fileIndex = filePathList.indexOf(inputFileName); 253 if (fileIndex == -1) throw new IllegalArgumentException ( 255 "inputFileName: " + inputFileName); 256 257 if (outputStartLine == 0) 263 return; 264 265 LineInfo li = new LineInfo(); 267 li.setInputStartLine(inputStartLine); 268 li.setInputLineCount(inputLineCount); 269 li.setOutputStartLine(outputStartLine); 270 li.setOutputLineIncrement(outputLineIncrement); 271 if (fileIndex != lastFileID) 272 li.setLineFileID(fileIndex); 273 lastFileID = fileIndex; 274 275 lineData.add(li); 277 } 278 279 282 285 public String getStratumName() { 286 return stratumName; 287 } 288 289 293 public String getString() { 294 if (fileNameList.size() == 0 || lineData.size() == 0) 296 return null; 297 298 StringBuffer out = new StringBuffer (); 299 300 out.append("*S " + stratumName + "\n"); 302 303 out.append("*F\n"); 305 int bound = fileNameList.size(); 306 for (int i = 0; i < bound; i++) { 307 if (filePathList.get(i) != null) { 308 out.append("+ " + i + " " + fileNameList.get(i) + "\n"); 309 String filePath = (String )filePathList.get(i); 312 if (filePath.startsWith("/")) { 313 filePath = filePath.substring(1); 314 } 315 out.append(filePath + "\n"); 316 } else { 317 out.append(i + " " + fileNameList.get(i) + "\n"); 318 } 319 } 320 321 out.append("*L\n"); 323 bound = lineData.size(); 324 for (int i = 0; i < bound; i++) { 325 LineInfo li = (LineInfo)lineData.get(i); 326 out.append(li.getString()); 327 } 328 329 return out.toString(); 330 } 331 332 public String toString() { 333 return getString(); 334 } 335 336 } 337 | Popular Tags |