001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor 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 * MActor 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 MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.extensions.xml;
021
022 import java.io.File;
023 import java.util.List;
024
025 import javax.xml.transform.Templates;
026 import javax.xml.transform.Transformer;
027 import javax.xml.transform.TransformerException;
028 import javax.xml.transform.TransformerFactory;
029 import javax.xml.transform.stream.StreamSource;
030
031 import org.dom4j.io.DocumentResult;
032 import org.dom4j.io.DocumentSource;
033 import org.mactor.brokers.Message;
034 import org.mactor.framework.ConfigException;
035 import org.mactor.framework.MactorException;
036 import org.mactor.framework.TestContext;
037 import org.mactor.framework.extensioninterface.MessageBuilderCommand;
038
039 public class ForwardXslMessageBuilder implements MessageBuilderCommand {
040 public Message buildMessage(TestContext context, String templatePath, List<String> params) throws MactorException {
041 Message last = context.getLastIncomingMessage();
042 if (last == null)
043 throw new MactorException("There is no incoming message to build a message from");
044 if (templatePath == null || templatePath.length() == 0)
045 throw new ConfigException("Invalid testspec. Template Path must be the relative path to the XSL file that should be used to transform the last received message into the result message");
046 return transform(last, templatePath);
047 }
048 public Message transform(Message message, String xslPath) throws MactorException {
049 DocumentSource source = new DocumentSource(message.getContentDocument());
050 DocumentResult result = new DocumentResult();
051 try {
052 getTransformer(xslPath).transform(source, result);
053 return Message.createMessage(result.getDocument());
054 } catch (TransformerException te) {
055 throw new MactorException("Failed to transform the received message using the specifed XSL '" + xslPath + "'. Problem:" + te.getMessage(), te);
056 }
057 }
058 private Transformer getTransformer(String xslPath) throws MactorException {
059 File absoluteXslPath = new File(xslPath);
060 if (!absoluteXslPath.exists())
061 throw new ConfigException("The specifed XSL file '" + absoluteXslPath.getAbsolutePath() + "' does not exist");
062 TransformerFactory factory = TransformerFactory.newInstance();
063 try {
064 Templates t = factory.newTemplates(new StreamSource(absoluteXslPath));
065 return t.newTransformer();
066 } catch (Exception e) {
067 throw new ConfigException("The specifed XSL file '" + absoluteXslPath.getAbsolutePath() + "' could not be loaded. Error:" + e.getMessage(), e);
068 }
069 }
070 }