Hello all,
today I faced the problem that a MWE workflow with an XPand-Component which generates some code ends up in an IOException once it sees a read-only file. (Access denied)
My current environment allows to overwrite a read-only file without thoughts.
I struggled around a bit, weighing my possibilities and found a solution with the following PostProcessor:
public class ForceOverwrite implements PostProcessor {
private static File lastFile;
private static Boolean lastFileReadOnly = false;
private static File getLastFile() {
return lastFile;
}
private static void setLastFile(File lastFile) {
ForceOverwrite.lastFile = lastFile;
}
private static Boolean getLastFileReadOnly() {
return lastFileReadOnly;
}
private static void setLastFileReadOnly(Boolean lastFileReadOnly) {
ForceOverwrite.lastFileReadOnly = lastFileReadOnly;
}
public void afterClose(FileHandle impl) {
// If current and last file are the same
if(impl.getTargetFile().compareTo(ForceOverwrite.getLastFile()) == 0) {
// If the file exists and it was read only set it read only again!
if(ForceOverwrite.getLastFile().exists() && ForceOverwrite.getLastFileReadOnly()) {
ForceOverwrite.getLastFile().setReadOnly();
}
}
}
public void beforeWriteAndClose(FileHandle impl) {
File target = impl.getTargetFile();
ForceOverwrite.setLastFile(target);
// Perform changes to file attributes only if the file already exists
if(target.exists()) {
// If it can't be written, set it readable
if(!target.canWrite()) {
ForceOverwrite.setLastFileReadOnly(true);
target.setWritable(true);
} else {
ForceOverwrite.setLastFileReadOnly(false);
}
} else {
// If the file does not exist
ForceOverwrite.setLastFileReadOnly(false);
}
}
}
The postprocessor is included like:
<outlet path="${src_gen_folder}">
<postprocessor class="somepackage.codegen.postprocess.ForceOverwrite"/>
</outlet>
Now, it works fine but I don't know if this is the best solution or if there even is an easier one. Maybe it was also too late for me to find the same 
I would be happy if you have some comments to it
And if the solution is OK I am happy to publish this code-snippet 
Bye Tom
[Updated on: Thu, 10 March 2011 12:20] by Moderator