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

File DefaultRenderingContext.java

 

Coverage histogram

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

Code metrics

14
47
19
2
273
166
31
0.66
2.47
9.5
1.63

Classes

Class Line # Actions
DefaultRenderingContext 47 34 0% 26 2
0.9682539796.8%
DefaultRenderingContext.Context 65 13 0% 5 6
0.6470588464.7%
 

Contributing tests

This file is covered by 434 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   
21    package org.xwiki.rendering.internal.transformation;
22   
23    import java.util.ArrayDeque;
24    import java.util.Deque;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.context.Execution;
31    import org.xwiki.context.ExecutionContext;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.XDOM;
34    import org.xwiki.rendering.syntax.Syntax;
35    import org.xwiki.rendering.transformation.Transformation;
36    import org.xwiki.rendering.transformation.TransformationContext;
37    import org.xwiki.rendering.transformation.TransformationException;
38   
39    /**
40    * The complete context of the transformation process.
41    *
42    * @version $Id: 0a437c3c6385bc7d6b27caa81235b43fcc8a9f38 $
43    * @since 6.0
44    */
45    @Component
46    @Singleton
 
47    public class DefaultRenderingContext implements MutableRenderingContext
48    {
49    /**
50    * Key of the this context in the execution context.
51    */
52    private static final String EXECUTION_CONTEXT_KEY = "rendering.context";
53   
54    /**
55    * A null context to avoid special cases.
56    */
57    private static final Context NULL_CONTEXT = new Context();
58   
59    /**
60    * Used to access the rendering context stack from the execution context.
61    */
62    @Inject
63    private Execution execution;
64   
 
65    protected static final class Context implements Cloneable
66    {
67    /**
68    * The complete {@link org.xwiki.rendering.block.XDOM} of the content currently being transformed.
69    */
70    private final XDOM xdom;
71   
72    /**
73    * The block current block processed by the transformation (ie the macro block).
74    */
75    private Block currentBlock;
76   
77    /**
78    * The current syntax of transformation.
79    */
80    private final Syntax syntax;
81   
82    /**
83    * In restricted mode, only transformations that are deemed safe for execution by untrusted users will be
84    * performed.
85    */
86    private final boolean restricted;
87   
88    /**
89    * The current Transformation instance being executed.
90    */
91    private final Transformation transformation;
92   
93    /**
94    * An id representing the transformation being evaluated.
95    */
96    private final String transformationId;
97   
98    /**
99    * The syntax of the renderer.
100    */
101    private Syntax targetSyntax;
102   
103    /**
104    * Create a null context.
105    */
 
106  80 toggle private Context()
107    {
108  80 this(null, null, null, null, false, null);
109    }
110   
111    /**
112    * Initialize a new rendering context.
113    *
114    * @param transformation the transformation being performed.
115    * @param xdom the complete XDOM being processed.
116    * @param syntax the current syntax.
117    * @param transformationId the id of the transformation.
118    * @param restricted true if the transformation is restricted.
119    */
 
120  25191 toggle private Context(Transformation transformation, XDOM xdom, Syntax syntax, String transformationId,
121    boolean restricted, Syntax targetSyntax)
122    {
123  25196 this.transformationId = transformationId;
124  25190 this.xdom = xdom;
125  25188 this.syntax = syntax;
126  25188 this.restricted = restricted;
127  25195 this.transformation = transformation;
128  25193 this.targetSyntax = targetSyntax;
129    }
130   
 
131  24975 toggle public String getTransformationId()
132    {
133  24975 return this.transformationId;
134    }
135   
 
136  0 toggle @Override
137    public Context clone()
138    {
139  0 Context newContext;
140  0 try {
141  0 newContext = (Context) super.clone();
142    } catch (CloneNotSupportedException e) {
143    // Should never happen
144  0 throw new RuntimeException("Failed to clone object", e);
145    }
146   
147  0 return newContext;
148    }
149    }
150   
 
151  16489 toggle @Override
152    public void push(Transformation transformation, TransformationContext context)
153    {
154  16489 push(transformation, context.getXDOM(), context.getSyntax(), context.getId(), context.isRestricted(),
155    context.getTargetSyntax());
156    }
157   
 
158  25183 toggle @Override
159    public void push(Transformation transformation, XDOM xdom, Syntax syntax, String id, boolean restricted,
160    Syntax targetSyntax)
161    {
162  25185 Deque<Context> stack = getContextStack(true);
163  25185 if (stack != null) {
164  25115 stack.push(new Context(transformation, xdom, syntax, id, restricted, targetSyntax));
165    }
166    }
167   
 
168  25190 toggle @Override
169    public void pop()
170    {
171  25190 Deque<Context> stack = getContextStack(false);
172  25190 if (stack != null) {
173  25121 stack.pop();
174    }
175    }
176   
 
177  16490 toggle @Override
178    public void transformInContext(Transformation transformation, TransformationContext context, Block block)
179    throws TransformationException
180    {
181  16488 try {
182  16489 push(transformation, context);
183  16490 transformation.transform(block, context);
184    } finally {
185  16487 pop();
186    }
187    }
188   
 
189  275421 toggle @SuppressWarnings("unchecked")
190    private Deque<Context> getContextStack(boolean create)
191    {
192  275431 ExecutionContext context = this.execution.getContext();
193   
194  275444 if (context != null) {
195  275098 Deque<Context> stack = (Deque<Context>) context.getProperty(EXECUTION_CONTEXT_KEY);
196   
197  275115 if (stack == null && create) {
198  9382 stack = new ArrayDeque<>();
199  9381 context.setProperty(EXECUTION_CONTEXT_KEY, stack);
200    }
201   
202  275109 return stack;
203    }
204   
205  343 return null;
206    }
207   
 
208  225046 toggle protected Context peek()
209    {
210  225050 Deque<Context> stack = getContextStack(false);
211  225071 return (stack != null && !stack.isEmpty()) ? stack.peek() : NULL_CONTEXT;
212    }
213   
 
214  8687 toggle @Override
215    public XDOM getXDOM()
216    {
217  8692 return peek().xdom;
218    }
219   
 
220  2105 toggle @Override
221    public Block getCurrentBlock()
222    {
223  2105 return peek().currentBlock;
224    }
225   
 
226  34874 toggle @Override
227    public void setCurrentBlock(Block block)
228    {
229  34874 Context context = peek();
230  34875 if (context != null && context != NULL_CONTEXT) {
231  34702 context.currentBlock = block;
232    }
233    }
234   
 
235  9527 toggle @Override
236    public Syntax getDefaultSyntax()
237    {
238  9534 return peek().syntax;
239    }
240   
 
241  27679 toggle @Override
242    public boolean isRestricted()
243    {
244  27682 return peek().restricted;
245    }
246   
 
247  8692 toggle @Override
248    public Transformation getTransformation()
249    {
250  8692 return peek().transformation;
251    }
252   
 
253  81661 toggle @Override
254    public String getTransformationId()
255    {
256  81646 return peek().transformationId;
257    }
258   
 
259  26754 toggle @Override
260    public Syntax getTargetSyntax()
261    {
262  26755 return peek().targetSyntax;
263    }
264   
 
265  82 toggle @Override
266    public void setTargetSyntax(Syntax targetSyntax)
267    {
268  82 Context context = peek();
269  82 if (context != null && context != NULL_CONTEXT) {
270  0 context.targetSyntax = targetSyntax;
271    }
272    }
273    }