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

File RenderingScriptService.java

 

Coverage histogram

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

Code metrics

8
52
10
1
263
143
22
0.42
5.2
10
2.2

Classes

Class Line # Actions
RenderingScriptService 58 52 0% 22 5
0.928571492.9%
 

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 org.xwiki.rendering.script;
21   
22    import java.io.StringReader;
23    import java.util.ArrayList;
24    import java.util.List;
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.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration;
38    import org.xwiki.rendering.configuration.RenderingConfiguration;
39    import org.xwiki.rendering.parser.ParseException;
40    import org.xwiki.rendering.parser.Parser;
41    import org.xwiki.rendering.renderer.BlockRenderer;
42    import org.xwiki.rendering.renderer.PrintRendererFactory;
43    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
44    import org.xwiki.rendering.renderer.printer.WikiPrinter;
45    import org.xwiki.rendering.syntax.Syntax;
46    import org.xwiki.rendering.syntax.SyntaxFactory;
47    import org.xwiki.script.service.ScriptService;
48   
49    /**
50    * Provides Rendering-specific Scripting APIs.
51    *
52    * @version $Id: 17abf8d517f374b486157e991a8986297cb40de3 $
53    * @since 2.3M1
54    */
55    @Component
56    @Named("rendering")
57    @Singleton
 
58    public class RenderingScriptService implements ScriptService
59    {
60    /**
61    * Used to lookup parsers and renderers to discover available syntaxes.
62    */
63    @Inject
64    @Named("context")
65    private Provider<ComponentManager> componentManagerProvider;
66   
67    /**
68    * @see #resolveSyntax(String)
69    */
70    @Inject
71    private SyntaxFactory syntaxFactory;
72   
73    @Inject
74    private Logger logger;
75   
76    @Inject
77    private RenderingConfiguration baseConfiguration;
78   
79    @Inject
80    private ExtendedRenderingConfiguration extendedConfiguration;
81   
82    /**
83    * @return the list of syntaxes for which a Parser is available
84    */
 
85  1 toggle public List<Syntax> getAvailableParserSyntaxes()
86    {
87  1 List<Syntax> syntaxes = new ArrayList<Syntax>();
88  1 try {
89  1 for (Parser parser : this.componentManagerProvider.get().<Parser>getInstanceList(Parser.class)) {
90  5 syntaxes.add(parser.getSyntax());
91    }
92    } catch (ComponentLookupException e) {
93    // Failed to lookup parsers, consider there are no syntaxes available
94  0 this.logger.error("Failed to lookup parsers", e);
95    }
96   
97  1 return syntaxes;
98    }
99   
100    /**
101    * @return the list of syntaxes for which a Renderer is available
102    */
 
103  1064 toggle public List<Syntax> getAvailableRendererSyntaxes()
104    {
105  1064 List<Syntax> syntaxes = new ArrayList<Syntax>();
106  1063 try {
107  1064 List<PrintRendererFactory> factories =
108    this.componentManagerProvider.get().getInstanceList(PrintRendererFactory.class);
109  1064 for (PrintRendererFactory factory : factories) {
110  8272 syntaxes.add(factory.getSyntax());
111    }
112    } catch (ComponentLookupException e) {
113    // Failed to lookup renderers, consider there are no syntaxes available
114  0 this.logger.error("Failed to lookup renderers", e);
115    }
116   
117  1064 return syntaxes;
118    }
119   
120    /**
121    * @return the names of Transformations that are configured in the Rendering Configuration and which are used by the
122    * Transformation Manager when running all transformations
123    */
 
124  17 toggle public List<String> getDefaultTransformationNames()
125    {
126  17 return this.baseConfiguration.getTransformationNames();
127    }
128   
129    /**
130    * Parses a text written in the passed syntax.
131    *
132    * @param text the text to parse
133    * @param syntaxId the id of the syntax in which the text is written in
134    * @return the XDOM representing the AST of the parsed text or null if an error occurred
135    * @since 3.2M3
136    */
 
137  4 toggle public XDOM parse(String text, String syntaxId)
138    {
139  4 XDOM result;
140  4 try {
141  4 Parser parser = this.componentManagerProvider.get().getInstance(Parser.class, syntaxId);
142  4 result = parser.parse(new StringReader(text));
143    } catch (Exception e) {
144  0 result = null;
145    }
146  4 return result;
147    }
148   
149    /**
150    * Render a list of Blocks into the passed syntax.
151    *
152    * @param block the block to render
153    * @param outputSyntaxId the syntax in which to render the blocks
154    * @return the string representing the passed blocks in the passed syntax or null if an error occurred
155    * @since 3.2M3
156    */
 
157  1319 toggle public String render(Block block, String outputSyntaxId)
158    {
159  1319 String result;
160  1319 WikiPrinter printer = new DefaultWikiPrinter();
161  1319 try {
162  1319 BlockRenderer renderer =
163    this.componentManagerProvider.get().getInstance(BlockRenderer.class, outputSyntaxId);
164  1318 renderer.render(block, printer);
165  1318 result = printer.toString();
166    } catch (Exception e) {
167  1 result = null;
168    }
169  1319 return result;
170    }
171   
172    /**
173    * Converts a Syntax specified as a String into a proper Syntax object.
174    *
175    * @param syntaxId the syntax as a string (eg "xwiki/2.0", "html/4.01", etc)
176    * @return the proper Syntax object representing the passed syntax
177    */
 
178  2 toggle public Syntax resolveSyntax(String syntaxId)
179    {
180  2 Syntax syntax;
181  2 try {
182  2 syntax = this.syntaxFactory.createSyntaxFromIdString(syntaxId);
183    } catch (ParseException exception) {
184  1 syntax = null;
185    }
186  2 return syntax;
187    }
188   
189    /**
190    * Escapes a give text using the escaping method specific to the given syntax.
191    * <p>
192    * One example of escaping method is using escape characters like {@code ~} for the {@link Syntax#XWIKI_2_1} syntax
193    * on all or just some characters of the given text.
194    * <p>
195    * The current implementation only escapes XWiki 1.0, 2.0 and 2.1 syntaxes.
196    *
197    * @param content the text to escape
198    * @param syntax the syntax to escape the content in (e.g. {@link Syntax#XWIKI_1_0}, {@link Syntax#XWIKI_2_0},
199    * {@link Syntax#XWIKI_2_1}, etc.). This is the syntax where the output will be used and not necessarily
200    * the same syntax of the input content
201    * @return the escaped text or {@code null} if the given content or the given syntax are {@code null}, or if the
202    * syntax is not supported
203    * @since 7.1M1
204    */
 
205  182 toggle public String escape(String content, Syntax syntax)
206    {
207  182 if (content == null || syntax == null) {
208  3 return null;
209    }
210  179 String input = String.valueOf(content);
211   
212    // Determine the escape character for the syntax.
213  179 char escapeChar;
214  179 try {
215  179 escapeChar = getEscapeCharacter(syntax);
216    } catch (Exception e) {
217    // We don`t know how to proceed, so we just return null.
218  1 return null;
219    }
220   
221    // Since we prefix all characters, the result size will be double the input's, so we can just use char[].
222  178 char[] result = new char[input.length() * 2];
223   
224    // Escape the content.
225  9464 for (int i = 0; i < input.length(); i++) {
226  9286 result[2 * i] = escapeChar;
227  9286 result[2 * i + 1] = input.charAt(i);
228    }
229   
230  178 return String.valueOf(result);
231    }
232   
233    /**
234    * @return the list of Rendering Syntaxes that are configured for the current wiki (i.e. that are proposed to the
235    * user when editing wik pages)
236    * @since 8.2M1
237    */
 
238  1 toggle public List<Syntax> getConfiguredSyntaxes()
239    {
240  1 return this.extendedConfiguration.getConfiguredSyntaxes();
241    }
242   
243    /**
244    * @return the list of Rendering Syntaxes that are disabled for the current wiki (i.e. that should not be proposed
245    * to the user when editing wiki pages)
246    * @since 8.2M1
247    */
 
248  0 toggle public List<Syntax> getDisabledSyntaxes()
249    {
250  0 return this.extendedConfiguration.getDisabledSyntaxes();
251    }
252   
 
253  179 toggle private char getEscapeCharacter(Syntax syntax) throws IllegalArgumentException
254    {
255  179 if (Syntax.XWIKI_1_0.equals(syntax)) {
256  7 return '\\';
257  172 } else if (Syntax.XWIKI_2_0.equals(syntax) || Syntax.XWIKI_2_1.equals(syntax)) {
258  171 return '~';
259    }
260   
261  1 throw new IllegalArgumentException(String.format("Escaping is not supported for Syntax [%s]", syntax));
262    }
263    }