Project

Profile

Help

Bug #4216 » XPathTest.java

Stefan Fritz, 2019-05-09 18:54

 
1
import java.io.StringReader;
2

    
3
import javax.xml.transform.sax.SAXSource;
4
import javax.xml.xpath.XPath;
5
import javax.xml.xpath.XPathConstants;
6
import javax.xml.xpath.XPathExpression;
7
import javax.xml.xpath.XPathFactory;
8

    
9
import org.xml.sax.InputSource;
10

    
11
import net.sf.saxon.om.NodeInfo;
12
import net.sf.saxon.xpath.XPathEvaluator;
13

    
14
public class XPathTest {
15

    
16
        public static void main(String[] args) throws Exception {
17

    
18
                final String xml = "<?xml version=\"1.0\"?> <message>    <headers>       <header name=\"CREATED_BY\"></header>       <header name=\"xx.isMultipart\">true</header>       <header name=\"JMSMessageID\">ID:ffffffffcbed081a:a5ab5a1:16A9D21B12A</header>       <header name=\"JMSExpiration\">0</header>       <header name=\"JMS_acme_ExtendedType\">x-acmeXX-multipart</header>       <header name=\"JMSRedelivered\">false</header>       <header name=\"JMSTimestamp\">1557414392106</header>       <header name=\"JMSDeliveryMode\">2</header>       <header name=\"JMSPriority\">0</header>       <header name=\"JMSDestination\">t.esb.SRIUnsolicitedInboundService1</header>       <header name=\"InboundEvent\">acme.apps.po.publish</header>       <header name=\"CHANNEL\">AUTOMATIC</header>       <header name=\"COUNTRY_CODE\">XX</header>    </headers>    <parts>       <part type=\"text/xml\" id=\"name\"> <WfEvent>    <Name>acme.apps.po.publish</Name>    <Key>acme.apps.po.publish305066</Key>    <Priority>       <test>123</test>       <test>234</test>    </Priority>    <SendDate>2019/05/01 12:21:40</SendDate>    <ReceiveDate>null</ReceiveDate>    <CorrelationId>null</CorrelationId>    <Parameters>              <JMSXDeliveryCount>1</JMSXDeliveryCount>              <JMS_OracleConnectionID>null</JMS_OracleConnectionID>              <JMSXGroupSeq>null</JMSXGroupSeq>              <JMSXAppID>null</JMSXAppID>              <JMSXRecvTimestamp>1556713300401</JMSXRecvTimestamp>              <JMS_OracleExcpQ>null</JMS_OracleExcpQ>              <JMS_OracleHeaderOnly>null</JMS_OracleHeaderOnly>              <JMS_OracleOriginalMessageID>null</JMS_OracleOriginalMessageID>              <JMSXGroupID>null</JMSXGroupID>              <JMSXUserID>null</JMSXUserID>              <JMS_OracleDeliveryMode>null</JMS_OracleDeliveryMode>              <JMS_OracleDelay>0</JMS_OracleDelay>              <JMSXState>0</JMSXState>    </Parameters>    <Data>       <EVENT_DATA>          <ESB_PO_PUB>             <PO_NUMBER>7340017377</PO_NUMBER>             <VENDOR_NUM>205415</VENDOR_NUM>             <VENDOR_SITE_CODE>ISP-FAI-734-NZD</VENDOR_SITE_CODE>             <ES_REQUISITION_LINE_ID>38946630</ES_REQUISITION_LINE_ID>             <QUANTITY>1</QUANTITY>             <UNIT_PRICE>36</UNIT_PRICE>             <CURRENCY_CODE>NZD</CURRENCY_CODE>             <RATE_TYPE/>             <RATE_DATE/>             <RATE/>             <NEED_BY_DATE>01-MAY-19</NEED_BY_DATE>             <ITEM_DESCRIPTION>4970438311 RTU-MEI CASH ACCEPTOR SC66 / SCL6607R</ITEM_DESCRIPTION>             <UNIT_MEAS_LOOKUP_CODE>Each</UNIT_MEAS_LOOKUP_CODE>             <SHIPMENT_NUM>1</SHIPMENT_NUM>             <DISTRIBUTION_NUM>1</DISTRIBUTION_NUM>             <PO_LINE_NUM>1</PO_LINE_NUM>             <CHARGE_ACCOUNT>734-715000000-1525-8871-000-000000-00</CHARGE_ACCOUNT>             <ES_REQUISITION_LINE_NUMBER>1</ES_REQUISITION_LINE_NUMBER>             <ES_REQUISITION_NUMBER>132201</ES_REQUISITION_NUMBER>             <CATEGORY>00000000</CATEGORY>             <ES_INSTANCE>WORLD</ES_INSTANCE>          </ESB_PO_PUB>       </EVENT_DATA>    </Data>    <FromAgent>              <Name>acme_PO_QAGENT</Name>              <System>ER64R06.WORLD</System>    </FromAgent> </WfEvent> </part>    </parts> </message> ";
19
                final String expression = "/message/headers/header [@name=\"COUNTRY_CODE\"]=\"GB\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"IE\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"IN\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"BE\" or /message/headers/header [@name=\"COUNTRY_CODE\"]=\"JP\"";
20

    
21
                // XPathFactory xpathFactory =
22
                // XPathFactory.newInstance(XPathConstants.DOM_OBJECT_MODEL);
23
                XPathFactory xpathFactory = new net.sf.saxon.xpath.XPathFactoryImpl();
24
                XPath xpath = xpathFactory.newXPath();
25
                // xpath.setNamespaceContext(nc);
26
                final XPathEvaluator xpathEvaluator_ = (XPathEvaluator) xpath;
27

    
28
                for (int i = 0; i < 20; i++) {
29

    
30
                        new Thread() {
31
                                public void run() {
32

    
33
                                        try {
34
                                                for (int c = 0; c < 10000; c++) {
35
                                                        InputSource source = new InputSource(new StringReader(xml));
36
                                                        XPathExpression xpathExpression = xpathEvaluator_.compile(expression);
37
                                                        NodeInfo doc = xpathEvaluator_.getConfiguration().buildDocumentTree(new SAXSource(source))
38
                                                                        .getRootNode();
39
                                                        Boolean result = (Boolean) xpathExpression.evaluate(doc, XPathConstants.BOOLEAN);
40

    
41
                                                        // This seems to reduce/solve it but not sure why
42
                                                        // Runtime.getRuntime().gc();
43
                                                        // ran it with -Xmx1g -XX:+UseG1GC
44
                                                        
45
                                                        System.out.println(Runtime.getRuntime().totalMemory()/1024/1000);
46
                                                        
47
                                                }
48
                                        } catch (Exception e) {
49
                                                e.printStackTrace();
50
                                        }
51

    
52
                                };
53
                        }.run();
54
                }
55

    
56
        }
57
}
(2-2/3)