Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/filter/XalanXPathEvaluator.java


1   package org.activemq.filter;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.StringReader;
5   
6   import javax.jms.BytesMessage;
7   import javax.jms.JMSException;
8   import javax.jms.Message;
9   import javax.jms.TextMessage;
10  import javax.xml.parsers.DocumentBuilder;
11  import javax.xml.parsers.DocumentBuilderFactory;
12  
13  import org.apache.xpath.CachedXPathAPI;
14  import org.w3c.dom.Document;
15  import org.w3c.dom.traversal.NodeIterator;
16  import org.xml.sax.InputSource;
17  
18  public class XalanXPathEvaluator implements XPathExpression.XPathEvaluator {
19      
20      private final String xpath;
21  
22      public XalanXPathEvaluator(String xpath) {
23          this.xpath = xpath;
24      }
25      
26      public boolean evaluate(Message message) throws JMSException {
27          if( message instanceof TextMessage ) {
28              String text = ((TextMessage)message).getText();
29              return evaluate(text);                
30          } else if ( message instanceof BytesMessage ) {
31              BytesMessage bm = (BytesMessage) message;
32              byte data[] = new byte[(int) bm.getBodyLength()];
33              bm.readBytes(data);
34              return evaluate(data);
35          }            
36          return false;
37      }
38  
39      private boolean evaluate(byte[] data) {
40          try {
41              
42              InputSource inputSource = new InputSource(new ByteArrayInputStream(data));
43              
44              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45              factory.setNamespaceAware(true);
46              DocumentBuilder dbuilder = factory.newDocumentBuilder();
47              Document doc = dbuilder.parse(inputSource);
48              
49              CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
50              NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath);
51              return iterator.nextNode()!=null;
52              
53          } catch (Throwable e) {
54              return false;
55          }
56      }
57  
58      private boolean evaluate(String text) {
59          try {
60              InputSource inputSource = new InputSource(new StringReader(text));
61              
62              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
63              factory.setNamespaceAware(true);
64              DocumentBuilder dbuilder = factory.newDocumentBuilder();
65              Document doc = dbuilder.parse(inputSource);
66              
67              // We should associated the cachedXPathAPI object with the message being evaluated
68              // since that should speedup subsequent xpath expressions.
69              CachedXPathAPI cachedXPathAPI = new CachedXPathAPI();
70              NodeIterator iterator = cachedXPathAPI.selectNodeIterator(doc,xpath);
71              return iterator.nextNode()!=null;
72          } catch (Throwable e) {
73              return false;
74          }
75      }
76  }