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.torque.generator.GeneratorException;
27 import org.apache.torque.generator.configuration.UnitConfiguration;
28 import org.apache.torque.generator.control.ControllerException;
29 import org.apache.torque.generator.control.ControllerHelper;
30 import org.apache.torque.generator.outlet.OutletResult;
31
32
33
34
35
36
37
38 public class AppendToTargetFileStrategy implements ExistingTargetStrategy
39 {
40
41 public static final String STRATEGY_NAME = "append";
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public boolean beforeGeneration(
60 String outputDirKey,
61 String outputPath,
62 String encoding,
63 UnitConfiguration unitConfiguration)
64 {
65 return true;
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 public void afterGeneration(
84 String outputDirKey,
85 String outputPath,
86 String encoding,
87 OutletResult generationResult,
88 UnitConfiguration unitConfiguration)
89 throws GeneratorException
90 {
91 File outputFile = ControllerHelper.getOutputFile(
92 outputDirKey,
93 outputPath,
94 unitConfiguration);
95 try
96 {
97 if (generationResult.isStringResult())
98 {
99 String originalContent = "";
100 if (outputFile.exists())
101 {
102 originalContent = FileUtils.readFileToString(
103 outputFile,
104 encoding);
105 }
106 FileUtils.writeStringToFile(
107 outputFile,
108 originalContent + generationResult.getStringResult(),
109 encoding);
110 }
111 else
112 {
113 byte[] originalContent = new byte[] {};
114 if (outputFile.exists())
115 {
116 originalContent = FileUtils.readFileToByteArray(
117 outputFile);
118 }
119 byte[] result = new byte[originalContent.length
120 + generationResult.getByteArrayResult().length];
121 System.arraycopy(
122 originalContent,
123 0,
124 result,
125 0,
126 originalContent.length);
127 System.arraycopy(generationResult.getByteArrayResult(),
128 0,
129 result,
130 originalContent.length,
131 generationResult.getByteArrayResult().length);
132
133 FileUtils.writeByteArrayToFile(
134 outputFile,
135 result);
136 }
137 }
138 catch (IOException e)
139 {
140 throw new ControllerException(
141 "Could not write file \""
142 + outputFile.getAbsolutePath()
143 + "\"",
144 e);
145 }
146 }
147
148
149
150
151
152
153 public String getStrategyName()
154 {
155 return STRATEGY_NAME;
156 }
157 }