1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.velocity

File VelocityMacro.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

12
30
4
1
173
93
12
0.4
7.5
4
3

Classes

Class Line # Actions
VelocityMacro 55 30 0% 12 7
0.8478260684.8%
 

Contributing tests

This file is covered by 20 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro.velocity;
21   
22    import java.io.StringReader;
23    import java.io.StringWriter;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang3.StringUtils;
30    import org.apache.velocity.VelocityContext;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.rendering.macro.MacroExecutionException;
35    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
36    import org.xwiki.rendering.macro.script.AbstractScriptMacro;
37    import org.xwiki.rendering.macro.velocity.VelocityMacroConfiguration;
38    import org.xwiki.rendering.macro.velocity.VelocityMacroParameters;
39    import org.xwiki.rendering.macro.velocity.filter.VelocityMacroFilter;
40    import org.xwiki.rendering.transformation.MacroTransformationContext;
41    import org.xwiki.script.ScriptContextManager;
42    import org.xwiki.velocity.VelocityManager;
43    import org.xwiki.velocity.XWikiVelocityException;
44   
45    /**
46    * Executes <a href="http://velocity.apache.org/">Velocity</a> on the content of this macro and optionally parse the
47    * resulting content with a wiki syntax parser.
48    *
49    * @version $Id: 1872c19427d18d90b2477370b580c39910f5827d $
50    * @since 1.5M2
51    */
52    @Component
53    @Named("velocity")
54    @Singleton
 
55    public class VelocityMacro extends AbstractScriptMacro<VelocityMacroParameters>
56    {
57    /**
58    * The description of the macro.
59    */
60    private static final String DESCRIPTION = "Executes a Velocity script.";
61   
62    /**
63    * The description of the macro content.
64    */
65    private static final String CONTENT_DESCRIPTION = "the velocity script to execute";
66   
67    /**
68    * Used to get the Velocity Engine and Velocity Context to use to evaluate the passed Velocity script.
69    */
70    @Inject
71    private VelocityManager velocityManager;
72   
73    /**
74    * The velocity macro configuration.
75    */
76    @Inject
77    private VelocityMacroConfiguration configuration;
78   
79    @Inject
80    private ScriptContextManager scriptContextManager;
81   
82    /**
83    * The logger to log.
84    */
85    @Inject
86    private Logger logger;
87   
88    /**
89    * Default constructor.
90    */
 
91  48 toggle public VelocityMacro()
92    {
93  48 super("Velocity", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION),
94    VelocityMacroParameters.class);
95    }
96   
 
97  114 toggle @Override
98    public boolean supportsInlineMode()
99    {
100  114 return true;
101    }
102   
 
103  8630 toggle @Override
104    protected String evaluateString(VelocityMacroParameters parameters, String content,
105    MacroTransformationContext context) throws MacroExecutionException
106    {
107  8630 String result = "";
108   
109  8630 try {
110  8630 VelocityContext velocityContext = this.velocityManager.getCurrentVelocityContext();
111   
112  8630 VelocityMacroFilter filter = getFilter(parameters);
113   
114  8630 String cleanedContent = content;
115   
116    // Execute pre filter
117  8630 if (filter != null) {
118  8630 cleanedContent = filter.before(cleanedContent, velocityContext);
119    }
120   
121  8630 StringWriter writer = new StringWriter();
122   
123    // Use the Transformation id as the name passed to the Velocity Engine. This name is used internally
124    // by Velocity as a cache index key for caching macros.
125  8629 String key = context.getTransformationContext().getId();
126  8630 if (key == null) {
127  1 key = "unknown namespace";
128    }
129   
130    // Execute Velocity context
131  8630 this.velocityManager.evaluate(writer, key, new StringReader(cleanedContent));
132  8630 result = writer.toString();
133   
134    // Execute post filter
135  8630 if (filter != null) {
136  8630 result = filter.after(result, velocityContext);
137    }
138    } catch (XWikiVelocityException e) {
139  0 throw new MacroExecutionException("Failed to evaluate Velocity Macro for content [" + content + "]", e);
140    }
141   
142  8629 return result;
143    }
144   
145    /**
146    * @param parameters the velocity macros parameters
147    * @return the velocity content filter
148    * @since 2.0M1
149    */
 
150  8629 toggle private VelocityMacroFilter getFilter(VelocityMacroParameters parameters)
151    {
152  8630 String filterName = parameters.getFilter();
153   
154  8630 if (StringUtils.isEmpty(filterName)) {
155  8598 filterName = this.configuration.getFilter();
156   
157  8599 if (StringUtils.isEmpty(filterName)) {
158  0 filterName = null;
159    }
160    }
161   
162  8630 VelocityMacroFilter filter = null;
163  8630 if (filterName != null) {
164  8630 try {
165  8630 filter = getComponentManager().getInstance(VelocityMacroFilter.class, filterName);
166    } catch (ComponentLookupException e) {
167  0 this.logger.error("Can't find velocity macro filter", e);
168    }
169    }
170   
171  8630 return filter;
172    }
173    }