001package org.hl7.fhir.dstu2.model; 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 024import java.io.IOException; 025import java.net.URISyntaxException; 026import java.text.ParseException; 027import java.util.UUID; 028 029import org.hl7.fhir.dstu2.model.ContactPoint.ContactPointSystem; 030import org.hl7.fhir.dstu2.model.Narrative.NarrativeStatus; 031import org.hl7.fhir.exceptions.FHIRException; 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 DateTimeType newDateTime(String value) throws ParseException { 093 if (value == null) 094 return null; 095 return new DateTimeType(value); 096 } 097 098 public static DateType newDate(String value) throws ParseException { 099 if (value == null) 100 return null; 101 return new DateType(value); 102 } 103 104 public static CodeType newCode(String value) { 105 if (value == null) 106 return null; 107 CodeType res = new CodeType(); 108 res.setValue(value); 109 return res; 110 } 111 112 public static IntegerType newInteger(int value) { 113 IntegerType res = new IntegerType(); 114 res.setValue(value); 115 return res; 116 } 117 118 public static IntegerType newInteger(java.lang.Integer value) { 119 if (value == null) 120 return null; 121 IntegerType res = new IntegerType(); 122 res.setValue(value); 123 return res; 124 } 125 126 public static BooleanType newBoolean(boolean value) { 127 BooleanType res = new BooleanType(); 128 res.setValue(value); 129 return res; 130 } 131 132 public static ContactPoint newContactPoint(ContactPointSystem system, String value) { 133 ContactPoint res = new ContactPoint(); 134 res.setSystem(system); 135 res.setValue(value); 136 return res; 137 } 138 139 public static Extension newExtension(String uri, Type value, boolean evenIfNull) { 140 if (!evenIfNull && (value == null || value.isEmpty())) 141 return null; 142 Extension e = new Extension(); 143 e.setUrl(uri); 144 e.setValue(value); 145 return e; 146 } 147 148 public static CodeableConcept newCodeableConcept(String code, String system, String display) { 149 CodeableConcept cc = new CodeableConcept(); 150 Coding c = new Coding(); 151 c.setCode(code); 152 c.setSystem(system); 153 c.setDisplay(display); 154 cc.getCoding().add(c); 155 return cc; 156 } 157 158 public static Reference makeReference(String url) { 159 Reference rr = new Reference(); 160 rr.setReference(url); 161 return rr; 162 } 163 164 public static Narrative newNarrative(NarrativeStatus status, String html) throws IOException, FHIRException { 165 Narrative n = new Narrative(); 166 n.setStatus(status); 167 n.setDiv(new XhtmlParser().parseFragment("<div>"+Utilities.escapeXml(html)+"</div>")); 168 return n; 169 } 170 171 public static Coding makeCoding(String code) throws FHIRException { 172 String[] parts = code.split("\\|"); 173 Coding c = new Coding(); 174 if (parts.length == 2) { 175 c.setSystem(parts[0]); 176 c.setCode(parts[1]); 177 } else if (parts.length == 3) { 178 c.setSystem(parts[0]); 179 c.setCode(parts[1]); 180 c.setDisplay(parts[2]); 181 } else 182 throw new FHIRException("Unable to understand the code '"+code+"'. Use the format system|code(|display)"); 183 return c; 184 } 185 186 public static Reference makeReference(String url, String text) { 187 Reference rr = new Reference(); 188 rr.setReference(url); 189 if (!Utilities.noString(text)) 190 rr.setDisplay(text); 191 return rr; 192 } 193 194 public static String createUUID() { 195 return "urn:uuid:"+UUID.randomUUID().toString().toLowerCase(); 196 } 197 198 public Type create(String name) throws FHIRException { 199 if (name.equals("boolean")) 200 return new BooleanType(); 201 else if (name.equals("integer")) 202 return new IntegerType(); 203 else if (name.equals("decimal")) 204 return new DecimalType(); 205 else if (name.equals("base64Binary")) 206 return new Base64BinaryType(); 207 else if (name.equals("instant")) 208 return new InstantType(); 209 else if (name.equals("string")) 210 return new StringType(); 211 else if (name.equals("uri")) 212 return new UriType(); 213 else if (name.equals("date")) 214 return new DateType(); 215 else if (name.equals("dateTime")) 216 return new DateTimeType(); 217 else if (name.equals("time")) 218 return new TimeType(); 219 else if (name.equals("code")) 220 return new CodeType(); 221 else if (name.equals("oid")) 222 return new OidType(); 223 else if (name.equals("id")) 224 return new IdType(); 225 else if (name.equals("unsignedInt")) 226 return new UnsignedIntType(); 227 else if (name.equals("positiveInt")) 228 return new PositiveIntType(); 229 else if (name.equals("markdown")) 230 return new MarkdownType(); 231 else if (name.equals("Annotation")) 232 return new Annotation(); 233 else if (name.equals("Attachment")) 234 return new Attachment(); 235 else if (name.equals("Identifier")) 236 return new Identifier(); 237 else if (name.equals("CodeableConcept")) 238 return new CodeableConcept(); 239 else if (name.equals("Coding")) 240 return new Coding(); 241 else if (name.equals("Quantity")) 242 return new Quantity(); 243 else if (name.equals("Range")) 244 return new Range(); 245 else if (name.equals("Period")) 246 return new Period(); 247 else if (name.equals("Ratio")) 248 return new Ratio(); 249 else if (name.equals("SampledData")) 250 return new SampledData(); 251 else if (name.equals("Signature")) 252 return new Signature(); 253 else if (name.equals("HumanName")) 254 return new HumanName(); 255 else if (name.equals("Address")) 256 return new Address(); 257 else if (name.equals("ContactPoint")) 258 return new ContactPoint(); 259 else if (name.equals("Timing")) 260 return new Timing(); 261 else if (name.equals("Reference")) 262 return new Reference(); 263 else if (name.equals("Meta")) 264 return new Meta(); 265 else 266 throw new FHIRException("Unknown data type name "+name); 267 } 268}