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