KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jspcompiler > JSPAntLogger


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jspcompiler;
21
22 import java.io.File JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
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 /**
35  * Ant logger which handles compilation of JSPs, both the JSP -> Java and
36  * the Java -> class compilation stages.
37  * Specifically, handles hyperlinking of errors from JspC and from Javac run on
38  * classes generated from JSPs.
39  * @author Petr Jiricka, Jesse Glick
40  * @see "#42525"
41  */

42 public final class JSPAntLogger extends AntLogger {
43     
44     /**
45      * Regexp matching the compilation error from JspC. Sample message could look like this:
46      * org.apache.jasper.JasperException: file:C:/project/AntParseTestProject2/build/web/index.jsp(6,0) Include action: Mandatory attribute page missing
47      */

48     private static final Pattern JavaDoc JSP_COMPILER_ERROR = Pattern.compile(
49         "(.*)(org.apache.jasper.JasperException: file:)(.*)"); // NOI18N
50

51     private static final Pattern JavaDoc FILE_PATTERN = Pattern.compile(
52         "([^\\(]*)\\(([0-9]+),([0-9]+)\\)"); // NOI18N
53

54     private static final String JavaDoc[] TASKS_OF_INTEREST = AntLogger.ALL_TASKS;
55     
56     private static final int[] LEVELS_OF_INTEREST = {
57         //AntEvent.LOG_DEBUG, // XXX is this needed?
58
//AntEvent.LOG_VERBOSE, // XXX is this needed?
59
AntEvent.LOG_INFO, // XXX is this needed?
60
AntEvent.LOG_WARN, // XXX is this needed?
61
AntEvent.LOG_ERR, // XXX is this needed?
62
};
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     /** Default constructor for lookup. */
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 JavaDoc[] interestedInTargets(AntSession session) {
80         return AntLogger.ALL_TARGETS;
81     }
82     
83     public boolean interestedInScript(File JavaDoc script, AntSession session) {
84         return true;
85     }
86     
87     public String JavaDoc[] interestedInTasks(AntSession session) {
88         return TASKS_OF_INTEREST;
89     }
90     
91     public int[] interestedInLogLevels(AntSession session) {
92         // XXX could exclude those in [INFO..ERR] greater than session.verbosity
93
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 JavaDoc line = event.getMessage();
101         assert line != null;
102
103         // XXX only check when the task is correct
104
Matcher JavaDoc m = JSP_COMPILER_ERROR.matcher(line);
105         if (m.matches()) { //it's our error
106
if (LOGGABLE) ERR.log("matched line: " + line);
107             // print the exception and error statement first
108
String JavaDoc jspErrorText = line.substring(line.lastIndexOf(')')+1);
109             session.println(line.substring(0, line.indexOf("file:")) + jspErrorText, true, null);
110             
111             // get the files from the line
112
String JavaDoc filePart = line.substring(line.indexOf("file"), line.lastIndexOf(')')+1);
113             if (LOGGABLE) ERR.log("file part: " + filePart);
114             
115             // now create hyperlinks for all the files
116
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 JavaDoc file = filePart.substring(start, end);
122                 if (LOGGABLE) ERR.log("file: " + file);
123
124                 // we've got the info for one file extracted, now extract the line/column and actual filename
125
Matcher JavaDoc fileMatcher = FILE_PATTERN.matcher(file);
126                 if (fileMatcher.matches()) {
127                     String JavaDoc 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 JavaDoc f = new File JavaDoc(jspFile);
133                     FileObject fo = FileUtil.toFileObject(f);
134                     // Check to see if this JSP is in the web module.
135
FileObject jspSource = getResourceInSources(fo);
136                     // and create the hyperlink if needed
137
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     /** Given a resource in the build directory, returns the corresponding resource in the source directory.
154      */

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             // get the mirror of this page in sources
162
FileObject webBuildDir = guessWebModuleOutputRoot(wm, inBuild);
163             if (webBuildDir != null) {
164                 String JavaDoc 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         /*
174         File outputF = wm.getWebOutputRoot();
175         if (outputF != null) {
176             return FileUtil.toFileObject(outputF);
177         }
178         */

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