001package org.andromda.core.cartridge; 002 003import java.io.File; 004import java.text.MessageFormat; 005import org.andromda.core.configuration.NamespaceProperties; 006import org.andromda.core.configuration.Namespaces; 007import org.andromda.core.configuration.Property; 008import org.apache.commons.lang.StringUtils; 009 010/** 011 * <p> 012 * This class implements the <code><resource></code> tag in a cartridge descriptor file and represents the base 013 * cartridge resource element. </p> 014 * 015 * @author Chad Brandon 016 * @author Bob Fields 017 */ 018public class Resource 019{ 020 /** 021 * Stores the output location logical name. 022 */ 023 private String outlet; 024 025 /** 026 * Gets the logical location to which output from this resource will be written. 027 * 028 * @return Returns the outlet. 029 */ 030 public String getOutlet() 031 { 032 return outlet; 033 } 034 035 /** 036 * Sets the logical location to which output from this resource will be written. 037 * 038 * @param outlet The outlet to set. 039 */ 040 public void setOutlet(final String outlet) 041 { 042 this.outlet = outlet; 043 } 044 045 /** 046 * Stores the outputCondition that must evaluate to true for the template to be written. 047 */ 048 private String outputCondition; 049 050 /** 051 * Sets the outputCondition that must evaluate to true in order for the template to be written. 052 * 053 * @param outputCondition the template engine outputCondition. 054 */ 055 public void setOutputCondition(final String outputCondition) 056 { 057 this.outputCondition = outputCondition; 058 } 059 060 /** 061 * Gets the outputCondition that must evaluate to true in order for the template to be written. 062 * 063 * @return the template engine outputCondition. 064 */ 065 public String getOutputCondition() 066 { 067 return this.outputCondition; 068 } 069 070 /** 071 * Returns the fully qualified name of the resource output to be written, this means: <ul> <li>the output pattern 072 * has been translated</li> <li>the output directory name has been prepended</li> </ul> 073 * 074 * @param arguments any arguments to be inserted into the MessageFormat style messages. 075 * @param directory the directory to which output will be written. 076 * @param outputPattern if undefined, the value of {@link #getOutputPattern()} will be used. 077 * @return File absolute directory. 078 */ 079 public File getOutputLocation( 080 final Object[] arguments, 081 final File directory, 082 String outputPattern) 083 { 084 File file = null; 085 086 // - clean any whitespace off the arguments 087 if (directory != null && arguments != null && arguments.length > 0) 088 { 089 for (int ctr = 0; ctr < arguments.length; ctr++) 090 { 091 arguments[ctr] = StringUtils.trimToEmpty(String.valueOf(arguments[ctr])); 092 } 093 if (StringUtils.isBlank(outputPattern)) 094 { 095 outputPattern = this.getOutputPattern(); 096 } 097 String outputFileName; 098 try 099 { 100 outputFileName = MessageFormat.format( 101 outputPattern, 102 arguments); 103 } 104 catch (final Exception exception) 105 { 106 // - means the output pattern can't be parsed (but we still 107 // want to output the bad path anyway) 108 outputFileName = outputPattern; 109 } 110 file = new File( 111 directory, 112 outputFileName); 113 } 114 return file; 115 } 116 117 /** 118 * Stores whether or not the resource should be overwritten. 119 */ 120 private boolean overwrite = false; 121 122 /** 123 * Tells us whether output files produced by this resource should be overwritten if they already exist. Overwriting 124 * can be turned on and off for entire cartridges by setting the <code>overwrite</code> property in a namespace. 125 * This is useful for cartridge developers when they always want produced resources to be overwritten at first. 126 * 127 * @return Returns the overwrite. 128 */ 129 public boolean isOverwrite() 130 { 131 final Property property = 132 Namespaces.instance().getProperty( 133 this.getCartridge().getNamespace(), 134 NamespaceProperties.OVERWRITE, 135 false); 136 if (property != null) 137 { 138 this.overwrite = Boolean.valueOf(property.getValue()); 139 } 140 return this.overwrite; 141 } 142 143 /** 144 * Sets whether output files produced by this resource should be overwritten if they already exist. 145 * 146 * @param overwrite The overwrite to set. 147 */ 148 public void setOverwrite(final boolean overwrite) 149 { 150 this.overwrite = overwrite; 151 } 152 153 /** 154 * Whether or not a last modified check should be performed before writing the resource. 155 */ 156 private boolean lastModifiedCheck; 157 158 /** 159 * Sets whether or not a last modified check should be performed before writing the resource. 160 * 161 * @param lastModifiedCheck true/false 162 */ 163 public void setLastModifiedCheck(final boolean lastModifiedCheck) 164 { 165 this.lastModifiedCheck = lastModifiedCheck; 166 } 167 168 /** 169 * Whether or not a last modified check should be performed before writing the resource. 170 * 171 * @return true/false 172 */ 173 public boolean isLastModifiedCheck() 174 { 175 return this.lastModifiedCheck; 176 } 177 178 /** 179 * Store the path to a cartridge resource. 180 */ 181 private String path; 182 183 /** 184 * Gets the path to the cartridge resource. 185 * 186 * @return Returns the path. 187 */ 188 public String getPath() 189 { 190 return this.path; 191 } 192 193 /** 194 * Sets the path to the cartridge resource. 195 * 196 * @param path The path to set. 197 */ 198 public void setPath(final String path) 199 { 200 this.path = path; 201 } 202 203 /** 204 * Stores the cartridge that owns this resource. 205 */ 206 private Cartridge cartridge; 207 208 /** 209 * The cartridge that owns this resource. 210 * 211 * @return Returns the owning cartridge. 212 */ 213 public Cartridge getCartridge() 214 { 215 return this.cartridge; 216 } 217 218 /** 219 * Sets the Cartridge parent to which this Resource belongs. 220 * 221 * @param cartridge the parent Cartridge to set. 222 */ 223 public void setCartridge(final Cartridge cartridge) 224 { 225 this.cartridge = cartridge; 226 } 227 228 /** 229 * Stores the output pattern for which the resource(s) should be written. 230 */ 231 private String outputPattern; 232 233 /** 234 * Sets the pattern that is used to build the name of the output file. 235 * 236 * @param outputPattern the pattern in java.text.MessageFormat syntax 237 */ 238 public void setOutputPattern(final String outputPattern) 239 { 240 this.outputPattern = outputPattern; 241 } 242 243 /** 244 * Gets the pattern that is used to build the name of the output file. 245 * 246 * @return String the pattern in java.text.MessageFormat syntax 247 */ 248 public String getOutputPattern() 249 { 250 return StringUtils.trimToEmpty(this.outputPattern); 251 } 252 253 /** 254 * @see java.lang.Object#toString() 255 */ 256 @Override 257 public String toString() 258 { 259 StringBuilder builder = new StringBuilder(); 260 builder.append(super.toString()); 261 builder.append(" [outlet=").append(this.outlet); 262 builder.append(", outputCondition=").append(this.outputCondition); 263 builder.append(", overwrite=").append(this.overwrite); 264 builder.append(", lastModifiedCheck=").append(this.lastModifiedCheck); 265 builder.append(", path=").append(this.path); 266 builder.append(", cartridge=").append(this.cartridge); 267 builder.append(", outputPattern=").append(this.outputPattern).append("]"); 268 return builder.toString(); 269 } 270}