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.util.HashMap;
023 import java.util.List;
024 import java.util.Map.Entry;
025
026 import org.dom4j.Document;
027 import org.dom4j.Node;
028 import org.dom4j.XPath;
029 import org.mactor.brokers.Message;
030 import org.mactor.framework.MactorException;
031 import org.mactor.framework.extensioninterface.MessageSelectorCommand;
032
033 /**
034 * Selects all messages matching the XPath/regexp expressions specified in the
035 * parameters. <br/> The syntax of a parameters are:<br>
036 * [XPath expression that selects the single attribute or element ]==[a regular
037 * expression that the field must match] <br/> I.e. the following setup matches
038 * alle messages containing an Name field with a value starting with 'MrMan'
039 *
040 * <pre>
041 * <message_selector command="java:org.mactor.extensions.xml.XPathIgnoreNsMessageSelector">
042 * <param>//Name==MrMan.*</param>
043 * </message_selector>
044 * </pre>
045 *
046 * Namespace information in the evaluated messages is ignored, so the XPath
047 * expressions must not include namespace prefixes
048 *
049 * @author Lars Ivar Almli
050 */
051 public class XPathIgnoreNsRegExpMessageSelector implements MessageSelectorCommand {
052 private HashMap<XPath, String> map = new HashMap<XPath, String>();
053 public void setParams(List<String> params) throws MactorException {
054 this.map = ParamUtil.parseXPathParams(params);
055 }
056 public boolean isAcceptableMessage(Message message) {
057 try {
058 Document doc = message.getContentDocumentNoNs();
059 if (map.size() == 0) {
060 return true;
061 }
062 for (Entry<XPath, String> e : map.entrySet()) {
063 Node n = e.getKey().selectSingleNode(doc);
064 if (n == null || !ParamUtil.isMatch(n.getText(), e.getValue()))
065 return false;
066 }
067 return true;
068 } catch (MactorException ce) {
069 ce.printStackTrace();
070 return false;
071 }
072 }
073 }