001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: XSLToolTest.java,v 1.2 2007/03/27 21:59:43 mlipp Exp $
021: *
022: * $Log: XSLToolTest.java,v $
023: * Revision 1.2 2007/03/27 21:59:43 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.1 2007/03/22 15:49:12 schnelle
027: * Component renamed.
028: *
029: * Revision 1.1 2007/03/22 13:49:11 schnelle
030: * Initial release.
031: *
032: */
033:
034: package de.danet.an.workflow.tools.test;
035:
036: import java.io.File;
037: import java.io.IOException;
038: import java.net.URL;
039: import java.rmi.RemoteException;
040: import java.util.Iterator;
041: import java.util.Map;
042:
043: import javax.xml.parsers.DocumentBuilder;
044: import javax.xml.parsers.DocumentBuilderFactory;
045: import javax.xml.parsers.ParserConfigurationException;
046:
047: import org.w3c.dom.Document;
048: import org.w3c.dom.Element;
049:
050: import de.danet.an.workflow.api.FormalParameter;
051: import de.danet.an.workflow.spis.aii.CannotExecuteException;
052: import de.danet.an.workflow.spis.aii.ToolAgent;
053: import de.danet.an.workflow.tools.XSLTTool;
054: import de.danet.an.workflow.util.XPDLUtil;
055:
056: /**
057: * Base class for tests with the {@link XSLTTool} tool.
058: *
059: * @author Dirk Schnelle
060: */
061: public abstract class XSLToolTest extends ToolAgentTestBase {
062: /** Mapping of results to output parameters. */
063: private Map mappings;
064:
065: /**
066: * Constructs a test case without a name.
067: */
068: public XSLToolTest() {
069: this (null);
070: }
071:
072: /**
073: * Constructs a test case with the specified name.
074: * @param name name of the test.
075: */
076: public XSLToolTest(String name) {
077: super (name);
078: mappings = new java.util.HashMap();
079: }
080:
081: /* (non-Javadoc)
082: * Comment copied from interface or super class.
083: */
084: public ToolAgent createToolAgent() {
085: return new XSLTTool();
086: }
087:
088: /**
089: * {@inheritDoc}
090: */
091: protected void setUp() throws Exception {
092: super .setUp();
093:
094: mappings.clear();
095: }
096:
097: /**
098: * Adds the given mapping of output parameters to XPATH expressions.
099: * @param name name of the parameter.
100: * @param select XPATH expression to evaluate.
101: */
102: protected void addMapping(String name, String select) {
103: mappings.put(name, select);
104: }
105:
106: /**
107: * Creates the mappings for the output.
108: * @return mapping element that can be used in the tool.
109: * @throws ParserConfigurationException
110: * error creating a document.
111: */
112: private Element createMappings()
113: throws ParserConfigurationException {
114: DocumentBuilderFactory factory = DocumentBuilderFactory
115: .newInstance();
116: DocumentBuilder builder = factory.newDocumentBuilder();
117: Document document = builder.newDocument();
118:
119: Element mappingsElement = document.createElementNS(
120: XPDLUtil.XPDL_EXTN_NS, "vx:OutputMappings");
121: document.appendChild(mappingsElement);
122:
123: Iterator iterator = mappings.keySet().iterator();
124: while (iterator.hasNext()) {
125: String name = (String) iterator.next();
126: String select = (String) mappings.get(name);
127: Element messageParameter = document.createElementNS(
128: XPDLUtil.XPDL_EXTN_NS, "vx:Parameter");
129: messageParameter.setAttribute("Name", name);
130: messageParameter.setAttribute("Select", select);
131: mappingsElement.appendChild(messageParameter);
132: }
133:
134: return document.getDocumentElement();
135: }
136:
137: /**
138: * Retrieves the XSLT tool.
139: *
140: * @return The XSLT tool.
141: */
142: public XSLTTool getXSLTTool() {
143: return (XSLTTool) getTool();
144: }
145:
146: /* (non-Javadoc)
147: * Comment copied from interface or super class.
148: */
149: protected void invokeTool(FormalParameter[] formPars, Map map)
150: throws RemoteException, CannotExecuteException {
151: if (!mappings.isEmpty()) {
152: Element mappingElement;
153: try {
154: mappingElement = createMappings();
155: } catch (ParserConfigurationException e) {
156: throw new CannotExecuteException(e.getMessage(), e);
157: }
158: getXSLTTool().setMappings(mappingElement);
159: }
160:
161: super .invokeTool(formPars, map);
162: }
163:
164: /**
165: * Loads the XSL transformation and sets it in the tool.
166: * @param xslt Filename of the XSL transformation to load.
167: * @throws IOException Error reading the XSLT.
168: */
169: protected void loadXslt(String xslt) throws IOException {
170: File xslFile = new File(xslt);
171: URL url = xslFile.toURL();
172: getXSLTTool().setXSLT(url.toString());
173: }
174: }
|