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 net.sf.jasperreports.annotations.properties.Property;
27 import net.sf.jasperreports.annotations.properties.PropertyScope;
28 import net.sf.jasperreports.engine.JRException;
29 import net.sf.jasperreports.engine.JRPropertiesUtil;
30 import net.sf.jasperreports.engine.JasperReportsContext;
31 import net.sf.jasperreports.engine.util.JRSingletonCache;
32 import net.sf.jasperreports.properties.PropertyConstants;
33
34 /**
35  * Helper class used to instantiate {@link JRXPathExecuter XPath executers}.
36  * <p/>
37  * The {@link JRXPathExecuterFactory XPath executer factory} class name is given by the
38  * {@link #PROPERTY_XPATH_EXECUTER_FACTORY net.sf.jasperreports.xpath.executer.factory} property.
39  * The class should have a public default constructor so that it can be instantiated via reflection.
40  * <p/>
41  * By default, {@link XalanXPathExecuter XPath executers} based on <a href="http://xml.apache.org/xalan-j/" target="_blank">Apache Xalan</a>
42  * are used.
43  * 
44  * @author Lucian Chirita (lucianc@users.sourceforge.net)
45  */

46 public final class JRXPathExecuterUtils
47 {
48
49     public static final String EXCEPTION_MESSAGE_KEY_XPATH_EXECUTER_FACTORY_NOT_FOUND = "util.xml.xpath.executer.factory.property.not.found";
50     /**
51      * Property that holds the {@link JRXPathExecuterFactory XPath executer factory} class name.
52      */

53     @Property(
54             category = PropertyConstants.CATEGORY_DATA_SOURCE,
55             defaultValue = "net.sf.jasperreports.engine.util.xml.XalanXPathExecuterFactory",
56             scopes = {PropertyScope.CONTEXT},
57             sinceVersion = PropertyConstants.VERSION_2_0_0
58             )
59     public static final String PROPERTY_XPATH_EXECUTER_FACTORY = JRPropertiesUtil.PROPERTY_PREFIX + "xpath.executer.factory";
60     
61     private static final JRSingletonCache<JRXPathExecuterFactory> cache = 
62             new JRSingletonCache<JRXPathExecuterFactory>(JRXPathExecuterFactory.class);
63     
64     
65     /**
66      * Return an {@link JRXPathExecuterFactory XPath executer factory} instance.
67      * 
68      * @return a JRXPathExecuterFactory instance
69      * @throws JRException if the {@link #PROPERTY_XPATH_EXECUTER_FACTORY XPath factory property} is not defined
70      * or the factory cannot be instantiated.
71      */

72     public static JRXPathExecuterFactory getXPathExecuterFactory(JasperReportsContext jasperReportsContext) throws JRException
73     {
74         String factoryClassName = JRPropertiesUtil.getInstance(jasperReportsContext).getProperty(PROPERTY_XPATH_EXECUTER_FACTORY);
75         if (factoryClassName == null)
76         {
77             throw 
78                 new JRException(
79                     EXCEPTION_MESSAGE_KEY_XPATH_EXECUTER_FACTORY_NOT_FOUND,
80                     new Object[]{PROPERTY_XPATH_EXECUTER_FACTORY});
81         }
82         
83         return cache.getCachedInstance(factoryClassName);
84     }
85     
86     
87     /**
88      * Produces an {@link JRXPathExecuter XPath executer} instance by means of the factory
89      * returned by {@link #getXPathExecuterFactory(JasperReportsContext) getXPathExecuterFactory(JasperReportsContext)}.
90      * 
91      * @return an JRXPathExecuter instance
92      * @throws JRException if the {@link #PROPERTY_XPATH_EXECUTER_FACTORY XPath factory property} is not defined
93      * or the factory cannot be instantiated.
94      */

95     public static JRXPathExecuter getXPathExecuter(JasperReportsContext jasperReportsContext) throws JRException
96     {
97         JRXPathExecuterFactory executerFactory = getXPathExecuterFactory(jasperReportsContext);
98         return executerFactory.getXPathExecuter();
99     }
100     
101
102     private JRXPathExecuterUtils()
103     {
104     }
105 }
106