001package org.hl7.fhir.r4.model; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r4 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 024import java.io.IOException; 025import java.net.URISyntaxException; 026import java.text.ParseException; 027import java.util.UUID; 028 029import org.hl7.fhir.exceptions.FHIRException; 030import org.hl7.fhir.r4.model.ContactPoint.ContactPointSystem; 031import org.hl7.fhir.r4.model.Narrative.NarrativeStatus; 032import org.hl7.fhir.utilities.Utilities; 033import org.hl7.fhir.utilities.xhtml.XhtmlParser; 034 035/* 036Copyright (c) 2011+, HL7, Inc 037All rights reserved. 038 039Redistribution and use in source and binary forms, with or without modification, 040are permitted provided that the following conditions are met: 041 042 * Redistributions of source code must retain the above copyright notice, this 043 list of conditions and the following disclaimer. 044 * Redistributions in binary form must reproduce the above copyright notice, 045 this list of conditions and the following disclaimer in the documentation 046 and/or other materials provided with the distribution. 047 * Neither the name of HL7 nor the names of its contributors may be used to 048 endorse or promote products derived from this software without specific 049 prior written permission. 050 051THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 052ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 053WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 054IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 055INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 056NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 057PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 058WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 059ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 060POSSIBILITY OF SUCH DAMAGE. 061 062*/ 063 064 065 066public class Factory { 067 068 public static IdType newId(String value) { 069 if (value == null) 070 return null; 071 IdType res = new IdType(); 072 res.setValue(value); 073 return res; 074 } 075 076 public static StringType newString_(String value) { 077 if (value == null) 078 return null; 079 StringType res = new StringType(); 080 res.setValue(value); 081 return res; 082 } 083 084 public static UriType newUri(String value) throws URISyntaxException { 085 if (value == null) 086 return null; 087 UriType res = new UriType(); 088 res.setValue(value); 089 return res; 090 } 091 092 public static UrlType newUrl(String value) throws URISyntaxException { 093 if (value == null) 094 return null; 095 UrlType res = new UrlType(); 096 res.setValue(value); 097 return res; 098 } 099 100 public static CanonicalType newCanonical(String value) throws URISyntaxException { 101 if (value == null) 102 return null; 103 CanonicalType res = new CanonicalType(); 104 res.setValue(value); 105 return res; 106 } 107 108 public static DateTimeType newDateTime(String value) throws ParseException { 109 if (value == null) 110 return null; 111 return new DateTimeType(value); 112 } 113 114 public static DateType newDate(String value) throws ParseException { 115 if (value == null) 116 return null; 117 return new DateType(value); 118 } 119 120 public static CodeType newCode(String value) { 121 if (value == null) 122 return null; 123 CodeType res = new CodeType(); 124 res.setValue(value); 125 return res; 126 } 127 128 public static IntegerType newInteger(int value) { 129 IntegerType res = new IntegerType(); 130 res.setValue(value); 131 return res; 132 } 133 134 public static IntegerType newInteger(java.lang.Integer value) { 135 if (value == null) 136 return null; 137 IntegerType res = new IntegerType(); 138 res.setValue(value); 139 return res; 140 } 141 142 public static BooleanType newBoolean(boolean value) { 143 BooleanType res = new BooleanType(); 144 res.setValue(value); 145 return res; 146 } 147 148 public static ContactPoint newContactPoint(ContactPointSystem system, String value) { 149 ContactPoint res = new ContactPoint(); 150 res.setSystem(system); 151 res.setValue(value); 152 return res; 153 } 154 155 public static Extension newExtension(String uri, Type value, boolean evenIfNull) { 156 if (!evenIfNull && (value == null || value.isEmpty())) 157 return null; 158 Extension e = new Extension(); 159 e.setUrl(uri); 160 e.setValue(value); 161 return e; 162 } 163 164 public static CodeableConcept newCodeableConcept(String code, String system, String display) { 165 CodeableConcept cc = new CodeableConcept(); 166 Coding c = new Coding(); 167 c.setCode(code); 168 c.setSystem(system); 169 c.setDisplay(display); 170 cc.getCoding().add(c); 171 return cc; 172 } 173 174 public static Reference makeReference(String url) { 175 Reference rr = new Reference(); 176 rr.setReference(url); 177 return rr; 178 } 179 180 public static Narrative newNarrative(NarrativeStatus status, String html) throws IOException, FHIRException { 181 Narrative n = new Narrative(); 182 n.setStatus(status); 183 try { 184 n.setDiv(new XhtmlParser().parseFragment("<div>"+Utilities.escapeXml(html)+"</div>")); 185 } catch (org.hl7.fhir.exceptions.FHIRException e) { 186 throw new FHIRException(e.getMessage(), e); 187 } 188 return n; 189 } 190 191 public static Coding makeCoding(String code) throws FHIRException { 192 String[] parts = code.split("\\|"); 193 Coding c = new Coding(); 194 if (parts.length == 2) { 195 c.setSystem(parts[0]); 196 c.setCode(parts[1]); 197 } else if (parts.length == 3) { 198 c.setSystem(parts[0]); 199 c.setCode(parts[1]); 200 c.setDisplay(parts[2]); 201 } else 202 throw new FHIRException("Unable to understand the code '"+code+"'. Use the format system|code(|display)"); 203 return c; 204 } 205 206 public static Reference makeReference(String url, String text) { 207 Reference rr = new Reference(); 208 rr.setReference(url); 209 if (!Utilities.noString(text)) 210 rr.setDisplay(text); 211 return rr; 212 } 213 214 public static String createUUID() { 215 return "urn:uuid:"+UUID.randomUUID().toString().toLowerCase(); 216 } 217 218 public Type create(String name) throws FHIRException { 219 if (name.equals("boolean")) 220 return new BooleanType(); 221 else if (name.equals("integer")) 222 return new IntegerType(); 223 else if (name.equals("decimal")) 224 return new DecimalType(); 225 else if (name.equals("base64Binary")) 226 return new Base64BinaryType(); 227 else if (name.equals("instant")) 228 return new InstantType(); 229 else if (name.equals("string")) 230 return new StringType(); 231 else if (name.equals("uri")) 232 return new UriType(); 233 else if (name.equals("url")) 234 return new UrlType(); 235 else if (name.equals("canonical")) 236 return new CanonicalType(); 237 else if (name.equals("date")) 238 return new DateType(); 239 else if (name.equals("dateTime")) 240 return new DateTimeType(); 241 else if (name.equals("time")) 242 return new TimeType(); 243 else if (name.equals("code")) 244 return new CodeType(); 245 else if (name.equals("oid")) 246 return new OidType(); 247 else if (name.equals("id")) 248 return new IdType(); 249 else if (name.equals("unsignedInt")) 250 return new UnsignedIntType(); 251 else if (name.equals("positiveInt")) 252 return new PositiveIntType(); 253 else if (name.equals("markdown")) 254 return new MarkdownType(); 255 else if (name.equals("Annotation")) 256 return new Annotation(); 257 else if (name.equals("Attachment")) 258 return new Attachment(); 259 else if (name.equals("Identifier")) 260 return new Identifier(); 261 else if (name.equals("CodeableConcept")) 262 return new CodeableConcept(); 263 else if (name.equals("Coding")) 264 return new Coding(); 265 else if (name.equals("Quantity")) 266 return new Quantity(); 267 else if (name.equals("Range")) 268 return new Range(); 269 else if (name.equals("Period")) 270 return new Period(); 271 else if (name.equals("Ratio")) 272 return new Ratio(); 273 else if (name.equals("SampledData")) 274 return new SampledData(); 275 else if (name.equals("Signature")) 276 return new Signature(); 277 else if (name.equals("HumanName")) 278 return new HumanName(); 279 else if (name.equals("Address")) 280 return new Address(); 281 else if (name.equals("ContactPoint")) 282 return new ContactPoint(); 283 else if (name.equals("Timing")) 284 return new Timing(); 285 else if (name.equals("Reference")) 286 return new Reference(); 287 else if (name.equals("Meta")) 288 return new Meta(); 289 else 290 throw new FHIRException("Unknown data type name "+name); 291 } 292}