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    package org.apache.activemq.filter;
018    
019    import java.io.StringReader;
020    import javax.jms.BytesMessage;
021    import javax.jms.JMSException;
022    import javax.jms.TextMessage;
023    import javax.xml.xpath.XPath;
024    import javax.xml.xpath.XPathConstants;
025    import javax.xml.xpath.XPathExpressionException;
026    import javax.xml.xpath.XPathFactory;
027    
028    import org.apache.activemq.command.Message;
029    import org.apache.activemq.util.ByteArrayInputStream;
030    import org.xml.sax.InputSource;
031    
032    public class JAXPXPathEvaluator implements XPathExpression.XPathEvaluator {
033    
034        private static final XPathFactory FACTORY = XPathFactory.newInstance();
035        private javax.xml.xpath.XPathExpression expression;
036    
037        public JAXPXPathEvaluator(String xpathExpression) {
038            try {
039                XPath xpath = FACTORY.newXPath();
040                expression = xpath.compile(xpathExpression);
041            } catch (XPathExpressionException e) {
042                throw new RuntimeException("Invalid XPath expression: " + xpathExpression);
043            }
044        }
045    
046        public boolean evaluate(Message message) throws JMSException {
047            if (message instanceof TextMessage) {
048                String text = ((TextMessage)message).getText();
049                return evaluate(text);
050            } else if (message instanceof BytesMessage) {
051                BytesMessage bm = (BytesMessage)message;
052                byte data[] = new byte[(int)bm.getBodyLength()];
053                bm.readBytes(data);
054                return evaluate(data);
055            }
056            return false;
057        }
058    
059        private boolean evaluate(byte[] data) {
060            try {
061                InputSource inputSource = new InputSource(new ByteArrayInputStream(data));
062                return ((Boolean)expression.evaluate(inputSource, XPathConstants.BOOLEAN)).booleanValue();
063            } catch (XPathExpressionException e) {
064                return false;
065            }
066        }
067    
068        private boolean evaluate(String text) {
069            try {
070                InputSource inputSource = new InputSource(new StringReader(text));
071                return ((Boolean)expression.evaluate(inputSource, XPathConstants.BOOLEAN)).booleanValue();
072            } catch (XPathExpressionException e) {
073                return false;
074            }
075        }
076    }