KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > filter > JAXPXPathEvaluator


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.filter;
19
20 import java.io.StringReader JavaDoc;
21
22 import javax.jms.BytesMessage JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.TextMessage JavaDoc;
25 import javax.xml.xpath.XPath JavaDoc;
26 import javax.xml.xpath.XPathConstants JavaDoc;
27 import javax.xml.xpath.XPathExpressionException JavaDoc;
28 import javax.xml.xpath.XPathFactory JavaDoc;
29
30 import org.apache.activemq.command.Message;
31 import org.apache.activemq.util.ByteArrayInputStream;
32 import org.xml.sax.InputSource JavaDoc;
33
34 public class JAXPXPathEvaluator implements XPathExpression.XPathEvaluator {
35     
36     private static final XPathFactory JavaDoc factory = XPathFactory.newInstance();
37     private javax.xml.xpath.XPathExpression JavaDoc expression;
38     
39     public JAXPXPathEvaluator(String JavaDoc xpathExpression) {
40         try {
41             XPath JavaDoc xpath = factory.newXPath();
42             expression = xpath.compile(xpathExpression);
43         } catch (XPathExpressionException JavaDoc e) {
44             throw new RuntimeException JavaDoc("Invalid XPath expression: "+xpathExpression);
45         }
46     }
47     
48     public boolean evaluate(Message JavaDoc message) throws JMSException JavaDoc {
49         if( message instanceof TextMessage JavaDoc ) {
50             String JavaDoc text = ((TextMessage JavaDoc)message).getText();
51             return evaluate(text);
52         } else if ( message instanceof BytesMessage JavaDoc ) {
53             BytesMessage JavaDoc bm = (BytesMessage JavaDoc) message;
54             byte data[] = new byte[(int) bm.getBodyLength()];
55             bm.readBytes(data);
56             return evaluate(data);
57         }
58         return false;
59     }
60
61     private boolean evaluate(byte[] data) {
62         try {
63             InputSource JavaDoc inputSource = new InputSource JavaDoc(new ByteArrayInputStream(data));
64             return ((Boolean JavaDoc)expression.evaluate(inputSource, XPathConstants.BOOLEAN)).booleanValue();
65         } catch (XPathExpressionException JavaDoc e) {
66             return false;
67         }
68     }
69
70     private boolean evaluate(String JavaDoc text) {
71         try {
72             InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(text));
73             return ((Boolean JavaDoc)expression.evaluate(inputSource, XPathConstants.BOOLEAN)).booleanValue();
74         } catch (XPathExpressionException JavaDoc e) {
75             return false;
76         }
77     }
78 }
79
Popular Tags