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