BrightSide Workbench Full Report + Source Code
Localizer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
20 import java.text.DecimalFormat;
21 import java.text.NumberFormat;
22 import java.text.SimpleDateFormat;
23 import org.turro.string.ObjectString;
24 import org.turro.elephant.context.IConstructor;
25 import org.turro.i18n.I_;
26 
31 public class Localizer {
33 
36  this.constructor = constructor;
37  }
38 
39  public String parseString(
40  String type,
41  String inFormat,
42  String outFormat,
43  String value) {
44  try {
45  String format = I_.compatibility().get("fmt." + inFormat);
46  if(format == null) format = inFormat;
47  if("date".equals(type)) {
48  SimpleDateFormat sdfi = new SimpleDateFormat(
49  (format == null ? ObjectString.DEFAULT_DATE : format)
50  );
51  return parseValue(type, outFormat, sdfi.parse(value));
52  }
53  else if("number".equals(type)) {
54  Number number = (Number)ObjectString.parseNativeString(value, Double.class, true);
55  return parseValue(type, outFormat, number);
56  }
57  else if("bytes".equals(type)) {
58  Long bytes = (Long)ObjectString.parseNativeString(value, Long.class, true);
59  return parseValue(type, outFormat, bytes);
60  }
61  else
62  return value;
63  } catch(Exception ex) {
64  return "";
65  }
66  }
67 
68  public Object parseString(
69  String type,
70  String inFormat,
71  String value) {
72  try {
73  String format = I_.compatibility().get("fmt." + inFormat);
74  if(format == null) format = inFormat;
75  if("date".equals(type)) {
76  SimpleDateFormat sdfi = new SimpleDateFormat(
77  (format == null ? ObjectString.DEFAULT_DATE : format)
78  );
79  return sdfi.parse(value);
80  }
81  else if("number".equals(type)) {
82  Number number = (Number)ObjectString.parseNativeString(value, Double.class, true);
83  return number;
84  }
85  else if("bytes".equals(type)) {
86  Long bytes = (Long)ObjectString.parseNativeString(value, Long.class, true);
87  return bytes;
88  }
89  else
90  return value;
91  } catch(Exception ex) {
92  return null;
93  }
94  }
95 
96  public String parseValue(
97  String type,
98  String outFormat,
99  Object value) {
100  try {
101  if("date".equals(type)) {
102  outFormat = I_.compatibility().get("fmt." + outFormat);
103  SimpleDateFormat sdfo = new SimpleDateFormat(
104  (outFormat == null ? ObjectString.DATE_PATTERN : outFormat),
105  new java.util.Locale(I_.compatibility().get("fmt.locale"))
106  );
107  return sdfo.format(value);
108  }
109  else if("number".equals(type)) {
110  outFormat = I_.compatibility().get("fmt." + outFormat);
111  NumberFormat dfo = NumberFormat.getInstance(
112  new java.util.Locale(I_.compatibility().get("fmt.locale"))
113  );
114  if(dfo instanceof DecimalFormat) {
115  ((DecimalFormat)dfo).applyPattern(
116  (outFormat == null ? ObjectString.DEFAULT_NUMBER : outFormat)
117  );
118  }
119  return dfo.format(value);
120  }
121  else if("bytes".equals(type)) {
122  return new org.turro.formatter.BytesFormatter(((Long)value).longValue()).toString();
123  }
124  else
125  return value.toString();
126  } catch(Exception ex) {
127  return "";
128  }
129  }
130 
131 }
Localizer(IConstructor constructor)
Definition: Localizer.java:35
Object parseString(String type, String inFormat, String value)
Definition: Localizer.java:68
String parseValue(String type, String outFormat, Object value)
Definition: Localizer.java:96
String parseString(String type, String inFormat, String outFormat, String value)
Definition: Localizer.java:39
static I18nCompatibilityWrapper compatibility()
Definition: I_.java:109