You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2009/09/11 12:00:06 UTC
svn commit: r813750 - in
/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy:
./ csv/ format/ kvp/
Author: cmoulliard
Date: Fri Sep 11 10:00:06 2009
New Revision: 813750
URL: http://svn.apache.org/viewvc?rev=813750&view=rev
Log:
Return line number where the error occurs in the exception throwed, test if the format of the string is >= date pattern to avoid to accept 20090911-10:33:00 when the pattern is yyyyMMdd
Modified:
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
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/BindyFactory.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/csv/BindyCsvDataFormat.java
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/format/DatePatternFormat.java
camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java
Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java?rev=813750&r1=813749&r2=813750&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java (original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyAbstractFactory.java Fri Sep 11 10:00:06 2009
@@ -100,7 +100,7 @@
*/
public abstract void initAnnotedFields() throws Exception;
- public abstract void bind(List<String> data, Map<String, Object> model) throws Exception;
+ public abstract void bind(List<String> data, Map<String, Object> model, int line) throws Exception;
public abstract String unbind(Map<String, Object> model) throws Exception;
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=813750&r1=813749&r2=813750&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 Fri Sep 11 10:00:06 2009
@@ -24,12 +24,12 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
-import java.util.jar.JarException;
import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
import org.apache.camel.dataformat.bindy.annotation.DataField;
import org.apache.camel.dataformat.bindy.annotation.Link;
import org.apache.camel.dataformat.bindy.annotation.Section;
+import org.apache.camel.dataformat.bindy.format.FormatException;
import org.apache.camel.dataformat.bindy.util.Converter;
import org.apache.camel.spi.PackageScanClassResolver;
import org.apache.camel.util.ObjectHelper;
@@ -136,7 +136,7 @@
}
}
- public void bind(List<String> tokens, Map<String, Object> model) throws Exception {
+ public void bind(List<String> tokens, Map<String, Object> model, int line) throws Exception {
int pos = 0;
int counterMandatoryFields = 0;
@@ -145,7 +145,7 @@
// Get DataField from model
DataField dataField = dataFields.get(pos);
- ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field : " + data);
+ ObjectHelper.notNull(dataField, "No position " + pos + " defined for the field : " + data + ", line nber : " + line);
if (dataField.required()) {
// Increment counter of mandatory fields
@@ -155,7 +155,7 @@
// This is not possible for mandatory fields
if (data.equals("")) {
throw new IllegalArgumentException("The mandatory field defined at the position " + pos
- + " is empty !");
+ + " is empty for the line nber : " + line);
}
}
@@ -184,8 +184,10 @@
if (!data.equals("")) {
try {
value = format.parse(data);
+ } catch (FormatException ie) {
+ throw new IllegalArgumentException( ie.getMessage() + ", position : " + pos + ", line nber : " + line, ie);
} catch (Exception e) {
- throw new IllegalArgumentException("Parsing error detected for field defined at the position : " + pos, e);
+ throw new IllegalArgumentException("Parsing error detected for field defined at the position : " + pos + ", line nber : " + line, e);
}
} else {
value = getDefaultValueforPrimitive(field.getType());
@@ -203,11 +205,11 @@
if (pos < totalFields) {
- throw new IllegalArgumentException("Some fields are missing (optional or mandatory) !!");
+ throw new IllegalArgumentException("Some fields are missing (optional or mandatory), line nber : " + line);
}
if (counterMandatoryFields < numberMandatoryFields) {
- throw new IllegalArgumentException("Some mandatory fields are missing !!");
+ throw new IllegalArgumentException("Some mandatory fields are missing, line nber : " + line);
}
}
Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java?rev=813750&r1=813749&r2=813750&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java (original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFactory.java Fri Sep 11 10:00:06 2009
@@ -41,9 +41,10 @@
* @param model Map<String, object> is a collection of objects used to bind
* data. String is the the key name of the class link to POJO
* objects
+ * @param line is the position of the record into the file
* @throws Exception can be thrown
*/
- void bind(List<String> data, Map<String, Object> model) throws Exception;
+ void bind(List<String> data, Map<String, Object> model, int line) throws Exception;
/**
* The unbind is used to transform the content of the classes model objects
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=813750&r1=813749&r2=813750&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 Fri Sep 11 10:00:06 2009
@@ -114,7 +114,7 @@
}
}
- public void bind(List<String> data, Map<String, Object> model) throws Exception {
+ public void bind(List<String> data, Map<String, Object> model, int line) throws Exception {
int pos = 0;
@@ -140,7 +140,7 @@
}
KeyValuePairField keyValuePairField = keyValuePairFields.get(tag);
- ObjectHelper.notNull(keyValuePairField, "No tag defined for the field : " + tag);
+ ObjectHelper.notNull(keyValuePairField, "No tag defined for the field : " + tag + ", line nber : " + line);
Field field = annotedFields.get(tag);
field.setAccessible(true);
@@ -165,9 +165,8 @@
try {
value = format.parse(keyValue);
} catch (Exception e) {
- throw new IllegalArgumentException(
- "Parsing error detected for field defined at the tag : "
- + tag, e);
+ throw new IllegalArgumentException("Parsing error detected for field defined at the tag : " + tag
+ + ", line nber : " + line, e);
}
field.set(modelField, value);
@@ -183,8 +182,7 @@
StringBuilder builder = new StringBuilder();
- Map<Integer, KeyValuePairField> keyValuePairFieldsSorted = new TreeMap<Integer, KeyValuePairField>(
- keyValuePairFields);
+ Map<Integer, KeyValuePairField> keyValuePairFieldsSorted = new TreeMap<Integer, KeyValuePairField>( keyValuePairFields );
Iterator<Integer> it = keyValuePairFieldsSorted.keySet().iterator();
// Map containing the OUT position of the field
Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java?rev=813750&r1=813749&r2=813750&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java (original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/csv/BindyCsvDataFormat.java Fri Sep 11 10:00:06 2009
@@ -93,6 +93,7 @@
ObjectHelper.notEmpty(separator, "The separator has not been defined in the annotation @CsvRecord or not instantiated during initModel.");
int count = 0;
+
try {
// If the first line of the CSV file contains columns name, then we skip this line
@@ -109,10 +110,9 @@
// skip if line is empty
continue;
}
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Counter " + count++ + " : content : " + line);
- }
+
+ // Increment counter
+ count++;
// Create POJO where CSV data will be stored
model = factory.factory();
@@ -133,7 +133,7 @@
}
// Bind data from CSV record with model classes
- factory.bind(result, model);
+ factory.bind(result, model, count);
// Link objects together
factory.link(model);
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=813750&r1=813749&r2=813750&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 Fri Sep 11 10:00:06 2009
@@ -49,16 +49,25 @@
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);
- date = df.parse(string);
-
- return date;
+
+ // Check length of the string with date pattern
+ // To avoid to parse a string date : 20090901-10:32:30 when
+ // the pattern is yyyyMMdd
+
+ if ( string.length() <= this.pattern.length() ) {
+
+ // Force the parser to be strict in the syntax of the date to be
+ // converted
+ df.setLenient(false);
+ date = df.parse(string);
+
+ return date;
+
+ } else {
+ throw new FormatException("Date provided does not fit the pattern defined");
+ }
}
Modified: camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java?rev=813750&r1=813749&r2=813750&view=diff
==============================================================================
--- camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java (original)
+++ camel/trunk/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/kvp/BindyKeyValuePairDataFormat.java Fri Sep 11 10:00:06 2009
@@ -94,41 +94,53 @@
while (scanner.hasNextLine()) {
- // Read the line
- String line = scanner.nextLine().trim();
+ // Read the line
+ String line = scanner.nextLine().trim();
- if (ObjectHelper.isEmpty(line)) {
- // skip if line is empty
- continue;
- }
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Counter " + count++ + " : content : " + line);
- }
-
- // Create POJO
- model = factory.factory();
-
- // Split the message according to the pair separator defined in
- // annotated class @Message
- List<String> result = Arrays.asList(line.split(separator));
-
- // Bind data from message with model classes
- factory.bind(result, model);
-
- // Link objects together
- factory.link(model);
-
- // Add objects graph to the list
- models.add(model);
-
- if (LOG.isDebugEnabled()) {
- LOG.debug("Graph of objects created : " + model);
- }
+ if (ObjectHelper.isEmpty(line)) {
+ // skip if line is empty
+ continue;
+ }
- }
+ // Increment counter
+ count++;
+
+ // Create POJO
+ model = factory.factory();
+
+ // Split the message according to the pair separator defined in
+ // annotated class @Message
+ List<String> result = Arrays.asList(line.split(separator));
+
+ if (result.size() == 0 || result.isEmpty()) {
+ throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
+ }
+
+ if (result.size() > 0) {
+
+ // Bind data from message with model classes
+ factory.bind(result, model, count);
- return models;
+ // Link objects together
+ factory.link(model);
+
+ // Add objects graph to the list
+ models.add(model);
+
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Graph of objects created : " + model);
+ }
+ }
+
+ }
+
+ // Test if models list is empty or not
+ // If this is the case (correspond to an empty stream, ...)
+ if (models.size() == 0) {
+ throw new java.lang.IllegalArgumentException("No records have been defined in the KVP !");
+ } else {
+ return models;
+ }
} finally {
scanner.close();