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

File WikiUIExtensionRenderer.java

 

Coverage histogram

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

Code metrics

0
24
3
1
162
74
7
0.29
8
3
2.33

Classes

Class Line # Actions
WikiUIExtensionRenderer 52 24 0% 7 4
0.851851985.2%
 

Contributing tests

This file is covered by 2 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.uiextension.internal;
21   
22    import java.util.HashMap;
23    import java.util.Map;
24   
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27    import org.xwiki.component.manager.ComponentLookupException;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.component.wiki.WikiComponentException;
30    import org.xwiki.component.wiki.internal.bridge.ContentParser;
31    import org.xwiki.context.Execution;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.rendering.block.CompositeBlock;
34    import org.xwiki.rendering.block.XDOM;
35    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
36    import org.xwiki.rendering.transformation.RenderingContext;
37    import org.xwiki.rendering.transformation.Transformation;
38    import org.xwiki.rendering.transformation.TransformationContext;
39    import org.xwiki.rendering.transformation.TransformationException;
40   
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.XWikiException;
43    import com.xpn.xwiki.doc.XWikiDocument;
44   
45    /**
46    * Parse the UI Extension content as an XDOM, cache it and when asked, apply Rendering Transformations on the XDOM
47    * and return the modified XDOM.
48    *
49    * @version $Id: 1652d1244a31657093942f10316cf6b286a5b87f $
50    * @since 5.0M1
51    */
 
52    public class WikiUIExtensionRenderer
53    {
54    /**
55    * The logger to log.
56    */
57    private static final Logger LOGGER = LoggerFactory.getLogger(WikiUIExtensionRenderer.class);
58   
59    /**
60    * Role hint of the UI extension this renderer is bound to.
61    */
62    private final String roleHint;
63   
64    /**
65    * Used to update the rendering context.
66    */
67    private final RenderingContext renderingContext;
68   
69    /**
70    * Used to transform the macros within the extension content.
71    */
72    private final Transformation macroTransformation;
73   
74    /**
75    * Used to retrieve the XWiki context.
76    */
77    private final Execution execution;
78   
79    /**
80    * The xdom from the parsed content, we keep it in order to parse it only once.
81    */
82    private final XDOM xdom;
83   
84    /**
85    * Reference to the document holding the extension.
86    */
87    private final DocumentReference documentReference;
88   
89    /**
90    * Default constructor.
91    *
92    * @param roleHint hint of the UI extension this renderer is bound to
93    * @param content content to render
94    * @param documentReference a reference to the document holding the extension
95    * @param cm the component manager
96    * @throws WikiComponentException if some required components can't be found in the Component Manager
97    */
 
98  129 toggle public WikiUIExtensionRenderer(String roleHint, String content, DocumentReference documentReference,
99    ComponentManager cm) throws WikiComponentException
100    {
101  129 this.roleHint = roleHint;
102   
103  129 try {
104  129 this.execution = cm.getInstance(Execution.class);
105  129 this.renderingContext = cm.getInstance(RenderingContext.class);
106  129 this.macroTransformation = cm.<Transformation>getInstance(Transformation.class, "macro");
107  129 ContentParser contentParser = cm.getInstance(ContentParser.class);
108  129 XWikiDocument xdoc = getXWikiContext().getWiki().getDocument(documentReference, getXWikiContext());
109  129 this.xdom = contentParser.parse(content, xdoc.getSyntax(), documentReference);
110  129 this.documentReference = documentReference;
111    } catch (ComponentLookupException ex) {
112  0 throw new WikiComponentException(
113    "Failed to get an instance for a component role required by Wiki Components.", ex);
114    } catch (XWikiException e) {
115  0 throw new WikiComponentException(String.format(
116    "Failed to retrieve document [%s]", documentReference), e);
117    }
118    }
119   
120    /**
121    * @return the rendered content of the extension
122    */
 
123  1294 toggle public CompositeBlock execute()
124    {
125    // We need to clone the xdom to avoid transforming the original and make it useless after the first
126    // transformation
127  1294 XDOM transformedXDOM = xdom.clone();
128   
129    // Perform macro transformations.
130  1294 try {
131    // Get the document holding the UIX and put it in the UIX context
132  1294 XWikiDocument xdoc = getXWikiContext().getWiki().getDocument(documentReference, getXWikiContext());
133  1294 Map<String, Object> uixContext = new HashMap<String, Object>();
134  1294 uixContext.put(WikiUIExtension.CONTEXT_UIX_DOC_KEY, xdoc.newDocument(getXWikiContext()));
135   
136    // Put the UIX context in the XWiki context
137  1294 getXWikiContext().put(WikiUIExtension.CONTEXT_UIX_KEY, uixContext);
138   
139    // Transform the macros
140  1294 TransformationContext transformationContext = new TransformationContext(xdom, xdoc.getSyntax());
141  1294 transformationContext.setId(roleHint);
142  1294 ((MutableRenderingContext) renderingContext).transformInContext(macroTransformation,
143    transformationContext, transformedXDOM);
144    } catch (TransformationException e) {
145  0 LOGGER.warn("Error while executing wiki component macro transformation for extension [{}]", roleHint);
146    } catch (XWikiException ex) {
147  0 LOGGER.warn("Failed to retrieve document [{}]", documentReference);
148    }
149   
150  1294 return new CompositeBlock(transformedXDOM.getChildren());
151    }
152   
153    /**
154    * Utility method for accessing XWikiContext.
155    *
156    * @return the XWikiContext.
157    */
 
158  5434 toggle private XWikiContext getXWikiContext()
159    {
160  5434 return (XWikiContext) this.execution.getContext().getProperty("xwikicontext");
161    }
162    }