001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.compiler;
020:
021: import org.apache.beehive.netui.compiler.typesystem.declaration.TypeDeclaration;
022: import org.apache.beehive.netui.compiler.typesystem.declaration.MemberDeclaration;
023: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationInstance;
024: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationTypeElementDeclaration;
025: import org.apache.beehive.netui.compiler.typesystem.declaration.AnnotationValue;
026: import org.apache.beehive.netui.compiler.typesystem.type.TypeInstance;
027: import org.apache.beehive.netui.compiler.typesystem.env.CoreAnnotationProcessorEnv;
028: import org.apache.beehive.netui.compiler.model.StrutsApp;
029: import org.apache.beehive.netui.compiler.model.XmlModelWriter;
030: import org.apache.beehive.netui.compiler.model.XmlModelWriterException;
031: import org.w3c.dom.Element;
032:
033: import java.util.Map;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Date;
037: import java.io.File;
038: import java.io.IOException;
039: import java.io.PrintWriter;
040:
041: public class AnnotationToXML {
042: private static final String ANNOTATIONS_FILE_PREFIX = "annotations";
043:
044: private XmlModelWriter _xw;
045: private TypeDeclaration _typeDecl;
046:
047: public AnnotationToXML(TypeDeclaration typeDecl)
048: throws IOException, XmlModelWriterException {
049: _typeDecl = typeDecl;
050: String typeName = typeDecl.getQualifiedName();
051: StringBuffer comment = new StringBuffer(" Generated from ");
052: comment.append(typeName);
053: comment.append(" on ").append(new Date().toString())
054: .append(' ');
055: _xw = new XmlModelWriter(null, "processed-annotations", null,
056: null, comment.toString());
057: _xw.addElementWithText(_xw.getRootElement(), "type-name",
058: typeName);
059: }
060:
061: public void include(MemberDeclaration memberDecl,
062: AnnotationInstance annotation) {
063: String name = memberDecl instanceof TypeDeclaration ? ((TypeDeclaration) memberDecl)
064: .getQualifiedName()
065: : memberDecl.getSimpleName();
066: Element annotatedElement = _xw.addElement(_xw.getRootElement(),
067: "annotated-element");
068: _xw.addElementWithText(annotatedElement, "element-name", name);
069: Element xmlAnnotation = _xw.addElement(annotatedElement,
070: "annotation");
071: include(xmlAnnotation, annotation);
072: }
073:
074: private void include(Element xmlAnnotation,
075: AnnotationInstance annotation) {
076: String annotationName = annotation.getAnnotationType()
077: .getAnnotationTypeDeclaration().getQualifiedName();
078: _xw.addElementWithText(xmlAnnotation, "annotation-name",
079: annotationName);
080:
081: Map elementValues = annotation.getElementValues();
082:
083: for (Iterator i = elementValues.entrySet().iterator(); i
084: .hasNext();) {
085: Map.Entry entry = (Map.Entry) i.next();
086: AnnotationTypeElementDeclaration elementDecl = (AnnotationTypeElementDeclaration) entry
087: .getKey();
088: AnnotationValue annotationValue = (AnnotationValue) entry
089: .getValue();
090:
091: String name = elementDecl.getSimpleName();
092: Object value = annotationValue.getValue();
093: Element xmlAttr = _xw.addElement(xmlAnnotation,
094: "annotation-attribute");
095: _xw.addElementWithText(xmlAttr, "attribute-name", name);
096:
097: if (value instanceof List) {
098: for (Iterator j = ((List) value).iterator(); j
099: .hasNext();) {
100: Object o = j.next();
101: assert o instanceof AnnotationValue : o.getClass()
102: .getName();
103: Object listVal = ((AnnotationValue) o).getValue();
104:
105: // we only handle lists of annotations at the moment
106: assert listVal instanceof AnnotationInstance : listVal
107: .getClass().getName();
108: include(
109: _xw.addElement(xmlAttr, "annotation-value"),
110: (AnnotationInstance) listVal);
111: }
112: } else {
113: // we only support a few types at the moment
114: assert value instanceof TypeInstance
115: || value instanceof String : value.getClass()
116: .getName();
117: _xw.addElementWithText(xmlAttr, "string-value", value
118: .toString());
119: }
120: }
121: }
122:
123: public void writeXml(Diagnostics diagnostics,
124: CoreAnnotationProcessorEnv env) throws IOException,
125: XmlModelWriterException {
126: String outputFilePath = getFilePath(_typeDecl);
127: File outputFile = new File(outputFilePath);
128: PrintWriter writer = env.getFiler().createTextFile(outputFile);
129:
130: try {
131: _xw.simpleFastWrite(writer);
132: } finally {
133: writer.close();
134: }
135: }
136:
137: public static String getFilePath(TypeDeclaration typeDecl) {
138: String typeName = typeDecl.getQualifiedName();
139: return StrutsApp.getOutputFileURI(ANNOTATIONS_FILE_PREFIX,
140: typeName, false);
141: }
142: }
|