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/regexp 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 ]==[a regular expression that the field must match]
36: * <br/>
37: * I.e. the following setup matches alle messages containing an Name field with a value starting with 'MrMan'
38: * <pre>
39: * <message_selector command="java:org.mactor.extensions.xml.XPathIgnoreNsMessageSelector">
40: * <param>//Name==MrMan.*</param>
41: * </message_selector>
42: * </pre>
43: *
44: * Namespace information in the evaluated messages is ignored, so the XPath expressions must not include namespace prefixes
45: *
46: * @author Lars Ivar Almli
47: */
48:
49: public class XPathIgnoreNsRegExpMessageSelector implements
50: MessageSelectorCommand {
51: private HashMap<XPath, String> map = new HashMap<XPath, String>();
52:
53: public void setParams(List<String> params) throws MactorException {
54: this .map = ParamUtil.parseXPathParams(params);
55: }
56:
57: public boolean isAcceptableMessage(Message message) {
58: try {
59: Document doc = message.getContentDocumentNoNs();
60: if (map.size() == 0) {
61: return true;
62: }
63: for (Entry<XPath, String> e : map.entrySet()) {
64: Node n = e.getKey().selectSingleNode(doc);
65: if (n == null
66: || !ParamUtil
67: .isMatch(n.getText(), e.getValue()))
68: return false;
69: }
70: return true;
71: } catch (MactorException ce) {
72: ce.printStackTrace();
73: return false;
74: }
75: }
76: }
|