1 19 20 package org.netbeans.modules.web.jspcompiler; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import org.apache.tools.ant.module.spi.AntEvent; 31 import org.apache.tools.ant.module.spi.AntLogger; 32 import org.apache.tools.ant.module.spi.AntSession; 33 import org.netbeans.modules.web.api.webmodule.WebModule; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileStateInvalidException; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.windows.OutputListener; 39 40 48 public final class JSPJavacAntLogger extends AntLogger { 49 50 63 private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(JSPJavacAntLogger.class.getName()); 64 private static final boolean LOGGABLE = ERR.isLoggable(ErrorManager.INFORMATIONAL); 65 66 70 private static final Pattern JSP_COMPILER_ERROR = Pattern.compile( 71 "(.*)(org.apache.jasper.JasperException: file:)([^\\(]*)\\(([0-9]+),([0-9]+)\\)(.*)"); 73 74 private static final String [] TASKS_OF_INTEREST = AntLogger.ALL_TASKS; 75 76 private static final int[] LEVELS_OF_INTEREST = { 77 AntEvent.LOG_INFO, AntEvent.LOG_WARN, AntEvent.LOG_ERR, }; 83 84 85 86 public JSPJavacAntLogger() {} 87 88 public boolean interestedInSession(AntSession session) { 89 return true; 90 } 91 92 public boolean interestedInAllScripts(AntSession session) { 93 return true; 94 } 95 96 public boolean interestedInScript(File script, AntSession session) { 97 return true; 98 } 99 100 public String [] interestedInTargets(AntSession session) { 101 return AntLogger.ALL_TARGETS; 102 } 103 104 public String [] interestedInTasks(AntSession session) { 105 return TASKS_OF_INTEREST; 106 } 107 108 public int[] interestedInLogLevels(AntSession session) { 109 return LEVELS_OF_INTEREST; 111 } 112 113 114 private static FileObject guessWebModuleOutputRoot(WebModule wm, FileObject fo) { 115 121 FileObject potentialRoot = fo.getParent(); 122 while (potentialRoot != null) { 123 if (potentialRoot.getFileObject("WEB-INF") != null) { 124 return potentialRoot; 125 } 126 potentialRoot = potentialRoot.getParent(); 127 } 128 return null; 129 } 130 131 public void messageLogged(AntEvent event) { 132 if (event.isConsumed()) { 133 return; 134 } 135 AntSession session = event.getSession(); 136 String line = event.getMessage(); 137 OutputListener hyper = findHyperlink(session, line); 138 if (hyper != null) { 140 event.getSession().println(line, event.getLogLevel() <= AntEvent.LOG_WARN, hyper); 141 event.consume(); 142 } 143 } 144 145 148 private static OutputListener findHyperlink(AntSession session, String line) { 149 if (LOGGABLE) ERR.log("line: " + line); 150 if (line.startsWith("file:///")) { line = line.substring(7); 156 if (LOGGABLE) ERR.log("removing file:///"); 157 } else if (line.startsWith("file:")) { line = line.substring(5); 159 if (LOGGABLE) ERR.log("removing file:"); 160 } else if (line.length() > 0 && line.charAt(0) == '/') { 161 if (LOGGABLE) ERR.log("result: looks like Unix file"); 162 } else if (line.length() > 2 && line.charAt(1) == ':' && line.charAt(2) == '\\') { 163 if (LOGGABLE) ERR.log("result: looks like Windows file"); 164 } else { 165 if (LOGGABLE) ERR.log("result: not a file"); 167 return null; 168 } 169 170 int colon1 = line.indexOf(':'); 171 if (colon1 == -1) { 172 if (LOGGABLE) ERR.log("result: no colon found"); 173 return null; 174 } 175 String fileName = line.substring (0, colon1); File file = FileUtil.normalizeFile(new File (fileName)); 177 if (!file.exists()) { 178 if (LOGGABLE) ERR.log("result: no FO for " + fileName); 179 colon1 = line.indexOf (':', colon1+1); 182 if (colon1 == -1) { 183 if (LOGGABLE) ERR.log("result: no second colon found"); 184 return null; 185 } 186 fileName = line.substring (0, colon1); 187 file = FileUtil.normalizeFile(new File (fileName)); 188 if (!file.exists()) { 189 if (LOGGABLE) ERR.log("result: no FO for " + fileName); 190 return null; 191 } 192 } 193 194 int line1 = -1, col1 = -1, line2 = -1, col2 = -1; 195 int start = colon1 + 1; int colon2 = line.indexOf (':', colon1 + 1); 197 if (colon2 != -1) { 198 try { 199 line1 = Integer.parseInt (line.substring (colon1 + 1, colon2).trim ()); 200 start = colon2 + 1; 201 int colon3 = line.indexOf (':', colon2 + 1); 202 if (colon3 != -1) { 203 col1 = Integer.parseInt (line.substring (colon2 + 1, colon3).trim ()); 204 start = colon3 + 1; 205 int colon4 = line.indexOf (':', colon3 + 1); 206 if (colon4 != -1) { 207 line2 = Integer.parseInt (line.substring (colon3 + 1, colon4).trim ()); 208 start = colon4 + 1; 209 int colon5 = line.indexOf (':', colon4 + 1); 210 if (colon5 != -1) { 211 col2 = Integer.parseInt (line.substring (colon4 + 1, colon5).trim ()); 212 if (col2 == col1) 213 col2 = -1; 214 start = colon5 + 1; 215 } 216 } 217 } 218 } catch (NumberFormatException nfe) { 219 } 221 } 222 String message = line.substring (start).trim (); 223 if (message.length () == 0) { 224 message = null; 225 } 226 if (LOGGABLE) ERR.log("Hyperlink: [" + file + "," + line1 + "," + col1 + "," + line2 + "," + col2 + "," + message + "]"); 227 228 File smapFile = getSMAPFileForFile(file); 229 if (LOGGABLE) ERR.log("smapfile: [" + smapFile + "]"); 230 if ((smapFile != null) && (smapFile.exists())) { 231 try { 232 SmapResolver resolver = new SmapResolver(new SmapFileReader(smapFile)); 233 String jspName = resolver.getJspFileName(line1, col1); 234 if (jspName == null) { 235 return null; 236 } 237 if (LOGGABLE) ERR.log("translate: [" + line1 + ", " + col1 + "]"); 238 int newRow = resolver.unmangle(line1, col1); 239 if (newRow == -1) { 242 newRow = resolver.unmangle(line1-1, col1); 243 jspName = resolver.getJspFileName(line1-1, col1); 244 } 245 if (newRow == -1) { 246 newRow = resolver.unmangle(line1+1, col1); 247 jspName = resolver.getJspFileName(line1+1, col1); 248 } 249 try { 250 WebModule wm = WebModule.getWebModule(FileUtil.toFileObject(file)); 251 if (wm == null) { 252 return null; 253 } 254 FileObject jspFO = wm.getDocumentBase().getFileObject(jspName); 255 if (jspFO != null) { 257 return session.createStandardHyperlink(FileUtil.toFile(jspFO).toURI().toURL(), message, newRow, -1, -1, -1); 258 } 259 return null; 260 } catch (MalformedURLException e) { 261 assert false : e; 262 return null; 263 } 264 } 265 catch (IOException e) { 266 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 267 return null; 269 } 270 catch (Exception e) { 271 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 274 return null; 275 } 276 } else { 277 return null; 278 } 279 280 } 281 282 private static final String JAVA_SUFFIX = ".java"; private static final String SMAP_SUFFIX = ".class.smap"; 286 public static File getSMAPFileForFile(File javaFile) { 287 File f = FileUtil.normalizeFile(javaFile); 288 File dir = f.getAbsoluteFile().getParentFile(); 289 String name = f.getName(); 290 if (!name.endsWith(JAVA_SUFFIX)) { 291 return null; 292 } 293 name = name.substring(0, name.length() - JAVA_SUFFIX.length()); 294 File newFile = new File (dir, name + SMAP_SUFFIX); 295 return newFile; 296 } 297 298 } 299 | Popular Tags |