KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > xdoclet > NetuiDocletTask


1 package org.apache.beehive.netui.xdoclet;
2
3 import org.apache.beehive.netui.compiler.typesystem.util.SourcePosition;
4 import org.apache.tools.ant.BuildException;
5 import xdoclet.DocletTask;
6
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  * Netui XDoclet task
15  *
16  * @ant.element name="netuidoclet" display-name="NetUI Task"
17  */

18 public class NetuiDocletTask extends DocletTask
19 {
20     private static HashMap JavaDoc _buildMessages = new HashMap JavaDoc(); // String filename -> List messages
21

22     private static File JavaDoc _webappRoot = null;
23
24
25
26     /**
27      * @throws BuildException
28      *
29      */

30     protected void start() throws BuildException
31     {
32         try
33         {
34             assert _webappRoot != null; // should have been set in the ant task
35
super.start();
36         }
37         finally
38         {
39             _webappRoot = null;
40
41             // list any warnings and errors
42
boolean overallError = false;
43             if ( _buildMessages != null )
44             {
45                 Iterator JavaDoc i = _buildMessages.keySet().iterator();
46                 
47                 while ( i.hasNext() )
48                 {
49                     String JavaDoc sourceFile = ( String JavaDoc ) i.next();
50                     List JavaDoc messages = ( List JavaDoc ) _buildMessages.get( sourceFile );
51                     int errorCount = 0;
52                     int warningCount = 0;
53                     
54                     for ( Iterator JavaDoc j = messages.iterator(); j.hasNext(); )
55                     {
56                         BuildMessage message = ( BuildMessage ) j.next();
57                         System.err.println();
58                         System.err.print( sourceFile );
59                         System.err.print( ": " );
60                         
61                         if ( message.getLine() > 0 )
62                         {
63                             String JavaDoc[] args = { new Integer JavaDoc( message.getLine() ).toString() };
64                             System.err.println( XDocletCompilerUtils.getMessage( "compiler.line", args ) );
65                         }
66                         
67                         if ( message.isError() )
68                         {
69                             overallError = true;
70                             ++errorCount;
71                         }
72                         else
73                         {
74                             System.err.print( XDocletCompilerUtils.getMessage( "compiler.warning", null ) );
75                             ++warningCount;
76                         }
77                         
78                         System.err.println( message.getMessage() );
79                     }
80                     
81                     System.err.println( XDocletCompilerUtils.getMessage( "compiler.build.results",
82                                                     new String JavaDoc[]{ new Integer JavaDoc( errorCount ).toString(),
83                                                                   new Integer JavaDoc( warningCount ).toString(),
84                                                                   sourceFile } ) );
85                 }
86             }
87
88             _buildMessages = null;
89
90             if ( overallError )
91             {
92                 System.err.println( XDocletCompilerUtils.getMessage( "compiler.build.failed", null ) );
93                 throw new NetuiBuildException();
94             }
95         }
96     }
97
98     public static void addError( String JavaDoc error, SourcePosition sourcePosition )
99     {
100         assert sourcePosition != null;
101         String JavaDoc sourceFilePath = sourcePosition.file().getPath();
102         int line = sourcePosition.line();
103         addError( error, sourceFilePath, line );
104     }
105     
106     public static void addError( String JavaDoc error, String JavaDoc sourceFile, int line )
107     {
108         List JavaDoc messages = ( List JavaDoc ) _buildMessages.get( sourceFile );
109         
110         if ( messages == null )
111         {
112             messages = new ArrayList JavaDoc();
113             _buildMessages.put( sourceFile, messages );
114         }
115         
116         messages.add( new BuildMessage( error, line, true ) );
117     }
118
119     public static void addWarning( String JavaDoc warning, SourcePosition sourcePosition )
120     {
121         assert sourcePosition != null;
122         String JavaDoc sourceFilePath = sourcePosition.file().getPath();
123         int line = sourcePosition.line();
124         addWarning( warning, sourceFilePath, line );
125     }
126     
127     public static void addWarning( String JavaDoc warning, String JavaDoc sourceFile, int line )
128     {
129         List JavaDoc messages = ( List JavaDoc ) _buildMessages.get( sourceFile );
130         
131         if ( messages == null )
132         {
133             messages = new ArrayList JavaDoc();
134             _buildMessages.put( sourceFile, messages );
135         }
136         
137         messages.add( new BuildMessage( warning, line, false ) );
138     }
139
140     private static class BuildMessage
141     {
142         private String JavaDoc _message;
143         private boolean _error;
144         private int _line;
145
146         public BuildMessage( String JavaDoc message, int line, boolean error )
147         {
148             _message = message;
149             _error = error;
150             _line = line;
151         }
152
153         public final String JavaDoc getMessage()
154         {
155             return _message;
156         }
157
158         public final boolean isError()
159         {
160             return _error;
161         }
162
163         public final int getLine()
164         {
165             return _line;
166         }
167     }
168     
169     /**
170      * Called by superclass before start() is called
171      *
172      * @throws BuildException Describe the exception
173      */

174     protected void validateOptions() throws BuildException
175     {
176         // we don't support the destdir attribute; so if it's null, just fake it so super won't
177
// fail validation
178
if ( getDestDir() == null )
179         {
180             setDestDir( new File JavaDoc( "bogus" ) );
181         }
182         super.validateOptions();
183     }
184
185     public void setWebappRoot( File JavaDoc file )
186     {
187         _webappRoot = file;
188     }
189
190     public static File JavaDoc getWebappRoot()
191     {
192         return _webappRoot;
193     }
194
195 }
196
Popular Tags