/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: XSLToolTest.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.1 2007/03/22 15:49:12 schnelle
* Component renamed.
*
* Revision 1.1 2007/03/22 13:49:11 schnelle
* Initial release.
*
*/
package de.danet.an.workflow.tools.test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import de.danet.an.workflow.api.FormalParameter;
import de.danet.an.workflow.spis.aii.CannotExecuteException;
import de.danet.an.workflow.spis.aii.ToolAgent;
import de.danet.an.workflow.tools.XSLTTool;
import de.danet.an.workflow.util.XPDLUtil;
/**
* Base class for tests with the {@link XSLTTool} tool.
*
* @author Dirk Schnelle
*/
public abstract class XSLToolTest extends ToolAgentTestBase {
/** Mapping of results to output parameters. */
private Map mappings;
/**
* Constructs a test case without a name.
*/
public XSLToolTest() {
this(null);
}
/**
* Constructs a test case with the specified name.
* @param name name of the test.
*/
public XSLToolTest(String name) {
super(name);
mappings = new java.util.HashMap();
}
/* (non-Javadoc)
* Comment copied from interface or super class.
*/
public ToolAgent createToolAgent() {
return new XSLTTool();
}
/**
* {@inheritDoc}
*/
protected void setUp() throws Exception {
super.setUp();
mappings.clear();
}
/**
* Adds the given mapping of output parameters to XPATH expressions.
* @param name name of the parameter.
* @param select XPATH expression to evaluate.
*/
protected void addMapping(String name, String select) {
mappings.put(name, select);
}
/**
* Creates the mappings for the output.
* @return mapping element that can be used in the tool.
* @throws ParserConfigurationException
* error creating a document.
*/
private Element createMappings() throws ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element mappingsElement
= document.createElementNS
(XPDLUtil.XPDL_EXTN_NS, "vx:OutputMappings");
document.appendChild(mappingsElement);
Iterator iterator = mappings.keySet().iterator();
while (iterator.hasNext()) {
String name = (String) iterator.next();
String select = (String) mappings.get(name);
Element messageParameter = document
.createElementNS(XPDLUtil.XPDL_EXTN_NS, "vx:Parameter");
messageParameter.setAttribute("Name", name);
messageParameter.setAttribute("Select", select);
mappingsElement.appendChild(messageParameter);
}
return document.getDocumentElement();
}
/**
* Retrieves the XSLT tool.
*
* @return The XSLT tool.
*/
public XSLTTool getXSLTTool() {
return (XSLTTool) getTool();
}
/* (non-Javadoc)
* Comment copied from interface or super class.
*/
protected void invokeTool(FormalParameter[] formPars, Map map)
throws RemoteException, CannotExecuteException {
if (!mappings.isEmpty()) {
Element mappingElement;
try {
mappingElement = createMappings();
} catch (ParserConfigurationException e) {
throw new CannotExecuteException(e.getMessage(), e);
}
getXSLTTool().setMappings(mappingElement);
}
super.invokeTool(formPars, map);
}
/**
* Loads the XSL transformation and sets it in the tool.
* @param xslt Filename of the XSL transformation to load.
* @throws IOException Error reading the XSLT.
*/
protected void loadXslt(String xslt) throws IOException {
File xslFile = new File(xslt);
URL url = xslFile.toURL();
getXSLTTool().setXSLT(url.toString());
}
}