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.HashMap;
22: import java.util.List;
23: import java.util.Map.Entry;
24: import org.dom4j.Document;
25: import org.dom4j.Node;
26: import org.dom4j.XPath;
27: import org.mactor.brokers.Message;
28: import org.mactor.framework.MactorException;
29: import org.mactor.framework.extensioninterface.MessageSelectorCommand;
30:
31: /**
32: * Selects all messages matching the XPath/value expressions specified in the parameters.
33: * <br/>
34: * The syntax of a parameters are:<br>
35: * [XPath expression that selects the single attribute or element ]==[value that the selected field must be equal to]
36: * <br/>
37: * I.e. the following setup matches alle messages containing an Orderid field with the value 5
38: * <pre>
39: * <message_selector command="java:org.mactor.extensions.xml.XPathIgnoreNsMessageSelector">
40: * <param>//OrderId==5</param>
41: * </message_selector>
42: * </pre>
43: * Namespace information in the evaluated messages is ignored, so the XPath expressions must not include namespace prefixes
44: * @author Lars Ivar Almli
45: */
46: public class XPathIgnoreNsMessageSelector implements
47: MessageSelectorCommand {
48: private HashMap<XPath, String> map = new HashMap<XPath, String>();
49:
50: public void setParams(List<String> params) throws MactorException {
51: this .map = ParamUtil.parseXPathParams(params);
52: }
53:
54: public boolean isAcceptableMessage(Message message) {
55: try {
56: Document doc = message.getContentDocumentNoNs();
57: if (map.size() == 0) {
58: return true;
59: }
60: for (Entry<XPath, String> e : map.entrySet()) {
61: Node n = e.getKey().selectSingleNode(doc);
62: if (n == null
63: || !ParamUtil
64: .isEqual(n.getText(), e.getValue()))
65: return false;
66: }
67: return true;
68: } catch (MactorException ce) {
69: ce.printStackTrace();
70: return false;
71: }
72: }
73: }
|