Author: cmoulliard
Date: Wed Sep  9 13:36:30 2009
New Revision: 812954

URL: http://svn.apache.org/viewvc?rev=812954&view=rev
Log:
Generate error IllegalArgumentException when parsing/formating error occurs 
(for parsing, the field/tag position is returned while for the value of the 
object)

Modified:
    
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
    
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
    
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java
    
camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java

Modified: 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java?rev=812954&r1=812953&r2=812954&view=diff
==============================================================================
--- 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
 (original)
+++ 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyCsvFactory.java
 Wed Sep  9 13:36:30 2009
@@ -182,7 +182,11 @@
             Object value = null;
             
             if (!data.equals("")) {
-                value = format.parse(data);
+               try {
+                       value = format.parse(data);
+               } catch (Exception e) {
+                       throw new IllegalArgumentException("Parsing error 
detected for field defined at the position : " + pos, e);
+               }
             } else {
                 value = getDefaultValueforPrimitive(field.getType());
             }
@@ -267,12 +271,13 @@
                         LOG.debug("Key generated : " + 
String.valueOf(keyGenerated) + ", for section : " + key1);
                     }                    
                     
-                    // Get field value
-                    //Object value = field.get(modelField);
-                    
                     if (value != null) {
                         // Format field value
-                        strValue = format.format(value);
+                       try {
+                               strValue = format.format(value);
+                       } catch (Exception e) {
+                               throw new IllegalArgumentException("Formating 
error detected for the value : " + value, e);
+                       }
                     } 
                     
                     // Add the content to the TreeMap according to the
@@ -292,7 +297,11 @@
                     if (value != null) {
 
                         // Format field value
-                        strValue = format.format(value);
+                       try {
+                               strValue = format.format(value);
+                       } catch (Exception e) {
+                               throw new IllegalArgumentException("Formating 
error detected for the value : " + value, e);
+                       }
                         
                     }
                     

Modified: 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java?rev=812954&r1=812953&r2=812954&view=diff
==============================================================================
--- 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
 (original)
+++ 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyKeyValuePairFactory.java
 Wed Sep  9 13:36:30 2009
@@ -118,57 +118,63 @@
 
         int pos = 0;
 
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("Data : " + data);
-        }
+               if (LOG.isDebugEnabled()) {
+                       LOG.debug("Data : " + data);
+               }
 
-        while (pos < data.size()) {
+               while (pos < data.size()) {
 
-            if (!data.get(pos).equals("")) {
+                       if (!data.get(pos).equals("")) {
 
-                // Separate the key from its value
-                // e.g 8=FIX 4.1 --> key = 8 and Value = FIX 4.1
-                ObjectHelper.notNull(this.keyValuePairSeparator,
-                                     "Key Value Pair not defined in the 
@Message annotation");
-                String[] keyValuePair = 
data.get(pos).split(this.getKeyValuePairSeparator());
+                               // Separate the key from its value
+                               // e.g 8=FIX 4.1 --> key = 8 and Value = FIX 4.1
+                               ObjectHelper.notNull(this.keyValuePairSeparator,
+                                               "Key Value Pair not defined in 
the @Message annotation");
+                               String[] keyValuePair = 
data.get(pos).split(this.getKeyValuePairSeparator());
 
-                int tag = Integer.parseInt(keyValuePair[0]);
-                String keyValue = keyValuePair[1];
+                               int tag = Integer.parseInt(keyValuePair[0]);
+                               String keyValue = keyValuePair[1];
 
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Key : " + tag + ", value : " + keyValue);
-                }
+                               if (LOG.isDebugEnabled()) {
+                                       LOG.debug("Key : " + tag + ", value : " 
+ keyValue);
+                               }
 
-                KeyValuePairField keyValuePairField = 
keyValuePairFields.get(tag);
-                ObjectHelper.notNull(keyValuePairField, "No tag defined for 
the field : " + tag);
+                               KeyValuePairField keyValuePairField = 
keyValuePairFields.get(tag);
+                               ObjectHelper.notNull(keyValuePairField, "No tag 
defined for the field : " + tag);
 
-                Field field = annotedFields.get(tag);
-                field.setAccessible(true);
+                               Field field = annotedFields.get(tag);
+                               field.setAccessible(true);
 
-                if (LOG.isDebugEnabled()) {
-                    LOG.debug("Tag : " + tag + ", Data : " + keyValue + ", 
Field type : " + field.getType());
-                }
+                               if (LOG.isDebugEnabled()) {
+                                       LOG.debug("Tag : " + tag + ", Data : " 
+ keyValue + ", Field type : " + field.getType());
+                               }
 
-                Format<?> format;
+                               Format<?> format;
 
-                // Get pattern defined for the field
-                String pattern = keyValuePairField.pattern();
+                               // Get pattern defined for the field
+                               String pattern = keyValuePairField.pattern();
 
-                // Create format object to format the field
-                format = FormatFactory.getFormat(field.getType(), pattern, 
keyValuePairField.precision());
+                               // Create format object to format the field
+                               format = 
FormatFactory.getFormat(field.getType(), pattern, 
keyValuePairField.precision());
 
-                // field object to be set
-                Object modelField = 
model.get(field.getDeclaringClass().getName());
+                               // field object to be set
+                               Object modelField = 
model.get(field.getDeclaringClass().getName());
 
-                // format the value of the key received
-                Object value = format.parse(keyValue);
+                               // format the value of the key received
+                               Object value;
+                               try {
+                                       value = format.parse(keyValue);
+                               } catch (Exception e) {
+                                       throw new 
IllegalArgumentException("Parsing error detected for field defined at the tag : 
"
+                                                       + tag, e);
+                               }
 
-                field.set(modelField, value);
+                               field.set(modelField, value);
 
-            }
+                       }
 
-            pos++;
-        }
+                       pos++;
+               }
 
     }
 
@@ -242,7 +248,13 @@
                     if (keyValue != null) {
 
                         // Format field value
-                        String valueFormated = format.format(keyValue);
+                        String valueFormated;
+                        
+                        try {
+                               valueFormated = format.format(keyValue);
+                        } catch (Exception e) {
+                               throw new IllegalArgumentException("Formating 
error detected for the tag : " + keyValuePairField.tag(), e);
+                        }
 
                         // Create the key value string
                         String value = keyValuePairField.tag() + 
this.getKeyValuePairSeparator()
@@ -262,7 +274,13 @@
                     if (keyValue != null) {
 
                         // Format field value
-                        String valueFormated = format.format(keyValue);
+                        String valueFormated;
+                        
+                        try {
+                               valueFormated = format.format(keyValue);
+                        } catch (Exception e) {
+                               throw new IllegalArgumentException("Formating 
error detected for the tag : " + keyValuePairField.tag(), e);
+                        }
 
                         // Create the key value string
                         String value = keyValuePairField.tag() + 
this.getKeyValuePairSeparator()

Modified: 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java?rev=812954&r1=812953&r2=812954&view=diff
==============================================================================
--- 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java
 (original)
+++ 
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java
 Wed Sep  9 13:36:30 2009
@@ -20,11 +20,16 @@
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.Locale;
 
 import org.apache.camel.dataformat.bindy.PatternFormat;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 public class DatePatternFormat implements PatternFormat<Date> {
+       
+       private static final transient Log LOG = 
LogFactory.getLog(DatePatternFormat.class);
 
     private String pattern;
 
@@ -44,23 +49,20 @@
 
        Date date;
         DateFormat df = this.getDateFormat();
+        LOG.info("Pattern : " + this.pattern);
 
         ObjectHelper.notNull(this.pattern, "pattern");
         
         // Force the parser to be strict in the syntax of the date to be 
converted
         df.setLenient(false);
-        
-        try {
-               date = df.parse(string);
-               return date;
-        } catch (ParseException pe) {
-            throw new IllegalArgumentException("Unparseable date : " + string 
);
-        }
+               date = df.parse(string);
+
+               return date;
          
     }
 
     protected java.text.DateFormat getDateFormat() {
-        return new SimpleDateFormat(this.pattern);
+        return new SimpleDateFormat(this.pattern, Locale.FRANCE);
     }
 
     public String getPattern() {

Modified: 
camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java?rev=812954&r1=812953&r2=812954&view=diff
==============================================================================
--- 
camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java
 (original)
+++ 
camel/trunk/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/csv/BindySimpleCsvMarshallTest.java
 Wed Sep  9 13:36:30 2009
@@ -42,7 +42,7 @@
 public class BindySimpleCsvMarshallTest extends 
AbstractJUnit4SpringContextTests {
 
     private List<Map<String, Object>> models = new ArrayList<Map<String, 
Object>>();
-    private String result = 
"1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,450.45,EUR,14-01-2009\r\n";
+    private String result = 
"1,B2,Keira,Knightley,ISIN,XX23456789,BUY,Share,400.25,EUR,14-01-2009\r\n";
 
     @Produce(uri = "direct:start")
     private ProducerTemplate template;
@@ -68,7 +68,7 @@
         order.setClientNr("B2");
         order.setFirstName("Keira");
         order.setLastName("Knightley");
-        order.setAmount(new BigDecimal("450.45").setScale(2));
+        order.setAmount(new BigDecimal("400.25"));
         order.setInstrumentCode("ISIN");
         order.setInstrumentNumber("XX23456789");
         order.setInstrumentType("Share");


Reply via email to