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

File TranslationMacro.java

 

Coverage histogram

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

Code metrics

14
21
3
1
159
89
11
0.52
7
3
3.67

Classes

Class Line # Actions
TranslationMacro 57 21 0% 11 3
0.9210526392.1%
 

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.localization.macro.internal;
21   
22    import java.io.StringReader;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.Locale;
26   
27    import javax.inject.Inject;
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.localization.LocalizationContext;
33    import org.xwiki.localization.LocalizationManager;
34    import org.xwiki.localization.Translation;
35    import org.xwiki.localization.macro.TranslationMacroParameters;
36    import org.xwiki.rendering.block.Block;
37    import org.xwiki.rendering.block.CompositeBlock;
38    import org.xwiki.rendering.block.GroupBlock;
39    import org.xwiki.rendering.macro.AbstractMacro;
40    import org.xwiki.rendering.macro.MacroContentParser;
41    import org.xwiki.rendering.macro.MacroExecutionException;
42    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
43    import org.xwiki.rendering.parser.ParseException;
44    import org.xwiki.rendering.parser.Parser;
45    import org.xwiki.rendering.transformation.MacroTransformationContext;
46    import org.xwiki.rendering.util.ParserUtils;
47   
48    /**
49    * Display a translation message.
50    *
51    * @version $Id: 885e43a7f4e9047cc525e61757049936b48ed692 $
52    * @since 4.3M2
53    */
54    @Component
55    @Named("translation")
56    @Singleton
 
57    public class TranslationMacro extends AbstractMacro<TranslationMacroParameters>
58    {
59    /**
60    * The description of the macro.
61    */
62    private static final String DESCRIPTION = "Display a translation message.";
63   
64    /**
65    * The description of the macro content.
66    */
67    private static final String CONTENT_DESCRIPTION = "the default translation message";
68   
69    /**
70    * Used to make blocks inline.
71    */
72    private static final ParserUtils PARSERUTILS = new ParserUtils();
73   
74    /**
75    * Used to find the translation.
76    */
77    @Inject
78    private LocalizationManager localization;
79   
80    /**
81    * Used to get the current Locale.
82    */
83    @Inject
84    private LocalizationContext localizationContext;
85   
86    /**
87    * Used to parse the translation macro content.
88    */
89    @Inject
90    private MacroContentParser macroContentParser;
91   
92    /**
93    * Used to parse text.
94    */
95    @Inject
96    @Named("plain/1.0")
97    private Parser plainParser;
98   
99    /**
100    * Create and initialize the descriptor of the macro.
101    */
 
102  21 toggle public TranslationMacro()
103    {
104  21 super("Translation", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION),
105    TranslationMacroParameters.class);
106   
107  21 setDefaultCategory(DEFAULT_CATEGORY_CONTENT);
108    }
109   
 
110  217 toggle @Override
111    public List<Block> execute(TranslationMacroParameters parameters, String content,
112    MacroTransformationContext context) throws MacroExecutionException
113    {
114  217 Locale locale =
115  217 parameters.getLocale() != null ? parameters.getLocale() : this.localizationContext.getCurrentLocale();
116   
117  217 Translation translation = this.localization.getTranslation(parameters.getKey(), locale);
118   
119  217 List<Block> blocks;
120  217 if (translation != null) {
121  215 Block block =
122  215 parameters.getParameters() != null ? translation.render(locale, (Object[]) parameters.getParameters())
123    : translation.render(locale);
124   
125  215 if (block instanceof CompositeBlock) {
126  203 blocks = block.getChildren();
127    } else {
128  12 blocks = Arrays.asList(block);
129    }
130   
131  215 if (!context.getCurrentMacroBlock().isInline()) {
132    // Make the content standalone
133  6 blocks = Arrays.<Block> asList(new GroupBlock(blocks));
134    }
135  2 } else if (content != null) {
136  1 blocks =
137    this.macroContentParser.parse(content, context, false, context.getCurrentMacroBlock().isInline())
138    .getChildren();
139    } else {
140  1 try {
141  1 blocks = this.plainParser.parse(new StringReader(parameters.getKey())).getChildren();
142   
143  1 if (context.getCurrentMacroBlock().isInline()) {
144  0 PARSERUTILS.removeTopLevelParagraph(blocks);
145    }
146    } catch (ParseException e) {
147  0 throw new MacroExecutionException("Failed to parse key [" + parameters.getKey() + "]", e);
148    }
149    }
150   
151  217 return blocks;
152    }
153   
 
154  209 toggle @Override
155    public boolean supportsInlineMode()
156    {
157  209 return true;
158    }
159    }