1 18 package org.apache.beehive.netui.compiler; 19 20 import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration; 21 import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration; 22 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance; 23 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration; 24 import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue; 25 import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance; 26 import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotationsDocument; 27 import org.apache.beehive.netui.compiler.schema.annotations.AnnotatedElement; 28 import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotation; 29 import org.apache.beehive.netui.compiler.schema.annotations.AnnotationAttribute; 30 import org.apache.beehive.netui.compiler.model.StrutsApp; 31 import org.apache.xmlbeans.XmlOptions; 32 33 import java.util.Map ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.io.File ; 37 import java.io.IOException ; 38 39 public class AnnotationToXML 40 { 41 private static final String ANNOTATIONS_FILE_PREFIX = "jpf-annotations"; 42 43 private ProcessedAnnotationsDocument _doc; 44 private TypeDeclaration _typeDecl; 45 46 public AnnotationToXML( TypeDeclaration typeDecl ) 47 { 48 _doc = ProcessedAnnotationsDocument.Factory.newInstance(); 49 _typeDecl = typeDecl; 50 ProcessedAnnotationsDocument.ProcessedAnnotations pa = _doc.addNewProcessedAnnotations(); 51 pa.setTypeName( typeDecl.getQualifiedName() ); 52 } 53 54 public void include( MemberDeclaration memberDecl, AnnotationInstance annotation ) 55 { 56 AnnotatedElement element = _doc.getProcessedAnnotations().addNewAnnotatedElement(); 57 String name = memberDecl instanceof TypeDeclaration 58 ? ( ( TypeDeclaration ) memberDecl ).getQualifiedName() 59 : memberDecl.getSimpleName(); 60 element.setElementName( name ); 61 ProcessedAnnotation xmlAnnotation = element.addNewAnnotation(); 62 include( xmlAnnotation, annotation ); 63 } 64 65 private void include( ProcessedAnnotation xmlAnnotation, AnnotationInstance annotation ) 66 { 67 xmlAnnotation.setAnnotationName( annotation.getAnnotationType().getAnnotationTypeDeclaration().getQualifiedName() ); 68 69 Map elementValues = annotation.getElementValues(); 70 71 for ( Iterator i = elementValues.entrySet().iterator(); i.hasNext(); ) 72 { 73 Map.Entry entry = ( Map.Entry ) i.next(); 74 AnnotationTypeElementDeclaration elementDecl = ( AnnotationTypeElementDeclaration ) entry.getKey(); 75 AnnotationValue annotationValue = ( AnnotationValue ) entry.getValue(); 76 77 String name = elementDecl.getSimpleName(); 78 Object value = annotationValue.getValue(); 79 AnnotationAttribute xmlAttr = xmlAnnotation.addNewAnnotationAttribute(); 80 xmlAttr.setAttributeName( name ); 81 82 if ( value instanceof List ) 83 { 84 for ( Iterator j = ( ( List ) value ).iterator(); j.hasNext(); ) 85 { 86 Object o = j.next(); 87 assert o instanceof AnnotationValue : o.getClass().getName(); 88 Object listVal = ( ( AnnotationValue ) o ).getValue(); 89 90 assert listVal instanceof AnnotationInstance : listVal.getClass().getName(); 92 include( xmlAttr.addNewAnnotationValue(), ( AnnotationInstance ) listVal ); 93 } 94 } 95 else 96 { 97 assert value instanceof TypeInstance || value instanceof String : value.getClass().getName(); 99 xmlAttr.setStringValue1( value.toString() ); 100 } 101 } 102 } 103 104 public void writeXml( String webappBuildRoot, Diagnostics diagnostics ) 105 { 106 String typeName = _typeDecl.getQualifiedName(); 107 String outputFilePath = webappBuildRoot + StrutsApp.getOutputFileURI( ANNOTATIONS_FILE_PREFIX, typeName, false ); 108 109 try 110 { 111 XmlOptions options = new XmlOptions(); 112 options.setSavePrettyPrint(); 113 _doc.save( new File ( outputFilePath ), options ); 114 } 115 catch ( IOException e ) 116 { 117 diagnostics.addError( _typeDecl, "error.could-not-generate", outputFilePath, e.getMessage() ); 118 } 119 } 120 } 121 | Popular Tags |