001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.activemq.filter; 019 020 import javax.jms.BytesMessage; 021 import javax.jms.JMSException; 022 import javax.jms.TextMessage; 023 024 import org.apache.activemq.command.Message; 025 import org.apache.activemq.util.ByteArrayInputStream; 026 import org.apache.xmlbeans.XmlObject; 027 028 public class XMLBeansXPathEvaluator implements XPathExpression.XPathEvaluator { 029 030 private final String xpath; 031 032 public XMLBeansXPathEvaluator(String xpath) { 033 this.xpath = xpath; 034 } 035 036 public boolean evaluate(Message message) throws JMSException { 037 if (message instanceof TextMessage) { 038 String text = ((TextMessage)message).getText(); 039 try { 040 XmlObject object = XmlObject.Factory.parse(text); 041 XmlObject[] objects = object.selectPath(xpath); 042 return object != null && objects.length > 0; 043 } catch (Throwable e) { 044 return false; 045 } 046 047 } else if (message instanceof BytesMessage) { 048 BytesMessage bm = (BytesMessage)message; 049 byte data[] = new byte[(int)bm.getBodyLength()]; 050 bm.readBytes(data); 051 try { 052 XmlObject object = XmlObject.Factory.parse(new ByteArrayInputStream(data)); 053 XmlObject[] objects = object.selectPath(xpath); 054 return object != null && objects.length > 0; 055 } catch (Throwable e) { 056 return false; 057 } 058 } 059 return false; 060 } 061 }