01: /******************************************************************************
02: * Copyright (C) Lars Ivar Almli. All rights reserved. *
03: * ---------------------------------------------------------------------------*
04: * This file is part of MActor. *
05: * *
06: * MActor is free software; you can redistribute it and/or modify *
07: * it under the terms of the GNU General Public License as published by *
08: * the Free Software Foundation; either version 2 of the License, or *
09: * (at your option) any later version. *
10: * *
11: * MActor is distributed in the hope that it will be useful, *
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14: * GNU General Public License for more details. *
15: * *
16: * You should have received a copy of the GNU General Public License *
17: * along with MActor; if not, write to the Free Software *
18: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
19: ******************************************************************************/package org.mactor.extensions.xml;
20:
21: import java.util.List;
22: import org.dom4j.Document;
23: import org.dom4j.InvalidXPathException;
24: import org.dom4j.Node;
25: import org.mactor.framework.ConfigException;
26: import org.mactor.framework.MactorException;
27: import org.mactor.framework.TestContext;
28: import org.mactor.framework.extensioninterface.ValueCommand;
29:
30: /**
31: * Extracts a selected field from the last received message
32: * <br/>
33: * The field to select is specified as a single parameter containing the XPath expression that selects the single attribute or element
34: * <br/>
35: * Namespace information in the evaluated messages is ignored, so the XPath expressions must not include namespace prefixes
36: * @author Lars Ivar Almli
37: */
38: public class XPathIgnoreNsValueExtractor implements ValueCommand {
39: public String extractValue(TestContext context, List<String> params)
40: throws MactorException {
41: try {
42: Document doc = context.getLastIncomingMessage()
43: .getContentDocumentNoNs();
44: if (params.size() == 0)
45: return null;
46: Node n = doc.selectSingleNode(params.get(0));
47: return n == null ? null : n.getText();
48: } catch (InvalidXPathException ie) {
49: throw new ConfigException("Invalid xpath expression '"
50: + params.get(0) + "'. Error: " + ie.getMessage(),
51: ie);
52: }
53: }
54: }
|