001 package gate.util.ant;
002
003 import java.io.File;
004 import java.io.FileOutputStream;
005 import java.io.PrintWriter;
006 import java.io.StringWriter;
007 import java.util.ArrayList;
008 import java.util.List;
009
010 import gate.Gate;
011 import gate.creole.CreoleAnnotationHandler;
012 import gate.creole.metadata.CreoleResource;
013 import gate.util.GateException;
014
015 import org.apache.tools.ant.BuildException;
016 import org.apache.tools.ant.DirectoryScanner;
017 import org.apache.tools.ant.Project;
018 import org.apache.tools.ant.Task;
019 import org.apache.tools.ant.types.FileSet;
020 import org.jdom.Document;
021 import org.jdom.input.SAXBuilder;
022 import org.jdom.output.XMLOutputter;
023
024 /**
025 * Ant task to take a bunch of creole.xml files, process the
026 * {@link CreoleResource} annotations on their resources, and write the
027 * augmented XML to a target directory.
028 */
029 public class ExpandCreoleXmls extends Task {
030
031 private static boolean gateInited = false;
032
033 private List<FileSet> srcFiles = new ArrayList<FileSet>();
034
035 private File toDir;
036
037 private SAXBuilder builder = new SAXBuilder();
038
039 private XMLOutputter outputter = new XMLOutputter();
040
041 public void addFileset(FileSet fs) {
042 srcFiles.add(fs);
043 }
044
045 public void setTodir(File toDir) {
046 this.toDir = toDir;
047 }
048
049 @Override
050 public void execute() throws BuildException {
051 if(toDir == null) {
052 throw new BuildException("Please specify a destination directory using todir", getLocation());
053 }
054 if(toDir.isFile()) {
055 throw new BuildException("Destination already exists and is not a directory", getLocation());
056 }
057
058 if(!gateInited) {
059 try {
060 Gate.init();
061 }
062 catch(GateException e) {
063 throw new BuildException("Error initialising GATE", e, getLocation());
064 }
065 }
066 for(FileSet fs : srcFiles) {
067 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
068 for(String f : ds.getIncludedFiles()) {
069 File creoleFile = new File(ds.getBasedir(), f);
070 try {
071 File plugin = creoleFile.getParentFile();
072 File destFile = new File(toDir, f);
073 File destPlugin = destFile.getParentFile();
074
075 log("Expanding " + creoleFile + " to " + destFile, Project.MSG_VERBOSE);
076 Gate.addKnownPlugin(plugin.toURI().toURL());
077 CreoleAnnotationHandler annotationHandler = new CreoleAnnotationHandler(creoleFile.toURI().toURL());
078 Document creoleDoc = builder.build(creoleFile);
079 annotationHandler.addJarsToClassLoader(creoleDoc);
080 annotationHandler.createResourceElementsForDirInfo(creoleDoc);
081 annotationHandler.processAnnotations(creoleDoc);
082
083 destPlugin.mkdirs();
084 FileOutputStream fos = new FileOutputStream(destFile);
085 try {
086 outputter.output(creoleDoc, fos);
087 }
088 finally {
089 fos.close();
090 }
091 }
092 catch(Exception e) {
093 log("Error processing " + creoleFile + ", skipped", Project.MSG_WARN);
094 StringWriter sw = new StringWriter();
095 PrintWriter pw = new PrintWriter(sw);
096 e.printStackTrace(pw);
097 log(sw.toString(), Project.MSG_VERBOSE);
098 }
099 }
100 }
101 }
102
103 public void setGateHome(File gateHome) {
104 Gate.setGateHome(gateHome);
105 }
106
107 public void setPluginsHome(File pluginsHome) {
108 Gate.setPluginsHome(pluginsHome);
109 }
110
111 }
|