View Javadoc
1   /*
2    * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and contributors.
3    *
4    * All rights reserved. This program and the accompanying materials
5    * are made available under the terms of the GNU Lesser General Public License
6    * (LGPL) version 2.1 which accompanies this distribution, and is available at
7    * http://www.gnu.org/licenses/lgpl-2.1.html
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * Contributors:
15   *     bstefanescu
16   */
17  package org.nuxeo.build.ant.ftl;
18  
19  import java.io.File;
20  import java.io.FileWriter;
21  import java.io.StringWriter;
22  
23  import org.apache.tools.ant.BuildException;
24  import org.apache.tools.ant.Task;
25  
26  /**
27   * TODO NXBT-258
28   */
29  public class ProcessTemplateTask extends Task {
30  
31      public File baseDir;
32  
33      public String[] extensions = { "ftl" };
34  
35      public boolean removeExtension = true;
36  
37      public boolean removeTemplate = true;
38  
39      public boolean explicitRemoveTemplate = false;
40  
41      public Object input;
42  
43      public File toDir;
44  
45      public FreemarkerEngine engine;
46  
47      public void setBasedir(File baseDir) {
48          this.baseDir = baseDir;
49      }
50  
51      public void setExtension(String extension) {
52          extensions = extension.trim().split("\\s*,\\s*");
53      }
54  
55      public void setRemoveExtension(boolean removeExtension) {
56          this.removeExtension = removeExtension;
57      }
58  
59      public void setRemoveTemplate(boolean removeTemplate) {
60          explicitRemoveTemplate = true;
61          this.removeTemplate = removeTemplate;
62      }
63  
64      public void setTodir(File toDir) {
65          this.toDir = toDir;
66      }
67  
68      @Override
69      public void execute() throws BuildException {
70          if (engine == null) {
71              engine = new FreemarkerEngine();
72          }
73          engine.setBaseDir(baseDir);
74          if (toDir == null) {
75              toDir = baseDir;
76          } else if (!explicitRemoveTemplate) {
77              removeTemplate = false;
78          }
79          toDir.mkdirs();
80          File dir = baseDir;
81          String relPath = "";
82          processDirectory(dir, relPath);
83      }
84  
85      public void processDirectory(File dir, String relPath) {
86          for (File file : dir.listFiles()) {
87              String name = file.getName();
88              if (file.isDirectory()) {
89                  processDirectory(file, relPath + "/" + name);
90              } else {
91                  int p = name.lastIndexOf('.');
92                  if (p > -1) {
93                      String ext = name.substring(p + 1);
94                      for (int i = 0; i < extensions.length; i++) {
95                          if (ext.equals(extensions[i])) {
96                              processFile(file, relPath + "/" + name, ext);
97                              break;
98                          }
99                      }
100                 }
101             }
102         }
103     }
104 
105     public void processFile(File file, String relPath, String ext) {
106         try {
107             StringWriter writer = new StringWriter();
108             if (input == null) {
109                 input = engine.createInput(getProject());
110             }
111             engine.process(input, relPath, writer);
112             if (removeExtension) {
113                 relPath = relPath.substring(0, relPath.length() - ext.length()
114                         - 1);
115             }
116             File f = new File(toDir, relPath);
117             f.getParentFile().mkdirs();
118             FileWriter out = new FileWriter(f);
119             try {
120                 out.write(writer.getBuffer().toString());
121             } finally {
122                 out.close();
123             }
124             if (removeTemplate) {
125                 file.delete();
126             }
127         } catch (Exception e) {
128             throw new BuildException("Failed to process template: " + relPath,
129                     e);
130         }
131     }
132 
133 }