~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/groovy/groovyproject/src/org/netbeans/modules/groovy/groovyproject/GroovyAntLogger.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.groovy.groovyproject;
 
43
 
 
44
import java.io.File;
 
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;
 
57
 
 
58
/**
 
59
 * Logger which should suppress or prettify typical Ant output from a
 
60
 * j2seproject's build-impl.xml.
 
61
 * @author Jesse Glick
 
62
 */
 
63
public final class GroovyAntLogger extends AntLogger {
 
64
//    static {
 
65
//        System.setProperty("org.netbeans.modules.groovy.groovyproject.GroovyAntLogger", "-1");
 
66
//    }
 
67
    private static final ErrorManager ERR = ErrorManager.getDefault().getInstance(GroovyAntLogger.class.getName());
 
68
    private static final boolean LOGGABLE = ERR.isLoggable(ErrorManager.INFORMATIONAL);
 
69
    
 
70
    /** Default constructor for lookup. */
 
71
    public GroovyAntLogger() {}
 
72
    
 
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;
 
77
    }
 
78
    
 
79
    private static boolean isGroovyProject(File dir) {
 
80
        FileObject projdir = FileUtil.toFileObject(FileUtil.normalizeFile(dir));
 
81
        try {
 
82
            Project proj = ProjectManager.getDefault().findProject(projdir);
 
83
            if (proj != null) {
 
84
                // Check if it is a GroovyProject.
 
85
                return proj.getLookup().lookup(GroovyProject.class) != null;
 
86
            }
 
87
        } catch (IOException e) {
 
88
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
 
89
        }
 
90
        return false;
 
91
    }
 
92
    
 
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);
 
100
                }
 
101
            }
 
102
        }
 
103
        // Was not a J2SEProject's nbproject/build-impl.xml; ignore it.
 
104
        return false;
 
105
    }
 
106
    
 
107
    public String[] interestedInTargets(AntSession session) {
 
108
        return AntLogger.ALL_TARGETS;
 
109
    }
 
110
    
 
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;
 
114
    }
 
115
    
 
116
    public int[] interestedInLogLevels(AntSession session) {
 
117
        return new int[] {
 
118
            AntEvent.LOG_WARN,
 
119
        };
 
120
    }
 
121
    
 
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);
 
130
            }
 
131
        }
 
132
    }
 
133
 
 
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
 
139
            event.consume();
 
140
        }
 
141
        
 
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);
 
148
        event.consume();
 
149
        
 
150
    }
 
151
    
 
152
    /**
 
153
     * Possibly hyperlink a message logged event.
 
154
     */
 
155
    private static OutputListener findHyperlink(AntSession session, String line) {
 
156
        GroovyErrorOutputSupport.HyperlinkData data = GroovyErrorOutputSupport.checkErrorLine( line, FileUtil.toFileObject(session.getOriginatingScript()) );
 
157
        if ( data != null ) {
 
158
            try {
 
159
                return session.createStandardHyperlink(data.file.toURI().toURL(), data.message, data.line1, data.column1, data.line2, data.column2);
 
160
            } catch (MalformedURLException e) {
 
161
                assert false : e;
 
162
            }
 
163
        }
 
164
        return null;
 
165
    }
 
166
 
 
167
}