001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. 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: ******************************************************************************/package org.mactor.extensions.xml;
020:
021: import java.io.File;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Map.Entry;
026: import org.dom4j.Document;
027: import org.dom4j.DocumentException;
028: import org.dom4j.Namespace;
029: import org.dom4j.Node;
030: import org.dom4j.XPath;
031: import org.dom4j.io.SAXReader;
032: import org.mactor.brokers.Message;
033: import org.mactor.framework.ConfigException;
034: import org.mactor.framework.MactorException;
035: import org.mactor.framework.TestContext;
036: import org.mactor.framework.extensioninterface.MessageBuilderCommand;
037:
038: /**
039: * Builds a message based on a XML file (the template), substituting the vales in the file with the values specied in the paramaters.<br/>
040: *
041: * The syntax of a parameters are:<br>
042: * [XPath expression that selects the single attribute or element ]==[value that should be assigned to the field]
043: * <br/>
044: * If the namespace prefixes are used in the template file, the same prefixes must be used in the XPath expression.<br/>
045: * If the a default namespace is used in the template file, 'def' must by used a namespace prefix in the XPath expression<br7>
046: * @author Lars Ivar Almli
047: */
048:
049: public class XPathTemplateMessageBuilder implements
050: MessageBuilderCommand {
051: public Message buildMessage(TestContext context,
052: String templatePath, List<String> params)
053: throws MactorException {
054: File template = new File(templatePath);
055: if (!template.exists())
056: throw new MactorException("The specified template '"
057: + template.getAbsolutePath() + "' does not exist");
058: Document doc = null;
059: try {
060: doc = new SAXReader().read(template);
061: } catch (DocumentException de) {
062: throw new MactorException("Failed to parse template '"
063: + template.getAbsolutePath() + "' as xml");
064: }
065: HashMap<XPath, String> map = ParamUtil.parseXPathParams(params);
066: // Ok, this is a hack to simplify the XPathTemplateMessageBuilder 'user
067: // interface':
068: // use 'def' as the default prefix
069: List nsList = doc.getRootElement().declaredNamespaces();
070: Map nsMap = new HashMap();
071: for (Object o : nsList) {
072: Namespace ns = (Namespace) o;
073: if (ns.getPrefix() == null || ns.getPrefix().length() == 0)
074: addNsPrefix(nsMap, "def", ns.getURI());
075: else
076: addNsPrefix(nsMap, ns.getPrefix(), ns.getURI());
077: }
078: for (Entry<XPath, String> e : map.entrySet()) {
079: e.getKey().setNamespaceURIs(nsMap);
080: }
081: for (Entry<XPath, String> e : map.entrySet()) {
082: Node n = e.getKey().selectSingleNode(doc);
083: if (n == null)
084: throw new ConfigException("The template '"
085: + templatePath
086: + "' does not contain the xpath '" + e.getKey()
087: + "'");
088: n.setText(e.getValue());
089: }
090: return Message.createMessage(doc);
091: }
092:
093: private void addNsPrefix(Map m, String prefix, String uri)
094: throws ConfigException {
095: if (m.containsKey(prefix))
096: throw new ConfigException(
097: "Namespace conflict. The namespace '" + prefix
098: + "' is used for both '" + uri + "' and '"
099: + m.get(prefix) + "'");
100: m.put(prefix, uri);
101: }
102: }
|