1 23 package org.objectweb.jorm.util.lib; 24 25 import org.apache.tools.ant.taskdefs.MatchingTask; 26 import org.apache.tools.ant.types.DTDLocation; 27 import org.apache.tools.ant.types.Path; 28 import org.apache.tools.ant.types.Reference; 29 import org.apache.tools.ant.BuildException; 30 import org.objectweb.jorm.util.io.api.PathExplorer; 31 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 32 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 33 import org.objectweb.jorm.compiler.lib.JormCompiler; 34 import org.objectweb.jorm.compiler.lib.JormCompilerParameterImpl; 35 import org.objectweb.jorm.compiler.lib.JormCompilerConfiguratorImpl; 36 import org.objectweb.jorm.compiler.api.JormCompilerConfigurator; 37 import org.objectweb.jorm.api.PException; 38 39 import java.io.File ; 40 import java.util.*; 41 42 public class AntJormParser extends MatchingTask { 43 private String jormConfFile = "jorm.properties"; 44 private File pdDir = null; 45 46 49 private final static char separator = File.separatorChar; 50 51 private boolean antDtdLocationsSet = false; 52 private ArrayList antDtdLocations = new ArrayList(); 53 private boolean classpathSet = false; 54 private PathExplorer classpath = new DirJavaExplorer(); 55 private boolean antLogConfFileSet = false; 56 private String antLogConfFile; 57 private boolean antProjectNameSet = false; 58 private String antProjectName; 59 private boolean antVerboseSet = false; 60 private boolean antVerbose; 61 62 private JormCompilerParameter jcp = null; 63 private JormCompilerConfigurator jcc = null; 64 private JormCompiler jormcompiler = null; 65 66 public AntJormParser() { 67 68 jcp = new JormCompilerParameterImpl(); 69 jcc = new JormCompilerConfiguratorImpl(); 70 jormcompiler = new JormCompiler(jcp, jcc); 71 } 76 77 81 public void setPdDir(File d) { 82 pdDir = d; 83 classpath.addPath(d.getAbsolutePath()); 84 System.out.println("pdDir " + pdDir.getAbsolutePath()); 85 } 86 87 92 public void setOtherPdFiles(Path cp) { 93 String [] dl = cp.list(); 94 for (int i = 0; i < dl.length; i++) { 95 classpath.addPath(dl[i]); 96 classpathSet = true; 97 } 98 } 99 100 public void setOtherPdFilesRef(Reference r) { 101 Path p = new Path(project); 102 p.setRefid(r); 103 setOtherPdFiles(p); 104 } 105 106 112 public DTDLocation createDTD() { 113 DTDLocation dtdLocation = new DTDLocation(); 114 antDtdLocations.add(dtdLocation); 115 antDtdLocationsSet = true; 116 return dtdLocation; 117 } 118 119 123 public void setJormConf(String s) { 124 jormConfFile = s; 125 } 126 127 131 public void setLogConf(String s) { 132 antLogConfFile = s; 133 antLogConfFileSet = true; 134 } 135 136 142 public void setProjectName(String s) { 143 antProjectName = s; 144 antProjectNameSet = true; 145 } 146 147 153 public void setVerbose(boolean verbose) { 154 antVerbose = verbose; 155 antVerboseSet = true; 156 } 157 158 public void initJormCompiler() throws PException { 159 try { 160 if (jormConfFile == null) { 161 jcc.configure(); 162 } else { 164 jcc.configure(jormConfFile); 165 } log("jcc.configure() ----------- ok"); 167 Collection c = jcc.getSubMappers("rdb"); 168 log("rdb sub mappers: " + c); 169 if (!antLogConfFileSet) { 170 jcc.configureLog(jormConfFile); 171 } else { 172 jcc.configureLog(antLogConfFile); 173 } 174 log("jcc.configureLog() ----------- ok"); 175 log("jormcOpts file= " + jcc.getJormcOptsFile()); 177 jcp.loadConfFile(jcc.getGlobalJormcOptsFile(), jcc.knownMappers()); 178 log("jcp.loadConfFile ----------- ok"); 179 log("pdDir= " + pdDir); 180 if (antDtdLocationsSet) { 182 jcp.setDtdLocations(antDtdLocations); 183 } 184 if (antProjectName != null) { 185 jcp.setProjectName(antProjectName); 186 } 187 log("jcp.projectName= " + jcp.getProjectName()); 188 Collection temp = new MyArrayList(super.getDirectoryScanner(pdDir).getIncludedFiles()); 189 String inputFile = null; 190 for (Iterator it = temp.iterator(); it.hasNext();) { 191 inputFile = pdDir.getAbsolutePath() + separator + (String ) it.next(); 192 jcp.getInputFiles().add(inputFile); 193 log("input file= " + inputFile); 194 } 195 log("jcp.inputFiles ----------- ok"); 196 if (classpathSet) { 197 jcp.setClasspath(classpath); 198 } else { 199 DirJavaExplorer pe = new DirJavaExplorer(); 200 pe.setLogger(jcc.getLoggerFactory().getLogger("org.objectweb.jorm.io.pathexplorer")); 201 jcp.setClasspath(pe); 202 } 203 log("jcp.classpath ----------- ok"); 204 jcp.setGeneratedWithMapperPackage(false); 205 if (antLogConfFileSet) { 207 jcp.setLogConfFile(antLogConfFile); 208 } 209 if (antVerboseSet) { 210 jcp.setVerbose(antVerbose); 211 } 212 } catch (PException e) {; 213 throw new PException(e, "Impossible to configure the Jorm parser"); 214 } 215 } 216 217 218 221 public void execute() throws BuildException { 222 try { 223 initJormCompiler(); 224 jormcompiler.parseFiles(jcp.getInputFiles()); 225 log("parseFiles...... done"); 226 } catch (PException e) { 227 Exception current = e; 228 while (current instanceof PException 229 && ((PException) current).getNestedException() != null) { 230 current = ((PException) current).getNestedException(); 231 } 232 throw new BuildException(current); 233 } 234 } 235 } 236 237 class MyArrayList extends ArrayList { 238 public MyArrayList(Object [] os) { 239 super(os.length); 240 for (int i = 0; i < os.length; i++) 241 add(os[i]); 242 } 243 } 244 | Popular Tags |