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
025import java.io.IOException;
026
027/*
028  Copyright (c) 2011+, HL7, Inc.
029  All rights reserved.
030  
031  Redistribution and use in source and binary forms, with or without modification, 
032  are permitted provided that the following conditions are met:
033  
034   * Redistributions of source code must retain the above copyright notice, this 
035     list of conditions and the following disclaimer.
036   * Redistributions in binary form must reproduce the above copyright notice, 
037     this list of conditions and the following disclaimer in the documentation 
038     and/or other materials provided with the distribution.
039   * Neither the name of HL7 nor the names of its contributors may be used to 
040     endorse or promote products derived from this software without specific 
041     prior written permission.
042  
043  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
044  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
045  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
046  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
047  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
048  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
049  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
050  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
051  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
052  POSSIBILITY OF SUCH DAMAGE.
053  
054*/
055
056
057import java.io.InputStream;
058import java.io.OutputStream;
059import java.io.UnsupportedEncodingException;
060
061import org.hl7.fhir.dstu2.model.Resource;
062import org.hl7.fhir.dstu2.model.Type;
063import org.hl7.fhir.exceptions.FHIRFormatError;
064import org.xmlpull.v1.XmlPullParserException;
065
066
067/**
068 * General interface - either an XML or JSON parser: read or write instances
069 *  
070 * Defined to allow a factory to create a parser of the right type
071 */
072public interface IParser {
073
074        /** 
075         * check what kind of parser this is
076         *  
077         * @return what kind of parser this is
078         */
079        public ParserType getType();
080        
081  // -- Parser Configuration ----------------------------------
082  /**
083   * Whether to parse or ignore comments - either reading or writing
084   */
085  public boolean getHandleComments(); 
086  public IParser setHandleComments(boolean value);
087
088  /**
089   * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it) when parsing
090   */
091  public boolean isAllowUnknownContent();
092  public IParser setAllowUnknownContent(boolean value);
093  
094  
095  public enum OutputStyle {
096    /**
097     * Produce normal output - no whitespace, except in HTML where whitespace is untouched
098     */
099    NORMAL,
100    
101    /**
102     * Produce pretty output - human readable whitespace, HTML whitespace untouched
103     */
104    PRETTY,
105    
106    /**
107     * Produce canonical output - no comments, no whitspace, HTML whitespace normlised, JSON attributes sorted alphabetically (slightly slower) 
108     */
109    CANONICAL,
110  }
111
112  /**
113   * Writing: 
114   */
115  public OutputStyle getOutputStyle();
116  public IParser setOutputStyle(OutputStyle value);
117  
118  /**
119   * This method is used by the publication tooling to stop the xhrtml narrative being generated. 
120   * It is not valid to use in production use. The tooling uses it to generate json/xml representations in html that are not cluttered by escaped html representations of the html representation
121   */
122  public IParser setSuppressXhtml(String message);
123
124  // -- Reading methods ----------------------------------------
125  
126  /**
127   * parse content that is known to be a resource  
128 * @throws XmlPullParserException 
129 * @throws FHIRFormatError 
130 * @throws IOException 
131   */
132  public Resource parse(InputStream input) throws IOException, FHIRFormatError;
133
134  /**
135   * parse content that is known to be a resource  
136 * @throws UnsupportedEncodingException 
137 * @throws IOException 
138 * @throws FHIRFormatError 
139   */
140  public Resource parse(String input) throws UnsupportedEncodingException, FHIRFormatError, IOException;
141  
142  /**
143   * parse content that is known to be a resource  
144 * @throws IOException 
145 * @throws FHIRFormatError 
146   */
147  public Resource parse(byte[] bytes) throws FHIRFormatError, IOException;
148
149  /**
150   * This is used to parse a type - a fragment of a resource. 
151   * There's no reason to use this in production - it's used 
152   * in the build tools 
153   * 
154   * Not supported by all implementations
155   * 
156   * @param input
157   * @param knownType. if this is blank, the parser may try to infer the type (xml only)
158   * @return
159 * @throws XmlPullParserException 
160 * @throws FHIRFormatError 
161 * @throws IOException 
162   */
163  public Type parseType(InputStream input, String knownType) throws IOException, FHIRFormatError;
164  /**
165   * This is used to parse a type - a fragment of a resource. 
166   * There's no reason to use this in production - it's used 
167   * in the build tools 
168   * 
169   * Not supported by all implementations
170   * 
171   * @param input
172   * @param knownType. if this is blank, the parser may try to infer the type (xml only)
173   * @return
174 * @throws UnsupportedEncodingException 
175 * @throws IOException 
176 * @throws FHIRFormatError 
177   */
178  public Type parseType(String input, String knownType) throws UnsupportedEncodingException, FHIRFormatError, IOException;
179  /**
180   * This is used to parse a type - a fragment of a resource. 
181   * There's no reason to use this in production - it's used 
182   * in the build tools 
183   * 
184   * Not supported by all implementations
185   * 
186   * @param input
187   * @param knownType. if this is blank, the parser may try to infer the type (xml only)
188   * @return
189 * @throws IOException 
190 * @throws FHIRFormatError 
191         */
192  public Type parseType(byte[] bytes, String knownType) throws FHIRFormatError, IOException;
193  
194  // -- Writing methods ----------------------------------------
195
196        /**
197         * Compose a resource to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
198         * @throws IOException 
199         */
200        public void compose(OutputStream stream, Resource resource) throws IOException;
201        
202  /**
203   * Compose a resource to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
204 * @throws IOException 
205   */
206        public String composeString(Resource resource) throws IOException;
207
208        /**
209   * Compose a resource to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
210         * @throws IOException 
211   */
212        public byte[] composeBytes(Resource resource) throws IOException;
213
214
215        /**
216         * Compose a type to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
217         * 
218         * Not supported by all implementations. rootName is ignored in the JSON format
219         * @throws XmlPullParserException 
220         * @throws FHIRFormatError 
221         * @throws IOException 
222         */
223        public void compose(OutputStream stream, Type type, String rootName) throws IOException;
224
225        /**
226   * Compose a type to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
227   * 
228   * Not supported by all implementations. rootName is ignored in the JSON format
229         * @throws IOException 
230   */
231  public String composeString(Type type, String rootName) throws IOException;
232
233        /**
234         * Compose a type to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production)
235   * 
236   * Not supported by all implementations. rootName is ignored in the JSON format
237         * @throws IOException 
238         */
239        public byte[] composeBytes(Type type, String rootName) throws IOException;
240
241
242}