}
public String addToOutput(String suggestedFileName, String mimeType,
byte[] data, boolean xhrCompatible) throws UnableToCompleteException {
TreeLogger logger = getLogger();
GeneratorContext context = getGeneratorContext();
PropertyOracle propertyOracle = context.getPropertyOracle();
// See if filename obfuscation should be enabled
String enableRenaming = null;
try {
enableRenaming = propertyOracle.getPropertyValue(logger, ENABLE_RENAMING);
} catch (BadPropertyValueException e) {
logger.log(TreeLogger.ERROR, "Bad value for " + ENABLE_RENAMING, e);
throw new UnableToCompleteException();
}
// Determine the final filename for the resource's file
String outputName;
if (Boolean.parseBoolean(enableRenaming)) {
String strongName = Util.computeStrongName(data);
// Determine the extension of the original file
String extension;
int lastIdx = suggestedFileName.lastIndexOf('.');
if (lastIdx != -1) {
extension = suggestedFileName.substring(lastIdx + 1);
} else {
extension = "noext";
}
// The name will be MD5.cache.ext
outputName = strongName + ".cache." + extension;
} else {
outputName =
suggestedFileName.substring(suggestedFileName.lastIndexOf('/') + 1);
}
// Ask the context for an OutputStream into the named resource
OutputStream out = context.tryCreateResource(logger, outputName);
// This would be null if the resource has already been created in the
// output (because two or more resources had identical content).
if (out != null) {
try {
out.write(data);
} catch (IOException e) {
logger.log(TreeLogger.ERROR, "Unable to write data to output name "
+ outputName, e);
throw new UnableToCompleteException();
}
// If there's an error, this won't be called and there will be nothing
// created in the output directory.
context.commitResource(logger, out);
logger.log(TreeLogger.DEBUG, "Copied " + data.length + " bytes to "
+ outputName, null);
}