1 package org.apache.torque.generator.control.existingtargetstrategy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.io.FilenameUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.torque.generator.GeneratorException;
30 import org.apache.torque.generator.configuration.UnitConfiguration;
31 import org.apache.torque.generator.control.ControllerException;
32 import org.apache.torque.generator.control.ControllerHelper;
33 import org.apache.torque.generator.merge.ThreeWayMerger;
34 import org.apache.torque.generator.outlet.OutletResult;
35
36
37
38
39
40
41 public class MergeTargetFileStrategy implements ExistingTargetStrategy
42 {
43
44 public static final String STRATEGY_NAME = "merge";
45
46
47 public static final String WORK_SUBDIR = "raw-generated";
48
49
50 private static Log log = LogFactory.getLog(MergeTargetFileStrategy.class);
51
52
53 private final ThreeWayMerger merger = new ThreeWayMerger();
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 public boolean beforeGeneration(
72 String outputDirKey,
73 String outputPath,
74 String encoding,
75 UnitConfiguration unitConfiguration)
76 {
77 return true;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 public void afterGeneration(
95 String outputDirKey,
96 String outputPath,
97 String encoding,
98 OutletResult generationResult,
99 UnitConfiguration unitConfiguration)
100 throws GeneratorException
101 {
102 File generationStorageDir
103 = new File(unitConfiguration.getWorkDirectory(), WORK_SUBDIR);
104 File generationStorageFile;
105 if (outputDirKey == null)
106 {
107 generationStorageFile
108 = new File(generationStorageDir,
109 FilenameUtils.concat("default", outputPath));
110 }
111 else
112 {
113 generationStorageFile
114 = new File(generationStorageDir,
115 FilenameUtils.concat("other",
116 FilenameUtils.concat(outputDirKey, outputPath)));
117 }
118 String oldGenerationContent = readFileToString(
119 generationStorageFile,
120 encoding);
121
122 File targetFile = ControllerHelper.getOutputFile(
123 outputDirKey,
124 outputPath,
125 unitConfiguration);
126 String oldTargetContent = readFileToString(
127 targetFile,
128 encoding);
129
130 if (!generationResult.isStringResult())
131 {
132 throw new GeneratorException(
133 "The merge target file strategy onlys works"
134 + " for String generation results (target file="
135 + targetFile.getAbsolutePath() + ")");
136 }
137
138 String newTargetContent = null;
139 if (oldTargetContent == null)
140 {
141 log.debug("no old target content found, using generation result");
142 newTargetContent = generationResult.getStringResult();
143 }
144 else if (oldGenerationContent == null)
145 {
146 log.info("no old generation content found,"
147 + "using old target content."
148 + " This is a bit unusual, but may be ok"
149 + " depending on the circumstances");
150 newTargetContent = generationResult.getStringResult();
151 }
152 else
153 {
154 log.debug("merging generation result and old target content");
155 newTargetContent = merger.merge(
156 oldGenerationContent,
157 generationResult.getStringResult(),
158 oldTargetContent,
159 encoding);
160 }
161 writeStringToFile(targetFile, newTargetContent, encoding);
162 writeStringToFile(
163 generationStorageFile,
164 generationResult.getStringResult(),
165 encoding);
166 }
167
168
169
170
171
172
173 public String getStrategyName()
174 {
175 return STRATEGY_NAME;
176 }
177
178
179
180
181
182
183
184
185
186
187
188 private String readFileToString(
189 File file,
190 String charset)
191 throws ControllerException
192 {
193 String result = null;
194 if (file.exists())
195 {
196 try
197 {
198 result = FileUtils.readFileToString(
199 file,
200 charset);
201 }
202 catch (IOException e)
203 {
204 throw new ControllerException(
205 "Could not read file \""
206 + file.getAbsolutePath()
207 + "\"",
208 e);
209 }
210 }
211 return result;
212 }
213
214
215
216
217
218
219
220
221
222
223
224 private void writeStringToFile(
225 File file,
226 String content,
227 String charset)
228 throws ControllerException
229 {
230 try
231 {
232 FileUtils.writeStringToFile(file, content, charset);
233 }
234 catch (IOException e)
235 {
236 throw new ControllerException(
237 "Could not write file \""
238 + file.getAbsolutePath()
239 + "\"",
240 e);
241 }
242 }
243 }