KickJava   Java API By Example, From Geeks To Geeks.

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


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
36  * @author Martin Grebac
37  */

38 public final class TldAntLogger extends AntLogger {
39     
40     /**
41      * Regexp matching the compilation error from JspC when error is in tld. Sample message could look like this:
42      * org.apache.jasper.JasperException: Unable to initialize TldLocationsCache: XML parsing error on file /WEB-INF/jsp2/jsp2-example-taglib.tld: (line 18, col -1)
43      */

44     private static final Pattern JavaDoc TLD_ERROR = Pattern.compile(
45         "(.*)(org.apache.jasper.JasperException:)(.*)( file )(.*)"); // NOI18N
46

47     private static final Pattern JavaDoc FILE_PATTERN = Pattern.compile(
48         "([^\\(]*)(: )\\(line ([0-9]+), col ([0-9-]+)\\)"); // NOI18N
49

50     private static final String JavaDoc[] TASKS_OF_INTEREST = AntLogger.ALL_TASKS;
51     
52     private static final int[] LEVELS_OF_INTEREST = {
53         //AntEvent.LOG_DEBUG, // XXX is this needed?
54
//AntEvent.LOG_VERBOSE, // XXX is this needed?
55
AntEvent.LOG_INFO, // XXX is this needed?
56
AntEvent.LOG_WARN, // XXX is this needed?
57
AntEvent.LOG_ERR, // XXX is this needed?
58
};
59     
60     private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(TldAntLogger.class.getName());
61     private static final boolean LOGGABLE = ERR.isLoggable(ErrorManager.INFORMATIONAL);
62     
63     /** Default constructor for lookup. */
64     public TldAntLogger() {
65     }
66     
67     public boolean interestedInSession(AntSession session) {
68         return true;
69     }
70     
71     public boolean interestedInAllScripts(AntSession session) {
72         return true;
73     }
74     
75     public String JavaDoc[] interestedInTargets(AntSession session) {
76         return AntLogger.ALL_TARGETS;
77     }
78     
79     public boolean interestedInScript(File JavaDoc script, AntSession session) {
80         return true;
81     }
82     
83     public String JavaDoc[] interestedInTasks(AntSession session) {
84         return TASKS_OF_INTEREST;
85     }
86     
87     public int[] interestedInLogLevels(AntSession session) {
88         // XXX could exclude those in [INFO..ERR] greater than session.verbosity
89
return LEVELS_OF_INTEREST;
90     }
91
92     public void messageLogged(AntEvent event) {
93         AntSession session = event.getSession();
94         int messageLevel = event.getLogLevel();
95         int sessionLevel = session.getVerbosity();
96         String JavaDoc line = event.getMessage();
97         assert line != null;
98
99         Matcher JavaDoc m = TLD_ERROR.matcher(line);
100         if (m.matches()) { //it's our error
101
if (LOGGABLE) ERR.log("matched line: " + line);
102             // print the exception and error statement first
103
String JavaDoc errorText = m.group(3) + m.group(4);
104             session.println(m.group(2) + errorText, true, null);
105             
106             // get the file from the line
107
String JavaDoc filePart = m.group(5).trim();
108             if (LOGGABLE) ERR.log("file part: " + filePart);
109             
110             // now create hyperlink for the file
111
Matcher JavaDoc fileMatcher = FILE_PATTERN.matcher(filePart);
112             if (fileMatcher.matches()) {
113                 String JavaDoc tldFile = fileMatcher.group(1).trim();
114                 if (LOGGABLE) ERR.log("tld file: " + tldFile);
115
116                 int lineNumber = Integer.parseInt(fileMatcher.group(3));
117                 int columnNumber = Integer.parseInt(fileMatcher.group(4));
118                 if (LOGGABLE) ERR.log("linking line: " + lineNumber + ", column: " + columnNumber);
119
120                 File JavaDoc scriptLoc = event.getScriptLocation();
121                 FileObject scriptLocFO = FileUtil.toFileObject(scriptLoc);
122                 WebModule wm = WebModule.getWebModule(scriptLocFO);
123                 if (LOGGABLE) ERR.log("wm: " + wm);
124                 
125                 if (wm == null) {
126                     session.println(tldFile, true, null);
127                     event.consume();
128                     return;
129                 }
130                 
131                 FileObject tldSource = wm.getDocumentBase().getFileObject(tldFile);
132                 if (LOGGABLE) ERR.log("tldSource: " + tldSource);
133                 
134                 if (tldSource == null) {
135                     session.println(tldFile, true, null);
136                     event.consume();
137                     return;
138                 }
139                 
140                 if (messageLevel <= sessionLevel && !event.isConsumed()) {
141                     try {
142                         session.println(tldFile, true, session.createStandardHyperlink(tldSource.getURL(), errorText + tldFile, lineNumber, columnNumber, -1, -1));
143                     } catch (FileStateInvalidException e) {
144                         assert false : e;
145                     }
146                 }
147             }
148             event.consume();
149         }
150     }
151 }
152
Popular Tags