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 instanceof DataType) 019 return (DataType) b; 020 else if (b.isMetadataBased()) 021 return ((org.hl7.fhir.r5.elementmodel.Element) b).asType(); 022 else 023 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 024 } 025 026 027 public static BooleanType castToBoolean(Base b) throws FHIRException { 028 if (b instanceof BooleanType) 029 return (BooleanType) b; 030 else 031 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Boolean"); 032 } 033 034 public static IntegerType castToInteger(Base b) throws FHIRException { 035 if (b instanceof IntegerType) 036 return (IntegerType) b; 037 else 038 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Integer"); 039 } 040 041 public static Integer64Type castToInteger64(Base b) throws FHIRException { 042 if (b instanceof Integer64Type) 043 return (Integer64Type) b; 044 else 045 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Integer"); 046 } 047 048 public static DecimalType castToDecimal(Base b) throws FHIRException { 049 if (b instanceof DecimalType) 050 return (DecimalType) b; 051 else if (b.hasPrimitiveValue()) 052 return new DecimalType(b.primitiveValue()); 053 else 054 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Decimal"); 055 } 056 057 public static Base64BinaryType castToBase64Binary(Base b) throws FHIRException { 058 if (b instanceof Base64BinaryType) 059 return (Base64BinaryType) b; 060 else 061 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Base64Binary"); 062 } 063 064 public static InstantType castToInstant(Base b) throws FHIRException { 065 if (b instanceof InstantType) 066 return (InstantType) b; 067 else 068 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Instant"); 069 } 070 071 public static StringType castToString(Base b) throws FHIRException { 072 if (b instanceof StringType) 073 return (StringType) b; 074 else if (b.hasPrimitiveValue()) 075 return new StringType(b.primitiveValue()); 076 else 077 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a String"); 078 } 079 080 public static UriType castToUri(Base b) throws FHIRException { 081 if (b instanceof UriType) 082 return (UriType) b; 083 else if (b.hasPrimitiveValue()) 084 return new UriType(b.primitiveValue()); 085 else 086 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 087 } 088 089 public static UrlType castToUrl(Base b) throws FHIRException { 090 if (b instanceof UrlType) 091 return (UrlType) b; 092 else if (b.hasPrimitiveValue()) 093 return new UrlType(b.primitiveValue()); 094 else 095 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 096 } 097 098 public static CanonicalType castToCanonical(Base b) throws FHIRException { 099 if (b instanceof CanonicalType) 100 return (CanonicalType) b; 101 else if (b.hasPrimitiveValue()) 102 return new CanonicalType(b.primitiveValue()); 103 else 104 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 105 } 106 107 public static DateType castToDate(Base b) throws FHIRException { 108 if (b instanceof DateType) 109 return (DateType) b; 110 else if (b.hasPrimitiveValue()) 111 return new DateType(b.primitiveValue()); 112 else 113 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Date"); 114 } 115 116 public static DateTimeType castToDateTime(Base b) throws FHIRException { 117 if (b instanceof DateTimeType) 118 return (DateTimeType) b; 119 else if (b.fhirType().equals("dateTime")) 120 return new DateTimeType(b.primitiveValue()); 121 else 122 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DateTime"); 123 } 124 125 public static TimeType castToTime(Base b) throws FHIRException { 126 if (b instanceof TimeType) 127 return (TimeType) b; 128 else 129 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Time"); 130 } 131 132 public static CodeType castToCode(Base b) throws FHIRException { 133 if (b instanceof CodeType) 134 return (CodeType) b; 135 else if (b.isPrimitive()) 136 return new CodeType(b.primitiveValue()); 137 else 138 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Code"); 139 } 140 141 public static OidType castToOid(Base b) throws FHIRException { 142 if (b instanceof OidType) 143 return (OidType) b; 144 else 145 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Oid"); 146 } 147 148 public static IdType castToId(Base b) throws FHIRException { 149 if (b instanceof IdType) 150 return (IdType) b; 151 else 152 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Id"); 153 } 154 155 public static UnsignedIntType castToUnsignedInt(Base b) throws FHIRException { 156 if (b instanceof UnsignedIntType) 157 return (UnsignedIntType) b; 158 else 159 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UnsignedInt"); 160 } 161 162 public static PositiveIntType castToPositiveInt(Base b) throws FHIRException { 163 if (b instanceof PositiveIntType) 164 return (PositiveIntType) b; 165 else 166 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a PositiveInt"); 167 } 168 169 public static MarkdownType castToMarkdown(Base b) throws FHIRException { 170 if (b instanceof MarkdownType) 171 return (MarkdownType) b; 172 else if (b.hasPrimitiveValue()) 173 return new MarkdownType(b.primitiveValue()); 174 else 175 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Markdown"); 176 } 177 178 public static Annotation castToAnnotation(Base b) throws FHIRException { 179 if (b instanceof Annotation) 180 return (Annotation) b; 181 else 182 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Annotation"); 183 } 184 185 public static Dosage castToDosage(Base b) throws FHIRException { 186 if (b instanceof Dosage) 187 return (Dosage) b; 188 else throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an DosageInstruction"); 189 } 190 191 192 public static Attachment castToAttachment(Base b) throws FHIRException { 193 if (b instanceof Attachment) 194 return (Attachment) b; 195 else 196 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Attachment"); 197 } 198 199 public static Identifier castToIdentifier(Base b) throws FHIRException { 200 if (b instanceof Identifier) 201 return (Identifier) b; 202 else 203 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Identifier"); 204 } 205 206 public static CodeableConcept castToCodeableConcept(Base b) throws FHIRException { 207 if (b instanceof CodeableConcept) 208 return (CodeableConcept) b; 209 else if (b instanceof Element) { 210 return ObjectConverter.readAsCodeableConcept((Element) b); 211 } else if (b instanceof CodeType) { 212 CodeableConcept cc = new CodeableConcept(); 213 cc.addCoding().setCode(((CodeType) b).asStringValue()); 214 return cc; 215 } else if(b instanceof StringType) { 216 CodeableConcept cc = new CodeableConcept(); 217 cc.addCoding().setCode(((StringType) b).asStringValue()); 218 return cc; 219 } else 220 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a CodeableConcept"); 221 } 222 223 public static CodeableReference castToCodeableReference(Base b) throws FHIRException { 224 if (b instanceof CodeableReference) { 225 return (CodeableReference) b; 226 } else if (b instanceof CodeType) { 227 CodeableReference cc = new CodeableReference(); 228 cc.getConcept().addCoding().setCode(((CodeType) b).asStringValue()); 229 return cc; 230 } else if (b instanceof Reference) { 231 CodeableReference cc = new CodeableReference(); 232 cc.setReference((Reference) b); 233 return cc; 234 } else if(b instanceof StringType) { 235 CodeableReference cc = new CodeableReference(); 236 cc.getConcept().addCoding().setCode(((StringType) b).asStringValue()); 237 return cc; 238 } else 239 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a CodeableConcept"); 240 } 241 242 public static Population castToPopulation(Base b) throws FHIRException { 243 if (b instanceof Population) 244 return (Population) b; 245 else 246 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Population"); 247 } 248 249 250 public static Coding castToCoding(Base b) throws FHIRException { 251 if (b instanceof Coding) 252 return (Coding) b; 253 else if (b instanceof Element) { 254 ICoding c = ((Element) b).getAsICoding(); 255 if (c != null) { 256 return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay()); 257 } else if (b.isPrimitive()) { 258 return new Coding().setCode(b.primitiveValue()); 259 } else { 260 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Coding"); 261 } 262 } else if (b instanceof ICoding) { 263 ICoding c = (ICoding) b; 264 return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay()); 265 } else if (b.isPrimitive()) { 266 return new Coding().setCode(b.primitiveValue()); 267 } else { 268 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Coding"); 269 } 270 } 271 272 public static Quantity castToQuantity(Base b) throws FHIRException { 273 if (b instanceof Quantity) 274 return (Quantity) b; 275 else 276 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Quantity"); 277 } 278 279 public static Money castToMoney(Base b) throws FHIRException { 280 if (b instanceof Money) 281 return (Money) b; 282 else 283 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Money"); 284 } 285 286 public static Duration castToDuration(Base b) throws FHIRException { 287 if (b instanceof Duration) 288 return (Duration) b; 289 else 290 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Duration"); 291 } 292 293 public static SimpleQuantity castToSimpleQuantity(Base b) throws FHIRException { 294 if (b instanceof SimpleQuantity) 295 return (SimpleQuantity) b; 296 else if (b instanceof Quantity) { 297 Quantity q = (Quantity) b; 298 SimpleQuantity sq = new SimpleQuantity(); 299 sq.setValueElement(q.getValueElement()); 300 sq.setComparatorElement(q.getComparatorElement()); 301 sq.setUnitElement(q.getUnitElement()); 302 sq.setSystemElement(q.getSystemElement()); 303 sq.setCodeElement(q.getCodeElement()); 304 return sq; 305 } else 306 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an SimpleQuantity"); 307 } 308 309 public static Range castToRange(Base b) throws FHIRException { 310 if (b instanceof Range) 311 return (Range) b; 312 else 313 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Range"); 314 } 315 316 public static Period castToPeriod(Base b) throws FHIRException { 317 if (b instanceof Period) 318 return (Period) b; 319 else 320 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Period"); 321 } 322 323 public static Ratio castToRatio(Base b) throws FHIRException { 324 if (b instanceof Ratio) 325 return (Ratio) b; 326 else 327 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Ratio"); 328 } 329 330 public static SampledData castToSampledData(Base b) throws FHIRException { 331 if (b instanceof SampledData) 332 return (SampledData) b; 333 else 334 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a SampledData"); 335 } 336 337 public static Signature castToSignature(Base b) throws FHIRException { 338 if (b instanceof Signature) 339 return (Signature) b; 340 else 341 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Signature"); 342 } 343 344 public static HumanName castToHumanName(Base b) throws FHIRException { 345 if (b instanceof HumanName) 346 return (HumanName) b; 347 else 348 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a HumanName"); 349 } 350 351 public static Address castToAddress(Base b) throws FHIRException { 352 if (b instanceof Address) 353 return (Address) b; 354 else 355 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Address"); 356 } 357 358 public static ContactDetail castToContactDetail(Base b) throws FHIRException { 359 if (b instanceof ContactDetail) 360 return (ContactDetail) b; 361 else 362 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactDetail"); 363 } 364 365 public static Contributor castToContributor(Base b) throws FHIRException { 366 if (b instanceof Contributor) 367 return (Contributor) b; 368 else 369 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Contributor"); 370 } 371 372 public static UsageContext castToUsageContext(Base b) throws FHIRException { 373 if (b instanceof UsageContext) 374 return (UsageContext) b; 375 else 376 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UsageContext"); 377 } 378 379 public static RelatedArtifact castToRelatedArtifact(Base b) throws FHIRException { 380 if (b instanceof RelatedArtifact) 381 return (RelatedArtifact) b; 382 else 383 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a RelatedArtifact"); 384 } 385 386 public static ContactPoint castToContactPoint(Base b) throws FHIRException { 387 if (b instanceof ContactPoint) 388 return (ContactPoint) b; 389 else 390 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactPoint"); 391 } 392 393 public static Timing castToTiming(Base b) throws FHIRException { 394 if (b instanceof Timing) 395 return (Timing) b; 396 else 397 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Timing"); 398 } 399 400 public static Reference castToReference(Base b) throws FHIRException { 401 if (b instanceof Reference) 402 return (Reference) b; 403 else if (b.isPrimitive() && Utilities.isURL(b.primitiveValue())) 404 return new Reference().setReference(b.primitiveValue()); 405 else if (b instanceof org.hl7.fhir.r5.elementmodel.Element && b.fhirType().equals("Reference")) { 406 org.hl7.fhir.r5.elementmodel.Element e = (org.hl7.fhir.r5.elementmodel.Element) b; 407 return new Reference().setReference(e.getChildValue("reference")).setDisplay(e.getChildValue("display")); 408 } else 409 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 410 } 411 412 public static Meta castToMeta(Base b) throws FHIRException { 413 if (b instanceof Meta) 414 return (Meta) b; 415 else 416 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Meta"); 417 } 418 419 420 public static MarketingStatus castToMarketingStatus(Base b) throws FHIRException { 421 if (b instanceof MarketingStatus) 422 return (MarketingStatus) b; 423 else 424 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a MarketingStatus"); 425 } 426 427 public static Statistic castToStatistic(Base b) throws FHIRException { 428 if (b instanceof Statistic) 429 return (Statistic) b; 430 else 431 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Statistic"); 432 } 433 434 435 public static OrderedDistribution castToOrderedDistribution(Base b) throws FHIRException { 436 if (b instanceof OrderedDistribution) 437 return (OrderedDistribution) b; 438 else 439 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a OrderedDistribution"); 440 } 441 442 public static ProductShelfLife castToProductShelfLife(Base b) throws FHIRException { 443 if (b instanceof ProductShelfLife) 444 return (ProductShelfLife) b; 445 else 446 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ProductShelfLife"); 447 } 448 449 public static ProdCharacteristic castToProdCharacteristic(Base b) throws FHIRException { 450 if (b instanceof ProdCharacteristic) 451 return (ProdCharacteristic) b; 452 else 453 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ProdCharacteristic"); 454 } 455 456 457 public static SubstanceAmount castToSubstanceAmount(Base b) throws FHIRException { 458 if (b instanceof SubstanceAmount) 459 return (SubstanceAmount) b; 460 else 461 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a SubstanceAmount"); 462 } 463 464 public static Extension castToExtension(Base b) throws FHIRException { 465 if (b instanceof Extension) 466 return (Extension) b; 467 else 468 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Extension"); 469 } 470 471 public static Resource castToResource(Base b) throws FHIRException { 472 if (b instanceof Resource) 473 return (Resource) b; 474 else 475 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Resource"); 476 } 477 478 public static Narrative castToNarrative(Base b) throws FHIRException { 479 if (b instanceof Narrative) 480 return (Narrative) b; 481 else 482 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Narrative"); 483 } 484 485 486 public static ElementDefinition castToElementDefinition(Base b) throws FHIRException { 487 if (b instanceof ElementDefinition) 488 return (ElementDefinition) b; 489 else 490 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ElementDefinition"); 491 } 492 493 public static DataRequirement castToDataRequirement(Base b) throws FHIRException { 494 if (b instanceof DataRequirement) 495 return (DataRequirement) b; 496 else 497 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DataRequirement"); 498 } 499 500 public static Expression castToExpression(Base b) throws FHIRException { 501 if (b instanceof Expression) 502 return (Expression) b; 503 else 504 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Expression"); 505 } 506 507 508 public static ParameterDefinition castToParameterDefinition(Base b) throws FHIRException { 509 if (b instanceof ParameterDefinition) 510 return (ParameterDefinition) b; 511 else 512 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ParameterDefinition"); 513 } 514 515 public static TriggerDefinition castToTriggerDefinition(Base b) throws FHIRException { 516 if (b instanceof TriggerDefinition) 517 return (TriggerDefinition) b; 518 else 519 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a TriggerDefinition"); 520 } 521 522 public static XhtmlNode castToXhtml(Base b) throws FHIRException { 523 if (b instanceof Element) { 524 return ((Element) b).getXhtml(); 525 } else if (b instanceof XhtmlType) { 526 return ((XhtmlType) b).getXhtml(); 527 } else if (b instanceof StringType) { 528 try { 529 return new XhtmlParser().parseFragment(((StringType) b).asStringValue()); 530 } catch (IOException e) { 531 throw new FHIRException(e); 532 } 533 } else 534 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml"); 535 } 536 537 public static String castToXhtmlString(Base b) throws FHIRException { 538 if (b instanceof Element) { 539 return ((Element) b).getValue(); 540 } else if (b instanceof XhtmlType) { 541 try { 542 return new XhtmlComposer(true).compose(((XhtmlType) b).getXhtml()); 543 } catch (IOException e) { 544 return null; 545 } 546 } else if (b instanceof StringType) { 547 return ((StringType) b).asStringValue(); 548 } else 549 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml string"); 550 } 551 552 553}