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