1 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 ; 32 import java.io.FileOutputStream ; 33 import java.io.FileReader ; 34 import java.io.PrintWriter ; 35 36 42 public class XmlAnnotationCompiler 43 { 44 public static void main(String [] args) throws Exception 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 pw, int indent) 56 { 57 for (int i = 0; i < indent * 3; i++) pw.print(" "); 58 } 59 60 61 public void compile(String [] args) throws Exception 62 { 63 if (args.length == 0) 64 { 65 usage(); 66 System.exit(1); 67 return; 68 } 69 String 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 f = new File (args[i]).getCanonicalFile(); 83 builder.addSource(new FileReader (f)); 84 } 85 86 FileOutputStream os = new FileOutputStream (outputFile); 87 PrintWriter pw = new PrintWriter (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 pw) throws Exception 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 pw, JavaMethod method, int indent, AnnotationDocletTag tag) throws Exception 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 pw, JavaField field, int indent, AnnotationDocletTag tag) throws Exception 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 pw, JavaMethod method, int indent, AnnotationDocletTag tag) throws Exception 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 |