001/*
002Copyright (c) 2011+, HL7, Inc
003All rights reserved.
004
005Redistribution and use in source and binary forms, with or without modification, 
006are permitted provided that the following conditions are met:
007
008 * Redistributions of source code must retain the above copyright notice, this 
009   list of conditions and the following disclaimer.
010 * Redistributions in binary form must reproduce the above copyright notice, 
011   this list of conditions and the following disclaimer in the documentation 
012   and/or other materials provided with the distribution.
013 * Neither the name of HL7 nor the names of its contributors may be used to 
014   endorse or promote products derived from this software without specific 
015   prior written permission.
016
017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
026POSSIBILITY OF SUCH DAMAGE.
027
028*/
029package org.hl7.fhir.utilities;
030
031/*-
032 * #%L
033 * org.hl7.fhir.utilities
034 * %%
035 * Copyright (C) 2014 - 2019 Health Level 7
036 * %%
037 * Licensed under the Apache License, Version 2.0 (the "License");
038 * you may not use this file except in compliance with the License.
039 * You may obtain a copy of the License at
040 * 
041 *      http://www.apache.org/licenses/LICENSE-2.0
042 * 
043 * Unless required by applicable law or agreed to in writing, software
044 * distributed under the License is distributed on an "AS IS" BASIS,
045 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
046 * See the License for the specific language governing permissions and
047 * limitations under the License.
048 * #L%
049 */
050
051
052
053import java.io.IOException;
054import java.io.InputStream;
055import java.io.InputStreamReader;
056import java.io.OutputStream;
057import java.io.OutputStreamWriter;
058import java.io.UnsupportedEncodingException;
059
060import org.hl7.fhir.exceptions.FHIRException;
061
062/**
063 * A file processor that reads a templated source file with markers ([%columnname%]), reads data
064 * from a CSV file and inserts data from that CSV file into those markers. Supports loops to
065 * interate through the CSV file.
066 * @author Ewout
067 *
068 */
069public class CSVProcessor {
070
071  public class DataReader extends CSVReader {
072
073    public DataReader(InputStream data) throws FHIRException, IOException {
074      super(data);
075    }
076
077    public void process() throws IOException, FHIRException  {
078      String[] titles = parseLine();
079      while (ready())
080      {
081        String[] values = parseLine();
082        processLine(titles, values);
083      }     
084      close();
085    }
086
087    private void processLine(String[] titles, String[] values) throws FHIRException  {
088      count++;
089      String src = loop;
090      while (src.contains("[%")) {
091        int i1 = src.indexOf("[%");
092        int i2 = src.indexOf("%]");
093        String s1 = src.substring(0, i1);
094        String s2 = src.substring(i1 + 2, i2).trim();
095        String s3 = src.substring(i2+2);
096        if ("count".equals(s2))
097          src = s1+Integer.toString(count)+s3;
098        else {
099          boolean b = false;
100          for (String t : titles) {
101            if (t.equals(s2)) {
102              src = s1+getColumn(titles, values, s2)+s3;
103              b = true;
104            }
105          }
106          if (!b)
107            throw new FHIRException("unknown column: '"+s2+"'");
108        }
109      }
110      dest.append(src);
111    }
112  }
113
114  private InputStream source;
115  private DataReader data;
116  private OutputStreamWriter out;
117
118  private String start;
119  private String loop;
120  private int count = 0;
121  private String stop;
122  private StringBuilder dest;
123  
124  public void setSource(InputStream source) {
125    this.source = source;
126  }
127
128  public void setData(InputStream data) throws FHIRException, IOException {
129    try {
130      this.data = new DataReader(data);
131    } catch (UnsupportedEncodingException e) {
132      // DataReader is fixed to "UTF-8", so this exception cannot really occur
133    }   
134  }
135
136  public void setOutput(OutputStream out) throws UnsupportedEncodingException {
137    this.out = new OutputStreamWriter(out, "UTF-8");   
138  }
139
140  public void process() throws IOException, FHIRException  {
141    buildTemplate(readSource());
142    dest = new StringBuilder();
143    dest.append(start);
144    data.process();
145    dest.append(stop);
146    out.write(dest.toString());
147    out.close();
148  }
149
150  private void buildTemplate(String template) throws FHIRException  {
151    int i = template.indexOf("[%loop");
152    if (i < 0)
153      throw new FHIRException("Unable to process template - didn't find [%loop");
154    start = template.substring(0, i);
155    template = template.substring(i+6);
156    i = template.indexOf("%]");
157    if (i < 0)
158      throw new FHIRException("Unable to process template - didn't find %] matching [%loop");
159    String tmp = template.substring(0, i);
160    if (tmp != null && !tmp.equals("")) {
161      if (!tmp.startsWith(" count="))
162        throw new FHIRException("Unable to process template - unrecognised content on [%loop");
163      count = Integer.parseInt(tmp.substring(7));
164    }
165    
166    template = template.substring(i+2);
167    i = template.indexOf("[%endloop%]");
168    if (i < 0)
169      throw new FHIRException("Unable to process template - didn't find [%endloop%]");
170    loop = template.substring(0, i);
171    stop = template.substring(i+11);
172  }
173
174  private String readSource() throws IOException  {
175    StringBuilder s = new StringBuilder();
176    InputStreamReader r = new InputStreamReader(source,"UTF-8");
177    while (r.ready()) {
178      s.append((char) r.read()); 
179    }
180    r.close();
181    return s.toString();
182  }
183
184}