Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » [XPand2] Overwriting of read-only files
[XPand2] Overwriting of read-only files [message #659019] Thu, 10 March 2011 17:18
Tom Brandenburg is currently offline Tom BrandenburgFriend
Messages: 58
Registered: October 2010
Location: Abstatt, Germany
Member
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 Smile

I would be happy if you have some comments to it Smile And if the solution is OK I am happy to publish this code-snippet Smile

Bye Tom

[Updated on: Thu, 10 March 2011 17:20]

Report message to a moderator

Previous Topic:[Xtend2] Migration Scenarios?
Next Topic:[Xtend] access static field/method
Goto Forum:
  


Current Time: Thu Apr 25 06:18:18 GMT 2024

Powered by FUDForum. Page generated in 0.03048 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top