KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > FlowControllerGenerator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.compiler;
19
20 import org.apache.beehive.netui.compiler.genmodel.GenStrutsApp;
21 import org.apache.beehive.netui.compiler.genmodel.GenValidationModel;
22 import org.apache.beehive.netui.compiler.typesystem.declaration.ClassDeclaration;
23 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
24 import org.apache.beehive.netui.compiler.typesystem.declaration.FieldDeclaration;
25 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
26 import org.apache.beehive.netui.compiler.typesystem.env.AnnotationProcessorEnvironment;
27 import org.apache.xmlbeans.XmlException;
28
29 import java.io.File JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34
35
36 abstract class FlowControllerGenerator
37         extends BaseGenerator
38 {
39     private static long _compilerJarTimestamp = -1;
40     private static final boolean ALWAYS_GENERATE = true; // TODO: this turns stale checking off. Do we need it?
41
private static final String JavaDoc CONTROL_ANNOTATION = JpfLanguageConstants.BEEHIVE_PACKAGE + ".controls.api.bean.Control";
42     
43     protected FlowControllerGenerator( AnnotationProcessorEnvironment env, FlowControllerInfo fcInfo,
44                                        Diagnostics diagnostics )
45     {
46         super( env, fcInfo, diagnostics );
47     }
48     
49     protected abstract GenStrutsApp createStrutsApp( ClassDeclaration cl )
50             throws XmlException, IOException JavaDoc, FatalCompileTimeException;
51     
52     public void generate( ClassDeclaration publicClass )
53     {
54         GenStrutsApp app = null;
55         getFCSourceFileInfo().startBuild( getEnv(), publicClass );
56         
57         try
58         {
59             // Write the Struts config XML, and the Validator config XML if appropriate.
60
app = createStrutsApp( publicClass );
61             GenValidationModel validationModel = new GenValidationModel( publicClass, app, getEnv() );
62             
63             if ( ! validationModel.isEmpty() )
64             {
65                 app.setValidationModel( validationModel );
66                 validationModel.writeToFile();
67             }
68             
69             generateStrutsConfig( app, publicClass );
70             
71             // First, write out XML for any fields annotated with @Jpf.SharedFlowField or @Control.
72
writeFieldAnnotations( publicClass, app );
73         }
74         catch ( FatalCompileTimeException e )
75         {
76             e.printDiagnostic( getDiagnostics() );
77         }
78         catch ( Exception JavaDoc e )
79         {
80             e.printStackTrace(); // @TODO log
81
assert e instanceof XmlException || e instanceof IOException JavaDoc || e instanceof FileNotFoundException JavaDoc
82                     : e.getClass().getName();
83             getDiagnostics().addError( publicClass, "error.could-not-generate",
84                                        app != null ? app.getStrutsConfigFile() : null, e.getMessage() );
85         }
86         finally
87         {
88             getFCSourceFileInfo().endBuild();
89         }
90     }
91
92     private void writeFieldAnnotations( ClassDeclaration classDecl, GenStrutsApp app )
93             throws FatalCompileTimeException
94     {
95         AnnotationToXML atx = new AnnotationToXML( classDecl );
96         
97         if ( includeFieldAnnotations( atx, classDecl, null ) )
98         {
99             atx.writeXml( CompilerUtils.getWebBuildRoot( getEnv() ), getDiagnostics() );
100         }
101     }
102     
103     static boolean includeFieldAnnotations( AnnotationToXML atx, TypeDeclaration typeDecl, String JavaDoc additionalAnnotation )
104     {
105         Collection JavaDoc fields = CompilerUtils.getClassFields( typeDecl );
106         boolean hasFieldAnnotations = false;
107         
108         if ( fields.size() > 0 )
109         {
110             for ( Iterator JavaDoc i = fields.iterator(); i.hasNext(); )
111             {
112                 FieldDeclaration field = ( FieldDeclaration ) i.next();
113                 AnnotationInstance fieldAnnotation =
114                         CompilerUtils.getAnnotation( field, JpfLanguageConstants.SHARED_FLOW_FIELD_TAG_NAME );
115                 
116                 if ( fieldAnnotation == null )
117                 {
118                     fieldAnnotation = CompilerUtils.getAnnotationFullyQualified( field, CONTROL_ANNOTATION );
119                 }
120                 
121                 if ( fieldAnnotation == null && additionalAnnotation != null )
122                 {
123                     fieldAnnotation = CompilerUtils.getAnnotation( field, additionalAnnotation );
124                 }
125                 
126                 if ( fieldAnnotation != null )
127                 {
128                     atx.include( field, fieldAnnotation );
129                     hasFieldAnnotations = true;
130                 }
131             }
132         }
133         
134         return hasFieldAnnotations;
135     }
136     
137     protected void generateStrutsConfig( GenStrutsApp app, ClassDeclaration publicClass )
138     {
139         File JavaDoc strutsConfigFile = null;
140         
141         try
142         {
143             strutsConfigFile = app.getStrutsConfigFile();
144
145             if ( ALWAYS_GENERATE || app.isStale() )
146             {
147                 // @TODO logger.info( "Writing Struts module: " + _strutsConfig.getStrutsConfigFile() );
148
app.writeToFile();
149             }
150             else if ( _compilerJarTimestamp > strutsConfigFile.lastModified() )
151             {
152                 // @TODO logger.info( _compilerJarName + " has been updated; writing Struts module "
153
// + _strutsConfig.getStrutsConfigFile() );
154
app.writeToFile();
155             }
156             else
157             {
158                 // @TODO logger.info( "Struts module " + _strutsConfig.getStrutsConfigFile() + " is up-to-date." );
159
}
160         }
161         catch ( FatalCompileTimeException e )
162         {
163             e.printDiagnostic( getDiagnostics() );
164         }
165         catch ( Exception JavaDoc e )
166         {
167             e.printStackTrace(); // @TODO get rid of this
168
assert e instanceof FileNotFoundException JavaDoc
169                    || e instanceof IOException JavaDoc
170                    || e instanceof XmlException
171                     : e.getClass().getName();
172             
173             getDiagnostics().addError( publicClass, "error.could-not-generate",
174                                        strutsConfigFile != null ? strutsConfigFile.getPath() : null,
175                                        e.getMessage() );
176         }
177     }
178
179     protected FlowControllerInfo getFCSourceFileInfo()
180     {
181         return ( FlowControllerInfo ) super.getSourceFileInfo();
182     }
183 }
184
Popular Tags