1 19 package org.enhydra.zeus.util; 20 21 import java.io.File ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.StringTokenizer ; 25 import java.util.Vector ; 26 import java.util.Properties ; 27 28 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.types.Reference; 33 34 import java.lang.reflect.*; 36 37 145 public class ZeusTask extends Task { 146 147 148 private static final String CONSTRAINT_TYPE_DTD = "DTD"; 149 150 151 private static final String CONSTRAINT_TYPE_XSD = "XSD"; 152 153 154 private File sourceDir; 155 156 157 private File destDir; 158 159 160 private String defaultJavaPackage; 161 162 163 private boolean defaultCollapseSimpleElements; 164 165 166 private Vector constraints; 167 168 169 174 public ZeusTask() { 175 defaultCollapseSimpleElements = false; 176 defaultJavaPackage = ""; 177 constraints = new Vector (); 178 } 179 180 195 public void execute() throws BuildException { 196 if (sourceDir == null) { 198 sourceDir = project.resolveFile("."); 199 } 200 201 if (destDir == null) { 203 throw new BuildException("The destDir attribute is required, and " + 204 "should indicate the destination directory for generated " + 205 "source files."); 206 } 207 208 for (Enumeration e = constraints.elements(); e.hasMoreElements(); ) { 210 generateFrom((Constraint)e.nextElement()); 211 } 212 } 213 214 222 public File getSourceDir() { 223 return sourceDir; 224 } 225 226 234 public void setSourceDir(File sourceDir) { 235 if (!sourceDir.isDirectory()) { 236 throw new IllegalArgumentException ("The source directory " + 237 "must be a valid, existing directory."); 238 } 239 this.sourceDir = sourceDir; 240 } 241 242 250 public File getDestDir() { 251 return destDir; 252 } 253 254 262 public void setDestDir(File destDir) { 263 if (!destDir.isDirectory()) { 264 throw new IllegalArgumentException ("The destination directory " + 265 "must be a valid, existing directory."); 266 } 267 this.destDir = destDir; 268 } 269 270 278 public String getDefaultJavaPackage() { 279 return defaultJavaPackage; 280 } 281 282 290 public void setDefaultJavaPackage(String defaultJavaPackage) { 291 this.defaultJavaPackage = defaultJavaPackage; 292 } 293 294 302 public void setDefaultCollapseSimpleElements( 303 boolean defaultCollapseSimpleElements ) { 304 305 this.defaultCollapseSimpleElements = defaultCollapseSimpleElements; 306 } 307 308 317 public Constraint createConstraint() { 318 Constraint constraint = new Constraint(); 319 constraints.addElement(constraint); 320 return constraint; 321 } 322 323 328 public void generateFrom(Constraint constraint) throws BuildException { 329 if (constraint.getType().equalsIgnoreCase(CONSTRAINT_TYPE_DTD)) { 330 try { 331 String [] args = null; 332 int i = 0; 333 334 if (constraint.getJavaPackage() != null) { 336 args = new String [6]; 337 } else { 338 args = new String [5]; 339 } 340 341 String filename = sourceDir.getAbsolutePath() + File.separator + 343 constraint.getConstraintFile().getName(); 344 args[i++] = "-constraints=" + filename; 345 346 args[i++] = "-outputDir=" + destDir.getAbsolutePath(); 348 349 args[i++] = "-ignoreIDAttributes=" + 351 constraint.ignoreIDAttributes(); 352 353 args[i++] = "-collapseSimpleElements=" + 355 constraint.collapseSimpleElements(); 356 357 args[i++] = "-generator=simple"; 359 360 if (constraint.getJavaPackage() != null) { 362 args[i++] = "-javaPackage=" + constraint.getJavaPackage(); 363 } 364 365 System.out.println("Generating Java from DTD: " + filename); 366 System.out.println(" + Output Directory: " + 367 destDir.getAbsolutePath()); 368 System.out.println(" + Java Package: " + 369 constraint.getJavaPackage()); 370 if (constraint.collapseSimpleElements()) { 371 System.out.println(" + Collapsing Simple Elements"); 372 } 373 if (constraint.ignoreIDAttributes()) { 374 System.out.println(" + Ignoring ID Attributes"); 375 } 376 System.out.println("\n"); 377 378 String generatorClassName = "org.enhydra.zeus.util.DTDSourceGenerator"; 380 Class c = null; 381 Method m = null; 382 try { 383 c = Class.forName(generatorClassName); 384 m = c.getMethod("main", new Class [] { String [].class }); 385 } catch (Exception e) { 386 e.printStackTrace(); 387 throw new BuildException(e); 388 } 389 390 if (m != null) { 391 m.invoke(null, new Object [] { args }); 392 } 393 } catch (Exception e) { 394 e.printStackTrace(); 395 throw new BuildException(e); 396 } 397 } else { 398 throw new BuildException("The specified constraint type, '" + 399 constraint.getType() + "', is not currently supported."); 400 } 401 } 402 403 410 public class Constraint { 411 412 413 private File constraintFile; 414 415 416 private String type; 417 418 419 private String javaPackage; 420 421 422 private boolean ignoreIDAttributes; 423 424 425 private boolean collapseSimpleElements; 426 427 432 public Constraint() { 433 javaPackage = ""; 434 ignoreIDAttributes = false; 435 collapseSimpleElements = false; 436 } 437 438 446 public File getConstraintFile() { 447 return constraintFile; 448 } 449 450 458 public void setConstraintFile(File constraintFile) { 459 this.constraintFile = constraintFile; 460 } 461 462 469 public String getType() { 470 return type; 471 } 472 473 480 public void setType(String type) { 481 this.type = type; 482 } 483 484 491 public String getJavaPackage() { 492 return javaPackage; 493 } 494 495 502 public void setJavaPackage(String javaPackage) { 503 this.javaPackage = javaPackage; 504 } 505 506 514 public boolean ignoreIDAttributes() { 515 return ignoreIDAttributes; 516 } 517 518 526 public void setIgnoreIDAttributes(boolean ignoreIDAttributes) { 527 this.ignoreIDAttributes = ignoreIDAttributes; 528 } 529 530 538 public boolean collapseSimpleElements() { 539 return collapseSimpleElements; 540 } 541 542 550 public void setCollapseSimpleElements(boolean collapseSimpleElements ) { 551 this.collapseSimpleElements = collapseSimpleElements; 552 } 553 } 554 } | Popular Tags |