001package org.hl7.fhir.r4.formats; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r4 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.exceptions.FHIRFormatError; 065import org.hl7.fhir.r4.model.Resource; 066import org.hl7.fhir.r4.model.Type; 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 parseAnyType(String input, String typeName) throws FHIRFormatError, IOException { 087 return parseAnyType(input.getBytes("UTF-8"), typeName); 088 } 089 090 public Type parseType(byte[] bytes, String typeName) throws FHIRFormatError, IOException { 091 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 092 return parseType(bi, typeName); 093 } 094 095 public Type parseAnyType(byte[] bytes, String typeName) throws FHIRFormatError, IOException { 096 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 097 return parseAnyType(bi, typeName); 098 } 099 100 public String composeString(Resource resource) throws IOException { 101 return new String(composeBytes(resource), "UTF-8"); 102 } 103 104 public byte[] composeBytes(Resource resource) throws IOException { 105 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 106 compose(bytes, resource); 107 bytes.close(); 108 return bytes.toByteArray(); 109 } 110 111 public String composeString(Type type, String typeName) throws IOException { 112 return new String(composeBytes(type, typeName)); 113 } 114 115 public byte[] composeBytes(Type type, String typeName) throws IOException { 116 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 117 compose(bytes, type, typeName); 118 bytes.close(); 119 return bytes.toByteArray(); 120 } 121 122 // -- Parser Configuration -------------------------------- 123 124 protected String xhtmlMessage; 125 126 @Override 127 public IParser setSuppressXhtml(String message) { 128 xhtmlMessage = message; 129 return this; 130 } 131 132 protected boolean handleComments = false; 133 134 public boolean getHandleComments() { 135 return handleComments; 136 } 137 138 public IParser setHandleComments(boolean value) { 139 this.handleComments = value; 140 return this; 141 } 142 143 /** 144 * Whether to throw an exception if unknown content is found (or just skip it) 145 */ 146 protected boolean allowUnknownContent; 147 148 /** 149 * @return Whether to throw an exception if unknown content is found (or just skip it) 150 */ 151 public boolean isAllowUnknownContent() { 152 return allowUnknownContent; 153 } 154 /** 155 * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it) 156 */ 157 public IParser setAllowUnknownContent(boolean allowUnknownContent) { 158 this.allowUnknownContent = allowUnknownContent; 159 return this; 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 242}