You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/07/22 10:10:23 UTC
[10/64] [partial] Hard rename of all 'org/eobjects' folders to
'org/apache'.
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/test/java/org/eobjects/metamodel/excel/ExcelUpdateCallbackTest.java
----------------------------------------------------------------------
diff --git a/excel/src/test/java/org/eobjects/metamodel/excel/ExcelUpdateCallbackTest.java b/excel/src/test/java/org/eobjects/metamodel/excel/ExcelUpdateCallbackTest.java
deleted file mode 100644
index 2bf778b..0000000
--- a/excel/src/test/java/org/eobjects/metamodel/excel/ExcelUpdateCallbackTest.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.File;
-import java.lang.reflect.Field;
-import java.util.Arrays;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.xssf.streaming.SXSSFRow;
-import org.apache.poi.xssf.streaming.SXSSFSheet;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.schema.Table;
-
-public class ExcelUpdateCallbackTest extends TestCase {
-
- public void testStreamingAPI() throws Exception {
- File file = new File("target/streaming-api-test.xlsx");
- if (file.exists()) {
- file.delete();
- }
-
- assertFalse(file.exists());
-
- // write using streaming writer
- {
- ExcelDataContext dc = new ExcelDataContext(file);
- ExcelUpdateCallback callback = new ExcelUpdateCallback(dc);
-
- SXSSFSheet sheet = (SXSSFSheet) callback.createSheet("foobar");
-
- Field windowSizeField = SXSSFSheet.class
- .getDeclaredField("_randomAccessWindowSize");
- windowSizeField.setAccessible(true);
- int windowSize = windowSizeField.getInt(sheet);
- assertEquals(1000, windowSize);
-
- Field rowsField = SXSSFSheet.class.getDeclaredField("_rows");
- rowsField.setAccessible(true);
- @SuppressWarnings("unchecked")
- Map<Integer, SXSSFRow> rows = (Map<Integer, SXSSFRow>) rowsField
- .get(sheet);
- assertEquals(0, rows.size());
-
- // create 5x the amound of rows as the streaming sheet will hold in
- // memory
- for (int i = 0; i < windowSize * 5; i++) {
- Row row = sheet.createRow(i);
- Cell cell = row.createCell(0);
- cell.setCellValue("value" + i);
-
- assertTrue(rows.size() <= 1000);
- }
-
- assertEquals(1000, rows.size());
-
- ExcelUtils.writeWorkbook(dc, sheet.getWorkbook());
- }
-
- assertTrue("Usually the file size will be circa 42000, but it was: "
- + file.length(), file.length() > 40000 && file.length() < 45000);
-
- // read to check results
- {
- ExcelDataContext dc = new ExcelDataContext(file);
- assertEquals("[foobar]",
- Arrays.toString(dc.getDefaultSchema().getTableNames()));
-
- Table table = dc.getDefaultSchema().getTableByName("foobar");
-
- assertEquals("[value0]", Arrays.toString(table.getColumnNames()));
-
- DataSet ds = dc.query().from(table).select("value0").execute();
- int recordNo = 1;
- while (ds.next()) {
- assertEquals("value" + recordNo, ds.getRow().getValue(0));
- recordNo++;
- }
-
- assertEquals(5000, recordNo);
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/excel/src/test/java/org/eobjects/metamodel/excel/ZeroBasedRowIteratorTest.java
----------------------------------------------------------------------
diff --git a/excel/src/test/java/org/eobjects/metamodel/excel/ZeroBasedRowIteratorTest.java b/excel/src/test/java/org/eobjects/metamodel/excel/ZeroBasedRowIteratorTest.java
deleted file mode 100644
index 730727e..0000000
--- a/excel/src/test/java/org/eobjects/metamodel/excel/ZeroBasedRowIteratorTest.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/**
- * 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.eobjects.metamodel.excel;
-
-import java.io.FileInputStream;
-
-import junit.framework.TestCase;
-
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.WorkbookFactory;
-import org.easymock.EasyMock;
-import org.eobjects.metamodel.excel.ZeroBasedRowIterator;
-
-public class ZeroBasedRowIteratorTest extends TestCase {
-
- public void testHasNext() throws Exception {
- Workbook workbook = WorkbookFactory.create(new FileInputStream(
- "src/test/resources/xls_single_cell_sheet.xls"));
- Sheet sheet = workbook.getSheetAt(0);
-
- // POI's row numbers are 0-based also - the last cell in the sheet is
- // actually A6.
- assertEquals(5, sheet.getLastRowNum());
-
- ZeroBasedRowIterator it = new ZeroBasedRowIterator(sheet);
-
- assertTrue(it.hasNext());
- assertNull(it.next());
-
- assertTrue(it.hasNext());
- assertNull(it.next());
-
- assertTrue(it.hasNext());
- assertNull(it.next());
-
- assertTrue(it.hasNext());
- assertNull(it.next());
-
- assertTrue(it.hasNext());
- assertNull(it.next());
-
- assertTrue(it.hasNext());
- assertNotNull(it.next());
-
- assertFalse(it.hasNext());
- }
-
- public void testUnsupportedRemove() throws Exception {
- ZeroBasedRowIterator it = new ZeroBasedRowIterator(EasyMock.createMock(Sheet.class));
-
- try {
- it.remove();
- fail("Exception expected");
- } catch (UnsupportedOperationException e) {
- assertEquals("remove() is not supported", e.getMessage());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
new file mode 100644
index 0000000..5054b01
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
@@ -0,0 +1,151 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import java.io.Serializable;
+import java.util.Arrays;
+import java.util.List;
+
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.util.BaseObject;
+import org.eobjects.metamodel.util.FileHelper;
+
+/**
+ * Configuration of metadata about a fixed width values datacontext.
+ *
+ * @author Kasper Sørensen
+ */
+public final class FixedWidthConfiguration extends BaseObject implements
+ Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ public static final int NO_COLUMN_NAME_LINE = 0;
+ public static final int DEFAULT_COLUMN_NAME_LINE = 1;
+
+ private final String encoding;
+ private final int fixedValueWidth;
+ private final int[] valueWidths;
+ private final int columnNameLineNumber;
+ private final boolean failOnInconsistentLineWidth;
+
+ public FixedWidthConfiguration(int fixedValueWidth) {
+ this(DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING,
+ fixedValueWidth);
+ }
+
+ public FixedWidthConfiguration(int[] valueWidth) {
+ this(DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING, valueWidth,
+ false);
+ }
+
+ public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
+ int fixedValueWidth) {
+ this(columnNameLineNumber, encoding, fixedValueWidth, false);
+ }
+
+ public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
+ int fixedValueWidth, boolean failOnInconsistentLineWidth) {
+ this.encoding = encoding;
+ this.fixedValueWidth = fixedValueWidth;
+ this.columnNameLineNumber = columnNameLineNumber;
+ this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+ this.valueWidths = new int[0];
+ }
+
+ public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
+ int[] valueWidths, boolean failOnInconsistentLineWidth) {
+ this.encoding = encoding;
+ this.fixedValueWidth = -1;
+ this.columnNameLineNumber = columnNameLineNumber;
+ this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+ this.valueWidths = valueWidths;
+ }
+
+ /**
+ * The line number (1 based) from which to get the names of the columns.
+ *
+ * @return an int representing the line number of the column headers/names.
+ */
+ public int getColumnNameLineNumber() {
+ return columnNameLineNumber;
+ }
+
+ /**
+ * Gets the file encoding to use for reading the file.
+ *
+ * @return the text encoding to use for reading the file.
+ */
+ public String getEncoding() {
+ return encoding;
+ }
+
+ /**
+ * Gets the width of each value within the fixed width value file.
+ *
+ * @return the fixed width to use when parsing the file.
+ */
+ public int getFixedValueWidth() {
+ return fixedValueWidth;
+ }
+
+ public int[] getValueWidths() {
+ return valueWidths;
+ }
+
+ /**
+ * Determines if the {@link DataSet#next()} should throw an exception in
+ * case of inconsistent line width in the fixed width value file.
+ *
+ * @return a boolean indicating whether or not to fail on inconsistent line
+ * widths.
+ */
+ public boolean isFailOnInconsistentLineWidth() {
+ return failOnInconsistentLineWidth;
+ }
+
+ @Override
+ protected void decorateIdentity(List<Object> identifiers) {
+ identifiers.add(columnNameLineNumber);
+ identifiers.add(encoding);
+ identifiers.add(fixedValueWidth);
+ identifiers.add(valueWidths);
+ identifiers.add(failOnInconsistentLineWidth);
+ }
+
+ @Override
+ public String toString() {
+ return "FixedWidthConfiguration[encoding=" + encoding
+ + ", fixedValueWidth=" + fixedValueWidth + ", valueWidths="
+ + Arrays.toString(valueWidths) + ", columnNameLineNumber="
+ + columnNameLineNumber + ", failOnInconsistentLineWidth="
+ + failOnInconsistentLineWidth + "]";
+ }
+
+ public boolean isConstantValueWidth() {
+ return fixedValueWidth != -1;
+ }
+
+ public int getValueWidth(int columnIndex) {
+ if (isConstantValueWidth()) {
+ return fixedValueWidth;
+ }
+ return valueWidths[columnIndex];
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
new file mode 100644
index 0000000..704b376
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
@@ -0,0 +1,192 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import java.io.File;
+import java.io.InputStream;
+import java.io.Reader;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.QueryPostprocessDataContext;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.ColumnType;
+import org.eobjects.metamodel.schema.MutableColumn;
+import org.eobjects.metamodel.schema.MutableSchema;
+import org.eobjects.metamodel.schema.MutableTable;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+import org.eobjects.metamodel.schema.TableType;
+import org.eobjects.metamodel.util.AlphabeticSequence;
+import org.eobjects.metamodel.util.FileHelper;
+import org.eobjects.metamodel.util.FileResource;
+import org.eobjects.metamodel.util.Resource;
+
+/**
+ * DataContext implementation for fixed width value files.
+ *
+ * @author Kasper Sørensen
+ */
+public class FixedWidthDataContext extends QueryPostprocessDataContext {
+
+ private final Resource _resource;
+ private final FixedWidthConfiguration _configuration;
+
+ /**
+ * @deprecated use
+ * {@link #FixedWidthDataContext(File, FixedWidthConfiguration)}
+ * instead
+ */
+ @Deprecated
+ public FixedWidthDataContext(String filename, String fileEncoding, int fixedValueWidth) {
+ this(new FileResource(filename), new FixedWidthConfiguration(fixedValueWidth));
+ }
+
+ /**
+ * @deprecated use
+ * {@link #FixedWidthDataContext(File, FixedWidthConfiguration)}
+ * instead
+ */
+ @Deprecated
+ public FixedWidthDataContext(File file, String fileEncoding, int fixedValueWidth, int headerLineNumber) {
+ this(file, new FixedWidthConfiguration(headerLineNumber, fileEncoding, fixedValueWidth));
+ }
+
+ public FixedWidthDataContext(File file, FixedWidthConfiguration configuration) {
+ _resource = new FileResource(file);
+ _configuration = configuration;
+ }
+
+ public FixedWidthDataContext(Resource resource, FixedWidthConfiguration configuration) {
+ _resource = resource;
+ _configuration = configuration;
+ }
+
+ /**
+ * Gets the Fixed width value configuration used.
+ *
+ * @return a fixed width configuration
+ */
+ public FixedWidthConfiguration getConfiguration() {
+ return _configuration;
+ }
+
+ /**
+ * Gets the file being read.
+ *
+ * @return a file
+ *
+ * @deprecated use {@link #getResource()} instead.
+ */
+ @Deprecated
+ public File getFile() {
+ if (_resource instanceof FileResource) {
+ return ((FileResource) _resource).getFile();
+ }
+ return null;
+ }
+
+ /**
+ * Gets the resource being read
+ *
+ * @return
+ */
+ public Resource getResource() {
+ return _resource;
+ }
+
+ @Override
+ protected Schema getMainSchema() throws MetaModelException {
+ final String schemaName = getDefaultSchemaName();
+ final MutableSchema schema = new MutableSchema(schemaName);
+ final String tableName = schemaName.substring(0, schemaName.length() - 4);
+ final MutableTable table = new MutableTable(tableName, TableType.TABLE, schema);
+ schema.addTable(table);
+
+ final FixedWidthReader reader = createReader();
+ final String[] columnNames;
+ try {
+ if (_configuration.getColumnNameLineNumber() != FixedWidthConfiguration.NO_COLUMN_NAME_LINE) {
+ for (int i = 1; i < _configuration.getColumnNameLineNumber(); i++) {
+ reader.readLine();
+ }
+ columnNames = reader.readLine();
+ } else {
+ columnNames = reader.readLine();
+ if (columnNames != null) {
+ AlphabeticSequence sequence = new AlphabeticSequence();
+ for (int i = 0; i < columnNames.length; i++) {
+ columnNames[i] = sequence.next();
+ }
+ }
+ }
+ } finally {
+ FileHelper.safeClose(reader);
+ }
+
+ if (columnNames != null) {
+ for (int i = 0; i < columnNames.length; i++) {
+ final String columnName = columnNames[i];
+ final MutableColumn column = new MutableColumn(columnName, ColumnType.VARCHAR, table, i, true);
+ column.setColumnSize(_configuration.getValueWidth(i));
+ table.addColumn(column);
+ }
+ }
+
+ return schema;
+ }
+
+ @Override
+ protected String getMainSchemaName() throws MetaModelException {
+ return _resource.getName();
+ }
+
+ @Override
+ public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
+ final FixedWidthReader reader = createReader();
+ try {
+ for (int i = 1; i <= _configuration.getColumnNameLineNumber(); i++) {
+ reader.readLine();
+ }
+ } catch (IllegalStateException e) {
+ FileHelper.safeClose(reader);
+ throw e;
+ }
+ if (maxRows > 0) {
+ return new FixedWidthDataSet(reader, columns, maxRows);
+ } else {
+ return new FixedWidthDataSet(reader, columns, null);
+ }
+ }
+
+ private FixedWidthReader createReader() {
+ final InputStream inputStream = _resource.read();
+ final Reader fileReader = FileHelper.getReader(inputStream, _configuration.getEncoding());
+ final FixedWidthReader reader;
+ if (_configuration.isConstantValueWidth()) {
+ reader = new FixedWidthReader(fileReader, _configuration.getFixedValueWidth(),
+ _configuration.isFailOnInconsistentLineWidth());
+ } else {
+ reader = new FixedWidthReader(fileReader, _configuration.getValueWidths(),
+ _configuration.isFailOnInconsistentLineWidth());
+ }
+ return reader;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataSet.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataSet.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataSet.java
new file mode 100644
index 0000000..3ac8047
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataSet.java
@@ -0,0 +1,115 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import org.eobjects.metamodel.data.AbstractDataSet;
+import org.eobjects.metamodel.data.DefaultRow;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.util.FileHelper;
+
+/**
+ * DataSet implementation for fixed width values.
+ *
+ * @author Kasper Sørensen
+ */
+class FixedWidthDataSet extends AbstractDataSet {
+
+ private final FixedWidthReader _reader;
+ private volatile Integer _rowsRemaining;
+ private volatile Row _row;
+
+ public FixedWidthDataSet(FixedWidthReader reader, Column[] columns,
+ Integer maxRows) {
+ super(columns);
+ _reader = reader;
+ _rowsRemaining = maxRows;
+ }
+
+ @Override
+ public void close() {
+ FileHelper.safeClose(_reader);
+ _row = null;
+ _rowsRemaining = null;
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ super.finalize();
+ // close is always safe to invoke
+ close();
+ }
+
+ @Override
+ public Row getRow() {
+ return _row;
+ }
+
+ @Override
+ public boolean next() {
+ if (_rowsRemaining != null && _rowsRemaining > 0) {
+ _rowsRemaining--;
+ return nextInternal();
+ } else if (_rowsRemaining == null) {
+ return nextInternal();
+ } else {
+ return false;
+ }
+ }
+
+ private boolean nextInternal() {
+ if (_reader == null) {
+ return false;
+ }
+
+ InconsistentValueWidthException exception;
+ String[] stringValues;
+ try {
+ stringValues = _reader.readLine();
+ exception = null;
+ } catch (InconsistentValueWidthException e) {
+ stringValues = e.getSourceResult();
+ exception = e;
+ }
+ if (stringValues == null) {
+ close();
+ return false;
+ }
+
+ final int size = getHeader().size();
+ Object[] rowValues = new Object[size];
+ for (int i = 0; i < size; i++) {
+ Column column = getHeader().getSelectItem(i).getColumn();
+ int columnNumber = column.getColumnNumber();
+ if (columnNumber < stringValues.length) {
+ rowValues[i] = stringValues[columnNumber];
+ } else {
+ // Ticket #125: Missing values should be enterpreted as
+ // null.
+ rowValues[i] = null;
+ }
+ }
+ _row = new DefaultRow(getHeader(), rowValues);
+
+ if (exception != null) {
+ throw new InconsistentValueWidthException(_row, exception);
+ }
+ return true;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/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
new file mode 100644
index 0000000..ca0aa6f
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthReader.java
@@ -0,0 +1,188 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.Reader;
+import java.text.CharacterIterator;
+import java.text.StringCharacterIterator;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Reader capable of separating values based on a fixed width setting.
+ *
+ * @author Kasper Sørensen
+ */
+final class FixedWidthReader implements Closeable {
+
+ private final BufferedReader _reader;
+ private final int _fixedValueWidth;
+ private final int[] _valueWidths;
+ private final boolean _failOnInconsistentLineWidth;
+ private final int expectedLineLength;
+ private final boolean constantWidth;
+ private volatile int _rowNumber;
+
+ public FixedWidthReader(Reader reader, int fixedValueWidth,
+ boolean failOnInconsistentLineWidth) {
+ this(new BufferedReader(reader), fixedValueWidth,
+ failOnInconsistentLineWidth);
+ }
+
+ public FixedWidthReader(BufferedReader reader, int fixedValueWidth,
+ boolean failOnInconsistentLineWidth) {
+ _reader = reader;
+ _fixedValueWidth = fixedValueWidth;
+ _failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+ _rowNumber = 0;
+ _valueWidths = null;
+
+ constantWidth = true;
+ expectedLineLength = -1;
+ }
+
+ public FixedWidthReader(Reader reader, int[] valueWidths,
+ boolean failOnInconsistentLineWidth) {
+ this(new BufferedReader(reader), valueWidths,
+ failOnInconsistentLineWidth);
+ }
+
+ public FixedWidthReader(BufferedReader reader, int[] valueWidths,
+ boolean failOnInconsistentLineWidth) {
+ _reader = reader;
+ _fixedValueWidth = -1;
+ _valueWidths = valueWidths;
+ _failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+ _rowNumber = 0;
+
+ constantWidth = false;
+ int expectedLineLength = 0;
+ if (_fixedValueWidth == -1) {
+ for (int i = 0; i < _valueWidths.length; i++) {
+ expectedLineLength += _valueWidths[i];
+ }
+ }
+ this.expectedLineLength = expectedLineLength;
+ }
+
+ /***
+ * Reads the next line in the file.
+ *
+ * @return an array of values in the next line, or null if the end of the
+ * file has been reached.
+ *
+ * @throws IllegalStateException
+ * if an exception occurs while reading the file.
+ */
+ public String[] readLine() throws IllegalStateException {
+
+ try {
+ final List<String> values = new ArrayList<String>();
+ final String line = _reader.readLine();
+ if (line == null) {
+ return null;
+ }
+
+ StringBuilder nextValue = new StringBuilder();
+
+ int valueIndex = 0;
+
+ final CharacterIterator it = new StringCharacterIterator(line);
+ for (char c = it.first(); c != CharacterIterator.DONE; c = it
+ .next()) {
+ nextValue.append(c);
+
+ final int valueWidth;
+ if (constantWidth) {
+ valueWidth = _fixedValueWidth;
+ } else {
+ if (valueIndex >= _valueWidths.length) {
+ if (_failOnInconsistentLineWidth) {
+ String[] result = values.toArray(new String[values
+ .size()]);
+ throw new InconsistentValueWidthException(result,
+ line, _rowNumber + 1);
+ } else {
+ // silently ignore the inconsistency
+ break;
+ }
+ }
+ valueWidth = _valueWidths[valueIndex];
+ }
+
+ if (nextValue.length() == valueWidth) {
+ // write the value
+ values.add(nextValue.toString().trim());
+ nextValue = new StringBuilder();
+ valueIndex++;
+ }
+ }
+
+ if (nextValue.length() > 0) {
+ values.add(nextValue.toString().trim());
+ }
+
+ String[] result = values.toArray(new String[values.size()]);
+
+ if (!_failOnInconsistentLineWidth && !constantWidth) {
+ if (result.length != _valueWidths.length) {
+ String[] correctedResult = new String[_valueWidths.length];
+ for (int i = 0; i < result.length
+ && i < _valueWidths.length; i++) {
+ correctedResult[i] = result[i];
+ }
+ result = correctedResult;
+ }
+ }
+
+ if (_failOnInconsistentLineWidth) {
+ _rowNumber++;
+ if (constantWidth) {
+ if (line.length() % _fixedValueWidth != 0) {
+ throw new InconsistentValueWidthException(result, line,
+ _rowNumber);
+ }
+ } else {
+ if (result.length != values.size()) {
+ throw new InconsistentValueWidthException(result, line,
+ _rowNumber);
+ }
+
+ if (line.length() != expectedLineLength) {
+ throw new InconsistentValueWidthException(result, line,
+ _rowNumber);
+ }
+ }
+ }
+
+ return result;
+ } catch (IOException e) {
+ throw new IllegalStateException(e);
+ }
+ }
+
+ @Override
+ public void close() throws IOException {
+ _reader.close();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/InconsistentValueWidthException.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/InconsistentValueWidthException.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/InconsistentValueWidthException.java
new file mode 100644
index 0000000..2fa3573
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/InconsistentValueWidthException.java
@@ -0,0 +1,67 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import org.eobjects.metamodel.InconsistentRowFormatException;
+import org.eobjects.metamodel.data.Row;
+
+/**
+ * Exception thrown when inconsistent widths of a Fixed Width Value file
+ *
+ * @author Kasper Sørensen
+ */
+public final class InconsistentValueWidthException extends
+ InconsistentRowFormatException {
+
+ private static final long serialVersionUID = 1L;
+ private final String[] _sourceResult;
+ private final String _sourceLine;
+
+ public InconsistentValueWidthException(String[] result, String line,
+ int rowNumber) {
+ super(null, rowNumber);
+ _sourceResult = result;
+ _sourceLine = line;
+ }
+
+ public InconsistentValueWidthException(Row proposedRow,
+ InconsistentValueWidthException cause) {
+ super(proposedRow, cause.getRowNumber(), cause);
+ _sourceResult = cause.getSourceResult();
+ _sourceLine = cause.getSourceLine();
+ }
+
+ /**
+ * Gets the source line as represented in the Fixed Width file
+ *
+ * @return the source line as a string
+ */
+ public String getSourceLine() {
+ return _sourceLine;
+ }
+
+ /**
+ * Gets the parsed result as read by the Fixed Width reader.
+ *
+ * @return the gracefully parsed line
+ */
+ public String[] getSourceResult() {
+ return _sourceResult;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/package-info.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/package-info.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/package-info.java
new file mode 100644
index 0000000..9702557
--- /dev/null
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/package-info.java
@@ -0,0 +1,23 @@
+/**
+ * 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.
+ */
+/**
+ * Module package for Fixed width value files
+ */
+package org.eobjects.metamodel.fixedwidth;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfiguration.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfiguration.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfiguration.java
deleted file mode 100644
index a5236a9..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfiguration.java
+++ /dev/null
@@ -1,151 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import java.io.Serializable;
-import java.util.Arrays;
-import java.util.List;
-
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.util.BaseObject;
-import org.eobjects.metamodel.util.FileHelper;
-
-/**
- * Configuration of metadata about a fixed width values datacontext.
- *
- * @author Kasper Sørensen
- */
-public final class FixedWidthConfiguration extends BaseObject implements
- Serializable {
-
- private static final long serialVersionUID = 1L;
-
- public static final int NO_COLUMN_NAME_LINE = 0;
- public static final int DEFAULT_COLUMN_NAME_LINE = 1;
-
- private final String encoding;
- private final int fixedValueWidth;
- private final int[] valueWidths;
- private final int columnNameLineNumber;
- private final boolean failOnInconsistentLineWidth;
-
- public FixedWidthConfiguration(int fixedValueWidth) {
- this(DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING,
- fixedValueWidth);
- }
-
- public FixedWidthConfiguration(int[] valueWidth) {
- this(DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING, valueWidth,
- false);
- }
-
- public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
- int fixedValueWidth) {
- this(columnNameLineNumber, encoding, fixedValueWidth, false);
- }
-
- public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
- int fixedValueWidth, boolean failOnInconsistentLineWidth) {
- this.encoding = encoding;
- this.fixedValueWidth = fixedValueWidth;
- this.columnNameLineNumber = columnNameLineNumber;
- this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
- this.valueWidths = new int[0];
- }
-
- public FixedWidthConfiguration(int columnNameLineNumber, String encoding,
- int[] valueWidths, boolean failOnInconsistentLineWidth) {
- this.encoding = encoding;
- this.fixedValueWidth = -1;
- this.columnNameLineNumber = columnNameLineNumber;
- this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
- this.valueWidths = valueWidths;
- }
-
- /**
- * The line number (1 based) from which to get the names of the columns.
- *
- * @return an int representing the line number of the column headers/names.
- */
- public int getColumnNameLineNumber() {
- return columnNameLineNumber;
- }
-
- /**
- * Gets the file encoding to use for reading the file.
- *
- * @return the text encoding to use for reading the file.
- */
- public String getEncoding() {
- return encoding;
- }
-
- /**
- * Gets the width of each value within the fixed width value file.
- *
- * @return the fixed width to use when parsing the file.
- */
- public int getFixedValueWidth() {
- return fixedValueWidth;
- }
-
- public int[] getValueWidths() {
- return valueWidths;
- }
-
- /**
- * Determines if the {@link DataSet#next()} should throw an exception in
- * case of inconsistent line width in the fixed width value file.
- *
- * @return a boolean indicating whether or not to fail on inconsistent line
- * widths.
- */
- public boolean isFailOnInconsistentLineWidth() {
- return failOnInconsistentLineWidth;
- }
-
- @Override
- protected void decorateIdentity(List<Object> identifiers) {
- identifiers.add(columnNameLineNumber);
- identifiers.add(encoding);
- identifiers.add(fixedValueWidth);
- identifiers.add(valueWidths);
- identifiers.add(failOnInconsistentLineWidth);
- }
-
- @Override
- public String toString() {
- return "FixedWidthConfiguration[encoding=" + encoding
- + ", fixedValueWidth=" + fixedValueWidth + ", valueWidths="
- + Arrays.toString(valueWidths) + ", columnNameLineNumber="
- + columnNameLineNumber + ", failOnInconsistentLineWidth="
- + failOnInconsistentLineWidth + "]";
- }
-
- public boolean isConstantValueWidth() {
- return fixedValueWidth != -1;
- }
-
- public int getValueWidth(int columnIndex) {
- if (isConstantValueWidth()) {
- return fixedValueWidth;
- }
- return valueWidths[columnIndex];
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContext.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContext.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContext.java
deleted file mode 100644
index 704b376..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContext.java
+++ /dev/null
@@ -1,192 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import java.io.File;
-import java.io.InputStream;
-import java.io.Reader;
-
-import org.eobjects.metamodel.MetaModelException;
-import org.eobjects.metamodel.QueryPostprocessDataContext;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.schema.ColumnType;
-import org.eobjects.metamodel.schema.MutableColumn;
-import org.eobjects.metamodel.schema.MutableSchema;
-import org.eobjects.metamodel.schema.MutableTable;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-import org.eobjects.metamodel.schema.TableType;
-import org.eobjects.metamodel.util.AlphabeticSequence;
-import org.eobjects.metamodel.util.FileHelper;
-import org.eobjects.metamodel.util.FileResource;
-import org.eobjects.metamodel.util.Resource;
-
-/**
- * DataContext implementation for fixed width value files.
- *
- * @author Kasper Sørensen
- */
-public class FixedWidthDataContext extends QueryPostprocessDataContext {
-
- private final Resource _resource;
- private final FixedWidthConfiguration _configuration;
-
- /**
- * @deprecated use
- * {@link #FixedWidthDataContext(File, FixedWidthConfiguration)}
- * instead
- */
- @Deprecated
- public FixedWidthDataContext(String filename, String fileEncoding, int fixedValueWidth) {
- this(new FileResource(filename), new FixedWidthConfiguration(fixedValueWidth));
- }
-
- /**
- * @deprecated use
- * {@link #FixedWidthDataContext(File, FixedWidthConfiguration)}
- * instead
- */
- @Deprecated
- public FixedWidthDataContext(File file, String fileEncoding, int fixedValueWidth, int headerLineNumber) {
- this(file, new FixedWidthConfiguration(headerLineNumber, fileEncoding, fixedValueWidth));
- }
-
- public FixedWidthDataContext(File file, FixedWidthConfiguration configuration) {
- _resource = new FileResource(file);
- _configuration = configuration;
- }
-
- public FixedWidthDataContext(Resource resource, FixedWidthConfiguration configuration) {
- _resource = resource;
- _configuration = configuration;
- }
-
- /**
- * Gets the Fixed width value configuration used.
- *
- * @return a fixed width configuration
- */
- public FixedWidthConfiguration getConfiguration() {
- return _configuration;
- }
-
- /**
- * Gets the file being read.
- *
- * @return a file
- *
- * @deprecated use {@link #getResource()} instead.
- */
- @Deprecated
- public File getFile() {
- if (_resource instanceof FileResource) {
- return ((FileResource) _resource).getFile();
- }
- return null;
- }
-
- /**
- * Gets the resource being read
- *
- * @return
- */
- public Resource getResource() {
- return _resource;
- }
-
- @Override
- protected Schema getMainSchema() throws MetaModelException {
- final String schemaName = getDefaultSchemaName();
- final MutableSchema schema = new MutableSchema(schemaName);
- final String tableName = schemaName.substring(0, schemaName.length() - 4);
- final MutableTable table = new MutableTable(tableName, TableType.TABLE, schema);
- schema.addTable(table);
-
- final FixedWidthReader reader = createReader();
- final String[] columnNames;
- try {
- if (_configuration.getColumnNameLineNumber() != FixedWidthConfiguration.NO_COLUMN_NAME_LINE) {
- for (int i = 1; i < _configuration.getColumnNameLineNumber(); i++) {
- reader.readLine();
- }
- columnNames = reader.readLine();
- } else {
- columnNames = reader.readLine();
- if (columnNames != null) {
- AlphabeticSequence sequence = new AlphabeticSequence();
- for (int i = 0; i < columnNames.length; i++) {
- columnNames[i] = sequence.next();
- }
- }
- }
- } finally {
- FileHelper.safeClose(reader);
- }
-
- if (columnNames != null) {
- for (int i = 0; i < columnNames.length; i++) {
- final String columnName = columnNames[i];
- final MutableColumn column = new MutableColumn(columnName, ColumnType.VARCHAR, table, i, true);
- column.setColumnSize(_configuration.getValueWidth(i));
- table.addColumn(column);
- }
- }
-
- return schema;
- }
-
- @Override
- protected String getMainSchemaName() throws MetaModelException {
- return _resource.getName();
- }
-
- @Override
- public DataSet materializeMainSchemaTable(Table table, Column[] columns, int maxRows) {
- final FixedWidthReader reader = createReader();
- try {
- for (int i = 1; i <= _configuration.getColumnNameLineNumber(); i++) {
- reader.readLine();
- }
- } catch (IllegalStateException e) {
- FileHelper.safeClose(reader);
- throw e;
- }
- if (maxRows > 0) {
- return new FixedWidthDataSet(reader, columns, maxRows);
- } else {
- return new FixedWidthDataSet(reader, columns, null);
- }
- }
-
- private FixedWidthReader createReader() {
- final InputStream inputStream = _resource.read();
- final Reader fileReader = FileHelper.getReader(inputStream, _configuration.getEncoding());
- final FixedWidthReader reader;
- if (_configuration.isConstantValueWidth()) {
- reader = new FixedWidthReader(fileReader, _configuration.getFixedValueWidth(),
- _configuration.isFailOnInconsistentLineWidth());
- } else {
- reader = new FixedWidthReader(fileReader, _configuration.getValueWidths(),
- _configuration.isFailOnInconsistentLineWidth());
- }
- return reader;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataSet.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataSet.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataSet.java
deleted file mode 100644
index 3ac8047..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataSet.java
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import org.eobjects.metamodel.data.AbstractDataSet;
-import org.eobjects.metamodel.data.DefaultRow;
-import org.eobjects.metamodel.data.Row;
-import org.eobjects.metamodel.schema.Column;
-import org.eobjects.metamodel.util.FileHelper;
-
-/**
- * DataSet implementation for fixed width values.
- *
- * @author Kasper Sørensen
- */
-class FixedWidthDataSet extends AbstractDataSet {
-
- private final FixedWidthReader _reader;
- private volatile Integer _rowsRemaining;
- private volatile Row _row;
-
- public FixedWidthDataSet(FixedWidthReader reader, Column[] columns,
- Integer maxRows) {
- super(columns);
- _reader = reader;
- _rowsRemaining = maxRows;
- }
-
- @Override
- public void close() {
- FileHelper.safeClose(_reader);
- _row = null;
- _rowsRemaining = null;
- }
-
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
- // close is always safe to invoke
- close();
- }
-
- @Override
- public Row getRow() {
- return _row;
- }
-
- @Override
- public boolean next() {
- if (_rowsRemaining != null && _rowsRemaining > 0) {
- _rowsRemaining--;
- return nextInternal();
- } else if (_rowsRemaining == null) {
- return nextInternal();
- } else {
- return false;
- }
- }
-
- private boolean nextInternal() {
- if (_reader == null) {
- return false;
- }
-
- InconsistentValueWidthException exception;
- String[] stringValues;
- try {
- stringValues = _reader.readLine();
- exception = null;
- } catch (InconsistentValueWidthException e) {
- stringValues = e.getSourceResult();
- exception = e;
- }
- if (stringValues == null) {
- close();
- return false;
- }
-
- final int size = getHeader().size();
- Object[] rowValues = new Object[size];
- for (int i = 0; i < size; i++) {
- Column column = getHeader().getSelectItem(i).getColumn();
- int columnNumber = column.getColumnNumber();
- if (columnNumber < stringValues.length) {
- rowValues[i] = stringValues[columnNumber];
- } else {
- // Ticket #125: Missing values should be enterpreted as
- // null.
- rowValues[i] = null;
- }
- }
- _row = new DefaultRow(getHeader(), rowValues);
-
- if (exception != null) {
- throw new InconsistentValueWidthException(_row, exception);
- }
- return true;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthReader.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthReader.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthReader.java
deleted file mode 100644
index ca0aa6f..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/FixedWidthReader.java
+++ /dev/null
@@ -1,188 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import java.io.BufferedReader;
-import java.io.Closeable;
-import java.io.IOException;
-import java.io.Reader;
-import java.text.CharacterIterator;
-import java.text.StringCharacterIterator;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Reader capable of separating values based on a fixed width setting.
- *
- * @author Kasper Sørensen
- */
-final class FixedWidthReader implements Closeable {
-
- private final BufferedReader _reader;
- private final int _fixedValueWidth;
- private final int[] _valueWidths;
- private final boolean _failOnInconsistentLineWidth;
- private final int expectedLineLength;
- private final boolean constantWidth;
- private volatile int _rowNumber;
-
- public FixedWidthReader(Reader reader, int fixedValueWidth,
- boolean failOnInconsistentLineWidth) {
- this(new BufferedReader(reader), fixedValueWidth,
- failOnInconsistentLineWidth);
- }
-
- public FixedWidthReader(BufferedReader reader, int fixedValueWidth,
- boolean failOnInconsistentLineWidth) {
- _reader = reader;
- _fixedValueWidth = fixedValueWidth;
- _failOnInconsistentLineWidth = failOnInconsistentLineWidth;
- _rowNumber = 0;
- _valueWidths = null;
-
- constantWidth = true;
- expectedLineLength = -1;
- }
-
- public FixedWidthReader(Reader reader, int[] valueWidths,
- boolean failOnInconsistentLineWidth) {
- this(new BufferedReader(reader), valueWidths,
- failOnInconsistentLineWidth);
- }
-
- public FixedWidthReader(BufferedReader reader, int[] valueWidths,
- boolean failOnInconsistentLineWidth) {
- _reader = reader;
- _fixedValueWidth = -1;
- _valueWidths = valueWidths;
- _failOnInconsistentLineWidth = failOnInconsistentLineWidth;
- _rowNumber = 0;
-
- constantWidth = false;
- int expectedLineLength = 0;
- if (_fixedValueWidth == -1) {
- for (int i = 0; i < _valueWidths.length; i++) {
- expectedLineLength += _valueWidths[i];
- }
- }
- this.expectedLineLength = expectedLineLength;
- }
-
- /***
- * Reads the next line in the file.
- *
- * @return an array of values in the next line, or null if the end of the
- * file has been reached.
- *
- * @throws IllegalStateException
- * if an exception occurs while reading the file.
- */
- public String[] readLine() throws IllegalStateException {
-
- try {
- final List<String> values = new ArrayList<String>();
- final String line = _reader.readLine();
- if (line == null) {
- return null;
- }
-
- StringBuilder nextValue = new StringBuilder();
-
- int valueIndex = 0;
-
- final CharacterIterator it = new StringCharacterIterator(line);
- for (char c = it.first(); c != CharacterIterator.DONE; c = it
- .next()) {
- nextValue.append(c);
-
- final int valueWidth;
- if (constantWidth) {
- valueWidth = _fixedValueWidth;
- } else {
- if (valueIndex >= _valueWidths.length) {
- if (_failOnInconsistentLineWidth) {
- String[] result = values.toArray(new String[values
- .size()]);
- throw new InconsistentValueWidthException(result,
- line, _rowNumber + 1);
- } else {
- // silently ignore the inconsistency
- break;
- }
- }
- valueWidth = _valueWidths[valueIndex];
- }
-
- if (nextValue.length() == valueWidth) {
- // write the value
- values.add(nextValue.toString().trim());
- nextValue = new StringBuilder();
- valueIndex++;
- }
- }
-
- if (nextValue.length() > 0) {
- values.add(nextValue.toString().trim());
- }
-
- String[] result = values.toArray(new String[values.size()]);
-
- if (!_failOnInconsistentLineWidth && !constantWidth) {
- if (result.length != _valueWidths.length) {
- String[] correctedResult = new String[_valueWidths.length];
- for (int i = 0; i < result.length
- && i < _valueWidths.length; i++) {
- correctedResult[i] = result[i];
- }
- result = correctedResult;
- }
- }
-
- if (_failOnInconsistentLineWidth) {
- _rowNumber++;
- if (constantWidth) {
- if (line.length() % _fixedValueWidth != 0) {
- throw new InconsistentValueWidthException(result, line,
- _rowNumber);
- }
- } else {
- if (result.length != values.size()) {
- throw new InconsistentValueWidthException(result, line,
- _rowNumber);
- }
-
- if (line.length() != expectedLineLength) {
- throw new InconsistentValueWidthException(result, line,
- _rowNumber);
- }
- }
- }
-
- return result;
- } catch (IOException e) {
- throw new IllegalStateException(e);
- }
- }
-
- @Override
- public void close() throws IOException {
- _reader.close();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/InconsistentValueWidthException.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/InconsistentValueWidthException.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/InconsistentValueWidthException.java
deleted file mode 100644
index 46b021a..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/InconsistentValueWidthException.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import org.eobjects.metamodel.InconsistentRowFormatException;
-import org.eobjects.metamodel.data.Row;
-
-/**
- * Exception thrown when inconsistent widths of a Fixed Width Value file
- *
- * @author Kasper Sørensen
- */
-public final class InconsistentValueWidthException extends
- InconsistentRowFormatException {
-
- private static final long serialVersionUID = 1L;
- private final String[] _sourceResult;
- private final String _sourceLine;
-
- public InconsistentValueWidthException(String[] result, String line,
- int rowNumber) {
- super(null, rowNumber);
- _sourceResult = result;
- _sourceLine = line;
- }
-
- public InconsistentValueWidthException(Row proposedRow,
- InconsistentValueWidthException cause) {
- super(proposedRow, cause.getRowNumber(), cause);
- _sourceResult = cause.getSourceResult();
- _sourceLine = cause.getSourceLine();
- }
-
- /**
- * Gets the source line as represented in the Fixed Width file
- *
- * @return the source line as a string
- */
- public String getSourceLine() {
- return _sourceLine;
- }
-
- /**
- * Gets the parsed result as read by the Fixed Width reader.
- *
- * @return the gracefully parsed line
- */
- public String[] getSourceResult() {
- return _sourceResult;
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/package-info.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/package-info.java b/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/package-info.java
deleted file mode 100644
index eaeb00b..0000000
--- a/fixedwidth/src/main/java/org/eobjects/metamodel/fixedwidth/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/**
- * 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.
- */
-/**
- * Module package for Fixed width value files
- */
-package org.eobjects.metamodel.fixedwidth;
-
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthConfigurationTest.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthConfigurationTest.java b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthConfigurationTest.java
new file mode 100644
index 0000000..ff9cb12
--- /dev/null
+++ b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthConfigurationTest.java
@@ -0,0 +1,44 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import org.eobjects.metamodel.fixedwidth.FixedWidthConfiguration;
+
+import junit.framework.TestCase;
+
+public class FixedWidthConfigurationTest extends TestCase {
+
+ public void testToString() throws Exception {
+ assertEquals(
+ "FixedWidthConfiguration[encoding=UTF8, fixedValueWidth=10, valueWidths=[], columnNameLineNumber=1, failOnInconsistentLineWidth=true]",
+ new FixedWidthConfiguration(1, "UTF8", 10, true).toString());
+ }
+
+ public void testEquals() throws Exception {
+ FixedWidthConfiguration conf1 = new FixedWidthConfiguration(1, "UTF8",
+ 10, true);
+ FixedWidthConfiguration conf2 = new FixedWidthConfiguration(1, "UTF8",
+ 10, true);
+ assertEquals(conf1, conf2);
+
+ FixedWidthConfiguration conf3 = new FixedWidthConfiguration(1, "UTF8",
+ 10, false);
+ assertFalse(conf1.equals(conf3));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthDataContextTest.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthDataContextTest.java b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthDataContextTest.java
new file mode 100644
index 0000000..cf95c84
--- /dev/null
+++ b/fixedwidth/src/test/java/org/apache/metamodel/fixedwidth/FixedWidthDataContextTest.java
@@ -0,0 +1,227 @@
+/**
+ * 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.eobjects.metamodel.fixedwidth;
+
+import java.io.File;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.fixedwidth.FixedWidthConfiguration;
+import org.eobjects.metamodel.fixedwidth.FixedWidthDataContext;
+import org.eobjects.metamodel.fixedwidth.InconsistentValueWidthException;
+import org.eobjects.metamodel.query.Query;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public class FixedWidthDataContextTest extends TestCase {
+
+ public void testEmptyFile() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/empty_file.txt"),
+ new FixedWidthConfiguration(10));
+ assertEquals(1, dc.getDefaultSchema().getTableCount());
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("empty_file", table.getName());
+ assertEquals(0, table.getColumnCount());
+ }
+
+ public void testEmptyFileNoHeaderLine() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/empty_file.txt"),
+ new FixedWidthConfiguration(FixedWidthConfiguration.NO_COLUMN_NAME_LINE, "UTF8", 10));
+ assertEquals(1, dc.getDefaultSchema().getTableCount());
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("empty_file", table.getName());
+ assertEquals(0, table.getColumnCount());
+ }
+
+ public void testUnexistingHeaderLine() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"),
+ new FixedWidthConfiguration(20, "UTF8", 10));
+ assertEquals(1, dc.getDefaultSchema().getTableCount());
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("example_simple1", table.getName());
+ assertEquals(0, table.getColumnCount());
+ }
+
+ public void testExampleSimple1() throws Exception {
+ FixedWidthConfiguration conf = new FixedWidthConfiguration(10);
+ FixedWidthDataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"), conf);
+
+ String[] schemaNames = dc.getSchemaNames();
+ assertEquals(2, schemaNames.length);
+ assertEquals("[information_schema, example_simple1.txt]", Arrays.toString(schemaNames));
+
+ Schema schema = dc.getDefaultSchema();
+ assertEquals("Schema[name=example_simple1.txt]", schema.toString());
+
+ assertEquals(1, schema.getTableCount());
+
+ Table table = schema.getTableByName("example_simple1");
+ assertEquals("Table[name=example_simple1,type=TABLE,remarks=null]", table.toString());
+
+ assertEquals("[greeting, greeter]", Arrays.toString(table.getColumnNames()));
+ assertEquals(10, table.getColumnByName("greeting").getColumnSize().intValue());
+ assertEquals(10, table.getColumnByName("greeter").getColumnSize().intValue());
+
+ Query q = dc.query().from(table).select(table.getColumns()).toQuery();
+ DataSet ds = dc.executeQuery(q);
+
+ assertTrue(ds.next());
+ assertEquals("[hello, world]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[hi, there]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[howdy, partner]", Arrays.toString(ds.getRow().getValues()));
+ assertFalse(ds.next());
+ }
+
+ public void testFailOnInconsistentWidth() throws Exception {
+ FixedWidthConfiguration conf = new FixedWidthConfiguration(FixedWidthConfiguration.NO_COLUMN_NAME_LINE, "UTF8",
+ 10, true);
+ FixedWidthDataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"), conf);
+
+ String[] schemaNames = dc.getSchemaNames();
+ assertEquals(2, schemaNames.length);
+ assertEquals("[information_schema, example_simple1.txt]", Arrays.toString(schemaNames));
+
+ Schema schema = dc.getDefaultSchema();
+ assertEquals("Schema[name=example_simple1.txt]", schema.toString());
+
+ assertEquals(1, schema.getTableCount());
+
+ Table table = schema.getTableByName("example_simple1");
+ assertEquals("Table[name=example_simple1,type=TABLE,remarks=null]", table.toString());
+
+ assertEquals("[A, B]", Arrays.toString(table.getColumnNames()));
+
+ Query q = dc.query().from(table).select(table.getColumns()).toQuery();
+ DataSet ds = dc.executeQuery(q);
+
+ assertTrue(ds.next());
+ assertEquals("greeting", ds.getRow().getValue(0));
+ assertEquals("greeter", ds.getRow().getValue(1));
+
+ try {
+ ds.next();
+ fail("Exception expected");
+ } catch (InconsistentValueWidthException e) {
+ assertEquals("Inconsistent row format of row no. 2.", e.getMessage());
+ assertEquals("[hello, world]", Arrays.toString(e.getProposedRow().getValues()));
+ }
+ assertTrue(ds.next());
+ assertEquals("[hi, there]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[howdy, partner]", Arrays.toString(ds.getRow().getValues()));
+ assertFalse(ds.next());
+ }
+
+ public void testVaryingValueLengthsCorrect() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
+ new FixedWidthConfiguration(new int[] { 1, 8, 7 }));
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));
+
+ assertEquals(1, table.getColumnByName("i").getColumnSize().intValue());
+ assertEquals(8, table.getColumnByName("greeting").getColumnSize().intValue());
+ assertEquals(7, table.getColumnByName("greeter").getColumnSize().intValue());
+
+ Query q = dc.query().from(table).select(table.getColumns()).toQuery();
+ DataSet ds = dc.executeQuery(q);
+
+ assertTrue(ds.next());
+ assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
+ assertFalse(ds.next());
+ }
+
+ public void testVaryingValueLengthsTooShortLength() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
+ new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 5, 7 }, true));
+ try {
+ dc.getDefaultSchema().getTables();
+ fail("Exception expected");
+ } catch (InconsistentValueWidthException e) {
+ assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
+ assertEquals("igreetinggreeter", e.getSourceLine());
+ assertEquals("[i, greet, inggree]", Arrays.toString(e.getSourceResult()));
+ }
+ }
+
+ public void testVaryingValueLengthsTooShortLengthErrorTolerant() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
+ new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 5,
+ 7 }, false));
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("[i, greet, inggree]", Arrays.toString(table.getColumnNames()));
+
+ Query q = dc.query().from(table).select(table.getColumns()).toQuery();
+ DataSet ds = dc.executeQuery(q);
+
+ assertTrue(ds.next());
+ assertEquals("[1, hello, worl]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[2, hi, ther]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[3, howdy, part]", Arrays.toString(ds.getRow().getValues()));
+ assertFalse(ds.next());
+ }
+
+ public void testVaryingValueLengthsTooLongLength() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
+ new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 8, 9 }, true));
+
+ try {
+ dc.getDefaultSchema().getTables();
+ fail("Exception expected");
+ } catch (InconsistentValueWidthException e) {
+ assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
+ assertEquals("igreetinggreeter", e.getSourceLine());
+ assertEquals("[i, greeting, greeter]", Arrays.toString(e.getSourceResult()));
+ }
+ }
+
+ public void testVaryingValueLengthsTooLongLengthErrorTolerant() throws Exception {
+ DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
+ new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 8,
+ 9 }, false));
+
+ Table table = dc.getDefaultSchema().getTables()[0];
+ assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));
+
+ Query q = dc.query().from(table).select(table.getColumns()).toQuery();
+ DataSet ds = dc.executeQuery(q);
+
+ assertTrue(ds.next());
+ assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
+ assertTrue(ds.next());
+ assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
+ assertFalse(ds.next());
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfigurationTest.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfigurationTest.java b/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfigurationTest.java
deleted file mode 100644
index 8cf6cd1..0000000
--- a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthConfigurationTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import org.eobjects.metamodel.fixedwidth.FixedWidthConfiguration;
-
-import junit.framework.TestCase;
-
-public class FixedWidthConfigurationTest extends TestCase {
-
- public void testToString() throws Exception {
- assertEquals(
- "FixedWidthConfiguration[encoding=UTF8, fixedValueWidth=10, valueWidths=[], columnNameLineNumber=1, failOnInconsistentLineWidth=true]",
- new FixedWidthConfiguration(1, "UTF8", 10, true).toString());
- }
-
- public void testEquals() throws Exception {
- FixedWidthConfiguration conf1 = new FixedWidthConfiguration(1, "UTF8",
- 10, true);
- FixedWidthConfiguration conf2 = new FixedWidthConfiguration(1, "UTF8",
- 10, true);
- assertEquals(conf1, conf2);
-
- FixedWidthConfiguration conf3 = new FixedWidthConfiguration(1, "UTF8",
- 10, false);
- assertFalse(conf1.equals(conf3));
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContextTest.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContextTest.java b/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContextTest.java
deleted file mode 100644
index cf95c84..0000000
--- a/fixedwidth/src/test/java/org/eobjects/metamodel/fixedwidth/FixedWidthDataContextTest.java
+++ /dev/null
@@ -1,227 +0,0 @@
-/**
- * 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.eobjects.metamodel.fixedwidth;
-
-import java.io.File;
-import java.util.Arrays;
-
-import junit.framework.TestCase;
-
-import org.eobjects.metamodel.DataContext;
-import org.eobjects.metamodel.data.DataSet;
-import org.eobjects.metamodel.fixedwidth.FixedWidthConfiguration;
-import org.eobjects.metamodel.fixedwidth.FixedWidthDataContext;
-import org.eobjects.metamodel.fixedwidth.InconsistentValueWidthException;
-import org.eobjects.metamodel.query.Query;
-import org.eobjects.metamodel.schema.Schema;
-import org.eobjects.metamodel.schema.Table;
-
-public class FixedWidthDataContextTest extends TestCase {
-
- public void testEmptyFile() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/empty_file.txt"),
- new FixedWidthConfiguration(10));
- assertEquals(1, dc.getDefaultSchema().getTableCount());
-
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("empty_file", table.getName());
- assertEquals(0, table.getColumnCount());
- }
-
- public void testEmptyFileNoHeaderLine() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/empty_file.txt"),
- new FixedWidthConfiguration(FixedWidthConfiguration.NO_COLUMN_NAME_LINE, "UTF8", 10));
- assertEquals(1, dc.getDefaultSchema().getTableCount());
-
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("empty_file", table.getName());
- assertEquals(0, table.getColumnCount());
- }
-
- public void testUnexistingHeaderLine() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"),
- new FixedWidthConfiguration(20, "UTF8", 10));
- assertEquals(1, dc.getDefaultSchema().getTableCount());
-
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("example_simple1", table.getName());
- assertEquals(0, table.getColumnCount());
- }
-
- public void testExampleSimple1() throws Exception {
- FixedWidthConfiguration conf = new FixedWidthConfiguration(10);
- FixedWidthDataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"), conf);
-
- String[] schemaNames = dc.getSchemaNames();
- assertEquals(2, schemaNames.length);
- assertEquals("[information_schema, example_simple1.txt]", Arrays.toString(schemaNames));
-
- Schema schema = dc.getDefaultSchema();
- assertEquals("Schema[name=example_simple1.txt]", schema.toString());
-
- assertEquals(1, schema.getTableCount());
-
- Table table = schema.getTableByName("example_simple1");
- assertEquals("Table[name=example_simple1,type=TABLE,remarks=null]", table.toString());
-
- assertEquals("[greeting, greeter]", Arrays.toString(table.getColumnNames()));
- assertEquals(10, table.getColumnByName("greeting").getColumnSize().intValue());
- assertEquals(10, table.getColumnByName("greeter").getColumnSize().intValue());
-
- Query q = dc.query().from(table).select(table.getColumns()).toQuery();
- DataSet ds = dc.executeQuery(q);
-
- assertTrue(ds.next());
- assertEquals("[hello, world]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[hi, there]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[howdy, partner]", Arrays.toString(ds.getRow().getValues()));
- assertFalse(ds.next());
- }
-
- public void testFailOnInconsistentWidth() throws Exception {
- FixedWidthConfiguration conf = new FixedWidthConfiguration(FixedWidthConfiguration.NO_COLUMN_NAME_LINE, "UTF8",
- 10, true);
- FixedWidthDataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple1.txt"), conf);
-
- String[] schemaNames = dc.getSchemaNames();
- assertEquals(2, schemaNames.length);
- assertEquals("[information_schema, example_simple1.txt]", Arrays.toString(schemaNames));
-
- Schema schema = dc.getDefaultSchema();
- assertEquals("Schema[name=example_simple1.txt]", schema.toString());
-
- assertEquals(1, schema.getTableCount());
-
- Table table = schema.getTableByName("example_simple1");
- assertEquals("Table[name=example_simple1,type=TABLE,remarks=null]", table.toString());
-
- assertEquals("[A, B]", Arrays.toString(table.getColumnNames()));
-
- Query q = dc.query().from(table).select(table.getColumns()).toQuery();
- DataSet ds = dc.executeQuery(q);
-
- assertTrue(ds.next());
- assertEquals("greeting", ds.getRow().getValue(0));
- assertEquals("greeter", ds.getRow().getValue(1));
-
- try {
- ds.next();
- fail("Exception expected");
- } catch (InconsistentValueWidthException e) {
- assertEquals("Inconsistent row format of row no. 2.", e.getMessage());
- assertEquals("[hello, world]", Arrays.toString(e.getProposedRow().getValues()));
- }
- assertTrue(ds.next());
- assertEquals("[hi, there]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[howdy, partner]", Arrays.toString(ds.getRow().getValues()));
- assertFalse(ds.next());
- }
-
- public void testVaryingValueLengthsCorrect() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
- new FixedWidthConfiguration(new int[] { 1, 8, 7 }));
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));
-
- assertEquals(1, table.getColumnByName("i").getColumnSize().intValue());
- assertEquals(8, table.getColumnByName("greeting").getColumnSize().intValue());
- assertEquals(7, table.getColumnByName("greeter").getColumnSize().intValue());
-
- Query q = dc.query().from(table).select(table.getColumns()).toQuery();
- DataSet ds = dc.executeQuery(q);
-
- assertTrue(ds.next());
- assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
- assertFalse(ds.next());
- }
-
- public void testVaryingValueLengthsTooShortLength() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
- new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 5, 7 }, true));
- try {
- dc.getDefaultSchema().getTables();
- fail("Exception expected");
- } catch (InconsistentValueWidthException e) {
- assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
- assertEquals("igreetinggreeter", e.getSourceLine());
- assertEquals("[i, greet, inggree]", Arrays.toString(e.getSourceResult()));
- }
- }
-
- public void testVaryingValueLengthsTooShortLengthErrorTolerant() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
- new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 5,
- 7 }, false));
-
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("[i, greet, inggree]", Arrays.toString(table.getColumnNames()));
-
- Query q = dc.query().from(table).select(table.getColumns()).toQuery();
- DataSet ds = dc.executeQuery(q);
-
- assertTrue(ds.next());
- assertEquals("[1, hello, worl]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[2, hi, ther]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[3, howdy, part]", Arrays.toString(ds.getRow().getValues()));
- assertFalse(ds.next());
- }
-
- public void testVaryingValueLengthsTooLongLength() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
- new FixedWidthConfiguration(0, "UTF8", new int[] { 1, 8, 9 }, true));
-
- try {
- dc.getDefaultSchema().getTables();
- fail("Exception expected");
- } catch (InconsistentValueWidthException e) {
- assertEquals("Inconsistent row format of row no. 1.", e.getMessage());
- assertEquals("igreetinggreeter", e.getSourceLine());
- assertEquals("[i, greeting, greeter]", Arrays.toString(e.getSourceResult()));
- }
- }
-
- public void testVaryingValueLengthsTooLongLengthErrorTolerant() throws Exception {
- DataContext dc = new FixedWidthDataContext(new File("src/test/resources/example_simple2.txt"),
- new FixedWidthConfiguration(FixedWidthConfiguration.DEFAULT_COLUMN_NAME_LINE, "UTF8", new int[] { 1, 8,
- 9 }, false));
-
- Table table = dc.getDefaultSchema().getTables()[0];
- assertEquals("[i, greeting, greeter]", Arrays.toString(table.getColumnNames()));
-
- Query q = dc.query().from(table).select(table.getColumns()).toQuery();
- DataSet ds = dc.executeQuery(q);
-
- assertTrue(ds.next());
- assertEquals("[1, hello, world]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[2, hi, there]", Arrays.toString(ds.getRow().getValues()));
- assertTrue(ds.next());
- assertEquals("[3, howdy, partner]", Arrays.toString(ds.getRow().getValues()));
- assertFalse(ds.next());
- }
-}