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.nio.charset.StandardCharsets; 044import java.text.ParseException; 045import java.util.HashMap; 046import java.util.Map; 047 048import org.apache.commons.codec.binary.Base64; 049import org.hl7.fhir.exceptions.FHIRFormatError; 050import org.hl7.fhir.r5.model.DataType; 051import org.hl7.fhir.r5.model.Resource; 052import org.hl7.fhir.r5.model.SearchParameter; 053import org.hl7.fhir.utilities.Utilities; 054 055public abstract class ParserBase extends FormatUtilities implements IParser { 056 057 // -- implementation of variant type methods from the interface -------------------------------- 058 059 public Resource parse(String input) throws FHIRFormatError, IOException { 060 return parse(input.getBytes(StandardCharsets.UTF_8)); 061 } 062 063 public Resource parse(byte[] bytes) throws FHIRFormatError, IOException { 064 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 065 return parse(bi); 066 } 067 068 public DataType parseType(String input, String typeName) throws FHIRFormatError, IOException { 069 return parseType(input.getBytes(StandardCharsets.UTF_8), typeName); 070 } 071 072 public DataType parseAnyType(String input, String typeName) throws FHIRFormatError, IOException { 073 return parseAnyType(input.getBytes(StandardCharsets.UTF_8), typeName); 074 } 075 076 public DataType parseType(byte[] bytes, String typeName) throws FHIRFormatError, IOException { 077 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 078 return parseType(bi, typeName); 079 } 080 081 public DataType parseAnyType(byte[] bytes, String typeName) throws FHIRFormatError, IOException { 082 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 083 return parseAnyType(bi, typeName); 084 } 085 086 public String composeString(Resource resource) throws IOException { 087 return new String(composeBytes(resource), StandardCharsets.UTF_8); 088 } 089 090 public byte[] composeBytes(Resource resource) throws IOException { 091 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 092 compose(bytes, resource); 093 bytes.close(); 094 return bytes.toByteArray(); 095 } 096 097 public String composeString(DataType type, String typeName) throws IOException { 098 return new String(composeBytes(type, typeName), StandardCharsets.UTF_8); 099 } 100 101 public byte[] composeBytes(DataType type, String typeName) throws IOException { 102 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 103 compose(bytes, type, typeName); 104 bytes.close(); 105 return bytes.toByteArray(); 106 } 107 108 109 // -- Parser Configuration -------------------------------- 110 111 protected String xhtmlMessage; 112 113 @Override 114 public IParser setSuppressXhtml(String message) { 115 xhtmlMessage = message; 116 return this; 117 } 118 119 protected boolean handleComments = false; 120 121 public boolean getHandleComments() { 122 return handleComments; 123 } 124 125 public IParser setHandleComments(boolean value) { 126 this.handleComments = value; 127 return this; 128 } 129 130 /** 131 * Whether to throw an exception if unknown content is found (or just skip it) 132 */ 133 protected boolean allowUnknownContent; 134 135 /** 136 * whether to allow comments in the json (special case for IG publisher source) 137 */ 138 protected boolean allowComments; 139 140 /** 141 * @return Whether to throw an exception if unknown content is found (or just skip it) 142 */ 143 public boolean isAllowUnknownContent() { 144 return true; // allowUnknownContent; 145 } 146 /** 147 * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it) 148 */ 149 public IParser setAllowUnknownContent(boolean allowUnknownContent) { 150 this.allowUnknownContent = allowUnknownContent; 151 return this; 152 } 153 154 public boolean isAllowComments() { 155 return allowComments; 156 } 157 158 public void setAllowComments(boolean allowComments) { 159 this.allowComments = allowComments; 160 } 161 162 protected OutputStyle style = OutputStyle.NORMAL; 163 164 public OutputStyle getOutputStyle() { 165 return style; 166 } 167 168 public IParser setOutputStyle(OutputStyle style) { 169 this.style = style; 170 return this; 171 } 172 173 // -- Parser Utilities -------------------------------- 174 175 176 protected Map<String, Object> idMap = new HashMap<String, Object>(); 177 178 179 protected int parseIntegerPrimitive(String value) { 180 if (value.startsWith("+") && Utilities.isInteger(value.substring(1))) 181 value = value.substring(1); 182 return java.lang.Integer.parseInt(value); 183 } 184 protected int parseIntegerPrimitive(java.lang.Long value) { 185 if (value < java.lang.Integer.MIN_VALUE || value > java.lang.Integer.MAX_VALUE) { 186 throw new IllegalArgumentException 187 (value + " cannot be cast to int without changing its value."); 188 } 189 return value.intValue(); 190 } 191 192 193 protected String parseCodePrimitive(String value) { 194 return value; 195 } 196 197 protected String parseTimePrimitive(String value) throws ParseException { 198 return value; 199 } 200 201 protected BigDecimal parseDecimalPrimitive(BigDecimal value) { 202 return value; 203 } 204 205 protected BigDecimal parseDecimalPrimitive(String value) { 206 return new BigDecimal(value); 207 } 208 209 protected String parseUriPrimitive(String value) { 210 return value; 211 } 212 213 protected byte[] parseBase64BinaryPrimitive(String value) { 214 return Base64.decodeBase64(value.getBytes()); 215 } 216 217 protected String parseOidPrimitive(String value) { 218 return value; 219 } 220 221 protected Boolean parseBooleanPrimitive(String value) { 222 return java.lang.Boolean.valueOf(value); 223 } 224 225 protected Boolean parseBooleanPrimitive(Boolean value) { 226 return java.lang.Boolean.valueOf(value); 227 } 228 229 protected String parseIdPrimitive(String value) { 230 return value; 231 } 232 233 protected String parseStringPrimitive(String value) { 234 return value; 235 } 236 237 protected String parseUuidPrimitive(String value) { 238 return value; 239 } 240 241 @Override 242 public Resource parseAndClose(InputStream input) throws IOException, FHIRFormatError { 243 try { 244 return parse(input); 245 } finally { 246 input.close(); 247 } 248 } 249 250 251}