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