2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.groovy.groovyproject;
45
import java.io.IOException;
46
import java.net.MalformedURLException;
47
import org.apache.tools.ant.module.spi.AntEvent;
48
import org.apache.tools.ant.module.spi.AntLogger;
49
import org.apache.tools.ant.module.spi.AntSession;
50
import org.netbeans.api.project.Project;
51
import org.netbeans.api.project.ProjectManager;
52
import org.netbeans.modules.groovy.support.api.GroovyErrorOutputSupport;
53
import org.openide.ErrorManager;
54
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileUtil;
56
import org.openide.windows.OutputListener;
59
* Logger which should suppress or prettify typical Ant output from a
60
* j2seproject's build-impl.xml.
63
public final class GroovyAntLogger extends AntLogger {
65
// System.setProperty("org.netbeans.modules.groovy.groovyproject.GroovyAntLogger", "-1");
67
private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(GroovyAntLogger.class.getName());
68
private static final boolean LOGGABLE = ERR.isLoggable(ErrorManager.INFORMATIONAL);
70
/** Default constructor for lookup. */
71
public GroovyAntLogger() {}
73
public boolean interestedInSession(AntSession session) {
74
// Even if the initiating project is not a J2SEProject, suppress these messages.
75
// However disable our tricks when running at VERBOSE or higher.
76
return session.getVerbosity() <= AntEvent.LOG_INFO;
79
private static boolean isGroovyProject(File dir) {
80
FileObject projdir = FileUtil.toFileObject(FileUtil.normalizeFile(dir));
82
Project proj = ProjectManager.getDefault().findProject(projdir);
84
// Check if it is a GroovyProject.
85
return proj.getLookup().lookup(GroovyProject.class) != null;
87
} catch (IOException e) {
88
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
93
public boolean interestedInScript(File script, AntSession session) {
94
if (script.getName().equals("build-impl.xml")) { // NOI18N
95
File parent = script.getParentFile();
96
if (parent != null && parent.getName().equals("nbproject")) { // NOI18N
97
File parent2 = parent.getParentFile();
98
if (parent2 != null) {
99
return isGroovyProject(parent2);
103
// Was not a J2SEProject's nbproject/build-impl.xml; ignore it.
107
public String[] interestedInTargets(AntSession session) {
108
return AntLogger.ALL_TARGETS;
111
public String[] interestedInTasks(AntSession session) {
112
// XXX will eventually need them all anyway; as is, could list just javac
113
return AntLogger.ALL_TASKS;
116
public int[] interestedInLogLevels(AntSession session) {
122
public void taskFinished(AntEvent event) {
123
if ("javac".equals(event.getTaskName())) { // NOI18N
124
Throwable t = event.getException();
125
AntSession session = event.getSession();
126
if (t != null && !session.isExceptionConsumed(t)) {
127
// Some error was thrown from build-impl.xml#compile. Ignore it; generally
128
// it will have been a compilation error which we do not wish to show.
129
session.consumeException(t);
134
public void messageLogged(AntEvent event) {
135
// #43968 - filter out following message
136
if (!event.isConsumed() && event.getLogLevel() == AntEvent.LOG_WARN &&
137
event.getMessage().startsWith("Trying to override old definition of " + // NOI18N
138
"task http://www.netbeans.org/ns/j2se-project/1:")) { // NOI18N
142
AntSession session = event.getSession();
143
String line = event.getMessage();
144
if (LOGGABLE) ERR.log("Received message: " + line);
145
OutputListener hyperlink = findHyperlink(session, line);
146
// XXX should translate tabs to spaces here as a safety measure
147
event.getSession().println(line, event.getLogLevel() <= AntEvent.LOG_WARN, hyperlink);
153
* Possibly hyperlink a message logged event.
155
private static OutputListener findHyperlink(AntSession session, String line) {
156
GroovyErrorOutputSupport.HyperlinkData data = GroovyErrorOutputSupport.checkErrorLine( line, FileUtil.toFileObject(session.getOriginatingScript()) );
157
if ( data != null ) {
159
return session.createStandardHyperlink(data.file.toURI().toURL(), data.message, data.line1, data.column1, data.line2, data.column2);
160
} catch (MalformedURLException e) {