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