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.scriptlets;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import net.sf.jasperreports.engine.JRAbstractScriptlet;
31 import net.sf.jasperreports.engine.JRException;
32 import net.sf.jasperreports.engine.JRParameter;
33 import net.sf.jasperreports.engine.JRScriptlet;
34 import net.sf.jasperreports.engine.util.JRClassLoader;
35
36 /**
37  * @author Teodor Danciu (teodord@users.sourceforge.net)
38  */

39 public final class DefaultScriptletFactory implements ScriptletFactory
40 {
41
42     private static final DefaultScriptletFactory INSTANCE = new DefaultScriptletFactory();
43     public static final String EXCEPTION_MESSAGE_KEY_CLASS_LOADING_ERROR = "scriptlets.class.loading.error";
44     public static final String EXCEPTION_MESSAGE_KEY_INSTANCE_LOADING_ERROR = "scriptlets.instance.loading.error";
45     
46     private DefaultScriptletFactory()
47     {
48     }
49     
50     /**
51      * 
52      */

53     public static DefaultScriptletFactory getInstance()
54     {
55         return INSTANCE;
56     }
57
58     @Override
59     public List<JRAbstractScriptlet> getScriplets(ScriptletFactoryContext context) throws JRException
60     {
61         List<JRAbstractScriptlet> scriptlets = new ArrayList<JRAbstractScriptlet>();
62
63         JRAbstractScriptlet scriptlet = (JRAbstractScriptlet)context.getParameterValues().get(JRParameter.REPORT_SCRIPTLET);
64         if (scriptlet == null)
65         {
66             String scriptletClassName = context.getDataset().getScriptletClass();
67             if (scriptletClassName != null)
68             {
69                 scriptlet = getScriptlet(scriptletClassName);
70                 context.getParameterValues().put(JRParameter.REPORT_SCRIPTLET, scriptlet);
71             }
72         }
73
74         if (scriptlet != null)
75         {
76             scriptlets.add(scriptlet);
77         }
78         
79         JRScriptlet[] scriptletsArray = context.getDataset().getScriptlets();
80         if (scriptletsArray != null)
81         {
82             for (int i = 0; i < scriptletsArray.length; i++)
83             {
84                 String paramName = scriptletsArray[i].getName() 
85                         + JRScriptlet.SCRIPTLET_PARAMETER_NAME_SUFFIX;
86                 scriptlet = (JRAbstractScriptlet)context.getParameterValues().get(paramName);
87                 if (scriptlet == null)
88                 {
89                     scriptlet = getScriptlet(scriptletsArray[i].getValueClassName());
90                     context.getParameterValues().put(paramName, scriptlet);
91                 }
92                 
93                 scriptlets.add(scriptlet);
94             }
95         }
96         
97         return scriptlets;
98     }
99     
100     /**
101      *
102      */

103     protected JRAbstractScriptlet getScriptlet(String scriptletClassName) throws JRException
104     {
105         JRAbstractScriptlet scriptlet = null;
106
107         try
108         {
109             Class<?> scriptletClass = JRClassLoader.loadClassForName(scriptletClassName);    
110             scriptlet = (JRAbstractScriptlet) scriptletClass.getDeclaredConstructor().newInstance();
111         }
112         catch (ClassNotFoundException e)
113         {
114             throw 
115                 new JRException(
116                     EXCEPTION_MESSAGE_KEY_CLASS_LOADING_ERROR,
117                     new Object[]{scriptletClassName}, 
118                     e);
119         }
120         catch (NoSuchMethodException | InvocationTargetException 
121             | IllegalAccessException | InstantiationException e)
122         {
123             throw 
124                 new JRException(
125                     EXCEPTION_MESSAGE_KEY_INSTANCE_LOADING_ERROR,
126                     new Object[]{scriptletClassName}, 
127                     e);
128         }
129         
130         return scriptlet;
131     }
132     
133 }
134