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

File XWikiRenderingContext.java

 

Coverage histogram

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

Code metrics

12
27
6
1
136
88
15
0.56
4.5
6
2.5

Classes

Class Line # Actions
XWikiRenderingContext 44 27 0% 15 1
0.977777897.8%
 

Contributing tests

This file is covered by 10 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.transformation;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.slf4j.Logger;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.component.manager.ComponentManager;
30    import org.xwiki.rendering.block.XDOM;
31    import org.xwiki.rendering.syntax.Syntax;
32    import org.xwiki.rendering.transformation.Transformation;
33    import org.xwiki.skin.SkinManager;
34    import org.xwiki.velocity.VelocityManager;
35   
36    /**
37    * Add some XWiki level stuff to {@link DefaultRenderingContext}.
38    *
39    * @version $Id: f9d621d87cd5cc18424c943889f5b4ebc07b8ae7 $
40    * @since 6.0
41    */
42    @Component
43    @Singleton
 
44    public class XWikiRenderingContext extends DefaultRenderingContext
45    {
46    @Inject
47    private ComponentManager componentManager;
48   
49    @Inject
50    private Provider<SkinManager> skinManagerProvider;
51   
52    @Inject
53    private Logger logger;
54   
55    private VelocityManager velocityManager;
56   
 
57  98885 toggle private VelocityManager getVelocityManager()
58    {
59  98890 if (this.velocityManager == null) {
60  94 try {
61  94 this.velocityManager = this.componentManager.getInstance(VelocityManager.class);
62    } catch (ComponentLookupException e) {
63  27 this.logger.debug("Failed to initialize VelocityManager, velocity cache won't be cleaned", e);
64    }
65    }
66   
67  98881 return this.velocityManager;
68    }
69   
 
70  24976 toggle @Override
71    public void push(Transformation transformation, XDOM xdom, Syntax syntax, String id, boolean restricted,
72    Syntax targetSyntax)
73    {
74  24976 super.push(transformation, xdom, syntax, id, restricted, targetSyntax);
75   
76  24969 String namespace = id;
77  24967 if (namespace != null) {
78  24733 openNamespace(namespace);
79    }
80    }
81   
 
82  24977 toggle @Override
83    public void pop()
84    {
85  24976 String namespace = peek().getTransformationId();
86  24974 if (namespace != null) {
87  24727 closeNamespace(namespace);
88    }
89   
90  24978 super.pop();
91    }
92   
 
93  24720 toggle private void openNamespace(String namespace)
94    {
95  24729 if (getVelocityManager() != null) {
96  24707 try {
97    // Mark that we're starting to use a different Velocity macro name-space.
98  24712 getVelocityManager().getVelocityEngine().startedUsingMacroNamespace(namespace);
99  24722 logger.debug("Started using velocity macro namespace [{}].", namespace);
100    } catch (Exception e) {
101    // Failed to get the Velocity Engine and thus to clear Velocity Macro cache. Log this as a warning but
102    // continue since it's not absolutely critical.
103  2 logger.warn("Failed to notify Velocity Macro cache for opening the [{}] namespace. Reason = [{}]",
104    namespace, e.getMessage());
105    }
106    }
107    }
108   
 
109  24724 toggle private void closeNamespace(String namespace)
110    {
111  24723 if (getVelocityManager() != null) {
112  24719 try {
113  24718 getVelocityManager().getVelocityEngine().stoppedUsingMacroNamespace(namespace);
114  24722 logger.debug("Stopped using velocity macro namespace [{}].", namespace);
115    } catch (Exception e) {
116    // Failed to get the Velocity Engine and thus to clear Velocity Macro cache. Log this as a warning but
117    // continue since it's not absolutely critical.
118  0 logger.warn("Failed to notify Velocity Macro cache for closing the [{}] namespace. Reason = [{}]",
119    namespace, e.getMessage());
120    }
121    }
122    }
123   
 
124  26769 toggle @Override
125    public Syntax getTargetSyntax()
126    {
127  26769 Syntax targetSyntax = super.getTargetSyntax();
128   
129  26763 if (targetSyntax == null) {
130    // Fallback to the skin syntax
131  11168 targetSyntax = skinManagerProvider.get().getCurrentSkin(true).getOutputSyntax();
132    }
133   
134  26766 return targetSyntax;
135    }
136    }