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