1 19 20 package org.netbeans.modules.web.jspcompiler; 21 22 import java.io.File ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 import org.apache.tools.ant.module.spi.AntEvent; 26 import org.apache.tools.ant.module.spi.AntLogger; 27 import org.apache.tools.ant.module.spi.AntSession; 28 import org.netbeans.modules.web.api.webmodule.WebModule; 29 import org.openide.ErrorManager; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileStateInvalidException; 32 import org.openide.filesystems.FileUtil; 33 34 42 public final class JSPAntLogger extends AntLogger { 43 44 48 private static final Pattern JSP_COMPILER_ERROR = Pattern.compile( 49 "(.*)(org.apache.jasper.JasperException: file:)(.*)"); 51 private static final Pattern FILE_PATTERN = Pattern.compile( 52 "([^\\(]*)\\(([0-9]+),([0-9]+)\\)"); 54 private static final String [] TASKS_OF_INTEREST = AntLogger.ALL_TASKS; 55 56 private static final int[] LEVELS_OF_INTEREST = { 57 AntEvent.LOG_INFO, AntEvent.LOG_WARN, AntEvent.LOG_ERR, }; 63 64 private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(JSPAntLogger.class.getName()); 65 private static final boolean LOGGABLE = ERR.isLoggable(ErrorManager.INFORMATIONAL); 66 67 68 public JSPAntLogger() { 69 } 70 71 public boolean interestedInSession(AntSession session) { 72 return true; 73 } 74 75 public boolean interestedInAllScripts(AntSession session) { 76 return true; 77 } 78 79 public String [] interestedInTargets(AntSession session) { 80 return AntLogger.ALL_TARGETS; 81 } 82 83 public boolean interestedInScript(File script, AntSession session) { 84 return true; 85 } 86 87 public String [] interestedInTasks(AntSession session) { 88 return TASKS_OF_INTEREST; 89 } 90 91 public int[] interestedInLogLevels(AntSession session) { 92 return LEVELS_OF_INTEREST; 94 } 95 96 public void messageLogged(AntEvent event) { 97 AntSession session = event.getSession(); 98 int messageLevel = event.getLogLevel(); 99 int sessionLevel = session.getVerbosity(); 100 String line = event.getMessage(); 101 assert line != null; 102 103 Matcher m = JSP_COMPILER_ERROR.matcher(line); 105 if (m.matches()) { if (LOGGABLE) ERR.log("matched line: " + line); 107 String jspErrorText = line.substring(line.lastIndexOf(')')+1); 109 session.println(line.substring(0, line.indexOf("file:")) + jspErrorText, true, null); 110 111 String filePart = line.substring(line.indexOf("file"), line.lastIndexOf(')')+1); 113 if (LOGGABLE) ERR.log("file part: " + filePart); 114 115 int startIndex = 0; 117 while (filePart.indexOf("file:", startIndex) > -1) { 118 int start = filePart.indexOf("file:", startIndex) + 5; 119 int end = filePart.indexOf(')', startIndex) + 1; 120 startIndex = end; 121 String file = filePart.substring(start, end); 122 if (LOGGABLE) ERR.log("file: " + file); 123 124 Matcher fileMatcher = FILE_PATTERN.matcher(file); 126 if (fileMatcher.matches()) { 127 String jspFile = fileMatcher.group(1).trim(); 128 int lineNumber = Integer.parseInt(fileMatcher.group(2)); 129 int columnNumber = Integer.parseInt(fileMatcher.group(3)) + 1; 130 if (LOGGABLE) ERR.log("linking line: " + lineNumber + ", column: " + columnNumber); 131 132 File f = new File (jspFile); 133 FileObject fo = FileUtil.toFileObject(f); 134 FileObject jspSource = getResourceInSources(fo); 136 if (jspSource != null) { 138 if (messageLevel <= sessionLevel && !event.isConsumed()) { 139 try { 140 session.println(file, true, session.createStandardHyperlink(jspSource.getURL(), jspErrorText, lineNumber, columnNumber, -1, -1)); 141 } catch (FileStateInvalidException e) { 142 assert false : e; 143 } 144 } 145 } 146 } 147 } 148 event.consume(); 149 } 150 } 151 152 153 155 private static FileObject getResourceInSources(FileObject inBuild) { 156 if (inBuild == null) { 157 return null; 158 } 159 WebModule wm = WebModule.getWebModule(inBuild); 160 if (wm != null) { 161 FileObject webBuildDir = guessWebModuleOutputRoot(wm, inBuild); 163 if (webBuildDir != null) { 164 String jspResourcePath = FileUtil.getRelativePath(webBuildDir, inBuild); 165 return wm.getDocumentBase().getFileObject(jspResourcePath); 166 } 167 168 } 169 return null; 170 } 171 172 private static FileObject guessWebModuleOutputRoot(WebModule wm, FileObject fo) { 173 179 FileObject potentialRoot = fo.getParent(); 180 while (potentialRoot != null) { 181 if (potentialRoot.getFileObject("WEB-INF") != null) { 182 return potentialRoot; 183 } 184 potentialRoot = potentialRoot.getParent(); 185 } 186 return null; 187 } 188 189 } 190 | Popular Tags |