1 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 ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 35 36 abstract class FlowControllerGenerator 37 extends BaseGenerator 38 { 39 private static long _compilerJarTimestamp = -1; 40 private static final boolean ALWAYS_GENERATE = true; private static final String 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 , FatalCompileTimeException; 51 52 public void generate( ClassDeclaration publicClass ) 53 { 54 GenStrutsApp app = null; 55 getFCSourceFileInfo().startBuild( getEnv(), publicClass ); 56 57 try 58 { 59 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 writeFieldAnnotations( publicClass, app ); 73 } 74 catch ( FatalCompileTimeException e ) 75 { 76 e.printDiagnostic( getDiagnostics() ); 77 } 78 catch ( Exception e ) 79 { 80 e.printStackTrace(); assert e instanceof XmlException || e instanceof IOException || e instanceof FileNotFoundException 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 additionalAnnotation ) 104 { 105 Collection fields = CompilerUtils.getClassFields( typeDecl ); 106 boolean hasFieldAnnotations = false; 107 108 if ( fields.size() > 0 ) 109 { 110 for ( Iterator 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 strutsConfigFile = null; 140 141 try 142 { 143 strutsConfigFile = app.getStrutsConfigFile(); 144 145 if ( ALWAYS_GENERATE || app.isStale() ) 146 { 147 app.writeToFile(); 149 } 150 else if ( _compilerJarTimestamp > strutsConfigFile.lastModified() ) 151 { 152 app.writeToFile(); 155 } 156 else 157 { 158 } 160 } 161 catch ( FatalCompileTimeException e ) 162 { 163 e.printDiagnostic( getDiagnostics() ); 164 } 165 catch ( Exception e ) 166 { 167 e.printStackTrace(); assert e instanceof FileNotFoundException 169 || e instanceof IOException 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 |