From 6df617ce2b1c5d1bfef6e32798c9a75c35b63090 Mon Sep 17 00:00:00 2001 From: oliveregger Date: Thu, 6 Jul 2023 23:33:52 +0200 Subject: [PATCH] detect delimiter and change canonical --- .../hl7/fhir/r5/elementmodel/CsvParser.java | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/CsvParser.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/CsvParser.java index d5b70654db..95adf4ded7 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/CsvParser.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/elementmodel/CsvParser.java @@ -1,6 +1,8 @@ package org.hl7.fhir.r5.elementmodel; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -12,6 +14,7 @@ import java.util.Map; import java.util.Set; + import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.hl7.fhir.exceptions.DefinitionException; @@ -29,7 +32,7 @@ import org.hl7.fhir.r5.utils.FHIRPathEngine; public class CsvParser extends ParserBase { - public static final String CSV_SD_URI = "http://hl7.org/fhir/StructureDefinition/CSV"; + public static final String CSV_SD_URI = "http://hl7.org/fhir/tools/StructureDefinition/CSV"; final private static String CSV = "CSV"; final private static String RECORD = "CSV.record"; @@ -49,13 +52,42 @@ public List parse(final InputStream stream) throws IOException, FHIRFormatError, DefinitionException, FHIRException { final List res = new ArrayList<>(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); - try (final CSVParser parser = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader())) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + stream.transferTo(baos); + + InputStream firstClone = new ByteArrayInputStream(baos.toByteArray()); + InputStream secondClone = new ByteArrayInputStream(baos.toByteArray()); + + final BufferedReader dectectReader = new BufferedReader(new InputStreamReader(firstClone)); + + final String firstLine = dectectReader.readLine(); + int countSemicolon = 0; + int countComma = 0; + char delimiter = ','; + if (firstLine != null) { + for (int i = 0; i < firstLine.length(); i++) { + final char c = firstLine.charAt(i); + if (c == ';') { + ++countSemicolon; + } + if (c == ',') { + ++countComma; + } + } + } + + if (countSemicolon > countComma) { + delimiter = ';'; + } + + final BufferedReader reader = new BufferedReader(new InputStreamReader(secondClone)); + + try (final CSVParser parser = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader().withDelimiter(delimiter))) { final String[] headers; final ElementDefinition recordDefinition; - if (CSV_SD_URI.equals(logical.getUrl())) { + if (logical == null || CSV_SD_URI.equals(logical.getUrl())) { final List names = parser.getHeaderNames(); headers = names.toArray(new String[names.size()]);