Author: jstrachan
Date: Tue Jul 15 11:50:28 2008
New Revision: 677005

URL: http://svn.apache.org/viewvc?rev=677005&view=rev
Log:
added support for a FlatpackDataFormat

Added:
    
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java
   (with props)
    
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java
   (with props)
    
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest.java
      - copied, changed from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedTest.java
    
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest.java
      - copied, changed from r676643, 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthTest.java
    
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest-context.xml
      - copied, changed from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedTest-context.xml
    
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest-context.xml
      - copied, changed from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthTest-context.xml

Added: 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java?rev=677005&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java
 (added)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java
 Tue Jul 15 11:50:28 2008
@@ -0,0 +1,77 @@
+/**
+ *
+ * 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.camel.component.flatpack;
+
+import net.sf.flatpack.DataSet;
+
+import java.util.AbstractList;
+import java.util.Iterator;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class DataSetList extends AbstractList {
+    private final DataSet dataSet;
+
+    public DataSetList(DataSet dataSet) {
+        this.dataSet = dataSet;
+    }
+
+    public Object get(int index) {
+        Iterator iter = iterator();
+        for (int i = 0; iter.hasNext(); i++) {
+            Object value = iter.next();
+            if (i == index) {
+                return value;
+            }
+        }
+        return null;
+    }
+
+    public int size() {
+        int answer = 0;
+        for (Iterator iter = iterator(); iter.hasNext(); ) {
+            iter.next();
+            answer++;
+        }
+        return answer;
+    }
+
+    @Override
+    public Iterator iterator() {
+        dataSet.goTop();
+        return new Iterator() {
+            public boolean hasNext() {
+                return dataSet.next();
+            }
+
+            public Object next() {
+                // TODO because of a limitation in split()
+                // we need to create an object for the current position
+                // otherwise strangeness occurs when the same object is used 
to represent
+                // each row
+                return FlatpackConverter.toMap(dataSet);
+            }
+
+            public void remove() {
+                throw new UnsupportedOperationException("remove() not 
supported");
+            }
+        };
+
+    }
+}

Propchange: 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/DataSetList.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java?rev=677005&view=auto
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java
 (added)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java
 Tue Jul 15 11:50:28 2008
@@ -0,0 +1,125 @@
+/**
+ *
+ * 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.camel.component.flatpack;
+
+import net.sf.flatpack.DefaultParserFactory;
+import net.sf.flatpack.Parser;
+import net.sf.flatpack.ParserFactory;
+import net.sf.flatpack.DataSet;
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.util.ObjectHelper;
+import org.springframework.core.io.Resource;
+
+import java.io.*;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class FlatpackDataFormat implements DataFormat {
+    private char delimiter = ',';
+    private char textQualifier = '"';
+    private boolean ignoreFirstRecord = true;
+    private Resource definition;
+    private boolean fixed = false;
+    private ParserFactory parserFactory = DefaultParserFactory.getInstance();
+
+    public void marshal(Exchange exchange, Object graph, OutputStream stream) 
throws Exception {
+        // TODO
+        throw new UnsupportedOperationException("marshal() not implemented 
yet!");
+    }
+
+    public Object unmarshal(Exchange exchange, InputStream stream) throws 
Exception {
+        // TODO should we just grab a Reader from the body?
+        InputStreamReader reader = new InputStreamReader(stream);
+        Parser parser = createParser(exchange, reader);
+        DataSet dataSet = parser.parse();
+        return new DataSetList(dataSet);
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+    public boolean isFixed() {
+        return fixed;
+    }
+
+    public void setFixed(boolean fixed) {
+        this.fixed = fixed;
+    }
+
+    public char getDelimiter() {
+        return delimiter;
+    }
+
+    public void setDelimiter(char delimiter) {
+        this.delimiter = delimiter;
+    }
+
+    public boolean isIgnoreFirstRecord() {
+        return ignoreFirstRecord;
+    }
+
+    public void setIgnoreFirstRecord(boolean ignoreFirstRecord) {
+        this.ignoreFirstRecord = ignoreFirstRecord;
+    }
+
+    public char getTextQualifier() {
+        return textQualifier;
+    }
+
+    public void setTextQualifier(char textQualifier) {
+        this.textQualifier = textQualifier;
+    }
+
+
+    public Resource getDefinition() {
+        return definition;
+    }
+
+    public void setDefinition(Resource definition) {
+        this.definition = definition;
+    }
+
+    public ParserFactory getParserFactory() {
+        return parserFactory;
+    }
+
+    public void setParserFactory(ParserFactory parserFactory) {
+        this.parserFactory = parserFactory;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+    protected Parser createParser(Exchange exchange, Reader bodyReader) throws 
IOException {
+        if (isFixed()) {
+            Resource resource = getDefinition();
+            ObjectHelper.notNull(resource, "resource property");
+            return getParserFactory().newFixedLengthParser(new 
InputStreamReader(resource.getInputStream()), bodyReader);
+        } else {
+            Resource resource = getDefinition();
+            if (resource == null) {
+                return getParserFactory().newDelimitedParser(bodyReader, 
delimiter, textQualifier);
+            } else {
+                return getParserFactory().newDelimitedParser(new 
InputStreamReader(resource.getInputStream()), bodyReader, delimiter, 
textQualifier, ignoreFirstRecord);
+            }
+        }
+    }
+
+}

Propchange: 
activemq/camel/trunk/components/camel-flatpack/src/main/java/org/apache/camel/component/flatpack/FlatpackDataFormat.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest.java
 (from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedTest.java)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest.java?p2=activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest.java&p1=activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedTest.java&r1=676659&r2=677005&rev=677005&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest.java
 Tue Jul 15 11:50:28 2008
@@ -34,7 +34,7 @@
  * @version $Revision: 1.1 $
  */
 @ContextConfiguration
-public class DelimitedTest extends AbstractJUnit38SpringContextTests {
+public class DelimitedWithUnmarshalTest extends 
AbstractJUnit38SpringContextTests {
     private static final transient Log LOG = 
LogFactory.getLog(FixedLengthTest.class);
 
     @EndpointInject(uri = "mock:results")
@@ -52,7 +52,7 @@
             Message in = exchange.getIn();
             Map body = in.getBody(Map.class);
             assertNotNull("Should have found body as a Map but was: " + 
ObjectHelper.className(in.getBody()), body);
-            assertEquals("ITEM_DESC", expectedItemDesc[counter], 
body.get("ITEM_DESC"));
+            assertEquals("ITEM_DESC result(" + counter + ")", 
expectedItemDesc[counter], body.get("ITEM_DESC"));
             LOG.info("Result: " + counter + " = " + body);
             counter++;
         }

Copied: 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest.java
 (from r676643, 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthTest.java)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest.java?p2=activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest.java&p1=activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthTest.java&r1=676643&r2=677005&rev=677005&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthTest.java
 (original)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/test/java/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest.java
 Tue Jul 15 11:50:28 2008
@@ -34,7 +34,7 @@
  * @version $Revision: 1.1 $
  */
 @ContextConfiguration
-public class FixedLengthTest extends AbstractJUnit38SpringContextTests {
+public class FixedLengthWithUnmarshalTest extends 
AbstractJUnit38SpringContextTests {
     private static final transient Log LOG = 
LogFactory.getLog(FixedLengthTest.class);
 
     @EndpointInject(uri = "mock:results")
@@ -59,4 +59,4 @@
 
     }
 
-}
+}
\ No newline at end of file

Copied: 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest-context.xml
 (from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedTest-context.xml)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest-context.xml?p2=activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest-context.xml&p1=activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedTest-context.xml&r1=676659&r2=677005&rev=677005&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedTest-context.xml
 (original)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/DelimitedWithUnmarshalTest-context.xml
 Tue Jul 15 11:50:28 2008
@@ -26,15 +26,18 @@
   <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
     <route>
       <from uri="file://src/test/data/delim?noop=true"/>
-      <to uri="flatpack:delim:INVENTORY-Delimited.pzmap.xml"/>
-    </route>
-
-    <route>
-      <from uri="flatpack:delim:INVENTORY-Delimited.pzmap.xml"/>
-      <convertBodyTo type="java.util.Map"/>
-      <to uri="mock:results"/>
+      <unmarshal ref="delimitedFormat"/>
+      <splitter>
+        <simple>in.body</simple>
+        <convertBodyTo type="java.util.Map"/>
+        <to uri="mock:results"/>
+      </splitter>
     </route>
   </camelContext>
   <!-- END SNIPPET: example -->
 
+  <bean id="delimitedFormat" 
class="org.apache.camel.component.flatpack.FlatpackDataFormat">
+    <property name="definition" value="INVENTORY-Delimited.pzmap.xml"/>
+  </bean>
+
 </beans>
\ No newline at end of file

Copied: 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest-context.xml
 (from r676659, 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthTest-context.xml)
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest-context.xml?p2=activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest-context.xml&p1=activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthTest-context.xml&r1=676659&r2=677005&rev=677005&view=diff
==============================================================================
--- 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthTest-context.xml
 (original)
+++ 
activemq/camel/trunk/components/camel-flatpack/src/test/resources/org/apache/camel/component/flatpack/FixedLengthWithUnmarshalTest-context.xml
 Tue Jul 15 11:50:28 2008
@@ -26,15 +26,19 @@
   <camelContext xmlns="http://activemq.apache.org/camel/schema/spring";>
     <route>
       <from uri="file://src/test/data/fixed?noop=true"/>
-      <to uri="flatpack:fixed:PEOPLE-FixedLength.pzmap.xml"/>
-    </route>
-
-    <route>
-      <from uri="flatpack:fixed:PEOPLE-FixedLength.pzmap.xml"/>
-      <convertBodyTo type="java.util.Map"/>
-      <to uri="mock:results"/>
+      <unmarshal ref="fixedFormat"/>
+      <splitter>
+        <simple>in.body</simple>
+        <convertBodyTo type="java.util.Map"/>
+        <to uri="mock:results"/>
+      </splitter>
     </route>
   </camelContext>
+
+  <bean id="fixedFormat" 
class="org.apache.camel.component.flatpack.FlatpackDataFormat">
+    <property name="definition" value="PEOPLE-FixedLength.pzmap.xml"/>
+    <property name="fixed" value="true"/>
+  </bean>
   <!-- END SNIPPET: example -->
 
 </beans>
\ No newline at end of file


Reply via email to