1 23 24 package org.objectweb.jorm.compiler.lib; 25 26 import org.objectweb.jorm.api.PException; 27 import org.objectweb.jorm.compiler.api.PExceptionCompiler; 28 import org.objectweb.jorm.compiler.api.JormCompilerParameter; 29 import org.objectweb.jorm.util.io.api.PathExplorer; 30 import org.objectweb.jorm.util.io.lib.DirJavaExplorer; 31 import org.objectweb.util.monolog.api.BasicLevel; 32 import org.objectweb.util.monolog.api.Logger; 33 import org.objectweb.util.monolog.api.LoggerFactory; 34 35 import java.io.FileInputStream ; 36 import java.io.IOException ; 37 import java.io.InputStream ; 38 import java.util.ArrayList ; 39 import java.util.Collection ; 40 import java.util.Properties ; 41 import java.util.StringTokenizer ; 42 import java.util.Iterator ; 43 44 50 public class JormCompilerParameterImpl implements JormCompilerParameter { 51 private String logConfFile = null; 52 55 private PathExplorer classpath = new DirJavaExplorer(); 56 57 60 private String output = null; 61 62 65 private boolean verbose = false; 66 67 70 private Collection inputFiles = new ArrayList (); 71 72 75 private boolean keepSrc = true; 76 77 81 private String javacName = "javac"; 82 83 86 private String projectName = ""; 87 88 92 private boolean javac = false; 93 94 97 private boolean parseOnly = false; 98 99 102 private String bindingInheritance = null; 103 104 107 private String classMappingInheritance = null; 108 109 113 private boolean bindingAbstract = false; 114 115 118 private Logger logger = null; 119 120 123 private ArrayList dtdLocations = new ArrayList (); 124 125 128 private boolean generatedPDFiles = false; 129 130 private boolean generatedWithMapperPackage = true; 131 132 135 public void print() { 136 if (logger.isLoggable(BasicLevel.DEBUG)) { 137 logger.log(BasicLevel.DEBUG, "output: " + output); 138 logger.log(BasicLevel.DEBUG, "verbose: " + verbose); 139 logger.log(BasicLevel.DEBUG, "inputFiles: "); 140 Iterator it = inputFiles.iterator(); 141 while (it.hasNext()) { 142 logger.log(BasicLevel.DEBUG, ((String ) it.next())); 143 } 144 } 145 } 146 147 154 public String computePClassMappingInheritance(String cn) { 155 return computeInheritanceString(cn, classMappingInheritance); 156 } 157 158 165 public String computePBindingInheritance(String cn) { 166 return computeInheritanceString(cn, bindingInheritance); 167 } 168 169 173 public void loadConfFile(String file, Iterator knownmappers) throws PException { 174 try { 175 loadConfFile(new FileInputStream (file), knownmappers); 176 } catch (IOException e) { 177 throw new PExceptionCompiler(e, "Cannot open the jorm configuration file: " + file); 178 } 179 } 180 181 185 public void loadConfFile(InputStream in, Iterator knownmappers) throws PException { 186 if (in == null) { 187 throw new PExceptionCompiler("Cannot load jorm configuration file: InputStream is null."); 188 } 189 Properties p = new Properties (); 190 try { 191 p.load(in); 192 } catch (IOException e) { 193 throw new PExceptionCompiler(e, "An error occured during the loading of the jorm configuration file."); 194 } 195 String s = null; 196 if ((s = p.getProperty("log.config.file")) != null) 197 logConfFile = s; 198 if ((s = p.getProperty("keepsrc")) != null) 199 keepSrc = new Boolean (s).booleanValue(); 200 if ((s = p.getProperty("verbose")) != null) 201 verbose = new Boolean (s).booleanValue(); 202 if ((s = p.getProperty("javacompiler")) != null) 203 javacName = s; 204 if ((s = p.getProperty("projectname")) != null) 205 projectName = s; 206 if ((s = p.getProperty("bindinginheritance")) != null) 207 bindingInheritance = s; 208 if ((s = p.getProperty("classmappinginheritance")) != null) 209 classMappingInheritance = s; 210 if ((s = p.getProperty("inputfiles")) != null) { 211 StringTokenizer tok = new StringTokenizer (s, ","); 212 while (tok.hasMoreTokens()) { 213 inputFiles.add(tok.nextToken().trim()); 214 } 215 } 216 } 217 218 public PathExplorer getClasspath() { 219 return classpath; 220 } 221 222 public String getOutput() { 223 return output; 224 } 225 226 public boolean isVerbose() { 227 return verbose; 228 } 229 230 public Collection getInputFiles() { 231 return inputFiles; 232 } 233 234 public boolean isKeepSrc() { 235 return keepSrc; 236 } 237 238 public String getJavacName() { 239 return javacName; 240 } 241 242 public String getProjectName() { 243 return projectName; 244 } 245 246 public boolean isJavac() { 247 return javac; 248 } 249 250 public boolean isParseOnly() { 251 return parseOnly; 252 } 253 254 public String getBindingInheritance() { 255 return bindingInheritance; 256 } 257 258 public String getClassMappingInheritance() { 259 return classMappingInheritance; 260 } 261 262 public boolean isBindingAbstract() { 263 return bindingAbstract; 264 } 265 266 public ArrayList getDtdLocations() { 267 return dtdLocations; 268 } 269 270 public boolean isGeneratedPDFiles() { 271 return generatedPDFiles; 272 } 273 274 public boolean isGeneratedWithMapperPackage() { 275 return generatedWithMapperPackage; 276 } 277 278 280 public void setLogger(Logger logger) { 281 this.logger = logger; 282 } 283 284 public void setLoggerFactory(LoggerFactory lf) { 285 } 286 287 public Logger getLogger() { 288 return logger; 289 } 290 291 public LoggerFactory getLoggerFactory() { 292 return null; 293 } 294 295 297 public Object clone() { 298 JormCompilerParameterImpl res = new JormCompilerParameterImpl(); 299 res.classpath = classpath; 300 res.output = output; 301 res.verbose = verbose; 302 res.inputFiles = inputFiles; 303 res.keepSrc = keepSrc; 304 res.javacName = javacName; 305 res.projectName = projectName; 306 res.javac = javac; 307 res.parseOnly = parseOnly; 308 res.bindingInheritance = bindingInheritance; 309 res.classMappingInheritance = classMappingInheritance; 310 res.bindingAbstract = bindingAbstract; 311 res.logger = logger; 312 res.dtdLocations = dtdLocations; 313 return res; 314 } 315 316 318 private static String computeInheritanceString(String cn, String pattern) { 319 if (cn == null || cn.length() == 0) { 320 return null; 321 } 322 int pIdx = pattern.indexOf(INHERITANCE_PACKAGE_PATTERN); 323 int cIdx = pattern.indexOf(INHERITANCE_CLASSNAME_PATTERN); 324 325 if (pIdx == -1 && cIdx == -1) { 326 return pattern; 327 } else { 328 String [] icn = isolatePackageName(cn); 329 StringTokenizer st = new StringTokenizer (pattern, "%cp", true); 330 StringBuffer sb = new StringBuffer (); 331 boolean hasPercent = false; 332 while (st.hasMoreTokens()) { 333 String token = st.nextToken(); 334 if (token.equals("%") && !hasPercent) { 335 hasPercent = true; 336 } else if (hasPercent) { 337 if (token.equals("p")) { 338 if (icn[0] != null) { 339 sb.append(icn[0]); 340 } 341 } else if (token.equals("c")) { 342 sb.append(icn[1]); 343 } else { 344 } 346 hasPercent = false; 347 } else { 348 sb.append(token); 349 } 350 } 351 return sb.toString(); 352 } 353 } 354 355 private static String [] isolatePackageName(String fqcn) { 356 int idx = fqcn.lastIndexOf("."); 357 if (idx == -1) 358 return new String []{null, fqcn}; 359 else 360 return new String []{ 361 fqcn.substring(0, idx + 1), 362 fqcn.substring(idx + 1, fqcn.length()) 363 }; 364 } 365 366 public void setClasspath(PathExplorer classpath) { 367 this.classpath = classpath; 368 } 369 370 public void setOutput(String output) { 371 this.output = output; 372 } 373 374 public void setVerbose(boolean verbose) { 375 this.verbose = verbose; 376 } 377 378 public void setKeepSrc(boolean keepSrc) { 379 this.keepSrc = keepSrc; 380 } 381 382 public void setProjectName(String projectName) { 383 this.projectName = projectName; 384 } 385 386 public void setJavac(boolean javac) { 387 this.javac = javac; 388 } 389 390 public void setBindingInheritance(String bindingInheritance) { 391 this.bindingInheritance = bindingInheritance; 392 } 393 394 public void setClassMappingInheritance(String classMappingInheritance) { 395 this.classMappingInheritance = classMappingInheritance; 396 } 397 398 public void setBindingAbstract(boolean bindingAbstract) { 399 this.bindingAbstract = bindingAbstract; 400 } 401 402 public void setDtdLocations(ArrayList dtdLocations) { 403 this.dtdLocations = dtdLocations; 404 } 405 406 public void setGeneratedPDFiles(boolean generatedPDFiles) { 407 this.generatedPDFiles = generatedPDFiles; 408 } 409 410 public void setInputFiles(Collection inputFiles) { 411 this.inputFiles = inputFiles; 412 } 413 414 public void setGeneratedWithMapperPackage(boolean generatedWithMapperPackage) { 415 this.generatedWithMapperPackage = generatedWithMapperPackage; 416 } 417 418 public void setLogConfFile(String logConfFile) { 419 this.logConfFile = logConfFile; 420 } 421 } 422 | Popular Tags |