Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to test if a file does not exist before generate it.

Is it possible to use something like isFileExist ?

[template public generate(conf : Configuration) ? (isFileExist('/path/to/file.txt'))]
[file ('/path/to/file.txt', false, 'UTF-8')]
file content
[/file]
[/template]

Thanks in advance.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I just had the same problem as you have, and I finally got it working by "externalizing" this function in an external Java class. That way, you can just define a method such as:

public boolean existsFile(String filepath) {
    Path p = Paths.get(filepath);
    p.normalize();
    File f = new File(filepath);
    return f.exists();
}

and then call it from Acceleo by defining a query like:

[query public existsFile(filePath : String): 
    Boolean = invoke('utils.AcceleoUtils', 'existsFile(java.lang.String)', Sequence{filePath})
/]

This way, in your example, you could do

[template public generate(conf : Configuration)]
    [if existsFile('/path/to/file.txt')/]
        [file ('/path/to/file.txt', false, 'UTF-8')]
            file content
        [/file]
    [/if]

[/template]

p.d: Be carefull with the paths, because the ´file´ tag outputs your files in your target path by default, so in case you´re not providing an absolute Path you will need to include it either when calling or inside the existsFile function.

share|improve this answer
    
So we do not use pre guard. I did not know [file /] tag could be enclosed by an [if /] tag. thank you. –  Mickaël Gauvin Sep 15 '14 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.