001package org.hl7.fhir.r5.elementmodel;
002
003import java.io.ByteArrayInputStream;
004
005/*
006  Copyright (c) 2011+, HL7, Inc.
007  All rights reserved.
008
009  Redistribution and use in source and binary forms, with or without modification, 
010  are permitted provided that the following conditions are met:
011
012 * Redistributions of source code must retain the above copyright notice, this 
013     list of conditions and the following disclaimer.
014 * Redistributions in binary form must reproduce the above copyright notice, 
015     this list of conditions and the following disclaimer in the documentation 
016     and/or other materials provided with the distribution.
017 * Neither the name of HL7 nor the names of its contributors may be used to 
018     endorse or promote products derived from this software without specific 
019     prior written permission.
020
021  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
022  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
023  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
024  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
025  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
026  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
027  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
028  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
029  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
030  POSSIBILITY OF SUCH DAMAGE.
031
032 */
033
034import java.io.IOException;
035import java.io.InputStream;
036import java.io.OutputStream;
037import java.nio.charset.StandardCharsets;
038import java.util.ArrayList;
039import java.util.List;
040
041import org.hl7.fhir.exceptions.FHIRException;
042import org.hl7.fhir.r5.conformance.profile.ProfileUtilities;
043import org.hl7.fhir.r5.context.IWorkerContext;
044import org.hl7.fhir.r5.formats.IParser.OutputStyle;
045import org.hl7.fhir.r5.formats.JsonCreator;
046import org.hl7.fhir.utilities.TextFile;
047import org.hl7.fhir.utilities.Utilities;
048import org.hl7.fhir.utilities.i18n.I18nConstants;
049import org.hl7.fhir.utilities.validation.ValidationMessage;
050import org.hl7.fhir.utilities.validation.ValidationMessage.IssueSeverity;
051import org.hl7.fhir.utilities.validation.ValidationMessage.IssueType;
052
053
054public class NDJsonParser extends ParserBase {
055
056  public NDJsonParser(IWorkerContext context, ProfileUtilities utilities) {
057    super(context, utilities);
058  }
059
060  public NDJsonParser(IWorkerContext context) {
061    super(context);
062  }
063  
064  private ValidatedFragment processLine(int lineCount, String line) throws FHIRException, IOException {
065    List<ValidatedFragment> list = new JsonParser(context).parse(new ByteArrayInputStream(line.getBytes(StandardCharsets.UTF_8)), lineCount);
066    return list.get(0);
067  }
068
069  @Override
070  public List<ValidatedFragment> parse(InputStream inStream) throws IOException, FHIRException {
071    String source = TextFile.streamToString(inStream);
072    int length = source.length();
073    int start = 0;
074    int cursor = start;
075    int lineCount = 0;
076
077    List<ValidatedFragment> res = new ArrayList<>();
078    while (cursor < length) {
079      while (cursor < length && source.charAt(cursor) != '\n') {
080        cursor++;
081      }
082      if (cursor < length) {
083        String line = source.substring(start, cursor);
084        if (line.endsWith("\r")) {
085          line = line.substring(0, line.length()-1);
086        }
087        if (Utilities.noString(line.trim())) {
088          ValidatedFragment vf = new ValidatedFragment(ValidatedFragment.ITEM_NAME, null, null, false);
089          logError(vf.getErrors(), "2024-06-30", lineCount+1, 1, null, IssueType.INFORMATIONAL, context.formatMessage(I18nConstants.NDJSON_EMPTY_LINE_WARNING), IssueSeverity.WARNING);
090          res.add(vf);
091        } else {
092          res.add(processLine(lineCount, line));
093        }
094        start = cursor+1;
095      } else if (cursor > start) {
096        String line = source.substring(start, cursor);
097        if (line.endsWith("\r")) {
098          line = line.substring(0, line.length()-1);
099        }
100        if (Utilities.noString(line.trim())) {
101          ValidatedFragment vf = new ValidatedFragment(ValidatedFragment.ITEM_NAME, null, null, false);
102          logError(vf.getErrors(), "2024-06-30", lineCount+1, 1, null, IssueType.INFORMATIONAL, context.formatMessage(I18nConstants.NDJSON_EMPTY_LINE_WARNING), IssueSeverity.WARNING);
103          res.add(vf);
104        } else {
105          res.add(processLine(lineCount, line));
106        }
107        start = cursor+1;
108      }
109      lineCount++;
110      cursor++;
111    }
112    return res;
113  }
114  
115  @Override
116  public void compose(Element e, OutputStream stream, OutputStyle style, String identity) throws FHIRException, IOException {
117    throw new Error("Not done yet");
118  }
119
120}