| 1 | |
package com.aurea.maven.plugins.util; |
| 2 | |
|
| 3 | |
import java.io.BufferedWriter; |
| 4 | |
import java.io.File; |
| 5 | |
import java.io.FileInputStream; |
| 6 | |
import java.io.FileWriter; |
| 7 | |
import java.io.IOException; |
| 8 | |
import java.util.Properties; |
| 9 | |
|
| 10 | |
import org.apache.maven.project.MavenProject; |
| 11 | |
import org.apache.velocity.Template; |
| 12 | |
import org.apache.velocity.VelocityContext; |
| 13 | |
import org.apache.velocity.app.Velocity; |
| 14 | |
|
| 15 | |
public class VelocityRunner { |
| 16 | |
|
| 17 | 0 | private static VelocityRunner instance = null; |
| 18 | 0 | private VelocityContext context = null; |
| 19 | 0 | private MavenProject project = null; |
| 20 | 0 | private File outputDirectory = null; |
| 21 | |
|
| 22 | 0 | private VelocityRunner(MavenProject project) { |
| 23 | |
try { |
| 24 | 0 | Velocity.init(); |
| 25 | 0 | context = new VelocityContext(); |
| 26 | 0 | this.project = project; |
| 27 | 0 | Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, project.getBasedir()); |
| 28 | 0 | } catch (Exception e) { |
| 29 | |
|
| 30 | 0 | } |
| 31 | 0 | } |
| 32 | |
|
| 33 | |
public static VelocityRunner getInstance(MavenProject project) { |
| 34 | 0 | if (instance == null) { |
| 35 | 0 | instance = new VelocityRunner(project); |
| 36 | |
} |
| 37 | 0 | return instance; |
| 38 | |
} |
| 39 | |
|
| 40 | |
public static VelocityRunner getInstance() throws Exception { |
| 41 | 0 | if (instance == null) |
| 42 | 0 | throw new Exception("VelocityRunner not initialized."); |
| 43 | |
|
| 44 | 0 | return instance; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void setOutputDirectory(String outputDirName) { |
| 48 | 0 | outputDirectory = new File(project.getBuild().getDirectory(), outputDirName); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
public void addProperties(String prefix, String propFileName) { |
| 52 | |
|
| 53 | 0 | Properties props = new Properties(); |
| 54 | |
|
| 55 | 0 | if (propFileName != null) { |
| 56 | 0 | File propFile = new File(project.getBasedir() + "/" + propFileName); |
| 57 | 0 | if (propFile.exists() && propFile.canRead()) { |
| 58 | |
try { |
| 59 | 0 | props.load(new FileInputStream(propFile)); |
| 60 | 0 | } catch (Exception e) { |
| 61 | |
|
| 62 | 0 | } |
| 63 | |
} |
| 64 | |
} |
| 65 | 0 | addProperty(prefix, props); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public void addProperty(String key, Object value) { |
| 69 | 0 | context.put(key, value); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
public void run(String tplFile, String outFileName) throws Exception { |
| 73 | 0 | if (!outputDirectory.exists()) { |
| 74 | 0 | outputDirectory.mkdirs(); |
| 75 | |
} |
| 76 | |
|
| 77 | 0 | BufferedWriter out = null; |
| 78 | |
try { |
| 79 | 0 | Template template = Velocity.getTemplate(tplFile); |
| 80 | 0 | File outFile = new File(outputDirectory.getAbsoluteFile(), outFileName); |
| 81 | 0 | outFile.getParentFile().mkdirs(); |
| 82 | 0 | out = new BufferedWriter(new FileWriter(outFile)); |
| 83 | 0 | template.merge(context, out); |
| 84 | 0 | } catch (Exception e) { |
| 85 | 0 | throw new Exception("Template generation failed ..."); |
| 86 | |
} finally { |
| 87 | 0 | try { |
| 88 | 0 | if (out != null) { |
| 89 | 0 | out.flush(); |
| 90 | 0 | out.close(); |
| 91 | |
} |
| 92 | 0 | } catch (IOException ioe) { |
| 93 | |
|
| 94 | 0 | } |
| 95 | 0 | } |
| 96 | 0 | } |
| 97 | |
} |