1 19 package org.enhydra.zeus.util; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileReader ; 24 import java.io.InputStream ; 25 import java.io.InputStreamReader ; 26 import java.io.IOException ; 27 import java.io.Reader ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 31 import org.enhydra.zeus.Binding; 33 import org.enhydra.zeus.Generator; 34 import org.enhydra.zeus.Transformer; 35 import org.enhydra.zeus.ZeusException; 36 import org.enhydra.zeus.generator.SimpleGenerator; 37 import org.enhydra.zeus.transform.DefaultsTransformer; 38 39 47 public abstract class BaseSourceGenerator { 48 49 50 protected Reader constraintsReader; 51 52 53 protected File outputDir; 54 55 56 protected String javaPackage; 57 58 59 protected boolean collapseSimpleElements; 60 61 62 protected boolean ignoreIDAttributes; 63 64 69 public BaseSourceGenerator() { 70 javaPackage = ""; 71 outputDir = new File ("."); 72 } 73 74 84 public void setConstraintsInput(String fileURI) throws IOException { 85 if (fileURI == null) { 86 throw new IllegalArgumentException ("A Source Generator cannot " + 87 "have a null file URI."); 88 } 89 90 FileReader reader = new FileReader (new File (fileURI)); 91 setConstraintsInput(reader); 92 } 93 94 104 public void setConstraintsInput(File file) throws IOException { 105 if (file == null) { 106 throw new IllegalArgumentException ("A Source Generator cannot " + 107 "have a null File."); 108 } 109 110 setConstraintsInput(new FileReader (file)); 111 } 112 113 122 public void setConstraintsInput(Reader reader) { 123 if (reader == null) { 124 throw new IllegalArgumentException ("A Source Generator cannot " + 125 "have a null Reader."); 126 } 127 constraintsReader = reader; 128 } 129 130 140 public void setConstraintsInput(InputStream inputStream) { 141 if (inputStream == null) { 142 throw new IllegalArgumentException ("A Source Generator cannot " + 143 "have a null InputStream."); 144 } 145 setConstraintsInput(new InputStreamReader (inputStream)); 146 } 147 148 159 public void setOutputDir(String outputDir) throws IOException { 160 if (outputDir == null) { 161 throw new IllegalArgumentException ("A Source Generator cannot " + 162 "have a null output directory."); 163 } 164 setOutputDir(new File (outputDir)); 165 } 166 167 178 public void setOutputDir(File outputDir) throws IOException { 179 if (outputDir == null) { 180 throw new IllegalArgumentException ("A Source Generator cannot " + 181 "have a null output directory."); 182 } 183 184 if ((!outputDir.exists()) || (!outputDir.isDirectory())) { 185 throw new IOException ("Output directory must exist before " + 186 "class generation."); 187 } 188 this.outputDir = outputDir; 189 } 190 191 198 public void setJavaPackage(String javaPackage) { 199 if (javaPackage == null) { 200 throw new IllegalArgumentException ("A Source Generator cannot " + 201 "have a null Java package."); 202 } 203 204 this.javaPackage = javaPackage; 205 } 206 207 217 public void setCollapseSimpleElements(boolean collapseSimpleElements) { 218 setCollapseSimpleElements(collapseSimpleElements, false); 219 } 220 221 233 public void setCollapseSimpleElements(boolean collapseSimpleElements, 234 boolean ignoreIDAttributes) { 235 236 this.collapseSimpleElements = collapseSimpleElements; 237 this.ignoreIDAttributes = ignoreIDAttributes; 238 } 239 240 248 public void generate() throws IOException , ZeusException { 249 Transformer transformer = new DefaultsTransformer(); 251 transformer.getTransformerOptions().setDefaultPackage(javaPackage); 252 253 Generator generator = getGenerator(); 255 generator.setOutputDirectory(outputDir); 256 257 List bindings = getConstraintBindings(); 259 260 List transformedBindings = transformer.transform(bindings); 262 263 for (Iterator i = transformedBindings.iterator(); i.hasNext(); ) { 265 Binding binding = (Binding)i.next(); 266 generator.generate(binding); 267 } 268 } 269 270 281 protected abstract List getConstraintBindings() throws IOException ; 282 283 295 protected Generator getGenerator() { 296 return new SimpleGenerator(); 297 } 298 } 299 | Popular Tags |