1 18 19 package org.apache.activemq.filter; 20 21 import javax.jms.BytesMessage ; 22 import javax.jms.JMSException ; 23 import javax.jms.TextMessage ; 24 25 import org.apache.activemq.command.Message; 26 import org.apache.activemq.util.ByteArrayInputStream; 27 import org.apache.xmlbeans.XmlObject; 28 29 public class XMLBeansXPathEvaluator implements XPathExpression.XPathEvaluator { 30 31 private final String xpath; 32 33 public XMLBeansXPathEvaluator(String xpath) { 34 this.xpath = xpath; 35 } 36 37 public boolean evaluate(Message message) throws JMSException { 38 if( message instanceof TextMessage ) { 39 String text = ((TextMessage )message).getText(); 40 try { 41 XmlObject object = XmlObject.Factory.parse(text); 42 XmlObject[] objects = object.selectPath(xpath); 43 return object!=null && objects.length>0; 44 } catch (Throwable e) { 45 return false; 46 } 47 48 } else if ( message instanceof BytesMessage ) { 49 BytesMessage bm = (BytesMessage ) message; 50 byte data[] = new byte[(int) bm.getBodyLength()]; 51 bm.readBytes(data); 52 try { 53 XmlObject object = XmlObject.Factory.parse(new ByteArrayInputStream(data)); 54 XmlObject[] objects = object.selectPath(xpath); 55 return object!=null && objects.length>0; 56 } catch (Throwable e) { 57 return false; 58 } 59 } 60 return false; 61 } 62 } 63 | Popular Tags |