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
018package org.apache.activemq.filter;
019
020import java.io.StringReader;
021
022import javax.jms.BytesMessage;
023import javax.jms.JMSException;
024import javax.jms.TextMessage;
025import javax.xml.parsers.DocumentBuilder;
026import javax.xml.parsers.DocumentBuilderFactory;
027import javax.xml.xpath.XPath;
028import javax.xml.xpath.XPathConstants;
029import javax.xml.xpath.XPathFactory;
030
031import org.w3c.dom.Document;
032import org.w3c.dom.traversal.NodeIterator;
033import org.xml.sax.InputSource;
034
035import org.apache.activemq.command.Message;
036import org.apache.activemq.util.ByteArrayInputStream;
037import org.apache.xpath.CachedXPathAPI;
038import org.apache.xpath.objects.XObject;
039
040public class XalanXPathEvaluator implements XPathExpression.XPathEvaluator {
041
042    private static final XPathFactory FACTORY = XPathFactory.newInstance();
043    private final String xpathExpression;
044    private final DocumentBuilder builder;
045    private final XPath xpath = FACTORY.newXPath();
046
047    public XalanXPathEvaluator(String xpathExpression, DocumentBuilder builder) throws Exception {
048        this.xpathExpression = xpathExpression;
049        if (builder != null) {
050            this.builder = builder;
051        } else {
052            throw new RuntimeException("No document builder available");
053        }
054    }
055
056    public boolean evaluate(Message m) throws JMSException {
057        if (m instanceof TextMessage) {
058            String text = ((TextMessage)m).getText();
059            return evaluate(text);
060        } else if (m instanceof BytesMessage) {
061            BytesMessage bm = (BytesMessage)m;
062            byte data[] = new byte[(int)bm.getBodyLength()];
063            bm.readBytes(data);
064            return evaluate(data);
065        }
066        return false;
067    }
068
069    private boolean evaluate(byte[] data) {
070        try {
071
072            InputSource inputSource = new InputSource(new ByteArrayInputStream(data));
073            Document inputDocument = builder.parse(inputSource);
074            return ((Boolean) xpath.evaluate(xpathExpression, inputDocument, XPathConstants.BOOLEAN)).booleanValue();
075        } catch (Exception e) {
076            return false;
077        }
078    }
079
080    private boolean evaluate(String text) {
081        try {
082            InputSource inputSource = new InputSource(new StringReader(text));
083            Document inputDocument = builder.parse(inputSource);
084            return ((Boolean) xpath.evaluate(xpathExpression, inputDocument, XPathConstants.BOOLEAN)).booleanValue();
085        } catch (Exception e) {
086            return false;
087        }
088    }
089
090    @Override
091    public String toString() {
092        return xpathExpression;
093    }
094}