XlsSchemaParser implements SchemaParser { @Override public Schema parse(Request request) { final Marker marker = Markers.dataset(request.getMetadata().getId()); LOGGER.debug(marker, "parsing {} "); try { List<Schema.SheetContent> sheetContents = parseAllSheets(request); Schema result; if (!sheetContents.isEmpty()) { if (sheetContents.size() == 1) { result = Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .sheetName(sheetContents.get(0).getName()) .build(); } else { result = Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(true) .sheetName(sheetContents.get(0).getName()) .build(); } } else { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } return result; } catch (TDPException e) { throw e; } catch (Exception e) { LOGGER.debug(marker, "IOException during parsing xls request :" + e.getMessage(), e); throw new TDPException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e); } }  @Override Schema parse(Request request);  }
@Test public void read_xls_TDP_143() throws Exception { String fileName = "state_table.xls"; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { List<ColumnMetadata> columnMetadatas = parser.parse(getRequest(inputStream, "#852")).getSheetContents().get(0).getColumnMetadatas(); assertThat(columnMetadatas).isNotNull().isNotEmpty().hasSize(17); } }
@Test public void read_xls_TDP_1957() throws Exception { String fileName = "email_with_empty_rows.xlsx"; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { List<ColumnMetadata> columnMetadatas = parser.parse(getRequest(inputStream, "#852")).getSheetContents().get(0).getColumnMetadatas(); assertThat(columnMetadatas).isNotNull().isNotEmpty().hasSize(2); } }
@Test(expected = TDPException.class) public void read_xls_TDP_() throws Exception { String fileName = "many column.xlsx"; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { parser.parse(getRequest(inputStream, "#852")).getSheetContents().get(0).getColumnMetadatas(); } }
@Test public void parse_should_extract_single_sheet_xls() throws Exception { final String fileName = "simple.xls"; SchemaParser.Request request; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { request = getRequest(inputStream, "My Dataset"); final Schema schema = parser.parse(request); assertThat(schema.getSheetContents(), is(notNullValue())); assertThat(schema.draft(), is(false)); assertThat(schema.getSheetName(), is("Feuille1")); } }
@Test public void parse_should_extract_multi_sheet_xls() throws Exception { final String fileName = "Talend_Desk-Tableau-Bord-011214.xls"; SchemaParser.Request request; try (InputStream inputStream = this.getClass().getResourceAsStream(fileName)) { request = getRequest(inputStream, "My Dataset"); final Schema schema = parser.parse(request); assertThat(schema.getSheetContents(), is(notNullValue())); assertThat(schema.draft(), is(true)); assertThat(schema.getSheetName(), is("Sumary")); } }