You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by br...@apache.org on 2004/03/18 12:44:59 UTC
cvs commit: cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor FormattingFloatConvertor.java FormattingFloatConvertorBuilder.java
bruno 2004/03/18 03:44:59
Modified: src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor
FormattingFloatConvertor.java
FormattingFloatConvertorBuilder.java
Log:
Previous code was an exact duplicate of the FormattingDecimalConvertor(Builder),
and couldn't ever have worked correctly.
Revision Changes Path
1.2 +14 -109 cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java
Index: FormattingFloatConvertor.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FormattingFloatConvertor.java 9 Mar 2004 10:34:06 -0000 1.1
+++ FormattingFloatConvertor.java 18 Mar 2004 11:44:58 -0000 1.2
@@ -1,12 +1,12 @@
/*
* Copyright 1999-2004 The Apache Software Foundation.
- *
+ *
* Licensed 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.
@@ -15,135 +15,40 @@
*/
package org.apache.cocoon.forms.datatype.convertor;
-import org.outerj.i18n.I18nSupport;
import org.outerj.i18n.DecimalFormat;
import java.util.Locale;
import java.text.ParseException;
-import java.math.BigDecimal;
-import java.math.BigInteger;
/**
* A Convertor for {@link Float}s backed by the
- * {@link java.text.NumberFormat NumberFormat} class.
- *
- * <p>A <strong>formatting pattern</strong> can be used. This can either be a locale-dependent
- * or locale-independent formatting pattern. When looking up a formatting pattern, a mechansim
- * similar to resource bundle lookup is used. Suppose the locale is nl-BE, then first a formatting
- * pattern for nl-BE will be sought, then one for nl, and if that is not
- * found, finally the locale-independent formatting pattern will be used.
+ * {@link java.text.DecimalFormat DecimalFormat} class.
*
- * <p>Note: the earlier statement about the fact that this class uses java.text.DecimalFormat
- * is not entirely correct. In fact, it uses a small wrapper class that will either delegate to
- * java.text.DecimalFormat or com.ibm.icu.text.DecimalFormat. The com.ibm version will automatically
- * be used if it is present on the classpath, otherwise the java.text version will be used.
+ * <p>This class is mostly the same as the {@link FormattingDecimalConvertor},
+ * so see there for more information.
*
* @version CVS $Id$
*/
-public class FormattingFloatConvertor implements Convertor {
- private int variant;
- /** Locale-specific formatting patterns. */
- private LocaleMap localizedPatterns;
- /** Non-locale specific formatting pattern. */
- private String nonLocalizedPattern;
-
- public static final int INTEGER = 0;
- public static final int NUMBER = 1;
- public static final int CURRENCY = 2;
- public static final int PERCENT = 3;
+public class FormattingFloatConvertor extends FormattingDecimalConvertor {
public FormattingFloatConvertor() {
- this.variant = getDefaultVariant();
- this.localizedPatterns = new LocaleMap();
- }
-
- protected int getDefaultVariant() {
- return NUMBER;
+ super();
}
public Object convertFromString(String value, Locale locale, Convertor.FormatCache formatCache) {
DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
try {
Number decimalValue = decimalFormat.parse(value);
- if (decimalValue instanceof BigDecimal)
- ;
- else if (decimalValue instanceof Integer)
- decimalValue = new BigDecimal(decimalValue .intValue());
- else if (decimalValue instanceof Long)
- decimalValue = new BigDecimal(decimalValue.longValue());
- else if (decimalValue instanceof Double)
- decimalValue = new BigDecimal(decimalValue.doubleValue());
- else if (decimalValue instanceof BigInteger)
- decimalValue = new BigDecimal((BigInteger)decimalValue);
+ if (decimalValue instanceof Float)
+ return decimalValue;
else
- return null;
-
- return decimalValue;
+ return new Float(decimalValue.floatValue());
} catch (ParseException e) {
return null;
}
}
- public String convertToString(Object value, Locale locale, Convertor.FormatCache formatCache) {
- DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
- return decimalFormat.format(value);
- }
-
- protected final DecimalFormat getDecimalFormat(Locale locale, Convertor.FormatCache formatCache) {
- DecimalFormat decimalFormat = null;
- if (formatCache != null)
- decimalFormat = (DecimalFormat)formatCache.get();
- if (decimalFormat == null) {
- decimalFormat = getDecimalFormat(locale);
- if (formatCache != null)
- formatCache.store(decimalFormat);
- }
- return decimalFormat;
- }
-
- private DecimalFormat getDecimalFormat(Locale locale) {
- DecimalFormat decimalFormat = null;
-
- switch (variant) {
- case INTEGER:
- decimalFormat = I18nSupport.getInstance().getIntegerFormat(locale);
- break;
- case NUMBER:
- decimalFormat = I18nSupport.getInstance().getNumberFormat(locale);
- break;
- case CURRENCY:
- decimalFormat = I18nSupport.getInstance().getCurrencyFormat(locale);
- break;
- case PERCENT:
- decimalFormat = I18nSupport.getInstance().getPercentFormat(locale);
- break;
- }
-
- String pattern = (String)localizedPatterns.get(locale);
-
- if (pattern != null)
- decimalFormat.applyLocalizedPattern(pattern);
- else if (nonLocalizedPattern != null)
- decimalFormat.applyPattern(nonLocalizedPattern);
-
- return decimalFormat;
- }
-
- public void setVariant(int variant) {
- if (variant != INTEGER && variant != NUMBER && variant != CURRENCY && variant != PERCENT)
- throw new IllegalArgumentException("Invalid value for variant parameter.");
- this.variant = variant;
- }
-
- public void addFormattingPattern(Locale locale, String pattern) {
- localizedPatterns.put(locale, pattern);
- }
-
- public void setNonLocalizedPattern(String pattern) {
- this.nonLocalizedPattern = pattern;
- }
-
public Class getTypeClass() {
- return java.math.BigDecimal.class;
+ return Float.class;
}
-}
+}
\ No newline at end of file
1.3 +4 -44 cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertorBuilder.java
Index: FormattingFloatConvertorBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-2.1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertorBuilder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- FormattingFloatConvertorBuilder.java 9 Mar 2004 13:08:46 -0000 1.2
+++ FormattingFloatConvertorBuilder.java 18 Mar 2004 11:44:59 -0000 1.3
@@ -23,52 +23,12 @@
import java.util.Locale;
/**
- * Builds {@link FormattingDecimalConvertor}s.
+ * Builds {@link FormattingFloatConvertor}s.
*
* @version CVS $Id$
*/
-public class FormattingFloatConvertorBuilder implements ConvertorBuilder {
- public Convertor build(Element configElement) throws Exception {
- FormattingDecimalConvertor convertor = createConvertor();
-
- if (configElement == null)
- return convertor;
-
- String variant = configElement.getAttribute("variant");
- if (!variant.equals("")) {
- if (variant.equals("integer"))
- convertor.setVariant(FormattingDecimalConvertor.INTEGER);
- else if (variant.equals("number"))
- convertor.setVariant(FormattingDecimalConvertor.NUMBER);
- else if (variant.equals("percent"))
- convertor.setVariant(FormattingDecimalConvertor.PERCENT);
- else if (variant.equals("currency"))
- convertor.setVariant(FormattingDecimalConvertor.CURRENCY);
- else
- throw new Exception("Invalid value \"" + variant + "\" for variant attribute at " + DomHelper.getLocation(configElement));
- }
-
- Element patternsEl = DomHelper.getChildElement(configElement, Constants.DEFINITION_NS, "patterns", false);
- if (patternsEl != null) {
- Element patternEl[] = DomHelper.getChildElements(patternsEl, Constants.DEFINITION_NS, "pattern");
- for (int i = 0; i < patternEl.length; i++) {
- String locale = patternEl[i].getAttribute("locale");
- String pattern = DomHelper.getElementText(patternEl[i]);
- if (pattern.equals(""))
- throw new Exception("pattern element does not contain any content at " + DomHelper.getLocation(patternEl[i]));
- if (locale.equals(""))
- convertor.setNonLocalizedPattern(pattern);
- else {
- Locale loc = I18nUtils.parseLocale(locale);
- convertor.addFormattingPattern(loc, pattern);
- }
- }
- }
-
- return convertor;
- }
-
+public class FormattingFloatConvertorBuilder extends FormattingDecimalConvertorBuilder {
protected FormattingDecimalConvertor createConvertor() {
- return new FormattingDecimalConvertor();
+ return new FormattingFloatConvertor();
}
}