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

File XWikiMacroDescriptorTranslator.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

6
20
2
1
108
51
5
0.25
10
2
2.5

Classes

Class Line # Actions
XWikiMacroDescriptorTranslator 40 20 0% 5 28
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.util.Map.Entry;
23   
24    import javax.inject.Inject;
25    import javax.inject.Singleton;
26   
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.gwt.wysiwyg.client.plugin.macro.MacroDescriptor;
29    import org.xwiki.gwt.wysiwyg.client.plugin.macro.ParameterDescriptor;
30    import org.xwiki.localization.ContextualLocalizationManager;
31    import org.xwiki.wysiwyg.server.plugin.macro.MacroDescriptorTranslator;
32   
33    /**
34    * XWiki specific implementation of {@link MacroDescriptorTranslator}.
35    *
36    * @version $Id: a0a6651af2b71c6451e8eaad5c8d343b43473c5e $
37    */
38    @Component
39    @Singleton
 
40    public class XWikiMacroDescriptorTranslator implements MacroDescriptorTranslator
41    {
42    /**
43    * The name field.
44    */
45    private static final String FIELD_NAME = ".name";
46   
47    /**
48    * The description field.
49    */
50    private static final String FIELD_DESCRIPTION = ".description";
51   
52    /**
53    * The prefix used for all the translation keys.
54    */
55    private static final String KEY_RENDERING = "rendering";
56   
57    /**
58    * The component used to translate the macro descriptor.
59    */
60    @Inject
61    private ContextualLocalizationManager localizationManager;
62   
 
63  0 toggle @Override
64    public MacroDescriptor translate(MacroDescriptor macroDescriptor)
65    {
66  0 String macroKey = KEY_RENDERING + ".macro." + macroDescriptor.getId();
67  0 macroDescriptor.setName(translate(macroKey + FIELD_NAME, macroDescriptor.getName()));
68  0 macroDescriptor.setDescription(translate(macroKey + FIELD_DESCRIPTION, macroDescriptor.getDescription()));
69   
70  0 String macroCategoryKey = KEY_RENDERING + ".macroCategory." + macroDescriptor.getCategory();
71  0 macroDescriptor.setCategory(translate(macroCategoryKey, macroDescriptor.getCategory()));
72   
73  0 ParameterDescriptor contentDescriptor = macroDescriptor.getContentDescriptor();
74  0 if (contentDescriptor != null) {
75  0 contentDescriptor.setName(translate(KEY_RENDERING + ".macroContent", contentDescriptor.getName()));
76  0 contentDescriptor.setDescription(translate(macroKey + ".content.description",
77    contentDescriptor.getDescription()));
78    }
79   
80  0 for (ParameterDescriptor paramDescriptor : macroDescriptor.getParameterDescriptorMap().values()) {
81  0 String paramKey = macroKey + ".parameter." + paramDescriptor.getId();
82  0 paramDescriptor.setName(translate(paramKey + FIELD_NAME, paramDescriptor.getName()));
83  0 paramDescriptor.setDescription(translate(paramKey + FIELD_DESCRIPTION, paramDescriptor.getDescription()));
84   
85  0 if (paramDescriptor.getType().isEnum()) {
86  0 for (Entry<String, String> entry : paramDescriptor.getType().getEnumConstants().entrySet()) {
87  0 String paramValueKey = paramKey + ".value." + entry.getKey();
88  0 entry.setValue(translate(paramValueKey, entry.getValue()));
89    }
90    }
91    }
92   
93  0 return macroDescriptor;
94    }
95   
96    /**
97    * Looks up the given translation key returning the specified default value if no value is found.
98    *
99    * @param key the translation key to look up
100    * @param defaultValue the value to return when there's no value associated with the given key
101    * @return the value associated with the given key if there is one, otherwise the default value
102    */
 
103  0 toggle private String translate(String key, String defaultValue)
104    {
105  0 String translation = localizationManager.getTranslationPlain(key);
106  0 return translation == null ? defaultValue : translation;
107    }
108    }