1 52 53 package freemarker.ext.ant; 54 55 import java.io.*; 56 import java.util.Map ; 57 58 import org.apache.tools.ant.BuildException; 59 import org.apache.tools.ant.ProjectHelper; 60 import org.apache.tools.ant.Task; 61 62 import freemarker.template.utility.ClassUtil; 63 64 72 public class JythonAntTask extends Task { 73 74 private File scriptFile; 75 private String script = ""; 76 private UnlinkedJythonOperations jythonOps; 77 78 public void setFile(File scriptFile) throws BuildException { 79 ensureJythonOpsExists(); 80 this.scriptFile = scriptFile; 81 } 82 83 public void addText(String text) { 84 script += text; 85 } 86 87 public void execute(Map vars) throws BuildException { 88 if (scriptFile != null) { 89 ensureJythonOpsExists(); 90 jythonOps.execute(scriptFile, vars); 91 } 92 if (script.trim().length() >0) { 93 ensureJythonOpsExists(); 94 String finalScript = ProjectHelper.replaceProperties( 95 project, script, project.getProperties()); 96 jythonOps.execute(finalScript, vars); 97 } 98 } 99 100 private void ensureJythonOpsExists() { 101 if (jythonOps == null) { 102 Class clazz; 103 try { 104 clazz = ClassUtil.forName( 105 "freemarker.ext.ant.UnlinkedJythonOperationsImpl"); 106 } catch (ClassNotFoundException e) { 107 throw new RuntimeException ( 108 "A ClassNotFoundException has been thrown when trying " 109 + "to get the " 110 + "freemarker.ext.ant.UnlinkedJythonOperationsImpl class. " 111 + "The error message was: " + e.getMessage()); 112 } 113 try { 114 jythonOps 115 = (UnlinkedJythonOperations) clazz.newInstance(); 116 } catch (Exception e) { 117 throw new RuntimeException ( 118 "An exception has been thrown when trying " 119 + "to create a freemarker.ext.ant.JythonAntTask " 120 + "object. The exception was: " + e); 121 } 122 } 123 } 124 125 } 126 | Popular Tags |