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

File DefaultOldRendering.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

14
40
9
1
234
151
18
0.45
4.44
9
2

Classes

Class Line # Actions
DefaultOldRendering 68 40 0% 18 22
0.650793765.1%
 

Contributing tests

This file is covered by 1 test. .

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.internal.render;
21   
22    import java.io.StringWriter;
23    import java.util.List;
24    import java.util.Set;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.apache.commons.lang3.StringUtils;
32    import org.apache.velocity.VelocityContext;
33    import org.slf4j.Logger;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.component.manager.ComponentManager;
36    import org.xwiki.model.EntityType;
37    import org.xwiki.model.reference.DocumentReference;
38    import org.xwiki.model.reference.DocumentReferenceResolver;
39    import org.xwiki.model.reference.EntityReference;
40    import org.xwiki.model.reference.EntityReferenceResolver;
41    import org.xwiki.model.reference.EntityReferenceSerializer;
42    import org.xwiki.rendering.block.Block;
43    import org.xwiki.rendering.block.XDOM;
44    import org.xwiki.rendering.listener.reference.ResourceReference;
45    import org.xwiki.rendering.listener.reference.ResourceType;
46    import org.xwiki.rendering.renderer.BlockRenderer;
47    import org.xwiki.velocity.VelocityEngine;
48    import org.xwiki.velocity.VelocityManager;
49    import org.xwiki.velocity.XWikiVelocityException;
50   
51    import com.xpn.xwiki.XWiki;
52    import com.xpn.xwiki.XWikiContext;
53    import com.xpn.xwiki.XWikiException;
54    import com.xpn.xwiki.doc.XWikiDocument;
55    import com.xpn.xwiki.doc.XWikiLink;
56    import com.xpn.xwiki.internal.cache.rendering.RenderingCache;
57    import com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString;
58   
59    /**
60    * Default implementation of {@link OldRendering} that try as much as possible to do something that makes sense without
61    * using old rendering engine.
62    *
63    * @version $Id: 7e354f300b024534da3630c0a537457e52c8363d $
64    * @since 7.1M1
65    */
66    @Component
67    @Singleton
 
68    public class DefaultOldRendering implements OldRendering
69    {
70    @Inject
71    protected Provider<RenderingCache> cache;
72   
73    @Inject
74    @Named("context")
75    protected Provider<ComponentManager> contextComponentManagerProvider;
76   
77    @Inject
78    @Named("compact")
79    protected EntityReferenceSerializer<String> compactEntityReferenceSerializer;
80   
81    @Inject
82    protected DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver;
83   
84    @Inject
85    protected EntityReferenceResolver<ResourceReference> resourceReferenceResolver;
86   
87    @Inject
88    protected Provider<VelocityManager> velocityManagerProvider;
89   
90    @Inject
91    protected Provider<ParseGroovyFromString> parseGroovyFromString;
92   
93    @Inject
94    protected Logger logger;
95   
96    @Inject
97    protected LinkedResourceHelper linkedResourceHelper;
98   
 
99  3 toggle @Override
100    public void renameLinks(XWikiDocument backlinkDocument, DocumentReference oldReference,
101    DocumentReference newReference, XWikiContext context) throws XWikiException
102    {
103    // FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#renameLinks in
104    // xwiki-platform-refactoring-default
105   
106  3 if (this.contextComponentManagerProvider.get().hasComponent(BlockRenderer.class,
107    backlinkDocument.getSyntax().toIdString())) {
108  3 refactorDocumentLinks(backlinkDocument, oldReference, newReference, context);
109    }
110    }
111   
 
112  0 toggle @Override
113    public void flushCache()
114    {
115  0 this.cache.get().flushWholeCache();
116    }
117   
118    /**
119    * @since 2.2M1
120    */
 
121  3 toggle private void refactorDocumentLinks(XWikiDocument document, DocumentReference oldDocumentReference,
122    DocumentReference newDocumentReference, XWikiContext context) throws XWikiException
123    {
124    // FIXME: Duplicate code. See org.xwiki.refactoring.internal.DefaultLinkRefactoring#renameLinks in
125    // xwiki-platform-refactoring-default
126   
127  3 DocumentReference currentDocumentReference = document.getDocumentReference();
128   
129  3 XDOM xdom = document.getXDOM();
130  3 List<Block> blocks = linkedResourceHelper.getBlocks(xdom);
131   
132  3 for (Block block : blocks) {
133  5 ResourceReference resourceReference = linkedResourceHelper.getResourceReference(block);
134  5 if (resourceReference == null) {
135    // Skip invalid blocks.
136  0 continue;
137    }
138   
139  5 ResourceType resourceType = resourceReference.getType();
140   
141    // TODO: support ATTACHMENT as well.
142  5 if (!ResourceType.DOCUMENT.equals(resourceType) && !ResourceType.SPACE.equals(resourceType)) {
143    // We are currently only interested in Document or Space references.
144  0 continue;
145    }
146   
147    // Resolve the resource reference.
148  5 EntityReference linkEntityReference =
149    resourceReferenceResolver.resolve(resourceReference, null, currentDocumentReference);
150    // Resolve the document of the reference.
151  5 DocumentReference linkTargetDocumentReference =
152    defaultReferenceDocumentReferenceResolver.resolve(linkEntityReference);
153  5 EntityReference newTargetReference = newDocumentReference;
154  5 ResourceType newResourceType = resourceType;
155   
156    // If the link was resolved to a space...
157  5 if (EntityType.SPACE.equals(linkEntityReference.getType())) {
158  0 if (XWiki.DEFAULT_SPACE_HOMEPAGE.equals(newDocumentReference.getName())) {
159    // If the new document reference is also a space (non-terminal doc), be careful to keep it
160    // serialized as a space still (i.e. without ".WebHome") and not serialize it as a doc by mistake
161    // (i.e. with ".WebHome").
162  0 newTargetReference = newDocumentReference.getLastSpaceReference();
163    } else {
164    // If the new target is a non-terminal document, we can not use a "space:" resource type to access
165    // it anymore. To fix it, we need to change the resource type of the link reference "doc:".
166  0 newResourceType = ResourceType.DOCUMENT;
167    }
168    }
169   
170    // If the link targets the old (renamed) document reference, we must update it.
171  5 if (linkTargetDocumentReference.equals(oldDocumentReference)) {
172  5 String newReferenceString =
173    this.compactEntityReferenceSerializer.serialize(newTargetReference, currentDocumentReference);
174   
175    // Update the reference in the XDOM.
176  5 linkedResourceHelper.setResourceReferenceString(block, newReferenceString);
177  5 linkedResourceHelper.setResourceType(block, newResourceType);
178    }
179    }
180   
181  3 document.setContent(xdom);
182    }
183   
 
184  46 toggle @Override
185    public String parseContent(String content, XWikiContext xcontext)
186    {
187  46 try {
188  46 if (StringUtils.isNotEmpty(content)) {
189  46 VelocityManager velocityManager = this.velocityManagerProvider.get();
190   
191  46 VelocityContext velocityContext = velocityManager.getVelocityContext();
192  46 VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
193   
194  46 StringWriter writer = new StringWriter();
195  46 velocityEngine.evaluate(velocityContext, writer, xcontext.getDoc().getPrefixedFullName(), content);
196  46 return writer.toString();
197    }
198    } catch (XWikiVelocityException e) {
199  0 this.logger.error("Faield to parse content [" + content + "]", e);
200    }
201   
202  0 return "";
203    }
204   
 
205  3805 toggle @Override
206    public Set<XWikiLink> extractLinks(XWikiDocument doc, XWikiContext context) throws XWikiException
207    {
208  3805 return doc.getUniqueWikiLinkedPages(context);
209    }
210   
 
211  0 toggle @Override
212    public void resetRenderingEngine(XWikiContext context) throws XWikiException
213    {
214    // xwiki/1.0 specific API
215    }
216   
 
217  0 toggle @Override
218    public String renderText(String text, XWikiDocument doc, XWikiContext xcontext)
219    {
220  0 return doc.getRenderedContent(text, doc.getSyntaxId(), xcontext);
221    }
222   
 
223  0 toggle @Override
224    public String renderTemplate(String template, String skin, XWikiContext xcontext)
225    {
226  0 return xcontext.getWiki().parseTemplate(template, skin, xcontext);
227    }
228   
 
229  8 toggle @Override
230    public String renderTemplate(String template, XWikiContext xcontext)
231    {
232  8 return xcontext.getWiki().parseTemplate(template, xcontext);
233    }
234    }