Fixes #105
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/b4bfc5cf Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/b4bfc5cf Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/b4bfc5cf Branch: refs/heads/5.x Commit: b4bfc5cf418eb1441bd1c76fc4798230cc9af333 Parents: d79c0ce Author: Kasper Sørensen <[email protected]> Authored: Sat Jun 25 10:39:14 2016 -0700 Committer: Kasper Sørensen <[email protected]> Committed: Sat Jun 25 10:39:14 2016 -0700 ---------------------------------------------------------------------- .../metamodel/fixedwidth/FixedWidthReader.java | 20 ++++--- .../fixedwidth/FixedWidthReaderTest.java | 57 ++++++++++++++++++++ .../apache/metamodel/DataContextFactory.java | 15 ++++++ 3 files changed, 86 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/b4bfc5cf/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java ---------------------------------------------------------------------- diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java index d740cff..40dc145 100644 --- a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java +++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java @@ -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 http://git-wip-us.apache.org/repos/asf/metamodel/blob/b4bfc5cf/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthReaderTest.java ---------------------------------------------------------------------- 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 index 0000000..dd45900 --- /dev/null +++ b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthReaderTest.java @@ -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()); + } +} http://git-wip-us.apache.org/repos/asf/metamodel/blob/b4bfc5cf/full/src/main/java/org/apache/metamodel/DataContextFactory.java ---------------------------------------------------------------------- diff --git a/full/src/main/java/org/apache/metamodel/DataContextFactory.java b/full/src/main/java/org/apache/metamodel/DataContextFactory.java index 0bf3618..b4bf3f6 100644 --- a/full/src/main/java/org/apache/metamodel/DataContextFactory.java +++ b/full/src/main/java/org/apache/metamodel/DataContextFactory.java @@ -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.
