1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.plugin.macro

File XWikiMacroService.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

24
80
8
1
269
174
22
0.28
10
8
2.75

Classes

Class Line # Actions
XWikiMacroService 57 80 0% 22 50
0.553571455.4%
 

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 org.xwiki.wysiwyg.server.internal.plugin.macro;
21   
22    import java.lang.reflect.Type;
23    import java.util.ArrayList;
24    import java.util.Collections;
25    import java.util.Comparator;
26    import java.util.LinkedHashMap;
27    import java.util.List;
28    import java.util.Map;
29   
30    import javax.inject.Inject;
31    import javax.inject.Provider;
32    import javax.inject.Singleton;
33   
34    import org.apache.commons.lang3.StringUtils;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor;
37    import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroService;
38    import org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterDescriptor;
39    import org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterType;
40    import org.xwiki.rendering.macro.Macro;
41    import org.xwiki.rendering.macro.MacroCategoryManager;
42    import org.xwiki.rendering.macro.MacroId;
43    import org.xwiki.rendering.macro.MacroManager;
44    import org.xwiki.rendering.syntax.Syntax;
45    import org.xwiki.rendering.syntax.SyntaxFactory;
46    import org.xwiki.wysiwyg.server.plugin.macro.MacroDescriptorTranslator;
47   
48    import com.xpn.xwiki.XWikiContext;
49   
50    /**
51    * XWiki specific implementation of {@link MacroService}.
52    *
53    * @version $Id: 0168899f7fa22bfd90973c304b90aa2e922e6b8f $
54    */
55    @Component
56    @Singleton
 
57    public class XWikiMacroService implements MacroService
58    {
59    /**
60    * The syntax factory used to create {@link Syntax} instances from string syntax identifiers.
61    */
62    @Inject
63    private SyntaxFactory syntaxFactory;
64   
65    /**
66    * The macro manager used to retrieve macros.
67    */
68    @Inject
69    private MacroManager macroManager;
70   
71    /**
72    * The macro category manager used to retrieve macro categories.
73    */
74    @Inject
75    private MacroCategoryManager categoryManager;
76   
77    /**
78    * The component used to translate macro descriptors into the execution context language.
79    */
80    @Inject
81    private MacroDescriptorTranslator macroDescriptorTranslator;
82   
83    /**
84    * Used to set the current wiki, depending on the request.
85    */
86    @Inject
87    private Provider<XWikiContext> xcontextProvider;
88   
 
89  1 toggle @Override
90    public MacroDescriptor getMacroDescriptor(String macroId, String syntaxId, String wikiId)
91    {
92  1 XWikiContext xcontext = xcontextProvider.get();
93   
94  1 String newWikiId = wikiId;
95  1 if (newWikiId == null) {
96  1 newWikiId = xcontext.getWikiId();
97    }
98   
99  1 String oldWikiId = xcontext.getWikiId();
100  1 if (oldWikiId != newWikiId) {
101    // Set the requested context wiki.
102  0 xcontext.setWikiId(newWikiId);
103    }
104   
105  1 try {
106  1 return macroDescriptorTranslator.translate(getUntranslatedMacroDescriptor(macroId, syntaxId));
107    } finally {
108    // Reset the context's current wiki.
109  1 if (oldWikiId != newWikiId) {
110  0 xcontext.setWikiId(oldWikiId);
111    }
112    }
113    }
114   
 
115  1 toggle @Override
116    public MacroDescriptor getMacroDescriptor(String macroId, String syntaxId)
117    {
118  1 return getMacroDescriptor(macroId, syntaxId, null);
119    }
120   
121    /**
122    * @param macroId the macro identifier
123    * @param syntaxId the syntax identifier
124    * @return the untranslated macro descriptor for the specified macro
125    */
 
126  1 toggle private MacroDescriptor getUntranslatedMacroDescriptor(String macroId, String syntaxId)
127    {
128  1 try {
129  1 MacroId macroIdObject = new MacroId(macroId, syntaxFactory.createSyntaxFromIdString(syntaxId));
130  1 Macro<?> macro = macroManager.getMacro(macroIdObject);
131  1 org.xwiki.rendering.macro.descriptor.MacroDescriptor descriptor = macro.getDescriptor();
132   
133  1 ParameterDescriptor contentDescriptor = null;
134  1 if (descriptor.getContentDescriptor() != null) {
135  1 contentDescriptor = new ParameterDescriptor();
136  1 contentDescriptor.setId("content");
137  1 contentDescriptor.setName("Content");
138  1 contentDescriptor.setDescription(descriptor.getContentDescriptor().getDescription());
139    // Just a hack to distinguish between regular strings and large strings.
140  1 contentDescriptor.setType(createMacroParameterType(StringBuffer.class));
141  1 contentDescriptor.setMandatory(descriptor.getContentDescriptor().isMandatory());
142    }
143   
144    // We use a linked hash map to preserve the order of the macro parameters.
145  1 Map<String, ParameterDescriptor> parameterDescriptorMap = new LinkedHashMap<String, ParameterDescriptor>();
146  1 for (Map.Entry<String, org.xwiki.rendering.macro.descriptor.ParameterDescriptor> entry : descriptor
147    .getParameterDescriptorMap().entrySet()) {
148  1 parameterDescriptorMap.put(entry.getKey(), createMacroParameterDescriptor(entry.getValue()));
149    }
150   
151  1 MacroDescriptor result = new MacroDescriptor();
152  1 result.setId(macroIdObject.getId());
153  1 result.setName(descriptor.getName());
154  1 result.setDescription(descriptor.getDescription());
155  1 result.setSupportingInlineMode(macro.supportsInlineMode());
156    // NOTE: we should set the category also, but we need a new method in MacroCategoryManager.
157  1 result.setContentDescriptor(contentDescriptor);
158  1 result.setParameterDescriptorMap(parameterDescriptorMap);
159   
160  1 return result;
161    } catch (Exception e) {
162  0 throw new RuntimeException("Exception while retrieving macro descriptor.", e);
163    }
164    }
165   
166    /**
167    * Creates a {@link ParameterDescriptor} from a {@link org.xwiki.rendering.macro.descriptor.ParameterDescriptor}.
168    *
169    * @param descriptor a macro parameter descriptor from the rendering package
170    * @return a macro parameter descriptor from the WYSIWYG package
171    */
 
172  1 toggle private ParameterDescriptor createMacroParameterDescriptor(
173    org.xwiki.rendering.macro.descriptor.ParameterDescriptor descriptor)
174    {
175  1 ParameterDescriptor result = new ParameterDescriptor();
176  1 result.setId(descriptor.getId());
177    // Fall-back on parameter id if parameter name if not specified.
178    // See XWIKI-4558 (Add macro parameter display name support to wiki macros).
179  1 result.setName(StringUtils.isBlank(descriptor.getName()) ? descriptor.getId() : descriptor.getName());
180  1 result.setDescription(descriptor.getDescription());
181  1 result.setType(createMacroParameterType(descriptor.getParameterType()));
182  1 Object defaultValue = descriptor.getDefaultValue();
183  1 if (defaultValue != null) {
184  1 result.setDefaultValue(String.valueOf(defaultValue));
185    }
186  1 result.setMandatory(descriptor.isMandatory());
187  1 return result;
188    }
189   
190    /**
191    * NOTE: We can't send a {@link Type} instance to the client side because GWT can't serialize it so we have to
192    * convert it to a {@link ParameterType} instance.
193    *
194    * @param type the type that defines the values a macro parameter can have
195    * @return the parameter type associated with the given type
196    */
 
197  2 toggle private ParameterType createMacroParameterType(Type type)
198    {
199  2 ParameterType parameterType = new ParameterType();
200  2 if (type instanceof Class) {
201  2 Class<?> parameterClass = (Class<?>) type;
202  2 parameterType.setName(parameterClass.getName());
203  2 if (parameterClass.isEnum()) {
204  0 Object[] parameterClassConstants = parameterClass.getEnumConstants();
205  0 Map<String, String> parameterTypeConstants = new LinkedHashMap<String, String>();
206  0 for (int i = 0; i < parameterClassConstants.length; i++) {
207  0 String constant = String.valueOf(parameterClassConstants[i]);
208    // We leave the constant unlocalized for now.
209  0 parameterTypeConstants.put(constant, constant);
210    }
211  0 parameterType.setEnumConstants(parameterTypeConstants);
212    }
213    }
214  2 return parameterType;
215    }
216   
 
217  0 toggle @Override
218    public List<MacroDescriptor> getMacroDescriptors(String syntaxId, String wikiId)
219    {
220  0 XWikiContext xcontext = xcontextProvider.get();
221   
222  0 String newWikiId = wikiId;
223  0 if (newWikiId == null) {
224  0 newWikiId = xcontext.getWikiId();
225    }
226   
227  0 String oldWikiId = xcontext.getWikiId();
228  0 if (oldWikiId != newWikiId) {
229    // Set the requested context wiki.
230  0 xcontext.setWikiId(newWikiId);
231    }
232   
233  0 try {
234  0 Syntax syntax = syntaxFactory.createSyntaxFromIdString(syntaxId);
235  0 List<MacroDescriptor> descriptors = new ArrayList<MacroDescriptor>();
236  0 for (String category : categoryManager.getMacroCategories(syntax)) {
237  0 for (MacroId macroId : categoryManager.getMacroIds(category, syntax)) {
238  0 MacroDescriptor descriptor = getUntranslatedMacroDescriptor(macroId.getId(), syntaxId);
239  0 descriptor.setCategory(category);
240  0 descriptors.add(macroDescriptorTranslator.translate(descriptor));
241    }
242    }
243   
244  0 Collections.sort(descriptors, new Comparator<MacroDescriptor>()
245    {
 
246  0 toggle public int compare(MacroDescriptor alice, MacroDescriptor bob)
247    {
248  0 return alice.getName().compareTo(bob.getName());
249    }
250    });
251   
252  0 return descriptors;
253    } catch (Exception e) {
254  0 throw new RuntimeException("Exception while retrieving the list of macro descriptors for syntax ["
255    + syntaxId + "].", e);
256    } finally {
257    // Reset the context's current wiki.
258  0 if (oldWikiId != newWikiId) {
259  0 xcontext.setWikiId(oldWikiId);
260    }
261    }
262    }
263   
 
264  0 toggle @Override
265    public List<MacroDescriptor> getMacroDescriptors(String syntaxId)
266    {
267  0 return getMacroDescriptors(syntaxId, null);
268    }
269    }