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