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

File DefaultExtendedRenderingConfiguration.java

 

Coverage histogram

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

Code metrics

10
37
8
1
193
124
17
0.46
4.62
8
2.12

Classes

Class Line # Actions
DefaultExtendedRenderingConfiguration 51 37 0% 17 2
0.9636363496.4%
 

Contributing tests

This file is covered by 6 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.configuration;
21   
22    import java.util.ArrayList;
23    import java.util.Collections;
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.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.configuration.ConfigurationSource;
35    import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration;
36    import org.xwiki.rendering.parser.ParseException;
37    import org.xwiki.rendering.parser.Parser;
38    import org.xwiki.rendering.syntax.Syntax;
39    import org.xwiki.rendering.syntax.SyntaxFactory;
40   
41    import com.xpn.xwiki.CoreConfiguration;
42   
43    /**
44    * Extended configuration options for the Rendering subsystem.
45    *
46    * @version $Id: 7dc9693f3bbc927abce1b7aa84846cc3b51840d5 $
47    * @since 8.2M1
48    */
49    @Component
50    @Singleton
 
51    public class DefaultExtendedRenderingConfiguration implements ExtendedRenderingConfiguration
52    {
53    /**
54    * Prefix for configuration keys for the Rendering module.
55    */
56    private static final String PREFIX = "rendering.";
57   
58    private static final String DISABLED_SYNTAXES_PROPERTY = "disabledSyntaxes";
59   
60    /**
61    * Defines from where to read the rendering configuration data.
62    */
63    @Inject
64    private ConfigurationSource configuration;
65   
66    @Inject
67    @Named("rendering")
68    private ConfigurationSource renderingConfiguration;
69   
70    @Inject
71    @Named("xwikicfg")
72    private ConfigurationSource xwikiCfgConfiguration;
73   
74    @Inject
75    private CoreConfiguration coreConfiguration;
76   
77    @Inject
78    private SyntaxFactory syntaxFactory;
79   
80    /**
81    * Used to lookup parsers and renderers to discover available syntaxes.
82    */
83    @Inject
84    @Named("context")
85    private Provider<ComponentManager> componentManagerProvider;
86   
 
87  224 toggle @Override
88    public int getImageWidthLimit()
89    {
90  224 return this.configuration.getProperty(PREFIX + "imageWidthLimit", -1);
91    }
92   
 
93  224 toggle @Override
94    public int getImageHeightLimit()
95    {
96  224 return this.configuration.getProperty(PREFIX + "imageHeightLimit", -1);
97    }
98   
 
99  275 toggle @Override
100    public boolean isImageDimensionsIncludedInImageURL()
101    {
102  275 return this.configuration.getProperty(PREFIX + "imageDimensionsIncludedInImageURL", true);
103    }
104   
 
105  12 toggle @Override
106    public List<Syntax> getDisabledSyntaxes()
107    {
108  12 List<Syntax> disabledSyntaxes = new ArrayList<>();
109   
110    // First, look in the document sources
111  12 List<String> disabledSyntaxesAsStrings = this.renderingConfiguration.getProperty(DISABLED_SYNTAXES_PROPERTY);
112   
113    // If not found, look in the xwiki cfg source for a xwiki.rendering.syntaxes property
114  12 if (disabledSyntaxesAsStrings == null || disabledSyntaxesAsStrings.isEmpty()) {
115  10 List<Syntax> configuredSyntaxes =
116    convertList(this.xwikiCfgConfiguration.getProperty("xwiki.rendering.syntaxes", List.class));
117    // If there's no such property, then only allow the default syntax. We do this since we don't want users to
118    // see bundled syntaxes by default. We only want them to see automatically syntaxes installed thereafter by
119    // the Extension Manager.
120  10 if (configuredSyntaxes == null || configuredSyntaxes.isEmpty()) {
121  8 disabledSyntaxes.addAll(computeDisabledSyntaxes(
122    Collections.singletonList(this.coreConfiguration.getDefaultDocumentSyntax())));
123    } else {
124    // Disable all syntaxes except those in xwiki.rendering.syntaxes
125  2 disabledSyntaxes.addAll(computeDisabledSyntaxes(configuredSyntaxes));
126    }
127    } else {
128    // Convert into a list of Syntax objects
129  2 disabledSyntaxes.addAll(convertList(disabledSyntaxesAsStrings));
130    }
131  12 return disabledSyntaxes;
132    }
133   
 
134  9 toggle @Override
135    public List<Syntax> getConfiguredSyntaxes()
136    {
137  9 List<Syntax> configuredSyntaxes = new ArrayList<>();
138  9 List<Syntax> disabledSyntaxes = getDisabledSyntaxes();
139  9 for (Syntax availablesyntax : getAvailableParserSyntaxes()) {
140  40 if (!disabledSyntaxes.contains(availablesyntax)) {
141  10 configuredSyntaxes.add(availablesyntax);
142    }
143    }
144  9 return configuredSyntaxes;
145    }
146   
 
147  12 toggle private List<Syntax> convertList(List<String> syntaxesAsStrings)
148    {
149  12 if (syntaxesAsStrings == null) {
150  8 return null;
151    }
152   
153  4 List<Syntax> syntaxes = new ArrayList<>();
154  4 for (String syntaxAsString : syntaxesAsStrings) {
155  8 try {
156  8 syntaxes.add(this.syntaxFactory.createSyntaxFromIdString(syntaxAsString));
157    } catch (ParseException e) {
158  0 throw new RuntimeException(
159    String.format("Failed to convert [%s] into Syntax object", syntaxAsString), e);
160    }
161    }
162  4 return syntaxes;
163    }
164   
 
165  10 toggle private List<Syntax> computeDisabledSyntaxes(List<Syntax> configuredSyntaxes)
166    {
167  10 List<Syntax> disabledSyntaxes = new ArrayList<>();
168  10 for (Syntax availableSyntax : getAvailableParserSyntaxes()) {
169  44 if (!configuredSyntaxes.contains(availableSyntax)) {
170  32 disabledSyntaxes.add(availableSyntax);
171    }
172    }
173  10 return disabledSyntaxes;
174    }
175   
176    /**
177    * @return the list of syntaxes for which a Parser is available
178    */
 
179  19 toggle public List<Syntax> getAvailableParserSyntaxes()
180    {
181  19 List<Syntax> syntaxes = new ArrayList<>();
182  19 try {
183  19 for (Parser parser : this.componentManagerProvider.get().<Parser>getInstanceList(Parser.class)) {
184  84 syntaxes.add(parser.getSyntax());
185    }
186    } catch (ComponentLookupException e) {
187    // This shouldn't happen; if it does then it's critical
188  0 throw new RuntimeException("Failed to lookup parsers", e);
189    }
190   
191  19 return syntaxes;
192    }
193    }