001package org.hl7.fhir.r5.formats;
002
003
004
005
006
007/*
008  Copyright (c) 2011+, HL7, Inc.
009  All rights reserved.
010  
011  Redistribution and use in source and binary forms, with or without modification, 
012  are permitted provided that the following conditions are met:
013  
014   * Redistributions of source code must retain the above copyright notice, this 
015     list of conditions and the following disclaimer.
016   * Redistributions in binary form must reproduce the above copyright notice, 
017     this list of conditions and the following disclaimer in the documentation 
018     and/or other materials provided with the distribution.
019   * Neither the name of HL7 nor the names of its contributors may be used to 
020     endorse or promote products derived from this software without specific 
021     prior written permission.
022  
023  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
024  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
025  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
026  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
027  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
028  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
029  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
030  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
031  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
032  POSSIBILITY OF SUCH DAMAGE.
033  
034*/
035
036
037import java.io.ByteArrayInputStream;
038import java.io.ByteArrayOutputStream;
039import java.io.FileInputStream;
040import java.io.IOException;
041import java.io.InputStream;
042import java.math.BigDecimal;
043import java.text.ParseException;
044import java.util.HashMap;
045import java.util.Map;
046
047import org.apache.commons.codec.binary.Base64;
048import org.hl7.fhir.exceptions.FHIRFormatError;
049import org.hl7.fhir.r5.model.DataType;
050import org.hl7.fhir.r5.model.Resource;
051import org.hl7.fhir.r5.model.SearchParameter;
052import org.hl7.fhir.utilities.Utilities;
053
054public abstract class ParserBase extends FormatUtilities implements IParser {
055
056  // -- implementation of variant type methods from the interface --------------------------------
057  
058  public Resource parse(String input) throws FHIRFormatError, IOException {
059        return parse(input.getBytes("UTF-8"));
060  }
061  
062  public Resource parse(byte[] bytes) throws FHIRFormatError, IOException {
063        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
064        return parse(bi);
065  }
066
067  public DataType parseType(String input, String typeName) throws FHIRFormatError, IOException {
068    return parseType(input.getBytes("UTF-8"), typeName);
069  }
070  
071  public DataType parseAnyType(String input, String typeName) throws FHIRFormatError, IOException {
072    return parseAnyType(input.getBytes("UTF-8"), typeName);
073  }
074  
075  public DataType parseType(byte[] bytes, String typeName) throws FHIRFormatError, IOException {
076    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
077    return parseType(bi, typeName);
078  }
079
080  public DataType parseAnyType(byte[] bytes, String typeName) throws FHIRFormatError, IOException {
081    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
082    return parseAnyType(bi, typeName);
083  }
084
085  public String composeString(Resource resource) throws IOException {
086    return new String(composeBytes(resource), "UTF-8");
087  }
088
089  public byte[] composeBytes(Resource resource) throws IOException {
090    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
091    compose(bytes, resource);
092    bytes.close();
093    return bytes.toByteArray();
094  }
095
096  public String composeString(DataType type, String typeName) throws IOException {
097    return new String(composeBytes(type, typeName));
098  }
099
100  public byte[] composeBytes(DataType type, String typeName) throws IOException {
101    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
102    compose(bytes, type, typeName);
103    bytes.close();
104    return bytes.toByteArray();
105  }
106
107
108  // -- Parser Configuration --------------------------------
109
110  protected String xhtmlMessage;
111  
112        @Override
113  public IParser setSuppressXhtml(String message) {
114    xhtmlMessage = message;
115    return this;
116  }
117  
118  protected boolean handleComments = false;
119  
120  public boolean getHandleComments() {
121    return handleComments;
122  }
123
124  public IParser setHandleComments(boolean value) {
125    this.handleComments = value;
126    return this;
127  }
128  
129  /**
130   * Whether to throw an exception if unknown content is found (or just skip it)
131   */
132  protected boolean allowUnknownContent;
133  
134  /**
135   * whether to allow comments in the json (special case for IG publisher source)
136   */
137  protected boolean allowComments;
138  
139  /**
140   * @return Whether to throw an exception if unknown content is found (or just skip it) 
141   */
142  public boolean isAllowUnknownContent() {
143    return allowUnknownContent;
144  }
145  /**
146   * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it)
147   */
148  public IParser setAllowUnknownContent(boolean allowUnknownContent) {
149    this.allowUnknownContent = allowUnknownContent;
150    return this;
151  }
152    
153  public boolean isAllowComments() {
154    return allowComments;
155  }
156
157  public void setAllowComments(boolean allowComments) {
158    this.allowComments = allowComments;
159  }
160
161  protected OutputStyle style = OutputStyle.NORMAL;
162  
163  public OutputStyle getOutputStyle() {
164    return style;
165  }
166
167  public IParser setOutputStyle(OutputStyle style) {
168    this.style = style;
169    return this;
170  }
171  
172  // -- Parser Utilities --------------------------------
173        
174
175  protected Map<String, Object> idMap = new HashMap<String, Object>();
176
177
178  protected int parseIntegerPrimitive(String value) {
179    if (value.startsWith("+") && Utilities.isInteger(value.substring(1)))
180      value = value.substring(1);
181        return java.lang.Integer.parseInt(value);
182  }
183  protected int parseIntegerPrimitive(java.lang.Long value) {
184    if (value < java.lang.Integer.MIN_VALUE || value > java.lang.Integer.MAX_VALUE) {
185        throw new IllegalArgumentException
186            (value + " cannot be cast to int without changing its value.");
187    }
188    return value.intValue();
189  }
190
191
192  protected String parseCodePrimitive(String value) {
193    return value;
194  }
195
196  protected String parseTimePrimitive(String value) throws ParseException {
197    return value;
198  }
199
200  protected BigDecimal parseDecimalPrimitive(BigDecimal value) {
201    return value;
202  }
203
204  protected BigDecimal parseDecimalPrimitive(String value) {
205    return new BigDecimal(value);
206  }
207
208  protected String parseUriPrimitive(String value) {
209         return value;
210  }
211
212  protected byte[] parseBase64BinaryPrimitive(String value) {
213    return Base64.decodeBase64(value.getBytes());
214  }
215  
216  protected String parseOidPrimitive(String value) {
217    return value;
218  }
219
220  protected Boolean parseBooleanPrimitive(String value) {
221    return java.lang.Boolean.valueOf(value);
222  }
223  
224  protected Boolean parseBooleanPrimitive(Boolean value) {
225    return java.lang.Boolean.valueOf(value);
226  }
227  
228  protected String parseIdPrimitive(String value) {
229    return value;
230  }
231
232  protected String parseStringPrimitive(String value) {
233    return value;
234  }
235
236  protected String parseUuidPrimitive(String value) {
237    return value;
238  }
239
240  @Override
241  public Resource parseAndClose(InputStream input) throws IOException, FHIRFormatError {
242    try {
243      return parse(input);
244    } finally {
245      input.close();
246    }
247  }
248
249
250}