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

File ParameterTranslationMessageElement.java

 

Coverage histogram

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

Code metrics

8
19
2
1
100
51
7
0.37
9.5
2
3.5

Classes

Class Line # Actions
ParameterTranslationMessageElement 42 19 0% 7 29
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.localization.internal.message;
21   
22    import java.io.StringReader;
23    import java.util.Collection;
24    import java.util.List;
25    import java.util.Locale;
26   
27    import org.xwiki.localization.TranslationBundle;
28    import org.xwiki.localization.message.TranslationMessageElement;
29    import org.xwiki.rendering.block.Block;
30    import org.xwiki.rendering.block.CompositeBlock;
31    import org.xwiki.rendering.block.XDOM;
32    import org.xwiki.rendering.parser.ParseException;
33    import org.xwiki.rendering.parser.Parser;
34    import org.xwiki.rendering.util.ParserUtils;
35   
36    /**
37    * A {@link TranslationMessageElement} resolved based on a passed parameter.
38    *
39    * @version $Id: 0faba7a50cf0e81aeb115d88a130fd72901d791a $
40    * @since 4.3M2
41    */
 
42    public class ParameterTranslationMessageElement implements TranslationMessageElement
43    {
44    /**
45    * Used to clean the result of plain text parser.
46    */
47    private static final ParserUtils PARSERUTILS = new ParserUtils();
48   
49    /**
50    * The index of the parameter to return.
51    */
52    private int index;
53   
54    /**
55    * Used to parse the String content.
56    */
57    private Parser plainParser;
58   
59    /**
60    * @param index the index of the paramater to return
61    * @param plainParser used to parse the String content
62    */
 
63  0 toggle public ParameterTranslationMessageElement(int index, Parser plainParser)
64    {
65  0 this.index = index;
66  0 this.plainParser = plainParser;
67    }
68   
 
69  0 toggle @Override
70    public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters)
71    {
72  0 Object parameter = parameters[index];
73   
74  0 Block block;
75  0 if (parameter instanceof Block) {
76  0 block = (Block) parameter;
77  0 } else if (parameter != null) {
78  0 try {
79  0 XDOM xdom = this.plainParser.parse(new StringReader(parameter.toString()));
80   
81  0 List<Block> blocks = xdom.getChildren();
82  0 PARSERUTILS.removeTopLevelParagraph(blocks);
83  0 if (blocks.isEmpty()) {
84  0 block = null;
85  0 } else if (blocks.size() == 1) {
86  0 block = blocks.get(0);
87    } else {
88  0 block = new CompositeBlock(blocks);
89    }
90    } catch (ParseException e) {
91    // make the #render fail instead ?
92  0 block = null;
93    }
94    } else {
95  0 block = null;
96    }
97   
98  0 return block;
99    }
100    }