001package org.hl7.fhir.r5.model;
002
003import java.io.IOException;
004
005import org.hl7.fhir.exceptions.FHIRException;
006import org.hl7.fhir.r5.elementmodel.Element;
007import org.hl7.fhir.r5.elementmodel.ObjectConverter;
008import org.hl7.fhir.utilities.Utilities;
009import org.hl7.fhir.utilities.xhtml.XhtmlComposer;
010import org.hl7.fhir.utilities.xhtml.XhtmlNode;
011import org.hl7.fhir.utilities.xhtml.XhtmlParser;
012
013public class TypeConvertor {
014
015  // -- converters for property setters
016  
017  public static DataType castToType(Base b) throws FHIRException {
018    if (b == null) {
019      return null;
020    }
021    if (b instanceof DataType)
022      return (DataType) b;
023    else if (b.isMetadataBased())
024      return ((org.hl7.fhir.r5.elementmodel.Element) b).asType();
025    else
026      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Reference");
027  }
028  
029
030  public static BooleanType castToBoolean(Base b) throws FHIRException {
031    if (b == null) {
032      return null;
033    }
034    if (b instanceof BooleanType)
035      return (BooleanType) b;
036    else
037      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Boolean");
038  }
039  
040  public static IntegerType castToInteger(Base b) throws FHIRException {
041    if (b == null) {
042      return null;
043    }
044    if (b instanceof IntegerType)
045      return (IntegerType) b;
046    else
047      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Integer");
048  }
049  
050  public static Integer64Type castToInteger64(Base b) throws FHIRException {
051    if (b == null) {
052      return null;
053    }
054    if (b instanceof Integer64Type)
055      return (Integer64Type) b;
056    else
057      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Integer");
058  }
059  
060  public static DecimalType castToDecimal(Base b) throws FHIRException {
061    if (b == null) {
062      return null;
063    }
064    if (b instanceof DecimalType)
065      return (DecimalType) b;
066    else if (b.hasPrimitiveValue())
067      return new DecimalType(b.primitiveValue());
068    else
069      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Decimal");
070  }
071  
072  public static Base64BinaryType castToBase64Binary(Base b) throws FHIRException {
073    if (b == null) {
074      return null;
075    }
076    if (b instanceof Base64BinaryType)
077      return (Base64BinaryType) b;
078    else
079      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Base64Binary");
080  }
081  
082  public static InstantType castToInstant(Base b) throws FHIRException {
083    if (b == null) {
084      return null;
085    }
086    if (b instanceof InstantType)
087      return (InstantType) b;
088    else
089      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Instant");
090  }
091  
092  public static StringType castToString(Base b) throws FHIRException {
093    if (b == null) {
094      return null;
095    }
096
097    if (b instanceof StringType)
098      return (StringType) b;
099    else if (b.hasPrimitiveValue())
100      return new StringType(b.primitiveValue());
101    else
102      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a String");
103  }
104  
105  public static UriType castToUri(Base b) throws FHIRException {
106    if (b == null) {
107      return null;
108    }
109
110    if (b instanceof UriType)
111      return (UriType) b;
112    else if (b.hasPrimitiveValue())
113      return new UriType(b.primitiveValue());
114    else
115      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Uri");
116  }
117  
118  public static UrlType castToUrl(Base b) throws FHIRException {
119    if (b == null) {
120      return null;
121    }
122
123    if (b instanceof UrlType)
124      return (UrlType) b;
125    else if (b.hasPrimitiveValue())
126      return new UrlType(b.primitiveValue());
127    else
128      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Uri");
129  }
130
131  public static UuidType castToUuid(Base b) throws FHIRException {
132    if (b == null) {
133      return null;
134    }
135
136    if (b instanceof UuidType)
137      return (UuidType) b;
138    else if (b.hasPrimitiveValue())
139      return new UuidType(b.primitiveValue());
140    else
141      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Uuid");
142  }
143  
144  public static CanonicalType castToCanonical(Base b) throws FHIRException {
145    if (b == null) {
146      return null;
147    }
148
149    if (b instanceof CanonicalType)
150      return (CanonicalType) b;
151    else if (b.hasPrimitiveValue())
152      return new CanonicalType(b.primitiveValue());
153    else
154      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Uri");
155  }
156  
157  public static DateType castToDate(Base b) throws FHIRException {
158    if (b == null) {
159      return null;
160    }
161
162    if (b instanceof DateType)
163      return (DateType) b;
164    else if (b.hasPrimitiveValue())
165      return new DateType(b.primitiveValue());
166    else
167      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Date");
168  }
169  
170  public static DateTimeType castToDateTime(Base b) throws FHIRException {
171    if (b == null) {
172      return null;
173    }
174
175    if (b instanceof DateTimeType)
176      return (DateTimeType) b;
177    else if (Utilities.existsInList(b.fhirType(), "dateTime", "date", "instant"))
178      return new DateTimeType(b.primitiveValue());
179    else
180      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a DateTime");
181  }
182  
183  public static TimeType castToTime(Base b) throws FHIRException {
184    if (b == null) {
185      return null;
186    }
187
188    if (b instanceof TimeType)
189      return (TimeType) b;
190    else
191      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Time");
192  }
193  
194  public static CodeType castToCode(Base b) throws FHIRException {
195    if (b == null) {
196      return null;
197    }
198
199    if (b instanceof CodeType)
200      return (CodeType) b;
201    else if (b instanceof PrimitiveType<?>) {      
202      return new CodeType(b.primitiveValue(), (PrimitiveType<?>) b);
203    } else if (b.isPrimitive())
204      return new CodeType(b.primitiveValue());
205    else
206      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Code");
207  }
208  
209  public static OidType castToOid(Base b) throws FHIRException {
210    if (b == null) {
211      return null;
212    }
213
214    if (b instanceof OidType)
215      return (OidType) b;
216    else
217      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Oid");
218  }
219  
220  public static IdType castToId(Base b) throws FHIRException {
221    if (b == null) {
222      return null;
223    }
224
225    if (b instanceof IdType)
226      return (IdType) b;
227    else
228      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Id");
229  }
230  
231  public static UnsignedIntType castToUnsignedInt(Base b) throws FHIRException {
232    if (b == null) {
233      return null;
234    }
235
236    if (b instanceof UnsignedIntType)
237      return (UnsignedIntType) b;
238    else
239      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a UnsignedInt");
240  }
241  
242  public static PositiveIntType castToPositiveInt(Base b) throws FHIRException {
243    if (b == null) {
244      return null;
245    }
246
247    if (b instanceof PositiveIntType)
248      return (PositiveIntType) b;
249    else
250      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a PositiveInt");
251  }
252  
253  public static MarkdownType castToMarkdown(Base b) throws FHIRException {
254    if (b == null) {
255      return null;
256    }
257
258    if (b instanceof MarkdownType)
259      return (MarkdownType) b;
260    else if (b.hasPrimitiveValue())
261      return new MarkdownType(b.primitiveValue());
262    else
263      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Markdown");
264  }
265    
266  public static Annotation castToAnnotation(Base b) throws FHIRException {
267    if (b == null) {
268      return null;
269    }
270
271    if (b instanceof Annotation)
272      return (Annotation) b;
273    else
274      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Annotation");
275  }
276  
277  public static Dosage castToDosage(Base b) throws FHIRException {
278    if (b == null) {
279      return null;
280    }
281
282    if (b instanceof Dosage)
283      return (Dosage) b;
284    else      
285      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an DosageInstruction");
286  }
287  
288  
289  public static Attachment castToAttachment(Base b) throws FHIRException {
290    if (b == null) {
291      return null;
292    }
293
294    if (b instanceof Attachment)
295      return (Attachment) b;
296    else
297      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Attachment");
298  }
299  
300  public static Identifier castToIdentifier(Base b) throws FHIRException {
301    if (b == null) {
302      return null;
303    }
304
305    if (b instanceof Identifier)
306      return (Identifier) b;
307    else
308      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Identifier");
309  }
310  
311  public static CodeableConcept castToCodeableConcept(Base b) throws FHIRException {
312    if (b == null) {
313      return null;
314    }
315
316    if (b instanceof CodeableConcept)
317      return (CodeableConcept) b;
318    else if (b instanceof Element) {
319      return ObjectConverter.readAsCodeableConcept((Element) b);
320    } else if (b instanceof CodeType) {
321      CodeableConcept cc = new CodeableConcept();
322      cc.addCoding().setCode(((CodeType) b).asStringValue());
323      return cc;
324    } else if(b instanceof StringType) {
325      CodeableConcept cc = new CodeableConcept();
326      cc.addCoding().setCode(((StringType) b).asStringValue());
327      return cc;
328    } else
329      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a CodeableConcept");
330  }
331  
332  public static CodeableReference castToCodeableReference(Base b) throws FHIRException {
333    if (b == null) {
334      return null;
335    }
336
337    if (b instanceof CodeableReference) {
338      return (CodeableReference) b;
339    } else if (b instanceof CodeType) {
340      CodeableReference cc = new CodeableReference();
341      cc.getConcept().addCoding().setCode(((CodeType) b).asStringValue());
342      return cc;
343    } else if (b instanceof Reference) {
344      CodeableReference cc = new CodeableReference();
345      cc.setReference((Reference) b);
346      return cc;
347    } else if(b instanceof StringType) {
348      CodeableReference cc = new CodeableReference();
349      cc.getConcept().addCoding().setCode(((StringType) b).asStringValue());
350      return cc;
351    } else
352      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a CodeableConcept");
353  }
354  
355  public static Coding castToCoding(Base b) throws FHIRException {
356    if (b == null) {
357      return null;
358    }
359
360    if (b instanceof Coding)
361      return (Coding) b;
362    else if (b instanceof Element) {
363      ICoding c = ((Element) b).getAsICoding();
364      if (c != null) {
365        return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay());
366      } else if (b instanceof PrimitiveType<?>) {
367        PrimitiveType<?> p = (PrimitiveType<?>) b;
368        Coding cc = new Coding();
369        cc.setCode(b.primitiveValue()).setId(p.getId()).getExtension().addAll(p.getExtension());
370        return cc;
371      } else if (b.isPrimitive()) {  
372        return new Coding().setCode(b.primitiveValue());
373      } else {
374        throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Coding");
375      }
376    } else if (b instanceof ICoding) {
377      ICoding c = (ICoding) b;
378      return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay());
379    } else if (b instanceof PrimitiveType<?>) {
380      PrimitiveType<?> p = (PrimitiveType<?>) b;
381      Coding cc = new Coding();
382      cc.setCode(b.primitiveValue()).setId(p.getId()).getExtension().addAll(p.getExtension());
383      return cc;
384    } else if (b.isPrimitive()) {  
385      return new Coding().setCode(b.primitiveValue());
386    } else {
387      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Coding");
388    }
389  }
390  
391  public static Quantity castToQuantity(Base b) throws FHIRException {
392    if (b == null) {
393      return null;
394    }
395
396    if (b instanceof Quantity)
397      return (Quantity) b;
398    else
399      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Quantity");
400  }
401  
402  public static Count castToCount(Base b) throws FHIRException {
403    if (b == null) {
404      return null;
405    }
406
407    if (b instanceof Count)
408      return (Count) b;
409    else
410      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Count");
411  }
412  
413  public static Money castToMoney(Base b) throws FHIRException {
414    if (b == null) {
415      return null;
416    }
417
418    if (b instanceof Money)
419      return (Money) b;
420    else if (b instanceof org.hl7.fhir.r5.elementmodel.Element && Utilities.tail(b.fhirType()).equals("Money")) {
421      org.hl7.fhir.r5.elementmodel.Element e = (org.hl7.fhir.r5.elementmodel.Element) b;
422      return new Money().setCurrency(e.getChildValue("currency")).setValue(Long.parseLong(e.getChildValue("value")));
423    } else
424      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Money");
425  }
426  
427  public static Duration castToDuration(Base b) throws FHIRException {
428    if (b == null) {
429      return null;
430    }
431
432    if (b instanceof Duration)
433      return (Duration) b;
434    else
435      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an Duration");
436  }
437  
438  public static SimpleQuantity castToSimpleQuantity(Base b) throws FHIRException {
439    if (b == null) {
440      return null;
441    }
442
443    if (b instanceof SimpleQuantity)
444      return (SimpleQuantity) b;
445    else if (b instanceof Quantity) {
446      Quantity q = (Quantity) b;
447      SimpleQuantity sq = new SimpleQuantity();
448      sq.setValueElement(q.getValueElement());
449      sq.setComparatorElement(q.getComparatorElement());
450      sq.setUnitElement(q.getUnitElement());
451      sq.setSystemElement(q.getSystemElement());
452      sq.setCodeElement(q.getCodeElement());
453      return sq;
454    } else
455      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to an SimpleQuantity");
456  }
457  
458  public static Range castToRange(Base b) throws FHIRException {
459    if (b == null) {
460      return null;
461    }
462
463    if (b instanceof Range)
464      return (Range) b;
465    else
466      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Range");
467  }
468  
469  public static Period castToPeriod(Base b) throws FHIRException {
470    if (b == null) {
471      return null;
472    }
473
474    if (b instanceof Period)
475      return (Period) b;
476    else
477      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Period");
478  }
479  
480  public static Ratio castToRatio(Base b) throws FHIRException {
481    if (b == null) {
482      return null;
483    }
484
485    if (b instanceof Ratio)
486      return (Ratio) b;
487    else
488      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Ratio");
489  }
490  
491  public static SampledData castToSampledData(Base b) throws FHIRException {
492    if (b == null) {
493      return null;
494    }
495
496    if (b instanceof SampledData)
497      return (SampledData) b;
498    else
499      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a SampledData");
500  }
501  
502  public static Signature castToSignature(Base b) throws FHIRException {
503    if (b == null) {
504      return null;
505    }
506
507    if (b instanceof Signature)
508      return (Signature) b;
509    else
510      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Signature");
511  }
512  
513  public static HumanName castToHumanName(Base b) throws FHIRException {
514    if (b == null) {
515      return null;
516    }
517
518    if (b instanceof HumanName)
519      return (HumanName) b;
520    else
521      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a HumanName");
522  }
523  
524  public static Address castToAddress(Base b) throws FHIRException {
525    if (b == null) {
526      return null;
527    }
528
529    if (b instanceof Address)
530      return (Address) b;
531    else
532      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Address");
533  }
534  
535  public static ContactDetail castToContactDetail(Base b) throws FHIRException {
536    if (b == null) {
537      return null;
538    }
539
540    if (b instanceof ContactDetail)
541      return (ContactDetail) b;
542    else
543      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ContactDetail");
544  }
545
546  public static Contributor castToContributor(Base b) throws FHIRException {
547    if (b == null) {
548      return null;
549    }
550
551    if (b instanceof Contributor)
552      return (Contributor) b;
553    else
554      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Contributor");
555  }
556
557  public static UsageContext castToUsageContext(Base b) throws FHIRException {
558    if (b == null) {
559      return null;
560    }
561
562    if (b instanceof UsageContext)
563      return (UsageContext) b;
564    else
565      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a UsageContext");
566  }
567
568  public static RelatedArtifact castToRelatedArtifact(Base b) throws FHIRException {
569    if (b == null) {
570      return null;
571    }
572
573    if (b instanceof RelatedArtifact)
574      return (RelatedArtifact) b;
575    else
576      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a RelatedArtifact");
577  }
578
579  public static ContactPoint castToContactPoint(Base b) throws FHIRException {
580    if (b == null) {
581      return null;
582    }
583
584    if (b instanceof ContactPoint)
585      return (ContactPoint) b;
586    else
587      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ContactPoint");
588  }
589  
590  public static Timing castToTiming(Base b) throws FHIRException {
591    if (b == null) {
592      return null;
593    }
594
595    if (b instanceof Timing)
596      return (Timing) b;
597    else
598      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Timing");
599  }
600  
601  public static Reference castToReference(Base b) throws FHIRException {
602    if (b == null) {
603      return null;
604    }
605
606    if (b instanceof Reference)
607      return (Reference) b;
608    else if (b.isPrimitive() && Utilities.isURL(b.primitiveValue()))
609      return new Reference().setReference(b.primitiveValue());
610    else if (b instanceof org.hl7.fhir.r5.elementmodel.Element && Utilities.tail(b.fhirType()).equals("Reference")) {
611      org.hl7.fhir.r5.elementmodel.Element e = (org.hl7.fhir.r5.elementmodel.Element) b;
612      return new Reference().setReference(e.getChildValue("reference")).setDisplay(e.getChildValue("display"));
613    } else
614      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Reference");
615  }
616  
617  public static Meta castToMeta(Base b) throws FHIRException {
618    if (b == null) {
619      return null;
620    }
621
622    if (b instanceof Meta)
623      return (Meta) b;
624    else
625      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Meta");
626  }
627    
628  
629  public static MarketingStatus castToMarketingStatus(Base b) throws FHIRException {
630    if (b == null) {
631      return null;
632    }
633
634    if (b instanceof MarketingStatus)
635      return (MarketingStatus) b;
636    else
637      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a MarketingStatus");
638  }
639    
640  public static Statistic castToStatistic(Base b) throws FHIRException {
641    if (b == null) {
642      return null;
643    }
644
645    if (b instanceof Statistic)
646      return (Statistic) b;
647    else
648      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Statistic");
649  }
650   
651  
652  public static OrderedDistribution castToOrderedDistribution(Base b) throws FHIRException {
653    if (b == null) {
654      return null;
655    }
656
657    if (b instanceof OrderedDistribution)
658      return (OrderedDistribution) b;
659    else
660      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a OrderedDistribution");
661  }
662  
663  public static ProductShelfLife castToProductShelfLife(Base b) throws FHIRException {   
664    if (b == null) {
665      return null;
666    }
667
668    if (b instanceof ProductShelfLife)
669      return (ProductShelfLife) b;
670    else
671      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ProductShelfLife");
672  }
673    
674  public static ProdCharacteristic castToProdCharacteristic(Base b) throws FHIRException {
675    if (b == null) {
676      return null;
677    }
678
679    if (b instanceof ProdCharacteristic)
680      return (ProdCharacteristic) b;
681    else
682      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ProdCharacteristic");
683  }
684    
685  
686  public static SubstanceAmount castToSubstanceAmount(Base b) throws FHIRException {
687    if (b == null) {
688      return null;
689    }
690
691    if (b instanceof SubstanceAmount)
692      return (SubstanceAmount) b;
693    else
694      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a SubstanceAmount");
695  }
696    
697  public static Extension castToExtension(Base b) throws FHIRException {
698    if (b == null) {
699      return null;
700    }
701
702    if (b instanceof Extension)
703      return (Extension) b;
704    else
705      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Extension");
706  }
707  
708  public static Resource castToResource(Base b) throws FHIRException {
709    if (b == null) {
710      return null;
711    }
712
713    if (b instanceof Resource)
714      return (Resource) b;
715    else
716      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Resource");
717  }
718  
719  public static Narrative castToNarrative(Base b) throws FHIRException {
720    if (b == null) {
721      return null;
722    }
723
724    if (b instanceof Narrative)
725      return (Narrative) b;
726    else
727      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Narrative");
728  }
729  
730  
731  public static ElementDefinition castToElementDefinition(Base b) throws FHIRException {
732    if (b == null) {
733      return null;
734    }
735
736    if (b instanceof ElementDefinition)
737      return (ElementDefinition) b;
738    else
739      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ElementDefinition");
740  }
741
742  public static DataRequirement castToDataRequirement(Base b) throws FHIRException {
743    if (b == null) {
744      return null;
745    }
746
747    if (b instanceof DataRequirement)
748      return (DataRequirement) b;
749    else
750      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a DataRequirement");
751  }
752
753  public static Expression castToExpression(Base b) throws FHIRException {
754    if (b == null) {
755      return null;
756    }
757
758    if (b instanceof Expression)
759      return (Expression) b;
760    else
761      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Expression");
762  }
763
764  
765  public static ParameterDefinition castToParameterDefinition(Base b) throws FHIRException {
766    if (b == null) {
767      return null;
768    }
769
770    if (b instanceof ParameterDefinition)
771      return (ParameterDefinition) b;
772    else
773      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ParameterDefinition");
774  }
775
776  public static TriggerDefinition castToTriggerDefinition(Base b) throws FHIRException {
777    if (b == null) {
778      return null;
779    }
780
781    if (b instanceof TriggerDefinition)
782      return (TriggerDefinition) b;
783    else
784      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a TriggerDefinition");
785  }
786  
787  public static ExtendedContactDetail castToExtendedContactDetail(Base b) throws FHIRException {
788    if (b == null) {
789      return null;
790    }
791
792    if (b instanceof ExtendedContactDetail)
793      return (ExtendedContactDetail) b;
794    else
795      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a ExtendedContactDetail");
796  }
797  
798  
799  
800
801  public static XhtmlNode castToXhtml(Base b) throws FHIRException {
802    if (b == null) {
803      return null;
804    }
805
806    if (b instanceof Element) {
807      return ((Element) b).getXhtml();
808    } else if (b instanceof XhtmlType) {
809      return ((XhtmlType) b).getXhtml();
810    } else if (b instanceof StringType) {
811      try {
812        return new XhtmlParser().parseFragment(((StringType) b).asStringValue());
813      } catch (IOException e) {
814        throw new FHIRException(e);
815      }
816    } else
817      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to XHtml");
818  }
819  
820  public static String castToXhtmlString(Base b) throws FHIRException {
821    if (b == null) {
822      return null;
823    }
824
825    if (b instanceof Element) {
826      return ((Element) b).getValue();
827    } else if (b instanceof XhtmlType) {
828      try {
829        return new XhtmlComposer(true).compose(((XhtmlType) b).getXhtml());
830      } catch (IOException e) {
831        return null;
832      }
833    } else if (b instanceof StringType) {
834      return ((StringType) b).asStringValue();
835    } else
836      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to XHtml string");
837  }
838
839
840  public static VirtualServiceDetail castToVirtualServiceDetail(Base b) throws FHIRException {
841    if (b == null) {
842      return null;
843    }
844
845    if (b instanceof VirtualServiceDetail)
846      return (VirtualServiceDetail) b;
847    else
848      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a VirtualServiceDetail");
849  }
850
851
852  public static Availability castToAvailability(Base b) throws FHIRException {
853    if (b == null) {
854      return null;
855    }
856
857    if (b instanceof Availability)
858      return (Availability) b;
859    else
860      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a Availability");
861  }
862
863
864  public static MonetaryComponent castToMonetaryComponent(Base b) throws FHIRException {
865    if (b == null) {
866      return null;
867    }
868
869    if (b instanceof MonetaryComponent)
870      return (MonetaryComponent) b;
871    else
872      throw new FHIRException("Unable to convert a "+b.fhirType()+"("+b.getClass().getName()+") to a MonetaryComponent");
873  }
874}