1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.render

File ScriptVelocityContext.java

 

Coverage histogram

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

Code metrics

10
21
6
1
114
63
12
0.57
3.5
6
2

Classes

Class Line # Actions
ScriptVelocityContext 37 21 0% 12 4
0.891891989.2%
 

Contributing tests

This file is covered by 15 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 com.xpn.xwiki.render;
21   
22    import java.util.Map;
23    import java.util.Set;
24   
25    import javax.script.Bindings;
26    import javax.script.ScriptContext;
27   
28    import org.apache.velocity.VelocityContext;
29    import org.apache.velocity.runtime.directive.Scope;
30   
31    /**
32    * Maintains the current ScriptContext in sync with any modification of the VelocityContext.
33    *
34    * @version $Id: f6a0a9a7da8b8db1c3290dbd7c4e16d1316b0523 $
35    * @since 8.3M1
36    */
 
37    public class ScriptVelocityContext extends VelocityContext
38    {
39    private final Set<String> reservedBindings;
40   
41    private ScriptContext scriptContext;
42   
43    /**
44    * @param parent the initial Velocity context
45    * @param reservedBindings the binding that should not be synchronized
46    */
 
47  11403 toggle public ScriptVelocityContext(VelocityContext parent, Set<String> reservedBindings)
48    {
49  11416 super(parent);
50   
51  11396 this.reservedBindings = reservedBindings;
52    }
53   
54    /**
55    * @return the current script context
56    */
 
57  0 toggle public ScriptContext getScriptContext()
58    {
59  0 return this.scriptContext;
60    }
61   
62    /**
63    * @param scriptContext the current script context
64    */
 
65  109360 toggle public void setScriptContext(ScriptContext scriptContext)
66    {
67  109349 this.scriptContext = scriptContext;
68   
69  109342 copyScriptContext(ScriptContext.GLOBAL_SCOPE);
70  109341 copyScriptContext(ScriptContext.ENGINE_SCOPE);
71    }
72   
 
73  218646 toggle private void copyScriptContext(int scope)
74    {
75  218674 Bindings bindings = this.scriptContext.getBindings(scope);
76  218688 if (bindings != null) {
77  109360 for (Map.Entry<String, Object> entry : bindings.entrySet()) {
78  7272728 if (!this.reservedBindings.contains(entry.getKey())) {
79  7272729 Object currentValue = get(entry.getKey());
80    // Don't replace internal Velocity bindings
81  7272474 if (!(currentValue instanceof Scope)) {
82  7272532 put(entry.getKey(), entry.getValue());
83    }
84    }
85    }
86    }
87    }
88   
89    // VelocityContext
90   
 
91  9783625 toggle @Override
92    public Object internalPut(String key, Object value)
93    {
94  9783655 try {
95  9783669 return super.internalPut(key, value);
96    } finally {
97  9785981 if (!this.reservedBindings.contains(key)) {
98  8969615 this.scriptContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
99    }
100    }
101    }
102   
 
103  247986 toggle @Override
104    public Object internalRemove(Object key)
105    {
106  247989 try {
107  247983 return super.internalRemove(key);
108    } finally {
109  247987 if (key instanceof String && !this.reservedBindings.contains(key)) {
110  191556 this.scriptContext.removeAttribute((String) key, ScriptContext.ENGINE_SCOPE);
111    }
112    }
113    }
114    }