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