1 16 package org.apache.ws.jaxme.js; 17 18 import java.io.BufferedWriter ; 19 import java.io.File ; 20 import java.io.FileReader ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.Reader ; 24 import java.io.StringWriter ; 25 import java.io.Writer ; 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.StringTokenizer ; 32 33 import org.apache.ws.jaxme.js.apps.JavaSourceResolver; 34 import org.apache.ws.jaxme.js.impl.TextFileImpl; 35 import org.apache.ws.jaxme.logging.Logger; 36 import org.apache.ws.jaxme.logging.LoggerAccess; 37 38 68 public class JavaSourceFactory implements JavaSourceResolver { 69 private Logger logger = LoggerAccess.getLogger(JavaSourceFactory.class); 70 private boolean overwriteForced; 71 private boolean settingReadOnly; 72 private Map sources = new HashMap (); 73 private List files = new ArrayList (); 74 75 77 public void setLogger(Logger pLogger) { 78 logger = pLogger; 79 } 80 81 83 public Logger getLogger() { 84 return logger; 85 } 86 87 89 public void setSettingReadOnly(boolean pSettingReadOnly) { 90 settingReadOnly = pSettingReadOnly; 91 } 92 93 95 public boolean isSettingReadOnly() { 96 return settingReadOnly; 97 } 98 99 104 public void setOverwriteForced(boolean pOverwriteForced) { 105 overwriteForced = pOverwriteForced; 106 } 107 108 113 public boolean isOverwriteForced() { 114 return overwriteForced; 115 } 116 117 120 public JavaSource newJavaSource(JavaQName pName) { 121 return newJavaSource(pName, JavaSource.DEFAULT_PROTECTION); 122 } 123 124 127 public JavaSource newJavaSource(JavaQName pName, JavaSource.Protection pProtection) { 128 if (sources.containsKey(pName)) { 129 throw new IllegalStateException ("The class " + pName + " has already been created."); 130 } 131 JavaSource result = new JavaSource(this, pName, pProtection); 132 sources.put(pName, result); 133 return result; 134 } 135 136 140 public JavaSource newJavaSource(JavaQName pName, String pProtection) { 141 return newJavaSource(pName, JavaSource.Protection.valueOf(pProtection)); 142 } 143 144 146 public Iterator getJavaSources() { 147 return sources.values().iterator(); 148 } 149 150 152 public Iterator getTextFiles() { 153 return files.iterator(); 154 } 155 156 159 public JavaSource getJavaSource(JavaQName pName) { 160 return (JavaSource) sources.get(pName); 161 } 162 163 166 public TextFile getTextFile(String pPackageName, String pFileName) { 167 for (Iterator iter = files.iterator(); iter.hasNext(); ) { 168 TextFile textFile = (TextFile) iter.next(); 169 if (textFile.getPackageName().equals(pPackageName) && textFile.getFileName().equals(pFileName)) { 170 return textFile; 171 } 172 } 173 return null; 174 } 175 176 184 public File getPackageDirectory(File pBaseDir, String pPackageName) { 185 if (pPackageName != null) { 186 for (StringTokenizer st = new StringTokenizer (pPackageName, "."); 187 st.hasMoreTokens(); ) { 188 String dir = st.nextToken(); 189 pBaseDir = new File (pBaseDir, dir); 190 } 191 } 192 return pBaseDir; 193 } 194 195 198 public File getSourceFile(File pBaseDir, JavaQName pQName) { 199 if (pQName == null) { 200 throw new NullPointerException ("The class name must not be null."); 201 } 202 File packageDirectory = getPackageDirectory(pBaseDir, pQName.getPackageName()); 203 String name = pQName.getClassName(); 204 int offset = name.indexOf('.'); 205 if (offset != -1) { 206 throw new IllegalArgumentException ("Source files must not be generated for inner classes: " + name); 207 } 208 offset = name.indexOf('.'); 209 if (offset != -1) { 210 throw new IllegalArgumentException ("Source files must not be generated for inner classes: " + name); 211 } 212 return new File (packageDirectory, name + ".java"); 213 } 214 215 228 public File getLocation(File pBaseDir, String pPackage) { 229 for (StringTokenizer st = new StringTokenizer (pPackage, "."); st.hasMoreTokens(); ) { 230 pBaseDir = new File (pBaseDir, st.nextToken()); 231 } 232 return pBaseDir; 233 } 234 235 244 public File getLocation(File pBaseDir, JavaSource pJs) { 245 if (pJs.isInnerClass()) { 246 throw new IllegalArgumentException ("Inner classes have no assigned location in the file system."); 247 } 248 return new File (getLocation(pBaseDir, pJs.getPackageName()), 249 pJs.getClassName() + ".java"); 250 } 251 252 261 public File getLocation(File pBaseDir, TextFile pTextFile) { 262 return new File (getLocation(pBaseDir, pTextFile.getPackageName()), pTextFile.getFileName()); 263 } 264 265 266 269 protected boolean isSameFile(JavaSource pJs, File pFile) throws IOException { 270 StringWriter sw = new StringWriter (); 271 pJs.write(sw); 272 sw.close(); 273 return isSameFile(sw.toString(), pFile); 274 } 275 276 279 protected boolean isSameFile(String pContents, File pFile) throws IOException { 280 Reader r = new FileReader (pFile); 281 StringWriter sw = new StringWriter (); 282 char[] buffer = new char[4096]; 283 for (;;) { 284 int len = r.read(buffer); 285 if (len == -1) { 286 break; 287 } else if (len > 0) { 288 sw.write(buffer, 0, len); 289 } 290 } 291 return isSameFile(pContents, sw.toString()); 292 } 293 294 296 protected boolean isSameFile(String pContents1, String pContents2) { 297 return pContents1.equals(pContents2); 298 } 299 300 302 protected void writeFile(File pFile, JavaSource pJs) throws IOException { 303 final String mName = "writeFile(File,JavaSource)"; 304 File p = pFile.getParentFile(); 305 if (p != null && !p.exists()) { 306 getLogger().fine(mName, "Creating directory " + p); 307 p.mkdirs(); 308 } 309 310 if (isOverwriteForced() || !pFile.exists() || !isSameFile(pJs, pFile)) { 311 getLogger().fine(mName, "Creating " + pFile); 312 Writer w = new BufferedWriter (new FileWriter (pFile), 4096); 313 pJs.write(w); 314 w.close(); 315 if (isSettingReadOnly()) { 316 pFile.setReadOnly(); 317 } 318 } else { 319 getLogger().fine(mName, "Uptodate: " + pFile); 320 } 321 } 322 323 325 protected void writeFile(File pFile, String pContents) throws IOException { 326 final String mName = "writeFile(File,String)"; 327 File p = pFile.getParentFile(); 328 if (p != null && !p.exists()) { 329 getLogger().fine(mName, "Creating directory " + p); 330 p.mkdirs(); 331 } 332 333 if (isOverwriteForced() || !pFile.exists() || !isSameFile(pContents, pFile)) { 334 getLogger().fine(mName, "Creating " + pFile); 335 Writer w = new BufferedWriter (new FileWriter (pFile), 4096); 336 w.write(pContents); 337 w.close(); 338 if (isSettingReadOnly()) { 339 pFile.setReadOnly(); 340 } 341 } else { 342 getLogger().fine(mName, "Uptodate: " + pFile); 343 } 344 } 345 346 352 public void write(File pBaseDir, JavaSource pJs) throws IOException { 353 writeFile(getLocation(pBaseDir, pJs), pJs); 354 } 355 356 362 public void write(File pBaseDir, TextFile pFile) throws IOException { 363 writeFile(getLocation(pBaseDir, pFile), pFile.getContents()); 364 } 365 366 370 public void write(File pBaseDir) throws IOException { 371 for (Iterator iter = getJavaSources(); iter.hasNext(); ) { 372 write(pBaseDir, (JavaSource) iter.next()); 373 } 374 for (Iterator iter = getTextFiles(); iter.hasNext(); ) { 375 write(pBaseDir, (TextFile) iter.next()); 376 } 377 } 378 379 381 public TextFile newTextFile(String pPackageName, String pFileName) { 382 for (Iterator iter = files.iterator(); iter.hasNext(); ) { 383 TextFile f = (TextFile) iter.next(); 384 if (f.getPackageName().equals(pPackageName) && f.getFileName().equals(pFileName)) { 385 throw new IllegalStateException ("A file named " + pFileName + " in package " + pPackageName + " already exists."); 386 } 387 } 388 for (Iterator iter = sources.keySet().iterator(); iter.hasNext(); ) { 389 JavaQName qName = (JavaQName) iter.next(); 390 if (qName.getPackageName().equals(pPackageName) && (qName.getClassName() + ".java").equals(pFileName)) { 391 throw new IllegalStateException ("A Java source file names " + pFileName + " in package " + pPackageName + " already exists."); 392 } 393 } 394 395 TextFile result = new TextFileImpl(pPackageName, pFileName); 396 files.add(result); 397 return result; 398 } 399 } 400 | Popular Tags |