View Javadoc

1   package org.apache.torque.generator.control.existingtargetstrategy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * A handler which implements the strategy to append the generation result
34   * to the existing target files.
35   *
36   * @version $Id: AppendToTargetFileStrategy.java 1368426 2012-08-02 11:46:37Z tfischer $
37   */
38  public class AppendToTargetFileStrategy implements ExistingTargetStrategy
39  {
40      /** The strategy name "append". */
41      public static final String STRATEGY_NAME = "append";
42  
43      /**
44       * Will be called before the generation is started and decides whether
45       * the generation process for this file should proceed.
46       *
47       * @param outputDirKey the key for the output directory
48       *        into which the generated file should be written,
49       *        null for the default output directory.
50       * @param outputPath the path to which the output should be written,
51       *        relative to the output base directory.
52       * @param encoding The character encoding of the generated file,
53       *        or null for the platform default encoding.
54       * @param unitConfiguration the configuration of the current configuration
55       *        unit, not null.
56       *
57       * @return true always.
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       * Processes the results of the generation.
70       *
71       * @param outputDirKey the key for the output directory
72       *        into which the generated file should be written,
73       *        null for the default output directory.
74       * @param outputPath the path to which the output should be written,
75       *        relative to the output base directory.
76       * @param encoding The character encoding of the generated file,
77       *        or null for the platform default encoding.
78       * @param generationResult the result of the generation, not null.
79       * @param unitConfiguration the configuration of the current configuration
80       *        unit, not null.
81       * @throws GeneratorException on an error.
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      * Returns the name of the existing target strategy.
150      *
151      * @return "replace"
152      */
153     public String getStrategyName()
154     {
155         return STRATEGY_NAME;
156     }
157 }