00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper.ant;
00011
00012 import java.io.File;
00013 import java.io.FileOutputStream;
00014 import java.io.InputStream;
00015 import java.lang.reflect.Method;
00016 import java.net.URL;
00017 import java.net.URLClassLoader;
00018
00019 import javax.xml.transform.Source;
00020 import javax.xml.transform.Transformer;
00021 import javax.xml.transform.TransformerFactory;
00022 import javax.xml.transform.stream.StreamResult;
00023 import javax.xml.transform.stream.StreamSource;
00024
00025 import org.apache.tools.ant.BuildException;
00026 import org.apache.tools.ant.Task;
00027
00028 import dochelper.Main;
00029 import dochelper.exceptions.InitializationException;
00030
00035 public class AntMain extends Task {
00036
00038 public AntMain() {
00039 }
00040
00044 String _docdef;
00045
00049 String _dochelperjar;
00050
00054 String _temppath = null;
00055
00056
00057
00062 public void setDocdef(String path) {
00063 _docdef = path;
00064 }
00065
00070 public void setTemppath(String path) {
00071 _temppath = path;
00072 }
00073
00078 public void setDochelperjar(String path) {
00079 _dochelperjar = path;
00080 }
00081
00086 public void init() throws BuildException {
00087 super.init();
00088
00089 _dochelperjar = System.getProperty("ant.home") + "/lib/dochelper_ant.jar";
00090
00091 try {
00092
00093 File t = new File(_dochelperjar);
00094
00095 if (!t.exists()) {
00096 throw new BuildException("The dochelper_ant.jar not installed in $ANT_HOME/lib.");
00097 }
00098 }
00099 catch(Exception e) {
00100 throw new BuildException("The dochelper_ant.jar not installed in $ANT_HOME/lib.");
00101 }
00102 }
00103
00107 public void execute() {
00108
00109 try {
00110
00111
00112 try {
00113
00114 new File(_docdef);
00115 }
00116 catch(Exception e) {
00117 throw new InitializationException(e.getMessage());
00118 }
00119
00120 if(_temppath == null)
00121 _temppath = System.getProperty("user.dir");
00122
00123
00124 String styleSheet = "xslt/CreateMain.xsl";
00125 InputStream in = Main.class.getClassLoader().getResourceAsStream(styleSheet);
00126 Source xsltSource = new StreamSource(in);
00127
00128
00129 File xmlFile = new File(_docdef);
00130 Source xmlSource = new StreamSource(xmlFile);
00131
00132
00133 TransformerFactory transFact = TransformerFactory.newInstance();
00134 Transformer trans = transFact.newTransformer(xsltSource);
00135
00136
00137 FileOutputStream fos = new FileOutputStream(_temppath + "/DocDef.java");
00138 trans.transform(xmlSource, new StreamResult(fos));
00139 fos.flush();
00140 fos.close();
00141
00142
00143 int errorCode = com.sun.tools.javac.Main.compile(new String[] {
00144 "-cp", _dochelperjar,
00145 "-d", _temppath,
00146 _temppath + "/DocDef.java" });
00147
00148
00149 File classesDir = new File(_temppath);
00150
00151
00152 URLClassLoader loader1 = new URLClassLoader( new URL[] { classesDir.toURI().toURL() }, Main.class.getClassLoader());
00153 Class<?> docdef = loader1.loadClass("DocDef");
00154
00155 Class<?> param[] = { String[].class };
00156 Method docdefMain = docdef.getDeclaredMethod("main", param);
00157 String params[] = { _docdef, _temppath};
00158 Object args[] = { params };
00159 docdefMain.invoke(null, args);
00160
00161
00162
00163 }
00164 catch(Exception e) {
00165 e.printStackTrace();
00166 }
00167
00168
00169 }
00170
00171 }