KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 package org.apache.activemq.filter;
20
21 import java.io.StringReader JavaDoc;
22
23 import javax.jms.BytesMessage JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.TextMessage JavaDoc;
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28
29 import org.apache.activemq.command.Message;
30 import org.apache.activemq.util.ByteArrayInputStream;
31 import org.apache.xpath.CachedXPathAPI;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.traversal.NodeIterator;
34 import org.xml.sax.InputSource JavaDoc;
35
36 public class XalanXPathEvaluator implements XPathExpression.XPathEvaluator {
37     
38     private final String JavaDoc xpath;
39
40     public XalanXPathEvaluator(String JavaDoc xpath) {
41         this.xpath = xpath;
42     }
43     
44     public boolean evaluate(Message JavaDoc m) throws JMSException JavaDoc {
45         if( m instanceof TextMessage JavaDoc ) {
46             String JavaDoc text = ((TextMessage JavaDoc)m).getText();
47             return evaluate(text);
48         } else if ( m instanceof BytesMessage JavaDoc ) {
49             BytesMessage JavaDoc bm = (BytesMessage JavaDoc) m;
50             byte data[] = new byte[(int) bm.getBodyLength()];
51             bm.readBytes(data);
52             return evaluate(data);
53         }
54         return false;
55     }
56
57     private boolean evaluate(byte[] data) {
58         try {
59             
60             InputSource JavaDoc inputSource = new InputSource JavaDoc(new ByteArrayInputStream(data));
61             
62             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
63             factory.setNamespaceAware(true);
64             DocumentBuilder JavaDoc dbuilder = factory.newDocumentBuilder();
65             Document JavaDoc doc = dbuilder.parse(inputSource);
66             
67             CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
68             NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath);
69             return iterator.nextNode()!=null;
70             
71         } catch (Throwable JavaDoc e) {
72             return false;
73         }
74     }
75
76     private boolean evaluate(String JavaDoc text) {
77         try {
78             InputSource JavaDoc inputSource = new InputSource JavaDoc(new StringReader JavaDoc(text));
79             
80             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
81             factory.setNamespaceAware(true);
82             DocumentBuilder JavaDoc dbuilder = factory.newDocumentBuilder();
83             Document JavaDoc doc = dbuilder.parse(inputSource);
84             
85             // We should associated the cachedXPathAPI object with the message being evaluated
86
// since that should speedup subsequent xpath expressions.
87
CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
88             NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath);
89             return iterator.nextNode()!=null;
90         } catch (Throwable JavaDoc e) {
91             return false;
92         }
93     }
94 }
95
Popular Tags