Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect delimiter and change canonical #1

Open
wants to merge 1 commit into
base: feature/20230627-csv-parser-for-fml
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -49,13 +52,42 @@ public List<NamedElement> parse(final InputStream stream)
throws IOException, FHIRFormatError, DefinitionException, FHIRException {
final List<NamedElement> 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<String> names = parser.getHeaderNames();

headers = names.toArray(new String[names.size()]);
Expand Down