/* * Moonlight|3D * Copyright (C) 2007 The Moonlight|3D team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Sep 20, 2007 */ package eu.moonlight3d.ant; import java.io.File; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; import org.w3c.dom.Document; import org.w3c.dom.Element; public class BuildPluginListTask extends Task { public class PluginDef extends Task { public String filename; public String classname; public void setFilename(String filename) { this.filename=filename; } public void setClassname(String classname) { this.classname=classname; } } private ArrayList pluginDefs; private String filename; public BuildPluginListTask() { pluginDefs=new ArrayList(); } public PluginDef createPluginDef() { PluginDef pluginDef=new PluginDef(); pluginDefs.add(pluginDef); return pluginDef; } public void setFilename(String filename) { this.filename=filename; } public void execute() { // TODO if(pluginDefs.size()==0) { throw new BuildException("refusing to build empty plugin list file"); } try { Document document=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element plugins=document.createElement("plugins"); for(PluginDef pluginDef : pluginDefs) { Element classpath=document.createElement("classpath"); classpath.setAttribute("location", pluginDef.filename); Element plugin=document.createElement("plugin"); plugin.setAttribute("className",pluginDef.classname); plugins.appendChild(classpath); plugins.appendChild(plugin); } document.appendChild(plugins); Source domSource=new DOMSource(document); Result streamResult=new StreamResult(new File(filename)); Transformer transformer=TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent","yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(domSource,streamResult); } catch (Exception e) { // rethrow anything that happens in this block as BuildException throw new BuildException(e); } } }