1 /*
2 * Nanning Aspects
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */
7 package org.codehaus.nanning.attribute;
8
9 import com.thoughtworks.qdox.parser.impl.JFlexLexer;
10 import com.thoughtworks.qdox.parser.impl.Parser;
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.DirectoryScanner;
13 import org.apache.tools.ant.Task;
14
15 import java.io.*;
16 import java.util.List;
17 import java.util.Iterator;
18
19 /***
20 * Compiles attributes from java sources, use as an ant task or directly from java.
21 * <p>
22 * Example usage as Ant task: <pre>
23 <taskdef name="attributes-compiler" classname="org.codehaus.nanning.attributes.AttributesCompiler"
24 classpath="path/to/lib/nanning-version.jar:path/to/lib/qdox-1.2.jar" />
25 <attributes-compiler src="path/to/java/files" dest="path/to/compile/target" />
26 </pre>
27 * <p>
28 * Example usage directly from Java: <pre>
29 AttributesCompiler attributesCompiler = new AttributesCompiler();
30 attributesCompiler.setSrc(source);
31 attributesCompiler.setDest(attributesDir);
32 attributesCompiler.execute();
33 </pre>
34 *
35 * After compilation, the destination-directory needs to be included in the classpath.
36 * <p>
37 * To use under Maven, add a preGoal to java:compile and test:compile that executes
38 * the above ant-code.
39 * <p>
40 * To use directly in JUnit (without the need for external recompilation
41 * good for execution within an IDE), write an abstract base-class for your
42 * tests that executes the above Java-code to compile your attributes before
43 * the actual tests are executed.
44 *
45 * <!-- $Id: AttributesCompiler.java,v 1.1 2003/07/04 10:53:59 lecando Exp $ -->
46 *
47 * @author $Author: lecando $
48 * @version $Revision: 1.1 $
49 */
50 public class AttributesCompiler extends Task {
51 private File src;
52 private File dest;
53
54 public void setSrc(File src) {
55 this.src = src;
56 }
57
58 public void setDest(File dest) {
59 this.dest = dest;
60 }
61
62 public void execute() {
63 try {
64 boolean hasCompiled = false;
65
66 String[] files = getJavaFiles();
67 for (int i = 0; i < files.length; i++) {
68 final File javaFile = new File(src, files[i]);
69 final File attributeFile = getAttributeFile(files[i]);
70 if (!attributeFile.exists() || attributeFile.lastModified() < javaFile.lastModified()) {
71 createAttributeFiles(javaFile);
72 if (!hasCompiled) {
73 System.out.println("Compiling attributes for " + src + " into " + dest);
74 hasCompiled = true;
75 }
76 }
77 }
78 } catch (IOException e) {
79 throw new BuildException(e);
80 }
81 }
82
83 private String[] getJavaFiles() {
84 DirectoryScanner scanner = new DirectoryScanner();
85 scanner.setBasedir(src);
86 scanner.setIncludes(new String[]{"**/*.java"});
87 scanner.scan();
88 return scanner.getIncludedFiles();
89 }
90
91 private File getAttributeFile(String javaFileName) {
92 File result = new File(dest, javaFileName.substring(0, javaFileName.length() - 5) +
93 PropertyFileAttributeLoader.ATTRIBUTE_FILE_SUFFIX);
94 result.getParentFile().mkdirs();
95 return result;
96 }
97
98 private void createAttributeFiles(File javaFile) throws IOException {
99 List result = parseClassAttribute(javaFile);
100 for (Iterator iterator = result.iterator(); iterator.hasNext();) {
101 ClassPropertiesHelper properties = (ClassPropertiesHelper) iterator.next();
102 properties.store(dest);
103 }
104 }
105
106 List parseClassAttribute(File javaFile) throws IOException {
107 InputStream input = new FileInputStream(javaFile);
108 try {
109 AttributesBuilder builder = new AttributesBuilder();
110 new Parser(new JFlexLexer(input), builder).parse();
111 return builder.getClassPropertiesHelpers();
112 } finally {
113 input.close();
114 }
115 }
116
117 }
This page was automatically generated by Maven