|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.impl.utils; |
|
20 |
|
21 import java.util.Date; |
|
22 |
|
23 /** |
|
24 * Formatter for Avkon based platform. |
|
25 * @see Formatter |
|
26 */ |
|
27 public class FormatterAvkon extends Formatter |
|
28 { |
|
29 /** Original pattern string */ |
|
30 private String pattern; |
|
31 |
|
32 /** String with latest replacements */ |
|
33 private String replaced; |
|
34 |
|
35 /** |
|
36 * Platform localisation type. |
|
37 * Either ResourceLoader.AVKON or ResourceLoader.QT. |
|
38 */ |
|
39 private final int locType; |
|
40 |
|
41 /** |
|
42 * The first text parameter replacement index. For Avkon based |
|
43 * localisation this is 0, for Qt based localisation this is 1. |
|
44 */ |
|
45 private final int startIndex; |
|
46 |
|
47 /** Next replacement index */ |
|
48 private int nextIndex; |
|
49 |
|
50 /*** ----------------------------- PUBLIC ------------------------------ */ |
|
51 |
|
52 /** |
|
53 * Replace the lowest numbered parameter in the string, which is not yet |
|
54 * replaced. |
|
55 * |
|
56 * @param string string to replace at the argument |
|
57 * @return same formatter |
|
58 */ |
|
59 public Formatter arg(String string) |
|
60 { |
|
61 // Try to replace with patterns %[N...N]n, %nU, %n, %U |
|
62 String maxPattern = findMaxPattern(); |
|
63 if ((maxPattern != null && replace(maxPattern, string)) || |
|
64 replace("%" + nextIndex + "U", string) || |
|
65 replace("%" + nextIndex, string) || |
|
66 replace("%U", string)) |
|
67 { |
|
68 nextIndex++; |
|
69 } |
|
70 else |
|
71 { |
|
72 Logger.WLOG(Logger.EUtils, |
|
73 "FormatterAvkon: String replacement failed on parameter " + |
|
74 nextIndex + ": " + pattern); |
|
75 } |
|
76 return this; |
|
77 } |
|
78 |
|
79 /** |
|
80 * Replace the least numbered parameter in the string, which is not yet |
|
81 * replaced. |
|
82 * |
|
83 * @param number number to replace at the argument |
|
84 * @return same formatter |
|
85 */ |
|
86 public Formatter arg(int number) |
|
87 { |
|
88 String localisedNumber = _formatInteger(number); |
|
89 |
|
90 // Try to replace with patterns %[N...N]n, %Ln, %nN, %n, %N |
|
91 String maxPattern = findMaxPattern(); |
|
92 if ((maxPattern != null && replace(maxPattern, localisedNumber)) || |
|
93 replace("%" + "L" + nextIndex, localisedNumber) || |
|
94 replace("%" + nextIndex + "N", localisedNumber) || |
|
95 replace("%" + nextIndex, localisedNumber) || |
|
96 replace("%N", localisedNumber)) |
|
97 { |
|
98 nextIndex++; |
|
99 |
|
100 } |
|
101 else |
|
102 { |
|
103 Logger.WLOG(Logger.EUtils, |
|
104 "FormatterAvkon: Integer replacement failed on parameter " + |
|
105 nextIndex + ": " + pattern); |
|
106 } |
|
107 return this; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Replace the least numbered parameter in the string, which is not yet |
|
112 * replaced. |
|
113 * |
|
114 * @param ch character to replace at the argument |
|
115 * @return same formatter |
|
116 */ |
|
117 public Formatter arg(char ch) |
|
118 { |
|
119 String chString = new String(new char[] { ch }); |
|
120 |
|
121 // Try to replace with patterns %nC, %n, %C |
|
122 if (replace("%" + nextIndex + "C", chString) || |
|
123 replace("%" + nextIndex, chString) || |
|
124 replace("%C", chString)) |
|
125 { |
|
126 nextIndex++; |
|
127 |
|
128 } |
|
129 else |
|
130 { |
|
131 Logger.WLOG(Logger.EUtils, |
|
132 "FormatterAvkon: Character replacement failed on parameter " + |
|
133 nextIndex + ": " + pattern); |
|
134 } |
|
135 return this; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Replace the least numbered parameter in the string, which is not yet |
|
140 * replaced. Date is formatted according to current device date format. |
|
141 * |
|
142 * @param date date to replace at the argument |
|
143 * @return same formatter |
|
144 */ |
|
145 public Formatter arg(Date date) |
|
146 { |
|
147 String dateString = _formatDate(date.getTime()); |
|
148 return arg(dateString); |
|
149 } |
|
150 |
|
151 /** |
|
152 * Convert the current pattern to string, along with parameter |
|
153 * replacements. |
|
154 * |
|
155 * @return string where parameters are replaced |
|
156 */ |
|
157 public String toString() |
|
158 { |
|
159 String result = replaced; |
|
160 |
|
161 // Reset for next usage |
|
162 replaced = pattern; |
|
163 nextIndex = startIndex; |
|
164 |
|
165 return result; |
|
166 } |
|
167 |
|
168 /** |
|
169 * Applies convertion from european digits into arabic-indic digits |
|
170 * based on existing language settings |
|
171 * |
|
172 * @param str String which might contain european digits |
|
173 * @return A string identical with the provided string but with the |
|
174 * european digits (if any) converted to arabic-indic digits |
|
175 */ |
|
176 public static String formatDigits(String str) |
|
177 { |
|
178 return _formatDigits(str); |
|
179 } |
|
180 |
|
181 /*** ----------------------------- PROTECTED -------------------------- */ |
|
182 |
|
183 /** |
|
184 * Default constructor. |
|
185 */ |
|
186 protected FormatterAvkon() |
|
187 { |
|
188 this(""); |
|
189 } |
|
190 |
|
191 /** |
|
192 * Create a new formatter |
|
193 * |
|
194 * @param pattern formatter pattern |
|
195 */ |
|
196 protected FormatterAvkon(String aPattern) |
|
197 { |
|
198 this(aPattern, ResourceLoader.AVKON); |
|
199 } |
|
200 |
|
201 /** |
|
202 * Create a new formatter |
|
203 * |
|
204 * @param pattern formatter pattern |
|
205 * @param aLocType platform localisation type |
|
206 */ |
|
207 protected FormatterAvkon(String aPattern, int aLocType) |
|
208 { |
|
209 pattern = aPattern; |
|
210 replaced = aPattern; |
|
211 locType = aLocType; |
|
212 startIndex = (locType == ResourceLoader.QT? 1: 0); |
|
213 nextIndex = startIndex; |
|
214 } |
|
215 |
|
216 /*** ----------------------------- PRIVATE ---------------------------- */ |
|
217 |
|
218 /** |
|
219 * Replace first occurrence of the string pattern in the replaced field. |
|
220 * |
|
221 * @param pattern string to search for |
|
222 * @param replacement string to replace patterns |
|
223 * @return true if pattern was found and replaced, false if pattern was |
|
224 * not found |
|
225 */ |
|
226 private boolean replace(String pattern, String replacement) |
|
227 { |
|
228 int index = replaced.indexOf(pattern); |
|
229 if (index != -1) |
|
230 { |
|
231 if (replaced.indexOf(pattern + "[]") != -1) |
|
232 { |
|
233 replaced = |
|
234 replaced.substring(0, index) + replacement + |
|
235 replaced.substring(index + pattern.length() + 2); |
|
236 } |
|
237 else if (replaced.indexOf(pattern + "[") != -1) |
|
238 { |
|
239 return replaceWithMax(pattern, replacement, index); |
|
240 } |
|
241 else |
|
242 { |
|
243 replaced = |
|
244 replaced.substring(0, index) + replacement + |
|
245 replaced.substring(index + pattern.length()); |
|
246 } |
|
247 return true; |
|
248 } |
|
249 return false; |
|
250 } |
|
251 |
|
252 /** |
|
253 * Replace first occurrence of the string pattern in the replaced field. |
|
254 * Replace [N...N] defined amount of characters. |
|
255 * |
|
256 * @param pattern string to search for |
|
257 * @param replacement string to replace patterns |
|
258 * @param index of replacement tag. |
|
259 * @return true if pattern was found and replaced, false if pattern was |
|
260 * not found |
|
261 */ |
|
262 private boolean replaceWithMax(String pattern, String replacement, int maxIndex) |
|
263 { |
|
264 boolean result = false; |
|
265 int closingIndex = replaced.indexOf("]", maxIndex + pattern.length()); |
|
266 |
|
267 // Check format [N...N] comply. If not skip. |
|
268 if (closingIndex > 0) |
|
269 { |
|
270 try |
|
271 { |
|
272 int maxLen = Integer.parseInt(replaced.substring( |
|
273 maxIndex + pattern.length() + 1, closingIndex)); |
|
274 |
|
275 if (maxLen > replacement.length()) |
|
276 { |
|
277 maxLen = replacement.length(); |
|
278 } |
|
279 |
|
280 replaced = replaced.substring(0, maxIndex) + |
|
281 replacement.substring(0, maxLen) + |
|
282 replaced.substring(closingIndex + 1); |
|
283 result = true; |
|
284 } |
|
285 catch (NumberFormatException nfe) |
|
286 { |
|
287 Logger.WLOG(Logger.EUtils, |
|
288 "FormatterAvkon: Replace with max failed to " + |
|
289 "invalid replacement amount"); |
|
290 } |
|
291 } |
|
292 return result; |
|
293 } |
|
294 |
|
295 /** |
|
296 * Finds next %[N...N]n pattern from the replaced field. |
|
297 * Returns found pattern, or null if no pattern was found. |
|
298 */ |
|
299 private String findMaxPattern() |
|
300 { |
|
301 String result = null; |
|
302 String startPattern = "%["; |
|
303 String endPattern = "]" + nextIndex; |
|
304 int startIndex = replaced.indexOf(startPattern); |
|
305 if (startIndex >= 0) |
|
306 { |
|
307 int endIndex = replaced.indexOf(endPattern, startIndex); |
|
308 if (endIndex >= 0) |
|
309 { |
|
310 result = replaced.substring( |
|
311 startIndex, endIndex + endPattern.length()); |
|
312 } |
|
313 } |
|
314 return result; |
|
315 } |
|
316 |
|
317 /*** ----------------------------- NATIVE ----------------------------- */ |
|
318 |
|
319 /** |
|
320 * Format integer to current locale. |
|
321 * |
|
322 * @param number to be formatted. |
|
323 * @return number formatted as current locale String. |
|
324 */ |
|
325 private static native String _formatInteger(int number); |
|
326 |
|
327 /** |
|
328 * Format date to current locale. |
|
329 * |
|
330 * @param timeInMilliSecs to be formatted. |
|
331 * @param format Date format. |
|
332 * @return date formatted as current locale String. |
|
333 * |
|
334 */ |
|
335 private static native String _formatDate(long timeInMilliSecs); |
|
336 |
|
337 /** |
|
338 * Applies conversion from european digits into arabic-indic digits |
|
339 * based on existing language settings |
|
340 * |
|
341 * @param str String which might contain european digits |
|
342 * @return A string identical with the provided string but with the |
|
343 * european digits (if any) converted to arabic-indic digits |
|
344 */ |
|
345 private static native String _formatDigits(String str); |
|
346 } |