KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38
39 public class AnnotationToXML
40 {
41     private static final String JavaDoc 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 JavaDoc 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 JavaDoc elementValues = annotation.getElementValues();
70         
71         for ( Iterator JavaDoc i = elementValues.entrySet().iterator(); i.hasNext(); )
72         {
73             Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) i.next();
74             AnnotationTypeElementDeclaration elementDecl = ( AnnotationTypeElementDeclaration ) entry.getKey();
75             AnnotationValue annotationValue = ( AnnotationValue ) entry.getValue();
76             
77             String JavaDoc name = elementDecl.getSimpleName();
78             Object JavaDoc value = annotationValue.getValue();
79             AnnotationAttribute xmlAttr = xmlAnnotation.addNewAnnotationAttribute();
80             xmlAttr.setAttributeName( name );
81             
82             if ( value instanceof List JavaDoc )
83             {
84                 for ( Iterator JavaDoc j = ( ( List JavaDoc ) value ).iterator(); j.hasNext(); )
85                 {
86                     Object JavaDoc o = j.next();
87                     assert o instanceof AnnotationValue : o.getClass().getName();
88                     Object JavaDoc listVal = ( ( AnnotationValue ) o ).getValue();
89                     
90                     // we only handle lists of annotations at the moment
91
assert listVal instanceof AnnotationInstance : listVal.getClass().getName();
92                     include( xmlAttr.addNewAnnotationValue(), ( AnnotationInstance ) listVal );
93                 }
94             }
95             else
96             {
97                 // we only support a few types at the moment
98
assert value instanceof TypeInstance || value instanceof String JavaDoc : value.getClass().getName();
99                 xmlAttr.setStringValue1( value.toString() );
100             }
101         }
102     }
103     
104     public void writeXml( String JavaDoc webappBuildRoot, Diagnostics diagnostics )
105     {
106         String JavaDoc typeName = _typeDecl.getQualifiedName();
107         String JavaDoc 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 JavaDoc( outputFilePath ), options );
114         }
115         catch ( IOException JavaDoc e )
116         {
117             diagnostics.addError( _typeDecl, "error.could-not-generate", outputFilePath, e.getMessage() );
118         }
119     }
120 }
121
Popular Tags