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