001    package org.activemq.filter;
002    
003    import java.io.ByteArrayInputStream;
004    
005    import javax.jms.BytesMessage;
006    import javax.jms.JMSException;
007    import javax.jms.Message;
008    import javax.jms.TextMessage;
009    
010    import org.apache.xmlbeans.XmlObject;
011    
012    public class XMLBeansXPathEvaluator implements XPathExpression.XPathEvaluator {
013    
014        private final String xpath;
015    
016        public XMLBeansXPathEvaluator(String xpath) {
017            this.xpath = xpath;        
018        }
019        
020        public boolean evaluate(Message message) throws JMSException {
021            if( message instanceof TextMessage ) {
022                String text = ((TextMessage)message).getText();
023                try {
024                    XmlObject object = XmlObject.Factory.parse(text);
025                    XmlObject[] objects = object.selectPath(xpath);
026                    return object!=null && objects.length>0;                       
027                } catch (Throwable e) {
028                    return false;
029                }
030                
031            } else if ( message instanceof BytesMessage ) {
032                BytesMessage bm = (BytesMessage) message;
033                byte data[] = new byte[(int) bm.getBodyLength()];
034                bm.readBytes(data);
035                try {
036                    XmlObject object = XmlObject.Factory.parse(new ByteArrayInputStream(data));
037                    XmlObject[] objects = object.selectPath(xpath);
038                    return object!=null && objects.length>0;                       
039                } catch (Throwable e) {
040                    return false;
041                }
042            }            
043            return false;
044        }
045    }