KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > annotation > compiler > XmlAnnotationCompiler


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.annotation.compiler;
23
24 import com.thoughtworks.qdox.JavaDocBuilder;
25 import com.thoughtworks.qdox.model.JavaClass;
26 import com.thoughtworks.qdox.model.JavaField;
27 import com.thoughtworks.qdox.model.JavaMethod;
28 import com.thoughtworks.qdox.model.JavaParameter;
29 import com.thoughtworks.qdox.model.JavaSource;
30
31 import java.io.File JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35
36 /**
37  * Convert doclet tags to JBoss AOP annotation XML
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @version $Revision: 37406 $
41  */

42 public class XmlAnnotationCompiler
43 {
44    public static void main(String JavaDoc[] args) throws Exception JavaDoc
45    {
46       XmlAnnotationCompiler c = new XmlAnnotationCompiler();
47       c.compile(args);
48    }
49
50    public void usage()
51    {
52       System.err.println("Usage: annotationc <files>+");
53    }
54
55    public static void indenter(PrintWriter JavaDoc pw, int indent)
56    {
57       for (int i = 0; i < indent * 3; i++) pw.print(" ");
58    }
59
60
61    public void compile(String JavaDoc[] args) throws Exception JavaDoc
62    {
63       if (args.length == 0)
64       {
65          usage();
66          System.exit(1);
67          return;
68       }
69       String JavaDoc outputFile = "metadata-aop.xml";
70       JavaDocBuilder builder = new JavaDocBuilder(new AnnotationDocletTagFactory());
71       for (int i = 0; i < args.length; i++)
72       {
73          if (args[i].equals("-o"))
74          {
75             outputFile = args[++i];
76             continue;
77          }
78          else if (args[i].equals("-xml"))
79          {
80             continue;
81          }
82          File JavaDoc f = new File JavaDoc(args[i]).getCanonicalFile();
83          builder.addSource(new FileReader JavaDoc(f));
84       }
85
86       FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(outputFile);
87       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
88       pw.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
89       pw.println("<aop>");
90       for (int i = 0; i < builder.getSources().length; i++)
91       {
92          JavaSource src = builder.getSources()[i];
93          for (int j = 0; j < src.getClasses().length; j++)
94          {
95             JavaClass clazz = src.getClasses()[j];
96             compileClass(clazz, pw);
97          }
98       }
99       pw.println("</aop>");
100       pw.close();
101       os.close();
102    }
103
104    private void compileClass(JavaClass clazz, PrintWriter JavaDoc pw) throws Exception JavaDoc
105    {
106       int indent = 1;
107       for (int i = 0; i < clazz.getTags().length; i++)
108       {
109          AnnotationDocletTag tag = (AnnotationDocletTag) clazz.getTags()[i];
110          if (tag.getAnnotation() == null) continue;
111
112          indenter(pw, indent);
113          pw.println("<metadata tag=\"" + tag.getName() + "\" class=\"" + clazz.getFullyQualifiedName() + "\">");
114          indent++;
115          indenter(pw, indent);
116          pw.println("<class>");
117          indent++;
118          XmlAnnotationVisitor visitor = new XmlAnnotationVisitor(indent, pw);
119          if (tag.getAnnotation().jjtGetNumChildren() > 0) tag.getAnnotation().jjtGetChild(0).jjtAccept(visitor, null);
120          indent--;
121          indenter(pw, indent);
122          pw.println("</class>");
123          indent--;
124          indenter(pw, indent);
125          pw.println("</metadata>");
126       }
127       for (int i = 0; i < clazz.getMethods().length; i++)
128       {
129          JavaMethod method = clazz.getMethods()[i];
130          for (int j = 0; j < method.getTags().length; j++)
131          {
132             AnnotationDocletTag tag = (AnnotationDocletTag) method.getTags()[j];
133             if (tag.getAnnotation() == null) continue;
134             indenter(pw, indent);
135             pw.println("<metadata tag=\"" + tag.getName() + "\" class=\"" + clazz.getFullyQualifiedName() + "\">");
136             indent++;
137             if (method.isConstructor())
138             {
139                indent = printConstructor(pw, method, indent, tag);
140             }
141             else
142             {
143                indent = printMethod(pw, method, indent, tag);
144             }
145             indent--;
146             indenter(pw, indent);
147             pw.println("</metadata>");
148          }
149       }
150       for (int i = 0; i < clazz.getFields().length; i++)
151       {
152          JavaField field = clazz.getFields()[i];
153          for (int j = 0; j < field.getTags().length; j++)
154          {
155             AnnotationDocletTag tag = (AnnotationDocletTag) field.getTags()[j];
156             if (tag.getAnnotation() == null) continue;
157             indenter(pw, indent);
158             pw.println("<metadata tag=\"" + tag.getName() + "\" class=\"" + clazz.getFullyQualifiedName() + "\">");
159             indent++;
160             printField(pw, field, indent, tag);
161             indent--;
162             indenter(pw, indent);
163             pw.println("</metadata>");
164          }
165       }
166
167
168    }
169
170    private int printMethod(PrintWriter JavaDoc pw, JavaMethod method, int indent, AnnotationDocletTag tag) throws Exception JavaDoc
171    {
172       indenter(pw, indent);
173       pw.print("<method expr=\"");
174       pw.print(method.getReturns().toString());
175       pw.print(" " + method.getName() + "(");
176       boolean first = true;
177       for (int k = 0; k < method.getParameters().length; k++)
178       {
179          JavaParameter param = method.getParameters()[k];
180          if (!first)
181             pw.print(", ");
182          else
183             first = false;
184          pw.print(param.getType().toString());
185       }
186       pw.println(")\">");
187       indent++;
188       XmlAnnotationVisitor visitor = new XmlAnnotationVisitor(indent, pw);
189       if (tag.getAnnotation().jjtGetNumChildren() > 0) tag.getAnnotation().jjtGetChild(0).jjtAccept(visitor, null);
190       indent--;
191       indenter(pw, indent);
192       pw.println("</method>");
193       return indent;
194    }
195
196    private int printField(PrintWriter JavaDoc pw, JavaField field, int indent, AnnotationDocletTag tag) throws Exception JavaDoc
197    {
198       indenter(pw, indent);
199       pw.println("<field name=\"" + field.getName() + "\">");
200       indent++;
201       XmlAnnotationVisitor visitor = new XmlAnnotationVisitor(indent, pw);
202       if (tag.getAnnotation().jjtGetNumChildren() > 0) tag.getAnnotation().jjtGetChild(0).jjtAccept(visitor, null);
203       indent--;
204       indenter(pw, indent);
205       pw.println("</field>");
206       return indent;
207    }
208
209    private int printConstructor(PrintWriter JavaDoc pw, JavaMethod method, int indent, AnnotationDocletTag tag) throws Exception JavaDoc
210    {
211       indenter(pw, indent);
212       pw.print("<constructor expr=\"");
213       pw.print(method.getName() + "(");
214       boolean first = true;
215       for (int k = 0; k < method.getParameters().length; k++)
216       {
217          JavaParameter param = method.getParameters()[k];
218          if (!first)
219             pw.print(", ");
220          else
221             first = false;
222          pw.print(param.getType().toString());
223       }
224       pw.println(")\">");
225       indent++;
226       XmlAnnotationVisitor visitor = new XmlAnnotationVisitor(indent, pw);
227       if (tag.getAnnotation().jjtGetNumChildren() > 0) tag.getAnnotation().jjtGetChild(0).jjtAccept(visitor, null);
228       indent--;
229       indenter(pw, indent);
230       pw.println("</constructor>");
231       return indent;
232    }
233 }
234
Popular Tags