KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jester > FileBasedClassSourceCodeChanger


1 package jester;
2
3 import java.io.*;
4
5 public class FileBasedClassSourceCodeChanger implements ClassSourceCodeChanger {
6     private String JavaDoc sourceFileName;
7     private Compiler JavaDoc compiler;
8     private IgnoreListDocument originalContents;
9
10     private int indexOfLastChange = -1;
11     private String JavaDoc valueChangedFrom = "not changed";
12     private String JavaDoc valueChangedTo = "not changed";
13
14     private Report myReport;
15
16     public FileBasedClassSourceCodeChanger(Configuration configuration, String JavaDoc sourceFileName, Report aReport) {
17         super();
18         this.sourceFileName = sourceFileName;
19         compiler = new RealCompiler(configuration);
20         myReport = aReport;
21     }
22
23     public IgnoreListDocument getOriginalContents() throws ConfigurationException {
24         try {
25             if (originalContents == null) {
26                 String JavaDoc ignoreListContents = "";
27                 try{
28                     ignoreListContents = Util.readFileOnClassPath(IgnoreListDocument.FILE_NAME);
29                 }catch(FileNotFoundException ignored){
30                     System.err.println("Warning - could not find "+IgnoreListDocument.FILE_NAME+" so not ignoring anything.");
31                 }
32                 originalContents = new IgnoreListDocument(Util.readFile(sourceFileName), new IgnoreList(ignoreListContents));
33             }
34             return originalContents;
35         } catch (IOException ex) {
36             throw new ConfigurationException(ex.getMessage());
37         }
38     }
39
40     public void writeOriginalContentsBack() throws SourceChangeException {
41         try {
42             Writer writer = new FileWriter(sourceFileName);
43             getOriginalContents().writeOnto(writer, 0, getOriginalContents().length());
44             writer.close();
45             compiler.compile(sourceFileName);
46         } catch (IOException ex) {
47             throw new SourceChangeException(ex.getMessage());
48         }
49     }
50     public void writeOverSourceReplacing(int index, String JavaDoc oldContents, String JavaDoc newContents) throws SourceChangeException {
51         indexOfLastChange = index;
52         valueChangedFrom = oldContents;
53         valueChangedTo = newContents;
54         //
55
try {
56             Writer writer = new FileWriter(sourceFileName);
57             getOriginalContents().writeOnto(writer, 0, index);
58             writer.write(newContents, 0, newContents.length());
59             int afterChangeIndex = index + oldContents.length();
60             getOriginalContents().writeOnto(writer, afterChangeIndex, getOriginalContents().length() - afterChangeIndex);
61             writer.close();
62             compiler.compile(sourceFileName);
63         } catch (IOException ex) {
64             throw new SourceChangeException(ex.getMessage());
65         }
66     }
67
68     public void finishJesting() throws SourceChangeException {
69         myReport.finishFile(sourceFileName);
70     }
71
72     public void lastChangeCausedTestsToFail() throws SourceChangeException {
73         myReport.changeThatCausedTestsToFail(indexOfLastChange, valueChangedFrom, valueChangedTo);
74     }
75
76     public void lastChangeDidNotCauseTestsToFail() throws SourceChangeException {
77         myReport.changeThatDidNotCauseTestsToFail(indexOfLastChange, valueChangedFrom, valueChangedTo);
78     }
79
80     public void startJesting() throws SourceChangeException {
81         myReport.startFile(sourceFileName, getOriginalContents());
82     }
83 }
Popular Tags