001: package net.sf.mockcreator.codegeneration;
002:
003: import java.io.File;
004: import java.io.FileWriter;
005: import java.io.IOException;
006: import java.io.Writer;
007: import java.util.StringTokenizer;
008:
009: import javax.xml.transform.Source;
010:
011: import net.sf.mockcreator.utils.SourceUtils;
012: import net.sf.mockcreator.utils.Utility;
013:
014: public class Saver {
015:
016: public static void saveMock(MockDescriptor md, String source)
017: throws Exception {
018: String dir = getMockFullOutputDirectoryName(md);
019: String fil = getMockFullOutputFileName(md);
020: saveFile(dir, fil, getStampLine(md) + "\n" + source);
021: createDumbySuperClasses(md, md.getTargetPackageName(), md
022: .getOutputPath(), md.getFakeDepth());
023: }
024:
025: public static void saveDumby(MockDescriptor md, String source)
026: throws IOException {
027: String dir = getDumbyFullOutputDirectoryName(md);
028: String fil = getDumbyFullOutputFileName(md);
029: saveFile(dir, fil, source);
030: }
031:
032: private static void saveFile(String dir, String fil, String output)
033: throws IOException {
034: File fdir = new File(dir);
035:
036: if (!fdir.exists() && !fdir.mkdirs()) {
037: throw new IOException("unable to create " + dir.toString());
038: }
039:
040: // FormattingOutputWriter fow = new FormattingOutputWriter(new FileWriter(fil));
041: Writer fow = new FileWriter(fil);
042: fow.write(output);
043: fow.close();
044: }
045:
046: private static String getStampLine(MockDescriptor md) {
047: return "// MockCreator v" + MockBuilder.version + "; HashCode:"
048: + md.hashCode() + "; " + md.getSourceFQN();
049: }
050:
051: public static boolean isUpToDate(MockDescriptor md) {
052: String fileName = getMockFullOutputFileName(md);
053: String hash = getStampLine(md);
054:
055: String line = Utility.getFirstLine(fileName);
056: if (line != null && line.startsWith(hash)) {
057: return true;
058: }
059:
060: return false;
061: }
062:
063: public static String getMockFullOutputFileName(MockDescriptor md) {
064: return getMockFullOutputDirectoryName(md) + md.getMockName()
065: + ".java";
066: }
067:
068: public static String getDumbyFullOutputFileName(MockDescriptor md) {
069: return getDumbyFullOutputDirectoryName(md) + md.getName()
070: + ".java";
071: }
072:
073: public static String getMockFullOutputDirectoryName(
074: MockDescriptor md) {
075: return md.getOutputPath() + File.separator
076: + getPackageDirectoryName(md.getTargetPackageName());
077: }
078:
079: public static String getDumbyFullOutputDirectoryName(
080: MockDescriptor md) {
081: return md.getOutputPath() + File.separator
082: + getPackageDirectoryName(md.getSourcePackageName());
083: }
084:
085: private static String getPackageDirectoryName(String packageName) {
086: StringTokenizer tokenizer = new StringTokenizer(packageName,
087: ".");
088: StringBuffer buf = new StringBuffer();
089:
090: while (tokenizer.hasMoreTokens()) {
091: buf.append(tokenizer.nextToken());
092: buf.append(File.separator);
093: }
094:
095: return buf.toString();
096: }
097:
098: public static MockDescriptor configureDescriptor(
099: String interfaceName, String targetPackage,
100: String outputPath, int fakeDepth) {
101: MockDescriptor md = new MockDescriptor(interfaceName);
102:
103: if (targetPackage != null && targetPackage.length() != 0) {
104: md.setTargetPackageName(targetPackage);
105: }
106:
107: if (outputPath != null) {
108: md.setOutputPath(outputPath);
109: }
110:
111: md.setFakeDepth(fakeDepth);
112:
113: return md;
114: }
115:
116: public static void createDumbySuperClasses(MockDescriptor md,
117: String targetPackage, String outputPath, int fakeDepth)
118: throws Exception {
119: while (fakeDepth-- > 0) {
120: Class type = md.getSourceClass();
121:
122: if (type.isInterface())
123: break;
124: if (type.getName().startsWith("java."))
125: break;
126:
127: String dumby = DumbyBuilder.createDumby(md);
128: if (dumby != null) {
129: System.out.print("\n dumbifying " + type + " ");
130: saveDumby(md, dumby);
131: }
132:
133: Class sup = type.getSuperclass();
134: MockDescriptor md2 = configureDescriptor(sup.getName(),
135: targetPackage, outputPath, fakeDepth);
136:
137: // do not follow super() into inner classes of other parents
138: if (md2.isInner()
139: && !md2.getSourceClass().getDeclaringClass()
140: .equals(
141: md.getSourceClass()
142: .getDeclaringClass()))
143: break;
144: md = md2;
145: }
146: }
147: }
|