Package org.apache.beehive.netui.compiler

Source Code of org.apache.beehive.netui.compiler.AnnotationToXML

/*
* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Header:$
*/
package org.apache.beehive.netui.compiler;

import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotationsDocument;
import org.apache.beehive.netui.compiler.schema.annotations.AnnotatedElement;
import org.apache.beehive.netui.compiler.schema.annotations.ProcessedAnnotation;
import org.apache.beehive.netui.compiler.schema.annotations.AnnotationAttribute;
import org.apache.beehive.netui.compiler.model.StrutsApp;
import org.apache.xmlbeans.XmlOptions;

import java.util.Map;
import java.util.Iterator;
import java.util.List;
import java.io.File;
import java.io.IOException;

public class AnnotationToXML
{
    private static final String ANNOTATIONS_FILE_PREFIX = "jpf-annotations";
   
    private ProcessedAnnotationsDocument _doc;
    private TypeDeclaration _typeDecl;
   
    public AnnotationToXML( TypeDeclaration typeDecl )
    {
        _doc = ProcessedAnnotationsDocument.Factory.newInstance();
        _typeDecl = typeDecl;
        ProcessedAnnotationsDocument.ProcessedAnnotations pa = _doc.addNewProcessedAnnotations();
        pa.setTypeName( typeDecl.getQualifiedName() );
    }
   
    public void include( MemberDeclaration memberDecl, AnnotationInstance annotation )
    {
        AnnotatedElement element = _doc.getProcessedAnnotations().addNewAnnotatedElement();
        String name = memberDecl instanceof TypeDeclaration
                      ? ( ( TypeDeclaration ) memberDecl ).getQualifiedName()
                      : memberDecl.getSimpleName();
        element.setElementName( name );
        ProcessedAnnotation xmlAnnotation = element.addNewAnnotation();
        include( xmlAnnotation, annotation );
    }
   
    private void include( ProcessedAnnotation xmlAnnotation, AnnotationInstance annotation )
    {
        xmlAnnotation.setAnnotationName( annotation.getAnnotationType().getAnnotationTypeDeclaration().getQualifiedName() );
       
        Map elementValues = annotation.getElementValues();
       
        for ( Iterator i = elementValues.entrySet().iterator(); i.hasNext(); )
        {
            Map.Entry entry = ( Map.Entry ) i.next();
            AnnotationTypeElementDeclaration elementDecl = ( AnnotationTypeElementDeclaration ) entry.getKey();
            AnnotationValue annotationValue = ( AnnotationValue ) entry.getValue();
           
            String name = elementDecl.getSimpleName();
            Object value = annotationValue.getValue();
            AnnotationAttribute xmlAttr = xmlAnnotation.addNewAnnotationAttribute();
            xmlAttr.setAttributeName( name );
           
            if ( value instanceof List )
            {
                for ( Iterator j = ( ( List ) value ).iterator(); j.hasNext(); )
                {
                    Object o = j.next();
                    assert o instanceof AnnotationValue : o.getClass().getName();
                    Object listVal = ( ( AnnotationValue ) o ).getValue();
                   
                    // we only handle lists of annotations at the moment
                    assert listVal instanceof AnnotationInstance : listVal.getClass().getName();
                    include( xmlAttr.addNewAnnotationValue(), ( AnnotationInstance ) listVal );
                }
            }
            else
            {
                // we only support a few types at the moment
                assert value instanceof TypeInstance || value instanceof String : value.getClass().getName();
                xmlAttr.setStringValue1( value.toString() );
            }
        }
    }
   
    public void writeXml( String webappBuildRoot, Diagnostics diagnostics )
    {
        String typeName = _typeDecl.getQualifiedName();
        String outputFilePath = webappBuildRoot + StrutsApp.getOutputFileURI( ANNOTATIONS_FILE_PREFIX, typeName, false );
       
        try
        {
            XmlOptions options = new XmlOptions();
            options.setSavePrettyPrint();
            _doc.save( new File( outputFilePath ), options );
        }
        catch ( IOException e )
        {
            diagnostics.addError( _typeDecl, "error.could-not-generate", outputFilePath, e.getMessage() );
        }
    }
}
TOP

Related Classes of org.apache.beehive.netui.compiler.AnnotationToXML

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.