001package org.hl7.fhir.dstu2016may.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.dstu2016may.model.Resource; 047import org.hl7.fhir.dstu2016may.model.Type; 048import org.hl7.fhir.exceptions.FHIRFormatError; 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 parseType(byte[] bytes, String typeName) throws FHIRFormatError, IOException { 069 ByteArrayInputStream bi = new ByteArrayInputStream(bytes); 070 return parseType(bi, typeName); 071 } 072 073 public String composeString(Resource resource) throws IOException { 074 return new String(composeBytes(resource)); 075 } 076 077 public byte[] composeBytes(Resource resource) throws IOException { 078 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 079 compose(bytes, resource); 080 bytes.close(); 081 return bytes.toByteArray(); 082 } 083 084 public String composeString(Type type, String typeName) throws IOException { 085 return new String(composeBytes(type, typeName)); 086 } 087 088 public byte[] composeBytes(Type type, String typeName) throws IOException { 089 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 090 compose(bytes, type, typeName); 091 bytes.close(); 092 return bytes.toByteArray(); 093 } 094 095 // -- Parser Configuration -------------------------------- 096 097 protected String xhtmlMessage; 098 099 @Override 100 public IParser setSuppressXhtml(String message) { 101 xhtmlMessage = message; 102 return this; 103 } 104 105 protected boolean handleComments = false; 106 107 public boolean getHandleComments() { 108 return handleComments; 109 } 110 111 public IParser setHandleComments(boolean value) { 112 this.handleComments = value; 113 return this; 114 } 115 116 /** 117 * Whether to throw an exception if unknown content is found (or just skip it) 118 */ 119 protected boolean allowUnknownContent; 120 121 /** 122 * @return Whether to throw an exception if unknown content is found (or just skip it) 123 */ 124 public boolean isAllowUnknownContent() { 125 return allowUnknownContent; 126 } 127 /** 128 * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it) 129 */ 130 public IParser setAllowUnknownContent(boolean allowUnknownContent) { 131 this.allowUnknownContent = allowUnknownContent; 132 return this; 133 } 134 135 protected OutputStyle style = OutputStyle.NORMAL; 136 137 public OutputStyle getOutputStyle() { 138 return style; 139 } 140 141 public IParser setOutputStyle(OutputStyle style) { 142 this.style = style; 143 return this; 144 } 145 146 // -- Parser Utilities -------------------------------- 147 148 149 protected Map<String, Object> idMap = new HashMap<String, Object>(); 150 151 152 protected int parseIntegerPrimitive(String value) { 153 if (value.startsWith("+") && Utilities.isInteger(value.substring(1))) 154 value = value.substring(1); 155 return java.lang.Integer.parseInt(value); 156 } 157 protected int parseIntegerPrimitive(java.lang.Long value) { 158 if (value < java.lang.Integer.MIN_VALUE || value > java.lang.Integer.MAX_VALUE) { 159 throw new IllegalArgumentException 160 (value + " cannot be cast to int without changing its value."); 161 } 162 return value.intValue(); 163 } 164 165 166 protected String parseCodePrimitive(String value) { 167 return value; 168 } 169 170 protected String parseTimePrimitive(String value) throws ParseException { 171 return value; 172 } 173 174 protected BigDecimal parseDecimalPrimitive(BigDecimal value) { 175 return value; 176 } 177 178 protected BigDecimal parseDecimalPrimitive(String value) { 179 return new BigDecimal(value); 180 } 181 182 protected String parseUriPrimitive(String value) { 183 return value; 184 } 185 186 protected byte[] parseBase64BinaryPrimitive(String value) { 187 return Base64.decodeBase64(value.getBytes()); 188 } 189 190 protected String parseOidPrimitive(String value) { 191 return value; 192 } 193 194 protected Boolean parseBooleanPrimitive(String value) { 195 return java.lang.Boolean.valueOf(value); 196 } 197 198 protected Boolean parseBooleanPrimitive(Boolean value) { 199 return java.lang.Boolean.valueOf(value); 200 } 201 202 protected String parseIdPrimitive(String value) { 203 return value; 204 } 205 206 protected String parseStringPrimitive(String value) { 207 return value; 208 } 209 210 protected String parseUuidPrimitive(String value) { 211 return value; 212 } 213 214 215}