Author: simoneg
Date: Fri Jun 15 00:50:12 2012
New Revision: 1350433
URL: http://svn.apache.org/viewvc?rev=1350433&view=rev
Log:
Added float formatter
Added:
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java
(with props)
Modified:
labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/FloatConverter.java
Modified:
labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/FloatConverter.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/FloatConverter.java?rev=1350433&r1=1350432&r2=1350433&view=diff
==============================================================================
---
labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/FloatConverter.java
(original)
+++
labs/magma/trunk/foundation-beans/src/main/java/org/apache/magma/conversion/string/FloatConverter.java
Fri Jun 15 00:50:12 2012
@@ -36,7 +36,7 @@ public class FloatConverter extends Prim
/**
* The error used when conversion is not possible.
*/
- private LocalizableString parsingError = new LocalizableString("Not a
valid number");
+ protected LocalizableString parsingError = new LocalizableString("Not a
valid number");
public FloatConverter() {
super(Float.class);
Added:
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java
URL:
http://svn.apache.org/viewvc/labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java?rev=1350433&view=auto
==============================================================================
---
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java
(added)
+++
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java
Fri Jun 15 00:50:12 2012
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.i18n.formatters;
+
+import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.util.Currency;
+import java.util.Locale;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.conversion.ConversionException;
+import org.apache.magma.conversion.string.DoubleConverter;
+import org.apache.magma.conversion.string.FloatConverter;
+import org.apache.magma.i18n.CurrentLocale;
+import org.apache.magma.i18n.Formatter;
+
+/**
+ * Formats float values.
+ *
+ * If no format string is specified, the default java formatting is used, see
{@link NumberFormat} for details.
+ *
+ * The format string can use three predefined settings :
+ * <dl>
+ * <dt>scientific</dt>
+ * <dd>Outputs and parses the scientific notation, like 0.05 being 5E-2</dd>
+ * <dt>percent</dt>
+ * <dd>Considers the float as a percentage. One is 100%, 0.5 if 50%, 2 id
200% etc..</dd>
+ * <dt>currency</dt>
+ * <dd>Considers the value as a currency. By default, the locale currency is
used (Dollars for US, Euro for European countries etc..),
+ * which is not the best thing because most probably you are referring to a
specific currency. You can specify the
+ * currency code (see the ISO-4217 for those codes) separating it with a
dash, like "currency-EUR" for Euro.
+ * Java usually appends the the currency code to the formatted string, in a
locale dependant way. For US people,
+ * 250 formatted with currency-EUR will result in 250.00 EUR, while for an
Italian it will be 250,00 €.
+ * If you don't want this to be displayed, you can append an additional
"-HIDDEN" at the end, like in
+ * "currency-EUR-HIDDEN".
+ * </dd>
+ * </dl>
+ *
+ * It is important to use the proper setting when dealing with money, cause we
don't want users to see the same quantity in
+ * euros or dollars or a local currency depending on where they are connecting
from, 200 dollars and 200 turkish liras are two very
+ * different amounts!
+ *
+ * @author Simone Gianni <[email protected]>
+ */
+public class FloatFormatter extends FloatConverter implements Formatter<Float>
{
+
+
+ private String myformat = "default";
+
+ public FloatFormatter buildDefault(Class<? extends Float> clazz) {
+ return new FloatFormatter();
+ }
+
+ public void setFormat(String format) {
+ myformat = format;
+ }
+
+ @Override
+ public String to(Float value) {
+ if (value == null) return "";
+ NumberFormat formatter = getFormatter(getFormatterName());
+ if (formatter == null) return super.to(value);
+ return formatter.format(value).trim();
+ }
+
+ @Override
+ public Float from(String value) {
+ if (value == null || value.length() == 0) return onNative ? 0f
: null;
+ String formattername = getFormatterName();
+ NumberFormat formatter = getFormatter(formattername);
+ if (formatter == null) return super.from(value);
+ try {
+ return formatter.parse(value).floatValue();
+ } catch (ParseException e) {
+ // Try to clean it up and do a row parse, just in case
of currencies
+ try {
+ if
(formattername.toLowerCase().startsWith("currency")) {
+ value = numberTrim(value);
+ if (value.length() > 0) {
+ formatter = getFormatter(null);
+ try {
+ return
formatter.parse(value).floatValue();
+ } catch (ParseException e1) {
+ throw new
ConversionException(parsingError, e1);
+ }
+ }
+ }
+ } catch (Exception e1) {}
+ throw new ConversionException(parsingError, e);
+ }
+ }
+
+ private String numberTrim(String value) {
+ value = value.trim();
+ while (value.length() > 0 &&
+ !Character.isDigit(value.charAt(0)) &&
+ value.charAt(0) != '-') value =
value.substring(1);
+ while (value.length() > 0 &&
!Character.isDigit(value.charAt(value.length() - 1))) value =
value.substring(0,value.length() - 1);
+ return value;
+ }
+
+ public String getFormatterName() {
+ String useformat = this.myformat;
+ if (this.myformat.toLowerCase().equals("default")) {
+ useformat = new
LocalizableString("float_format").toString();
+ if (useformat.toString().endsWith("float_format")) {
+ return null;
+ }
+ }
+ return useformat;
+ }
+
+ public NumberFormat getFormatter(String useformat) {
+ Locale locale = CurrentLocale.getLocale();
+ if (useformat == null) {
+ return NumberFormat.getInstance(locale);
+ }
+ if (useformat.equalsIgnoreCase("scientific")) {
+ return null;
+ }
+ if (useformat.toLowerCase().startsWith("currency")) {
+ NumberFormat instance =
NumberFormat.getCurrencyInstance(locale);
+ if (instance instanceof DecimalFormat) {
+ DecimalFormat df = (DecimalFormat) instance;
+ String currency =
useformat.substring(9).toUpperCase();
+ boolean hidden = currency.endsWith("-HIDDEN");
+ if (hidden) currency = currency.substring(0,
currency.length() - 7);
+ df.setCurrency(Currency.getInstance(currency));
+ if (hidden) {
+ DecimalFormatSymbols symbols =
df.getDecimalFormatSymbols();
+ symbols.setCurrencySymbol("");
+ df.setDecimalFormatSymbols(symbols);
+ }
+ }
+ return instance;
+ } else if (useformat.toLowerCase().startsWith("percent")) {
+ NumberFormat instance =
NumberFormat.getPercentInstance(locale);
+ if (useformat.length() > 7) {
+ if (instance instanceof DecimalFormat) {
+ DecimalFormat df = (DecimalFormat)
instance;
+
df.setMaximumFractionDigits(Integer.valueOf(useformat.substring(8)));
+ }
+ }
+ return instance;
+ } else {
+ NumberFormat instance =
NumberFormat.getInstance(locale);
+ if (instance instanceof DecimalFormat) {
+ DecimalFormat df = (DecimalFormat) instance;
+ df.applyPattern(useformat);
+ }
+ return instance;
+ }
+ }
+
+
+
+ public String getPattern() {
+ NumberFormat formatter = getFormatter(getFormatterName());
+ if (formatter instanceof DecimalFormat) {
+ return ((DecimalFormat)formatter).toPattern();
+ }
+ return null;
+ }
+
+}
Propchange:
labs/magma/trunk/foundation-i18n/src/main/java/org/apache/magma/i18n/formatters/FloatFormatter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]