• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.script;
2 
3 /*
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are
8  * permitted provided that the following conditions are met: Redistributions of source code
9  * must retain the above copyright notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution. Neither the name of the Sun Microsystems nor the names of
13  * is contributors may be used to endorse or promote products derived from this software
14  * without specific prior written permission.
15 
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
17  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
22  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /*
28  * Factory class for the Velocity scripting interface. Please refer to the
29  * javax.script.ScriptEngineFactory documentation for details.
30  *
31  * @author A. Sundararajan
32  * @author <a href="mailto:claude.brisson@gmail.com">Claude Brisson</a>
33  * @version $Id: VelocityScriptEngineFactory.java$
34  */
35 
36 import javax.script.ScriptEngine;
37 import javax.script.ScriptEngineFactory;
38 import java.util.ArrayList;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.Properties;
42 
43 import org.apache.velocity.runtime.VelocityEngineVersion;
44 
45 public class VelocityScriptEngineFactory implements ScriptEngineFactory
46 {
47 
48     private static final String VELOCITY_NAME = "Velocity";
49     private static final String VELOCITY_VERSION = VelocityEngineVersion.VERSION;
50     private static final String VELOCITY_LANGUAGE = "VTL";
51 
52     private static List<String> names;
53     private static List<String> extensions;
54     private static List<String> mimeTypes;
55 
56     private static Properties parameters;
57 
58     static
59     {
60         names = new ArrayList<>();
61         names.add("velocity");
62         names.add("Velocity");
63         names = Collections.unmodifiableList(names);
64         extensions = new ArrayList<>();
65         extensions.add("vm");
66         extensions.add("vtl");
67         extensions.add("vhtml");
68         extensions = Collections.unmodifiableList(extensions);
69         mimeTypes = new ArrayList<>();
70         mimeTypes.add("text/x-velocity");
71         mimeTypes = Collections.unmodifiableList(mimeTypes);
72         parameters = new Properties();
parameters.put(ScriptEngine.NAME, VELOCITY_NAME)73         parameters.put(ScriptEngine.NAME, VELOCITY_NAME);
parameters.put(ScriptEngine.ENGINE_VERSION, VELOCITY_VERSION)74         parameters.put(ScriptEngine.ENGINE_VERSION, VELOCITY_VERSION);
parameters.put(ScriptEngine.ENGINE, VELOCITY_NAME)75         parameters.put(ScriptEngine.ENGINE, VELOCITY_NAME);
parameters.put(ScriptEngine.LANGUAGE, VELOCITY_LANGUAGE)76         parameters.put(ScriptEngine.LANGUAGE, VELOCITY_LANGUAGE);
parameters.put(ScriptEngine.LANGUAGE_VERSION, VELOCITY_VERSION)77         parameters.put(ScriptEngine.LANGUAGE_VERSION, VELOCITY_VERSION);
78         parameters.put("THREADING", "MULTITHREADED");
79     }
80 
81     /**
82      * get engine name
83      * @return engine name, aka "Velocity"
84      */
85     @Override
getEngineName()86     public String getEngineName()
87     {
88         return VELOCITY_NAME;
89     }
90 
91     /**
92      * get engine version
93      * @return engine version string
94      */
95     @Override
getEngineVersion()96     public String getEngineVersion()
97     {
98         return VELOCITY_VERSION;
99     }
100 
101     /**
102      * get the list of file extensions handled by Velocity: vm, vtl, vhtml
103      * @return extensions list
104      */
105     @Override
getExtensions()106     public List<String> getExtensions()
107     {
108         return extensions;
109     }
110 
111     /**
112      * get language name
113      * @return language name, aka "VTL"
114      */
115     @Override
getLanguageName()116     public String getLanguageName()
117     {
118         return VELOCITY_NAME;
119     }
120 
121     /**
122      * get language version (same as engine version)
123      * @return language version string
124      */
125     @Override
getLanguageVersion()126     public String getLanguageVersion()
127     {
128         return VELOCITY_VERSION;
129     }
130 
131     /**
132      * get Velocity syntax for calling method 'm' on object 'obj' with provided arguments
133      * @param obj
134      * @param m
135      * @param args
136      * @return VTL call ${obj.m(args...)}
137      */
138     @Override
getMethodCallSyntax(String obj, String m, String... args)139     public String getMethodCallSyntax(String obj, String m, String... args)
140     {
141         StringBuilder buf = new StringBuilder();
142         buf.append("${");
143         buf.append(obj);
144         buf.append(".");
145         buf.append(m);
146         buf.append("(");
147         if (args.length != 0)
148         {
149             int i = 0;
150             for (; i < args.length - 1; i++)
151             {
152                 buf.append("$").append(args[i]);
153                 buf.append(", ");
154             }
155             buf.append("$").append(args[i]);
156         }
157         buf.append(")}");
158         return buf.toString();
159     }
160 
161     /**
162      * get the list of Velocity mime types
163      * @return singleton { 'text/x-velocity' }
164      */
165     @Override
getMimeTypes()166     public List<String> getMimeTypes()
167     {
168         return mimeTypes;
169     }
170 
171     /**
172      * get the list of names
173      * @return { 'velocity', 'Velocity' }
174      */
175     @Override
getNames()176     public List<String> getNames()
177     {
178         return names;
179     }
180 
181     /**
182      * get VTL expression used to display specified string
183      * @param toDisplay
184      * @return escaped string #[[toDisplay]]#
185      */
186     @Override
getOutputStatement(String toDisplay)187     public String getOutputStatement(String toDisplay)
188     {
189         StringBuilder buf = new StringBuilder();
190         buf.append("#[[").append(toDisplay).append("]]#");
191         return buf.toString();
192     }
193 
194     /**
195      * get engine parameter for provided key
196      * @param key
197      * @return found parameter, or null
198      */
199     @Override
getParameter(String key)200     public String getParameter(String key)
201     {
202         return parameters.getProperty(key);
203     }
204 
205     /**
206      * get whole VTL program given VTL lines
207      * @param statements VTL lines
208      * @return lines concatenated with carriage returns
209      */
210     @Override
getProgram(String... statements)211     public String getProgram(String... statements)
212     {
213         StringBuilder buf = new StringBuilder();
214         for (String statement : statements)
215         {
216             buf.append(statement);
217             buf.append(System.lineSeparator());
218         }
219         return buf.toString();
220     }
221 
222     /**
223      * get a Velocity script engine
224      * @return a new Velocity script engine
225      */
226     @Override
getScriptEngine()227     public ScriptEngine getScriptEngine()
228     {
229         return new VelocityScriptEngine(this);
230     }
231 }
232