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

File MessageFormatTranslationMessage.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
25
4
1
133
70
12
0.48
6.25
4
3

Classes

Class Line # Actions
MessageFormatTranslationMessage 42 25 0% 12 4
0.897435989.7%
 

Contributing tests

This file is covered by 23 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.messagetool.internal;
21   
22    import java.io.StringReader;
23    import java.text.MessageFormat;
24    import java.util.Collection;
25    import java.util.List;
26    import java.util.Locale;
27   
28    import org.xwiki.localization.TranslationBundle;
29    import org.xwiki.localization.message.TranslationMessage;
30    import org.xwiki.rendering.block.Block;
31    import org.xwiki.rendering.block.CompositeBlock;
32    import org.xwiki.rendering.parser.ParseException;
33    import org.xwiki.rendering.parser.Parser;
34    import org.xwiki.rendering.util.ParserUtils;
35   
36    /**
37    * {@link MessageFormat} based {@link TranslationMessage}.
38    *
39    * @version $Id: db8b2c50af0368f050867b5fad72464583a2b1c4 $
40    * @since 4.3M2
41    */
 
42    public class MessageFormatTranslationMessage implements TranslationMessage
43    {
44    /**
45    * Used to "inline" the content parsed with plain text parser.
46    */
47    private static final ParserUtils PARSERUTILS = new ParserUtils();
48   
49    /**
50    * The message.
51    */
52    private String message;
53   
54    /**
55    * The plain text parser.
56    */
57    private Parser plainParser;
58   
59    /**
60    * Keep a cache of parsed version of the message without parameters.
61    */
62    private Block noParamCache;
63   
64    /**
65    * @param message the message
66    * @param plainParser the plain text parser
67    */
 
68  297085 toggle public MessageFormatTranslationMessage(String message, Parser plainParser)
69    {
70  297085 this.message = message;
71  297085 this.plainParser = plainParser;
72    }
73   
 
74  76258 toggle @Override
75    public Block render(Locale locale, Collection<TranslationBundle> bundles, Object... parameters)
76    {
77    // Directly return cache if any available
78  76256 if (parameters.length == 0 && this.noParamCache != null) {
79  66101 return this.noParamCache.clone();
80    }
81   
82    // Format the message
83  10149 String result;
84  10149 if (parameters.length > 0) {
85  3826 try {
86  3826 result = MessageFormat.format(this.message, parameters);
87    } catch (IllegalArgumentException e) {
88    // TODO: log the error ?
89  0 result = this.message;
90    }
91    } else {
92  6323 result = this.message;
93    }
94   
95    // Parse it to rendering blocks
96  10148 Block block;
97  10148 try {
98  10148 List<Block> blocks = this.plainParser.parse(new StringReader(result)).getChildren();
99   
100  10149 PARSERUTILS.removeTopLevelParagraph(blocks);
101   
102  10148 if (blocks.size() == 0) {
103  0 block = new CompositeBlock();
104  10148 } else if (blocks.size() == 1) {
105  2505 block = blocks.get(0);
106    } else {
107  7643 block = new CompositeBlock(blocks);
108    }
109   
110    // Store cache of the message if there is no parameters
111  10149 if (parameters.length == 0) {
112  6323 this.noParamCache = block.clone();
113    }
114    } catch (ParseException e) {
115    // Should never happen since plain text parser cannot fail
116  0 block = null;
117    }
118   
119  10149 return block;
120    }
121   
 
122  3483 toggle @Override
123    public String getRawSource()
124    {
125  3483 return this.message;
126    }
127   
 
128  3459 toggle @Override
129    public String toString()
130    {
131  3459 return getRawSource();
132    }
133    }