/* * Copyright 2005-2010 Ignis Software Tools Ltd. All rights reserved. */ package jsystem.framework.scripts.ant; import java.io.File; import java.util.ArrayList; import javax.swing.ImageIcon; import jsystem.framework.scripts.ScriptEngine; import jsystem.framework.scripts.ScriptExecutor; import jsystem.utils.FileUtils; import org.apache.xpath.XPathAPI; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * Used to execute ANT files * @author guy.arieli * */ public class AntScriptEngine implements ScriptEngine { /** * Accept files that are xml file has a main 'project' tag and don't have the * jsystem scenario footprint. */ public boolean accept(File file) { if(file.getName().toLowerCase().endsWith(".xml")){ try { Document doc = FileUtils.readDocumentFromFile(file); if(((Element) doc.getDocumentElement()).getTagName().equals("project")){ return (FileUtils.read(file).indexOf("auto-generated by the jsystem") < 0); } } catch (Exception e) { // ignored } } return false; } /** * An executor will be created for every target in the xml file */ public ScriptExecutor[] getExecutor(File file){ Document doc; NodeList nodeList; try { doc = FileUtils.readDocumentFromFile(file); nodeList = XPathAPI.selectNodeList(doc, "/project/target"); } catch (Exception e) { return null; } ArrayList<AntScriptExecutor> ants = new ArrayList<AntScriptExecutor>(); String scriptName = file.getName().substring(0, file.getName().length() - 4); for(int i = 0; i < nodeList.getLength(); i++){ if(nodeList.item(i) instanceof Element){ Element target = (Element)nodeList.item(i); String name = target.getAttribute("name"); if(name != null){ AntScriptExecutor executor = new AntScriptExecutor(); executor.configScriptName(scriptName); executor.configTarget(name); ants.add(executor); } } } return ants.toArray(new ScriptExecutor[0]); } /** * The executor ID is the class name of the script executor. */ public boolean accept(String id) { return AntScriptExecutor.class.getName().equals(id); } /** * Constract executor from the executor tag. */ public ScriptExecutor getExecutor(String tag) { AntScriptExecutor se = new AntScriptExecutor(); String[] tags = tag.split("\\."); if(tags.length < 2){ return null; } se.configScriptName(tags[0]); se.configTarget(tags[1]); return se; } public String getId() { return AntScriptExecutor.class.getName(); } public ImageIcon getBasicImageIcon() { return null; } public ImageIcon getErrorImageIcon() { return null; } public ImageIcon getFailImageIcon() { return null; } public ImageIcon getRunningImageIcon() { return null; } public ImageIcon getOKImageIcon() { return null; } }