Fixes #105
authorKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Sat, 25 Jun 2016 17:39:14 +0000 (10:39 -0700)
committerKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Sat, 25 Jun 2016 17:39:14 +0000 (10:39 -0700)
fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java
fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthReaderTest.java [new file with mode: 0644]
full/src/main/java/org/apache/metamodel/DataContextFactory.java

index d740cff..40dc145 100644 (file)
@@ -30,7 +30,7 @@ import java.util.List;
 /**
  * Reader capable of separating values based on a fixed width setting.
  */
-final class FixedWidthReader implements Closeable {
+final public class FixedWidthReader implements Closeable {
 
        private final BufferedReader _reader;
        private final int _fixedValueWidth;
@@ -82,6 +82,7 @@ final class FixedWidthReader implements Closeable {
                this.expectedLineLength = expectedLineLength;
        }
 
+       
        /***
         * Reads the next line in the file.
         * 
@@ -92,10 +93,20 @@ final class FixedWidthReader implements Closeable {
         *             if an exception occurs while reading the file.
         */
        public String[] readLine() throws IllegalStateException {
+        String line;
+        try {
+            line = _reader.readLine();
+            return readLine(line);
+        } catch (IOException e) {
+            throw new IllegalStateException(e);
+        }
+       }
+       
+       public String[] readLine(String line) throws IOException {
+
 
-               try {
                        final List<String> values = new ArrayList<String>();
-                       final String line = _reader.readLine();
+               
                        if (line == null) {
                                return null;
                        }
@@ -173,9 +184,6 @@ final class FixedWidthReader implements Closeable {
                        }
 
                        return result;
-               } catch (IOException e) {
-                       throw new IllegalStateException(e);
-               }
        }
 
        @Override
diff --git a/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthReaderTest.java b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthReaderTest.java
new file mode 100644 (file)
index 0000000..dd45900
--- /dev/null
@@ -0,0 +1,57 @@
+/**
+ * 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.metamodel.fixedwidth;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class FixedWidthReaderTest {
+
+    @Test
+    public void testBufferedReader() throws IOException {
+        final File file = new File("src/test/resources/example_simple1.txt");
+        final BufferedReader reader = new BufferedReader(new FileReader(file));
+        int[] widths = new int[] { 8, 9 };
+        try (final FixedWidthReader fixedWidthReader = new FixedWidthReader(reader, widths, false)) {
+            final String[] line1 = fixedWidthReader.readLine();
+            assertEquals("[greeting, greeter]", Arrays.asList(line1).toString());
+            final String[] line2 = fixedWidthReader.readLine();
+            assertEquals("[hello, world]", Arrays.asList(line2).toString());
+            final String[] line3 = fixedWidthReader.readLine();
+            assertEquals("[hi, there]", Arrays.asList(line3).toString());
+        }
+    }
+
+    @Test
+    public void testNoBufferReader() throws IOException {
+        int[] widths = new int[] { 8, 9 };
+        final String lineToBeRead = "greeting  greeter  ";
+        @SuppressWarnings("resource")
+        final FixedWidthReader fixedWidthReader = new FixedWidthReader(null, widths, false);
+        final String[] line = fixedWidthReader.readLine(lineToBeRead);
+        assertEquals("[greeting, greeter]", Arrays.asList(line).toString());
+    }
+}
index 0bf3618..b4bf3f6 100644 (file)
@@ -50,6 +50,7 @@ import org.apache.metamodel.salesforce.SalesforceDataContext;
 import org.apache.metamodel.schema.TableType;
 import org.apache.metamodel.sugarcrm.SugarCrmDataContext;
 import org.apache.metamodel.util.FileHelper;
+import org.apache.metamodel.util.Resource;
 import org.apache.metamodel.util.SimpleTableDef;
 import org.apache.metamodel.xml.XmlDomDataContext;
 import org.ektorp.http.StdHttpClient.Builder;
@@ -291,6 +292,20 @@ public class DataContextFactory {
         FixedWidthDataContext dc = new FixedWidthDataContext(file, configuration);
         return dc;
     }
+    /**
+    * Creates a DataContext based on a fixed width file.
+    * 
+    * @param file
+    *            the file to read from.
+    * @param configuration
+    *            the fixed width configuration to use
+    * @return a DataContext object that matches the request
+    */
+   public static DataContext createFixedWidthDataContext(Resource resource, FixedWidthConfiguration configuration) {
+       final FixedWidthDataContext dc = new FixedWidthDataContext(resource, configuration);
+       return dc;
+   }
+
 
     /**
      * Creates a DataContext based on a fixed width file.