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

File VelocityRenderer.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
13
1
1
92
39
4
0.31
13
1
4

Classes

Class Line # Actions
VelocityRenderer 42 13 0% 4 0
1.0100%
 

Contributing tests

This file is covered by 3 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.icon.internal;
21   
22    import java.io.StringWriter;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.apache.velocity.VelocityContext;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.icon.IconException;
30    import org.xwiki.velocity.VelocityEngine;
31    import org.xwiki.velocity.VelocityManager;
32    import org.xwiki.velocity.XWikiVelocityException;
33   
34    /**
35    * Internal helper to render safely any velocity code.
36    *
37    * @version $Id: 84d86808e5ec7bbb78c3798212ed6805a2c35627 $
38    * @since 6.4M1
39    */
40    @Component(roles = VelocityRenderer.class)
41    @Singleton
 
42    public class VelocityRenderer
43    {
44    @Inject
45    private VelocityManager velocityManager;
46   
47    /**
48    * Render a velocity code without messing with the document context and namespace.
49    * @param code code to render
50    * @return the rendered code
51    * @throws IconException if problem occurs
52    */
 
53  2675 toggle public String render(String code) throws IconException
54    {
55    // The macro namespace to use by the velocity engine, see afterwards.
56  2675 String namespace = "IconVelocityRenderer_" + Thread.currentThread().getId();
57   
58    // Create the output writer
59  2675 StringWriter output = new StringWriter();
60   
61  2674 VelocityEngine engine = null;
62  2675 try {
63    // Get the velocity engine
64  2674 engine = velocityManager.getVelocityEngine();
65   
66    // Use a new macro namespace to prevent the code redefining existing macro.
67    // We use the thread name to have a unique id.
68  2674 engine.startedUsingMacroNamespace(namespace);
69   
70    // Create a new VelocityContext to prevent the code creating variables in the current context.
71    // See http://jira.xwiki.org/browse/XWIKI-11400.
72    // We set the current context as inner context of the new one to be able to read existing variables.
73    // See http://jira.xwiki.org/browse/XWIKI-11426.
74  2674 VelocityContext context = new VelocityContext(velocityManager.getVelocityContext());
75   
76    // Render the code
77  2674 if (engine.evaluate(context, output, "DefaultIconRenderer", code)) {
78  2673 return output.toString();
79    } else {
80    // I don't know how to check the velocity runtime log
81  1 throw new IconException("Failed to render the icon. See the Velocity runtime log.", null);
82    }
83    } catch (XWikiVelocityException e) {
84  1 throw new IconException("Failed to render the icon.", e);
85    } finally {
86    // Do not forget to close the macro namespace we have created previously
87  2675 if (engine != null) {
88  2674 engine.stoppedUsingMacroNamespace(namespace);
89    }
90    }
91    }
92    }