001package org.hl7.fhir.instance.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.instance.model.ContactPoint.ContactPointSystem; 010import org.hl7.fhir.instance.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 n.setDiv(new XhtmlParser().parseFragment("<div>"+Utilities.escapeXml(html)+"</div>")); 147 return n; 148 } 149 150 public static Coding makeCoding(String code) throws FHIRException { 151 String[] parts = code.split("\\|"); 152 Coding c = new Coding(); 153 if (parts.length == 2) { 154 c.setSystem(parts[0]); 155 c.setCode(parts[1]); 156 } else if (parts.length == 3) { 157 c.setSystem(parts[0]); 158 c.setCode(parts[1]); 159 c.setDisplay(parts[2]); 160 } else 161 throw new FHIRException("Unable to understand the code '"+code+"'. Use the format system|code(|display)"); 162 return c; 163 } 164 165 public static Reference makeReference(String url, String text) { 166 Reference rr = new Reference(); 167 rr.setReference(url); 168 if (!Utilities.noString(text)) 169 rr.setDisplay(text); 170 return rr; 171 } 172 173 public static String createUUID() { 174 return "urn:uuid:"+UUID.randomUUID().toString().toLowerCase(); 175 } 176 177 public Type create(String name) throws FHIRException { 178 if (name.equals("boolean")) 179 return new BooleanType(); 180 else if (name.equals("integer")) 181 return new IntegerType(); 182 else if (name.equals("decimal")) 183 return new DecimalType(); 184 else if (name.equals("base64Binary")) 185 return new Base64BinaryType(); 186 else if (name.equals("instant")) 187 return new InstantType(); 188 else if (name.equals("string")) 189 return new StringType(); 190 else if (name.equals("uri")) 191 return new UriType(); 192 else if (name.equals("date")) 193 return new DateType(); 194 else if (name.equals("dateTime")) 195 return new DateTimeType(); 196 else if (name.equals("time")) 197 return new TimeType(); 198 else if (name.equals("code")) 199 return new CodeType(); 200 else if (name.equals("oid")) 201 return new OidType(); 202 else if (name.equals("id")) 203 return new IdType(); 204 else if (name.equals("unsignedInt")) 205 return new UnsignedIntType(); 206 else if (name.equals("positiveInt")) 207 return new PositiveIntType(); 208 else if (name.equals("markdown")) 209 return new MarkdownType(); 210 else if (name.equals("Annotation")) 211 return new Annotation(); 212 else if (name.equals("Attachment")) 213 return new Attachment(); 214 else if (name.equals("Identifier")) 215 return new Identifier(); 216 else if (name.equals("CodeableConcept")) 217 return new CodeableConcept(); 218 else if (name.equals("Coding")) 219 return new Coding(); 220 else if (name.equals("Quantity")) 221 return new Quantity(); 222 else if (name.equals("Range")) 223 return new Range(); 224 else if (name.equals("Period")) 225 return new Period(); 226 else if (name.equals("Ratio")) 227 return new Ratio(); 228 else if (name.equals("SampledData")) 229 return new SampledData(); 230 else if (name.equals("Signature")) 231 return new Signature(); 232 else if (name.equals("HumanName")) 233 return new HumanName(); 234 else if (name.equals("Address")) 235 return new Address(); 236 else if (name.equals("ContactPoint")) 237 return new ContactPoint(); 238 else if (name.equals("Timing")) 239 return new Timing(); 240 else if (name.equals("Reference")) 241 return new Reference(); 242 else if (name.equals("Meta")) 243 return new Meta(); 244 else 245 throw new FHIRException("Unknown data type name "+name); 246 } 247}