001package org.hl7.fhir.instance.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.InputStream; 035import java.io.OutputStream; 036 037import org.hl7.fhir.instance.model.Resource; 038import org.hl7.fhir.instance.model.Type; 039 040 041/** 042 * General interface - either an XML or JSON parser: read or write instances 043 * 044 * Defined to allow a factory to create a parser of the right type 045 */ 046public interface IParser { 047 048 /** 049 * check what kind of parser this is 050 * 051 * @return what kind of parser this is 052 */ 053 public ParserType getType(); 054 055 // -- Parser Configuration ---------------------------------- 056 /** 057 * Whether to parse or ignore comments - either reading or writing 058 */ 059 public boolean getHandleComments(); 060 public IParser setHandleComments(boolean value); 061 062 /** 063 * @param allowUnknownContent Whether to throw an exception if unknown content is found (or just skip it) when parsing 064 */ 065 public boolean isAllowUnknownContent(); 066 public IParser setAllowUnknownContent(boolean value); 067 068 069 public enum OutputStyle { 070 /** 071 * Produce normal output - no whitespace, except in HTML where whitespace is untouched 072 */ 073 NORMAL, 074 075 /** 076 * Produce pretty output - human readable whitespace, HTML whitespace untouched 077 */ 078 PRETTY, 079 080 /** 081 * Produce canonical output - no comments, no whitspace, HTML whitespace normlised, JSON attributes sorted alphabetically (slightly slower) 082 */ 083 CANONICAL, 084 } 085 086 /** 087 * Writing: 088 */ 089 public OutputStyle getOutputStyle(); 090 public IParser setOutputStyle(OutputStyle value); 091 092 /** 093 * This method is used by the publication tooling to stop the xhrtml narrative being generated. 094 * 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 095 */ 096 public IParser setSuppressXhtml(String message); 097 098 // -- Reading methods ---------------------------------------- 099 100 /** 101 * parse content that is known to be a resource 102 */ 103 public Resource parse(InputStream input) throws Exception; 104 105 /** 106 * parse content that is known to be a resource 107 */ 108 public Resource parse(String input) throws Exception; 109 110 /** 111 * parse content that is known to be a resource 112 */ 113 public Resource parse(byte[] bytes) throws Exception; 114 115 /** 116 * This is used to parse a type - a fragment of a resource. 117 * There's no reason to use this in production - it's used 118 * in the build tools 119 * 120 * Not supported by all implementations 121 * 122 * @param input 123 * @param knownType. if this is blank, the parser may try to infer the type (xml only) 124 * @return 125 */ 126 public Type parseType(InputStream input, String knownType) throws Exception; 127 /** 128 * This is used to parse a type - a fragment of a resource. 129 * There's no reason to use this in production - it's used 130 * in the build tools 131 * 132 * Not supported by all implementations 133 * 134 * @param input 135 * @param knownType. if this is blank, the parser may try to infer the type (xml only) 136 * @return 137 */ 138 public Type parseType(String input, String knownType) throws Exception; 139 /** 140 * This is used to parse a type - a fragment of a resource. 141 * There's no reason to use this in production - it's used 142 * in the build tools 143 * 144 * Not supported by all implementations 145 * 146 * @param input 147 * @param knownType. if this is blank, the parser may try to infer the type (xml only) 148 * @return 149 */ 150 public Type parseType(byte[] bytes, String knownType) throws Exception; 151 152 // -- Writing methods ---------------------------------------- 153 154 /** 155 * 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) 156 */ 157 public void compose(OutputStream stream, Resource resource) throws Exception; 158 159 /** 160 * 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) 161 */ 162 public String composeString(Resource resource) throws Exception; 163 164 /** 165 * 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) 166 */ 167 public byte[] composeBytes(Resource resource) throws Exception; 168 169 170 /** 171 * 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) 172 * 173 * Not supported by all implementations. rootName is ignored in the JSON format 174 */ 175 public void compose(OutputStream stream, Type type, String rootName) throws Exception; 176 177 /** 178 * 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) 179 * 180 * Not supported by all implementations. rootName is ignored in the JSON format 181 */ 182 public String composeString(Type type, String rootName) throws Exception; 183 184 /** 185 * 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) 186 * 187 * Not supported by all implementations. rootName is ignored in the JSON format 188 */ 189 public byte[] composeBytes(Type type, String rootName) throws Exception; 190 191 192}