1 17 package org.apache.ws.jaxme.generator.impl; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.apache.ws.jaxme.generator.Generator; 26 import org.apache.ws.jaxme.generator.SchemaReader; 27 import org.apache.ws.jaxme.generator.sg.SchemaSG; 28 import org.apache.ws.jaxme.js.JavaSource; 29 import org.apache.ws.jaxme.js.JavaSourceFactory; 30 import org.apache.ws.jaxme.logging.Logger; 31 import org.apache.ws.jaxme.logging.LoggerAccess; 32 import org.xml.sax.EntityResolver ; 33 import org.xml.sax.InputSource ; 34 35 36 42 public class GeneratorImpl implements Generator { 43 private static final Logger log = LoggerAccess.getLogger(GeneratorImpl.class); 44 private SchemaReader schemaReader; 45 private java.io.File targetDirectory; 46 private Map properties = new HashMap (); 47 private int nextKey; 48 private boolean isValidating, isForcingOverwrite, isSettingReadOnly; 49 private EntityResolver entityResolver; 50 51 54 public void setEntityResolver(EntityResolver pEntityResolver) { 55 entityResolver = pEntityResolver; 56 } 57 58 61 public EntityResolver getEntityResolver() { 62 return entityResolver; 63 } 64 65 67 public boolean isForcingOverwrite() { 68 return isForcingOverwrite; 69 } 70 71 73 public void setForcingOverwrite(boolean pIsForcingOverwrite) { 74 isForcingOverwrite = pIsForcingOverwrite; 75 } 76 77 79 public boolean isSettingReadOnly() { 80 return isSettingReadOnly; 81 } 82 83 85 public void setSettingReadOnly(boolean pIsSettingReadOnly) { 86 isSettingReadOnly = pIsSettingReadOnly; 87 } 88 89 public boolean isValidating() { 90 return isValidating; 91 } 92 93 public void setValidating(boolean pIsValidating) { 94 isValidating = pIsValidating; 95 } 96 97 98 public GeneratorImpl() { 99 } 100 101 103 public void setSchemaReader(SchemaReader pReader) { 104 schemaReader = pReader; 105 } 106 107 109 public SchemaReader getSchemaReader() { 110 return schemaReader; 111 } 112 113 public void setTargetDirectory(File pDirectory) { 114 targetDirectory = pDirectory; 115 } 116 117 public File getTargetDirectory() { 118 return targetDirectory; 119 } 120 121 public SchemaSG generate(InputSource pSource) throws Exception { 122 SchemaReader sr = getSchemaReader(); 123 sr.setGenerator(this); 124 125 SchemaSG s = sr.parse(pSource); 126 s.generate(); 127 File targetDir = getTargetDirectory(); 128 JavaSourceFactory jsf = s.getJavaSourceFactory(); 129 for (Iterator iter = jsf.getJavaSources(); iter.hasNext(); ) { 130 ((JavaSource) iter.next()).setForcingFullyQualifiedName(true); 131 } 132 s.getJavaSourceFactory().write(targetDir); 133 134 return s; 135 } 136 137 138 141 public SchemaSG generate(File pFile) throws Exception { 142 final String mName = "generate(File)"; 143 log.finer(mName, "->", pFile); 144 String path = pFile.getAbsolutePath(); 145 if (!pFile.exists()) { 146 throw new java.io.FileNotFoundException ("File does not exist: " + path); 147 } 148 if (!pFile.isFile()) { 149 throw new java.io.FileNotFoundException ("Not a file: " + path); 150 } 151 152 InputSource isource = new InputSource (new FileInputStream (pFile)); 153 isource.setSystemId(pFile.toURL().toString()); 154 SchemaSG s = generate(isource); 155 log.finer(mName, "<-", s); 156 return s; 157 } 158 159 162 public SchemaSG generate(java.net.URL pURL) throws Exception { 163 final String mName = "generate(URL)"; 164 log.entering(mName, pURL); 165 java.net.URLConnection conn = pURL.openConnection(); 166 167 InputSource isource = new InputSource (conn.getInputStream()); 168 isource.setSystemId(pURL.toString()); 169 SchemaSG s = generate(isource); 170 log.exiting(mName, s); 171 return s; 172 } 173 174 public String getProperty(String pName) { 175 return (String ) properties.get(pName); 176 } 177 178 public String getProperty(String pName, String pDefault) { 179 String result = (String ) properties.get(pName); 180 return (result == null) ? pDefault : result; 181 } 182 183 public void setProperty(String pName, String pValue) { 184 properties.put(pName, pValue); 185 } 186 187 public String getKey() { 188 return Integer.toString(nextKey++); 189 } 190 } 191 | Popular Tags |