1 | /* | |
2 | * $Id: $ | |
3 | * | |
4 | * Copyright 2009 by Paulo Soares. | |
5 | * | |
6 | * The contents of this file are subject to the Mozilla Public License Version 1.1 | |
7 | * (the "License"); you may not use this file except in compliance with the License. | |
8 | * You may obtain a copy of the License at http://www.mozilla.org/MPL/ | |
9 | * | |
10 | * Software distributed under the License is distributed on an "AS IS" basis, | |
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 | * for the specific language governing rights and limitations under the License. | |
13 | * | |
14 | * The Original Code is 'iText, a free JAVA-PDF library'. | |
15 | * | |
16 | * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by | |
17 | * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. | |
18 | * All Rights Reserved. | |
19 | * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer | |
20 | * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. | |
21 | * | |
22 | * Contributor(s): all the names of the contributors are added in the source code | |
23 | * where applicable. | |
24 | * | |
25 | * Alternatively, the contents of this file may be used under the terms of the | |
26 | * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the | |
27 | * provisions of LGPL are applicable instead of those above. If you wish to | |
28 | * allow use of your version of this file only under the terms of the LGPL | |
29 | * License and not to allow others to use your version of this file under | |
30 | * the MPL, indicate your decision by deleting the provisions above and | |
31 | * replace them with the notice and other provisions required by the LGPL. | |
32 | * If you do not delete the provisions above, a recipient may use your version | |
33 | * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. | |
34 | * | |
35 | * This library is free software; you can redistribute it and/or modify it | |
36 | * under the terms of the MPL as stated above or under the terms of the GNU | |
37 | * Library General Public License as published by the Free Software Foundation; | |
38 | * either version 2 of the License, or any later version. | |
39 | * | |
40 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
41 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
42 | * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more | |
43 | * details. | |
44 | * | |
45 | * If you didn't download this code from the following link, you should check if | |
46 | * you aren't using an obsolete version: | |
47 | * http://www.lowagie.com/iText/ | |
48 | */ | |
49 | package com.lowagie.text.error_messages; | |
50 | ||
51 | import com.lowagie.text.pdf.BaseFont; | |
52 | import java.io.BufferedReader; | |
53 | import java.io.Reader; | |
54 | import java.io.IOException; | |
55 | import java.io.InputStream; | |
56 | import java.io.InputStreamReader; | |
57 | import java.nio.charset.StandardCharsets; | |
58 | import java.util.HashMap; | |
59 | ||
60 | /** | |
61 | * Localizes error messages. The messages are located in the package | |
62 | * com.lowagie.text.error_messages in the form language_country.lng. | |
63 | * The internal file encoding is UTF-8 without any escape chars, it's not a | |
64 | * normal property file. See en.lng for more information on the internal format. | |
65 | * @author Paulo Soares (psoares@glintt.com) | |
66 | */ | |
67 | public final class MessageLocalization { | |
68 | private static HashMap defaultLanguage = new HashMap(); | |
69 | private static HashMap currentLanguage; | |
70 | private static final String BASE_PATH = "com/lowagie/text/error_messages/"; | |
71 | ||
72 | private MessageLocalization() { | |
73 | } | |
74 | ||
75 | static { | |
76 | try { | |
77 | defaultLanguage = getLanguageMessages("en", null); | |
78 | } catch (Exception ex) { | |
79 | // do nothing | |
80 | } | |
81 | if (defaultLanguage == null) | |
82 | defaultLanguage = new HashMap(); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Get a message without parameters. | |
87 | * @param key the key to the message | |
88 | * @return the message | |
89 | */ | |
90 | public static String getMessage(String key) { | |
91 | HashMap cl = currentLanguage; | |
92 | String val; | |
93 |
1
1. getMessage : negated conditional → NO_COVERAGE |
if (cl != null) { |
94 | val = (String)cl.get(key); | |
95 |
1
1. getMessage : negated conditional → NO_COVERAGE |
if (val != null) |
96 |
1
1. getMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return val; |
97 | } | |
98 | cl = defaultLanguage; | |
99 | val = (String)cl.get(key); | |
100 |
1
1. getMessage : negated conditional → NO_COVERAGE |
if (val != null) |
101 |
1
1. getMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return val; |
102 |
1
1. getMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return "No message found for " + key; |
103 | } | |
104 | ||
105 | /** | |
106 | * Get a message without parameters. | |
107 | * @param key the key to the message | |
108 | * @return the message | |
109 | */ | |
110 | public static String getComposedMessage(String key) { | |
111 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getComposedMessage(key, null, null, null, null); |
112 | } | |
113 | ||
114 | /** | |
115 | * Get a message with one parameter. The parameter will replace the string | |
116 | * "{1}" found in the message. | |
117 | * @param key the key to the message | |
118 | * @param p1 the parameter | |
119 | * @return the message | |
120 | */ | |
121 | public static String getComposedMessage(String key, Object p1) { | |
122 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getComposedMessage(key, p1, null, null, null); |
123 | } | |
124 | ||
125 | /** | |
126 | * Get a message with one parameter. The parameter will replace the string | |
127 | * "{1}" found in the message. | |
128 | * @param key the key to the message | |
129 | * @param p1 the parameter | |
130 | * @return the message | |
131 | */ | |
132 | public static String getComposedMessage(String key, int p1) { | |
133 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getComposedMessage(key, String.valueOf(p1), null, null, null); |
134 | } | |
135 | ||
136 | /** | |
137 | * Get a message with one parameter. The parameter will replace the string | |
138 | * "{1}", "{2}" found in the message. | |
139 | * @param key the key to the message | |
140 | * @param p1 the parameter | |
141 | * @param p2 the parameter | |
142 | * @return the message | |
143 | */ | |
144 | public static String getComposedMessage(String key, Object p1, Object p2) { | |
145 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getComposedMessage(key, p1, p2, null, null); |
146 | } | |
147 | ||
148 | /** | |
149 | * Get a message with one parameter. The parameter will replace the string | |
150 | * "{1}", "{2}", "{3}" found in the message. | |
151 | * @param key the key to the message | |
152 | * @param p1 the parameter | |
153 | * @param p2 the parameter | |
154 | * @param p3 the parameter | |
155 | * @return the message | |
156 | */ | |
157 | public static String getComposedMessage(String key, Object p1, Object p2, Object p3) { | |
158 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return getComposedMessage(key, p1, p2, p3, null); |
159 | } | |
160 | ||
161 | /** | |
162 | * Get a message with two parameters. The parameters will replace the strings | |
163 | * "{1}", "{2}", "{3}", "{4}" found in the message. | |
164 | * @param key the key to the message | |
165 | * @param p1 the parameter | |
166 | * @param p2 the parameter | |
167 | * @param p3 the parameter | |
168 | * @param p4 the parameter | |
169 | * @return the message | |
170 | */ | |
171 | public static String getComposedMessage(String key, Object p1, Object p2, Object p3, Object p4) { | |
172 | String msg = getMessage(key); | |
173 |
1
1. getComposedMessage : negated conditional → NO_COVERAGE |
if (p1 != null) { |
174 | msg = msg.replaceAll("\\{1}", p1.toString()); | |
175 | } | |
176 |
1
1. getComposedMessage : negated conditional → NO_COVERAGE |
if (p2 != null) { |
177 | msg = msg.replaceAll("\\{2}", p2.toString()); | |
178 | } | |
179 |
1
1. getComposedMessage : negated conditional → NO_COVERAGE |
if (p3 != null) { |
180 | msg = msg.replaceAll("\\{3}", p3.toString()); | |
181 | } | |
182 |
1
1. getComposedMessage : negated conditional → NO_COVERAGE |
if (p4 != null) { |
183 | msg = msg.replaceAll("\\{4}", p4.toString()); | |
184 | } | |
185 |
1
1. getComposedMessage : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::getComposedMessage to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return msg; |
186 | } | |
187 | ||
188 | /** | |
189 | * Sets the language to be used globally for the error messages. The language | |
190 | * is a two letter lowercase country designation like "en" or "pt". The country | |
191 | * is an optional two letter uppercase code like "US" or "PT". | |
192 | * @param language the language | |
193 | * @param country the country | |
194 | * @return true if the language was found, false otherwise | |
195 | * @throws IOException on error | |
196 | */ | |
197 | public static boolean setLanguage(String language, String country) throws IOException { | |
198 | HashMap lang = getLanguageMessages(language, country); | |
199 |
1
1. setLanguage : negated conditional → NO_COVERAGE |
if (lang == null) |
200 |
1
1. setLanguage : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return false; |
201 | currentLanguage = lang; | |
202 |
1
1. setLanguage : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE |
return true; |
203 | } | |
204 | ||
205 | /** | |
206 | * Sets the error messages directly from a Reader. | |
207 | * @param r the Reader | |
208 | * @throws IOException on error | |
209 | */ | |
210 | public static void setMessages(Reader r) throws IOException { | |
211 | currentLanguage = readLanguageStream(r); | |
212 | } | |
213 | ||
214 | private static HashMap getLanguageMessages(String language, String country) throws IOException { | |
215 | if (language == null) | |
216 | throw new IllegalArgumentException("The language cannot be null."); | |
217 | InputStream is = null; | |
218 | try { | |
219 | String file; | |
220 | if (country != null) | |
221 | file = language + "_" + country + ".lng"; | |
222 | else | |
223 | file = language + ".lng"; | |
224 | is = BaseFont.getResourceStream(BASE_PATH + file, MessageLocalization.class.getClassLoader()); | |
225 | if (is != null) | |
226 | return readLanguageStream(is); | |
227 | if (country == null) | |
228 | return null; | |
229 | file = language + ".lng"; | |
230 | is = BaseFont.getResourceStream(BASE_PATH + file, MessageLocalization.class.getClassLoader()); | |
231 | if (is != null) | |
232 | return readLanguageStream(is); | |
233 | else | |
234 | return null; | |
235 | } | |
236 | finally { | |
237 | try { | |
238 | is.close(); | |
239 | } catch (Exception ignored) { | |
240 | // do nothing | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | private static HashMap readLanguageStream(InputStream is) throws IOException { | |
246 |
1
1. readLanguageStream : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::readLanguageStream to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return readLanguageStream(new InputStreamReader(is, StandardCharsets.UTF_8)); |
247 | } | |
248 | ||
249 | private static HashMap readLanguageStream(Reader r) throws IOException { | |
250 | HashMap lang = new HashMap(); | |
251 | BufferedReader br = new BufferedReader(r); | |
252 | String line; | |
253 |
1
1. readLanguageStream : negated conditional → NO_COVERAGE |
while ((line = br.readLine()) != null) { |
254 | int idxeq = line.indexOf('='); | |
255 |
2
1. readLanguageStream : changed conditional boundary → NO_COVERAGE 2. readLanguageStream : negated conditional → NO_COVERAGE |
if (idxeq < 0) |
256 | continue; | |
257 | String key = line.substring(0, idxeq).trim(); | |
258 |
1
1. readLanguageStream : negated conditional → NO_COVERAGE |
if (key.startsWith("#")) |
259 | continue; | |
260 |
1
1. readLanguageStream : Replaced integer addition with subtraction → NO_COVERAGE |
lang.put(key, line.substring(idxeq + 1)); |
261 | } | |
262 |
1
1. readLanguageStream : mutated return of Object value for com/lowagie/text/error_messages/MessageLocalization::readLanguageStream to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE |
return lang; |
263 | } | |
264 | } | |
Mutations | ||
93 |
1.1 |
|
95 |
1.1 |
|
96 |
1.1 |
|
100 |
1.1 |
|
101 |
1.1 |
|
102 |
1.1 |
|
111 |
1.1 |
|
122 |
1.1 |
|
133 |
1.1 |
|
145 |
1.1 |
|
158 |
1.1 |
|
173 |
1.1 |
|
176 |
1.1 |
|
179 |
1.1 |
|
182 |
1.1 |
|
185 |
1.1 |
|
199 |
1.1 |
|
200 |
1.1 |
|
202 |
1.1 |
|
246 |
1.1 |
|
253 |
1.1 |
|
255 |
1.1 2.2 |
|
258 |
1.1 |
|
260 |
1.1 |
|
262 |
1.1 |