1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.util.xml;
25
26 import javax.xml.transform.TransformerException;
27
28 import org.apache.xpath.CachedXPathAPI;
29 import org.apache.xpath.objects.XObject;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.NodeList;
32
33 import net.sf.jasperreports.engine.JRException;
34
35
36 /**
37  * XPath executer implementation that uses <a href="http://xml.apache.org/xalan-j/" target="_blank">Apache Xalan</a>.
38  * 
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  */

41 public class XalanXPathExecuter implements JRXPathExecuter {
42
43     public static final String EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE = "util.xml.xalan.xpath.selection.failure";
44
45     // XPath API facade
46     private CachedXPathAPI xpathAPI = new CachedXPathAPI();
47
48     /**
49      * Default constructor.
50      */

51     public XalanXPathExecuter() {
52     }
53     
54     @Override
55     public NodeList selectNodeList(Node contextNode, String expression) throws JRException
56     {
57         try {
58             return xpathAPI.selectNodeList(contextNode, expression);
59         } catch (TransformerException e) {
60             throw 
61                 new JRException(
62                     EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
63                     new Object[]{expression},
64                     e);
65         }
66     }
67
68     @Override
69     public Object selectObject(Node contextNode, String expression) throws JRException {
70         try {
71             Object value;
72             XObject object = xpathAPI.eval(contextNode, expression);
73             switch (object.getType()) {
74                 case XObject.CLASS_NODESET:
75                     value = object.nodeset().nextNode();
76                     break;
77                 case XObject.CLASS_BOOLEAN:
78                     value = object.bool();
79                     break;
80                 case XObject.CLASS_NUMBER:
81                     value = object.num();
82                     break;
83                 default:
84                     value = object.str();
85                     break;
86             }
87             return value;
88         } catch (TransformerException e) {
89             throw 
90                 new JRException(
91                     EXCEPTION_MESSAGE_KEY_XPATH_SELECTION_FAILURE,
92                     new Object[]{expression},
93                     e);
94         }
95     }
96     
97 }
98