KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > metadata > commons > CommonsAttributeCompilerUtils


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.metadata.commons;
18
19 import java.io.File JavaDoc;
20 import java.net.URL JavaDoc;
21
22 import org.apache.commons.attributes.compiler.AttributeCompiler;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.taskdefs.Javac;
25 import org.apache.tools.ant.types.FileSet;
26 import org.apache.tools.ant.types.Path;
27
28 import org.springframework.core.ControlFlowFactory;
29
30 /**
31  * <p>Programmatic support classes for compiling with Commons Attributes
32  * so that tests can run within Eclipse.</p>
33  *
34  * <p>tools.jar needs to be on the Eclipse classpath (just add it explicitly when setting up
35  * the JDK. This class also has a dependency on the target test tree beeing '/target/test-classes'</p>
36  *
37  * @author Rod Johnson
38  */

39 public class CommonsAttributeCompilerUtils {
40
41     public static final String JavaDoc MARKER_FILE = "/org.springframework.test.marker";
42     
43     public static void compileAttributesIfNecessary(String JavaDoc testWildcards) {
44         if (inIde()) {
45             ideAttributeCompile(testWildcards);
46         }
47     }
48
49     public static boolean inIde() {
50         return inEclipse();
51     }
52
53     public static boolean inEclipse() {
54         // Use our AOP control flow functionality
55
return ControlFlowFactory.createControlFlow().underToken("eclipse.jdt");
56     }
57
58     public static void ideAttributeCompile(String JavaDoc testWildcards) {
59         System.out.println("Compiling attributes under IDE");
60         Project project = new Project();
61         
62         URL JavaDoc markerUrl = CommonsAttributeCompilerUtils.class.getResource(MARKER_FILE);
63         File JavaDoc markerFile = new File JavaDoc(markerUrl.getFile());
64         // we know marker is in /target/test-classes
65
File JavaDoc root = markerFile.getParentFile().getParentFile().getParentFile();
66         
67         project.setBaseDir(root);
68         project.init();
69
70         AttributeCompiler commonsAttributesCompiler = new AttributeCompiler();
71         commonsAttributesCompiler.setProject(project);
72
73         //commonsAttributesCompiler.setSourcepathref("test");
74
String JavaDoc tempPath = "target/generated-commons-attributes-src";
75         commonsAttributesCompiler.setDestdir(new File JavaDoc(tempPath));
76         FileSet fileset = new FileSet();
77         fileset.setDir(new File JavaDoc(root.getPath() + File.separator + "test"));
78         String JavaDoc attributeClasses = testWildcards;
79         fileset.setIncludes(attributeClasses);
80         commonsAttributesCompiler.addFileset(fileset);
81
82         commonsAttributesCompiler.execute();
83
84         System.out.println("Compiling Java sources generated by Commons Attributes using Javac: requires tools.jar on Eclipse project classpath");
85         // We now have the generated Java source: compile it.
86
// This requires Javac on the source path
87
Javac javac = new Javac();
88         javac.setProject(project);
89         //project.setCoreLoader(Thread.currentThread().getContextClassLoader());
90
Path path = new Path(project, tempPath);
91         javac.setSrcdir(path);
92
93         // Couldn't get this to work: trying to use Eclipse
94
//javac.setCompiler("org.eclipse.jdt.core.JDTCompilerAdapter");
95
javac.setDestdir(new File JavaDoc(root.getPath() + File.separator + "target/test-classes"));
96         javac.setIncludes(attributeClasses);
97         javac.execute();
98     }
99
100 }
101
Popular Tags