1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.render

File ScriptVelocityContext.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% 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  67576 toggle public ScriptVelocityContext(VelocityContext parent, Set<String> reservedBindings)
48    {
49  67314 super(parent);
50   
51  67647 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  1828210 toggle public void setScriptContext(ScriptContext scriptContext)
66    {
67  1828329 this.scriptContext = scriptContext;
68   
69  1828384 copyScriptContext(ScriptContext.GLOBAL_SCOPE);
70  1828323 copyScriptContext(ScriptContext.ENGINE_SCOPE);
71    }
72   
 
73  3656905 toggle private void copyScriptContext(int scope)
74    {
75  3656916 Bindings bindings = this.scriptContext.getBindings(scope);
76  3656618 if (bindings != null) {
77  1828412 for (Map.Entry<String, Object> entry : bindings.entrySet()) {
78  231707926 if (!this.reservedBindings.contains(entry.getKey())) {
79  231708980 Object currentValue = get(entry.getKey());
80    // Don't replace internal Velocity bindings
81  231710506 if (!(currentValue instanceof Scope)) {
82  231710875 put(entry.getKey(), entry.getValue());
83    }
84    }
85    }
86    }
87    }
88   
89    // VelocityContext
90   
 
91  282910710 toggle @Override
92    public Object internalPut(String key, Object value)
93    {
94  282912985 try {
95  282915076 return super.internalPut(key, value);
96    } finally {
97  282904058 if (!this.reservedBindings.contains(key)) {
98  269028589 this.scriptContext.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
99    }
100    }
101    }
102   
 
103  5444112 toggle @Override
104    public Object internalRemove(Object key)
105    {
106  5444267 try {
107  5444351 return super.internalRemove(key);
108    } finally {
109  5443617 if (key instanceof String && !this.reservedBindings.contains(key)) {
110  4548041 this.scriptContext.removeAttribute((String) key, ScriptContext.ENGINE_SCOPE);
111    }
112    }
113    }
114    }