KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEAntLogger


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.java.j2seproject;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException 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.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  * Logger which should suppress or prettify typical Ant output from a
36  * j2seproject's build-impl.xml.
37  * @author Jesse Glick
38  */

39 public final class J2SEAntLogger extends AntLogger {
40     
41     /** Default constructor for lookup. */
42     public J2SEAntLogger() {}
43     
44     public boolean interestedInSession(AntSession session) {
45         // Even if the initiating project is not a J2SEProject, suppress these messages.
46
// However disable our tricks when running at VERBOSE or higher.
47
return session.getVerbosity() <= AntEvent.LOG_INFO;
48     }
49     
50     private static boolean isJ2SEProject(File JavaDoc dir) {
51         FileObject projdir = FileUtil.toFileObject(FileUtil.normalizeFile(dir));
52         try {
53             Project proj = ProjectManager.getDefault().findProject(projdir);
54             if (proj != null) {
55                 // Check if it is a J2SEProject.
56
return proj.getLookup().lookup(J2SEProject.class) != null;
57             }
58         } catch (IOException JavaDoc e) {
59             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
60         }
61         return false;
62     }
63     
64     public boolean interestedInScript(File JavaDoc script, AntSession session) {
65         if (script.getName().equals("build-impl.xml")) { // NOI18N
66
File JavaDoc parent = script.getParentFile();
67             if (parent != null && parent.getName().equals("nbproject")) { // NOI18N
68
File JavaDoc parent2 = parent.getParentFile();
69                 if (parent2 != null) {
70                     return isJ2SEProject(parent2);
71                 }
72             }
73         }
74         // Was not a J2SEProject's nbproject/build-impl.xml; ignore it.
75
return false;
76     }
77     
78     public String JavaDoc[] interestedInTargets(AntSession session) {
79         return AntLogger.ALL_TARGETS;
80     }
81     
82     public String JavaDoc[] interestedInTasks(AntSession session) {
83         // XXX will eventually need them all anyway; as is, could list just javac
84
return AntLogger.ALL_TASKS;
85     }
86     
87     public int[] interestedInLogLevels(AntSession session) {
88         return new int[] {
89             AntEvent.LOG_WARN,
90         };
91     }
92     
93     public void taskFinished(AntEvent event) {
94         if ("javac".equals(event.getTaskName())) { // NOI18N
95
Throwable JavaDoc t = event.getException();
96             AntSession session = event.getSession();
97             if (t != null && !session.isExceptionConsumed(t)) {
98                 // Some error was thrown from build-impl.xml#compile. Ignore it; generally
99
// it will have been a compilation error which we do not wish to show.
100
session.consumeException(t);
101             }
102         }
103     }
104
105     public void messageLogged(AntEvent event) {
106         // #43968 - filter out following message
107
if (!event.isConsumed() && event.getLogLevel() == AntEvent.LOG_WARN &&
108             event.getMessage().startsWith("Trying to override old definition of " + // NOI18N
109
"task http://www.netbeans.org/ns/j2se-project/")) { // NOI18N
110
event.consume();
111         }
112     }
113
114 }
115
Popular Tags