@Test public void should_guess_separator_with_ISO_8859_1_encoded_file() throws IOException { try (InputStream inputStream = this.getClass().getResourceAsStream("iso-8859-1.csv")) { SchemaParser.Request request = getRequest(inputStream, "#3"); request.getMetadata().setEncoding("ISO-8859-1"); csvSchemaParser.parse(request); final Map<String, String> parameters = request.getMetadata().getContent().getParameters(); char actual = parameters.get(CSVFormatFamily.SEPARATOR_PARAMETER).charAt(0); assertEquals(';', actual); } }
@Override public Schema parse(Request request) { List<Schema.SheetContent> sheetContents = new ArrayList<>(); sheetContents.add(new Schema.SheetContent(META_KEY, new ArrayList<>())); try { final DataSetMetadata metadata = request.getMetadata(); final Map<String, String> parameters = guess(request, metadata.getEncoding()); metadata.getContent().setParameters(parameters); metadata.getContent().setNbLinesInHeader(Integer.parseInt(parameters.get(HEADER_NB_LINES_PARAMETER))); List<String> header = csvFormatUtils.retrieveHeader(parameters); if (header == null || header.isEmpty()) { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } LOGGER.debug("Columns found: {}", header); LOGGER.debug("Setting default type for columns..."); int i = 0; for (String column : header) { sheetContents .stream() .filter(sheetContent -> META_KEY.equals(sheetContent.getName())) .findFirst() .get() .getColumnMetadatas() .add(column().id(i++).name(column).type(Type.STRING).build()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_READ_CONTENT, e); } return Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .build(); }
CSVSchemaParser implements SchemaParser { @Override public Schema parse(Request request) { List<Schema.SheetContent> sheetContents = new ArrayList<>(); sheetContents.add(new Schema.SheetContent(META_KEY, new ArrayList<>())); try { final DataSetMetadata metadata = request.getMetadata(); final Map<String, String> parameters = guess(request, metadata.getEncoding()); metadata.getContent().setParameters(parameters); metadata.getContent().setNbLinesInHeader(Integer.parseInt(parameters.get(HEADER_NB_LINES_PARAMETER))); List<String> header = csvFormatUtils.retrieveHeader(parameters); if (header == null || header.isEmpty()) { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } LOGGER.debug("Columns found: {}", header); LOGGER.debug("Setting default type for columns..."); int i = 0; for (String column : header) { sheetContents .stream() .filter(sheetContent -> META_KEY.equals(sheetContent.getName())) .findFirst() .get() .getColumnMetadatas() .add(column().id(i++).name(column).type(Type.STRING).build()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_READ_CONTENT, e); } return Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .build(); } }
CSVSchemaParser implements SchemaParser { @Override public Schema parse(Request request) { List<Schema.SheetContent> sheetContents = new ArrayList<>(); sheetContents.add(new Schema.SheetContent(META_KEY, new ArrayList<>())); try { final DataSetMetadata metadata = request.getMetadata(); final Map<String, String> parameters = guess(request, metadata.getEncoding()); metadata.getContent().setParameters(parameters); metadata.getContent().setNbLinesInHeader(Integer.parseInt(parameters.get(HEADER_NB_LINES_PARAMETER))); List<String> header = csvFormatUtils.retrieveHeader(parameters); if (header == null || header.isEmpty()) { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } LOGGER.debug("Columns found: {}", header); LOGGER.debug("Setting default type for columns..."); int i = 0; for (String column : header) { sheetContents .stream() .filter(sheetContent -> META_KEY.equals(sheetContent.getName())) .findFirst() .get() .getColumnMetadatas() .add(column().id(i++).name(column).type(Type.STRING).build()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_READ_CONTENT, e); } return Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .build(); }  }
CSVSchemaParser implements SchemaParser { @Override public Schema parse(Request request) { List<Schema.SheetContent> sheetContents = new ArrayList<>(); sheetContents.add(new Schema.SheetContent(META_KEY, new ArrayList<>())); try { final DataSetMetadata metadata = request.getMetadata(); final Map<String, String> parameters = guess(request, metadata.getEncoding()); metadata.getContent().setParameters(parameters); metadata.getContent().setNbLinesInHeader(Integer.parseInt(parameters.get(HEADER_NB_LINES_PARAMETER))); List<String> header = csvFormatUtils.retrieveHeader(parameters); if (header == null || header.isEmpty()) { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } LOGGER.debug("Columns found: {}", header); LOGGER.debug("Setting default type for columns..."); int i = 0; for (String column : header) { sheetContents .stream() .filter(sheetContent -> META_KEY.equals(sheetContent.getName())) .findFirst() .get() .getColumnMetadatas() .add(column().id(i++).name(column).type(Type.STRING).build()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_READ_CONTENT, e); } return Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .build(); }  @Override boolean accept(DataSetMetadata metadata); @Override Schema parse(Request request); Map<String, String> guess(SchemaParser.Request request, String encoding); }
CSVSchemaParser implements SchemaParser { @Override public Schema parse(Request request) { List<Schema.SheetContent> sheetContents = new ArrayList<>(); sheetContents.add(new Schema.SheetContent(META_KEY, new ArrayList<>())); try { final DataSetMetadata metadata = request.getMetadata(); final Map<String, String> parameters = guess(request, metadata.getEncoding()); metadata.getContent().setParameters(parameters); metadata.getContent().setNbLinesInHeader(Integer.parseInt(parameters.get(HEADER_NB_LINES_PARAMETER))); List<String> header = csvFormatUtils.retrieveHeader(parameters); if (header == null || header.isEmpty()) { throw new TDPException(DataSetErrorCodes.UNABLE_TO_READ_DATASET_CONTENT); } LOGGER.debug("Columns found: {}", header); LOGGER.debug("Setting default type for columns..."); int i = 0; for (String column : header) { sheetContents .stream() .filter(sheetContent -> META_KEY.equals(sheetContent.getName())) .findFirst() .get() .getColumnMetadatas() .add(column().id(i++).name(column).type(Type.STRING).build()); } } catch (Exception e) { throw new TDPException(CommonErrorCodes.UNABLE_TO_READ_CONTENT, e); } return Schema.Builder .parserResult() .sheetContents(sheetContents) .draft(false) .build(); }  @Override boolean accept(DataSetMetadata metadata); @Override Schema parse(Request request); Map<String, String> guess(SchemaParser.Request request, String encoding); static final List<Character> DEFAULT_VALID_SEPARATORS; }