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 replace existing target files. 34 * 35 * @version $Id: ReplaceTargetFileStrategy.java 1368426 2012-08-02 11:46:37Z tfischer $ 36 */ 37 public class ReplaceTargetFileStrategy implements ExistingTargetStrategy 38 { 39 /** The strategy name "replace". */ 40 public static final String STRATEGY_NAME = "replace"; 41 42 /** 43 * Will be called before the generation is started and decides whether 44 * the generation process for this file should proceed. 45 * 46 * @param outputDirKey the key for the output directory 47 * into which the generated file should be written, 48 * null for the default output directory. 49 * @param outputPath the path to which the output should be written, 50 * relative to the output base directory. 51 * @param encoding The character encoding of the generated file, 52 * or null for the platform default encoding. 53 * @param unitConfiguration the configuration of the current configuration 54 * unit, not null. 55 * 56 * @return true always. 57 */ 58 public boolean beforeGeneration( 59 String outputDirKey, 60 String outputPath, 61 String encoding, 62 UnitConfiguration unitConfiguration) 63 { 64 return true; 65 } 66 67 /** 68 * Processes the results of the generation. 69 * 70 * @param outputDirKey the key for the output directory 71 * into which the generated file should be written, 72 * null for the default output directory. 73 * @param outputPath the path to which the output should be written, 74 * relative to the output base directory. 75 * @param encoding The character encoding of the generated file, 76 * or null for the platform default encoding. 77 * @param generationResult the result of the generation, not null. 78 * @param unitConfiguration the configuration of the current configuration 79 * unit, not null. 80 * @throws GeneratorException on an error. 81 */ 82 public void afterGeneration( 83 String outputDirKey, 84 String outputPath, 85 String encoding, 86 OutletResult generationResult, 87 UnitConfiguration unitConfiguration) 88 throws GeneratorException 89 { 90 File outputFile = ControllerHelper.getOutputFile( 91 outputDirKey, 92 outputPath, 93 unitConfiguration); 94 try 95 { 96 if (generationResult.isStringResult()) 97 { 98 FileUtils.writeStringToFile( 99 outputFile, 100 generationResult.getStringResult(), 101 encoding); 102 } 103 else 104 { 105 FileUtils.writeByteArrayToFile( 106 outputFile, 107 generationResult.getByteArrayResult()); 108 } 109 } 110 catch (IOException e) 111 { 112 throw new ControllerException( 113 "Could not write file \"" 114 + outputFile.getAbsolutePath() 115 + "\"", 116 e); 117 } 118 } 119 120 /** 121 * Returns the name of the existing target strategy. 122 * 123 * @return "replace" 124 */ 125 public String getStrategyName() 126 { 127 return STRATEGY_NAME; 128 } 129 }