cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jer...@apache.org
Subject svn commit: r694362 - /cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/
Date Thu, 11 Sep 2008 17:12:58 GMT
Author: jeremy
Date: Thu Sep 11 10:12:55 2008
New Revision: 694362

URL: http://svn.apache.org/viewvc?rev=694362&view=rev
Log:
adjust formatting convertors to use icu4j-3.8.1, in order to get proper synchronisation of
L10N formats between CForms and Dojo 1.1.1. Output convertor details to generation pipeline,
so client-side validation on data-type may occur.

Modified:
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertor.java
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertorBuilder.java
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDoubleConvertor.java
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingIntegerConvertor.java
    cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingLongConvertor.java

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertor.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertor.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertor.java
Thu Sep 11 10:12:55 2008
@@ -18,10 +18,13 @@
 
 import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
+import org.apache.cocoon.forms.FormsConstants;
+import org.apache.cocoon.xml.AttributesImpl;
 
+import com.ibm.icu.util.Currency;
 import java.util.Locale;
-import java.text.DecimalFormat;
-import java.text.NumberFormat;
+import com.ibm.icu.text.DecimalFormat;
+import com.ibm.icu.text.NumberFormat;
 import java.text.ParseException;
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -43,11 +46,12 @@
  */
 public class FormattingDecimalConvertor implements Convertor {
     private int variant;
+    private Currency currency = Currency.getInstance(Locale.getDefault()); // lets get some
consistency here.
     /** 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;
@@ -63,19 +67,24 @@
     }
 
     public ConversionResult convertFromString(String value, Locale locale, Convertor.FormatCache
formatCache) {
+        
         // Some locales (e.g. "fr") produce non-breaking spaces sent back as space by the
browser
-        value = value.replace(' ', (char)160);
+        //value = value.replace(' ', (char)160); [JQ] This was breaking with the Dojo upgrade

         DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
-
+        //decimalFormat.setParseBigDecimal(true);
         Number decimalValue;
         try {
             decimalValue = decimalFormat.parse(value);
         } catch (ParseException e) {
+            System.out.println("Exception converting: " + value + " : " + e.getMessage());
             return ConversionResult.create("decimal");
         }
 
         if (decimalValue instanceof BigDecimal) {
             // no need for conversion
+        } else if (decimalValue instanceof com.ibm.icu.math.BigDecimal) {
+            // no need for conversion
+            decimalValue = ((com.ibm.icu.math.BigDecimal)decimalValue).toBigDecimal();
         } else if (decimalValue instanceof Integer) {
             decimalValue = new BigDecimal(decimalValue.intValue());
         } else if (decimalValue instanceof Long) {
@@ -122,12 +131,17 @@
                 break;
             case CURRENCY:
                 decimalFormat = (DecimalFormat)NumberFormat.getCurrencyInstance(locale);
+                decimalFormat.setCurrency(this.currency);
+                int frac = this.currency.getDefaultFractionDigits();
+                decimalFormat.setMinimumFractionDigits(frac);
+                decimalFormat.setMaximumFractionDigits(frac);
+                
                 break;
             case PERCENT:
                 decimalFormat = (DecimalFormat)NumberFormat.getPercentInstance(locale);
                 break;
         }
-
+        
         String pattern = (String)localizedPatterns.get(locale);
 
         if (pattern != null) {
@@ -144,6 +158,35 @@
         this.variant = variant;
     }
 
+    /** 
+     *   Set the currency of the Convertor
+     *   @param currencyCode String the ISO 4217 code of the currency (http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/currency_codes/currency_codes_list-1.htm)
+     */
+    public void setCurrency(String currencyCode) throws IllegalArgumentException, NullPointerException
{
+        Currency currency = Currency.getInstance(currencyCode);
+        this.setCurrency(currency);
+    }
+    
+    /** 
+     *   Set the currency of the Convertor
+     *   @param currency java.util.Currency the currency to display for this field
+     */
+    public void setCurrency(Currency currency) throws IllegalArgumentException {
+        if (this.variant != CURRENCY)
+            throw new IllegalArgumentException("Cannot set currency on this variant of convertor.");
+        this.currency = currency;
+    }
+
+    /** 
+     *   Get the L10N name of the Currency of this Convertor
+     */
+    public String getCurrencyName(Locale locale) throws IllegalArgumentException {
+        if (this.variant != CURRENCY)
+            throw new IllegalArgumentException("Cannot set currency on this variant of convertor.");
+            
+        return this.currency.getName(locale, Currency.LONG_NAME, new boolean[1]);
+    }
+
     public void addFormattingPattern(Locale locale, String pattern) {
         localizedPatterns.put(locale, pattern);
     }
@@ -156,7 +199,31 @@
         return java.math.BigDecimal.class;
     }
 
+    private static final String CONVERTOR_EL = "convertor";
+
     public void generateSaxFragment(ContentHandler contentHandler, Locale locale) throws
SAXException {
-        // intentionally empty
+        String pattern = (String)localizedPatterns.get(locale);
+        if (pattern == null) pattern = nonLocalizedPattern;
+        String variantString = null; // we have to convert the variant back into a string
+        String currencyCode = null; // there may be a currency code
+        String currencySymbol = null; // there may be a currency symbol
+        if (this.variant == INTEGER) variantString = "integer";
+        if (this.variant == NUMBER) variantString = "number";
+        if (this.variant == CURRENCY) { 
+            variantString = "currency"; 
+            currencyCode = this.currency.getCurrencyCode();
+            currencySymbol = this.currency.getSymbol(locale);
+        }
+        if (this.variant == PERCENT) variantString = "percent";
+
+        if (pattern != null || variantString != null) {
+            AttributesImpl attrs = new AttributesImpl();
+            if (pattern != null) attrs.addCDATAAttribute("pattern", pattern);
+            if (variantString != null) attrs.addCDATAAttribute("variant", variantString);
+            if (currencyCode != null) attrs.addCDATAAttribute("currency", currencyCode);
+            if (currencySymbol != null) attrs.addCDATAAttribute("symbol", currencySymbol);
+            contentHandler.startElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON
+ CONVERTOR_EL, attrs);
+            contentHandler.endElement(FormsConstants.INSTANCE_NS, CONVERTOR_EL, FormsConstants.INSTANCE_PREFIX_COLON
+ CONVERTOR_EL);
+        }
     }
 }

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertorBuilder.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertorBuilder.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertorBuilder.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDecimalConvertorBuilder.java
Thu Sep 11 10:12:55 2008
@@ -37,16 +37,20 @@
 
         String variant = configElement.getAttribute("variant");
         if (!variant.equals("")) {
-            if (variant.equals("integer"))
+            if (variant.equals("integer")) {
                 convertor.setVariant(FormattingDecimalConvertor.INTEGER);
-            else if (variant.equals("number"))
+            } else if (variant.equals("number")) {
                 convertor.setVariant(FormattingDecimalConvertor.NUMBER);
-            else if (variant.equals("percent"))
+            } else if (variant.equals("percent")) {
                 convertor.setVariant(FormattingDecimalConvertor.PERCENT);
-            else if (variant.equals("currency"))
+            } else if (variant.equals("currency")) {
                 convertor.setVariant(FormattingDecimalConvertor.CURRENCY);
-            else
+                // NB. if currency is not set manually, the currency for the locale will
be used
+                String currencyCode = configElement.getAttribute("currency");
+                if (!currencyCode.equals("")) convertor.setCurrency(currencyCode);
+            } else {
                 throw new Exception("Invalid value \"" + variant + "\" for variant attribute
at " + DomHelper.getLocation(configElement));
+            }
         }
 
         Element patternsEl = DomHelper.getChildElement(configElement, FormsConstants.DEFINITION_NS,
"patterns", false);

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDoubleConvertor.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDoubleConvertor.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDoubleConvertor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingDoubleConvertor.java
Thu Sep 11 10:12:55 2008
@@ -17,7 +17,7 @@
 package org.apache.cocoon.forms.datatype.convertor;
 
 import java.util.Locale;
-import java.text.DecimalFormat;
+import com.ibm.icu.text.DecimalFormat;
 import java.text.ParseException;
 
 /**

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingFloatConvertor.java
Thu Sep 11 10:12:55 2008
@@ -17,7 +17,7 @@
 package org.apache.cocoon.forms.datatype.convertor;
 
 import java.util.Locale;
-import java.text.DecimalFormat;
+import com.ibm.icu.text.DecimalFormat;
 import java.text.ParseException;
 
 /**

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingIntegerConvertor.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingIntegerConvertor.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingIntegerConvertor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingIntegerConvertor.java
Thu Sep 11 10:12:55 2008
@@ -17,7 +17,7 @@
 package org.apache.cocoon.forms.datatype.convertor;
 
 import java.util.Locale;
-import java.text.DecimalFormat;
+import com.ibm.icu.text.DecimalFormat;
 import java.text.ParseException;
 
 /**

Modified: cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingLongConvertor.java
URL: http://svn.apache.org/viewvc/cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingLongConvertor.java?rev=694362&r1=694361&r2=694362&view=diff
==============================================================================
--- cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingLongConvertor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X-dojo1_1/src/blocks/forms/java/org/apache/cocoon/forms/datatype/convertor/FormattingLongConvertor.java
Thu Sep 11 10:12:55 2008
@@ -17,7 +17,7 @@
 package org.apache.cocoon.forms.datatype.convertor;
 
 import java.util.Locale;
-import java.text.DecimalFormat;
+import com.ibm.icu.text.DecimalFormat;
 import java.text.ParseException;
 
 /**



Mime
View raw message