001package org.hl7.fhir.r5.model; 002 003/*- 004 * #%L 005 * org.hl7.fhir.r5 006 * %% 007 * Copyright (C) 2014 - 2019 Health Level 7 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023 024import java.io.IOException; 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.Date; 028import java.util.HashMap; 029import java.util.List; 030import java.util.Map; 031 032import org.hl7.fhir.exceptions.FHIRException; 033import org.hl7.fhir.instance.model.api.IBase; 034import org.hl7.fhir.r5.elementmodel.Element; 035import org.hl7.fhir.r5.elementmodel.ObjectConverter; 036import org.hl7.fhir.utilities.Utilities; 037import org.hl7.fhir.utilities.xhtml.XhtmlComposer; 038import org.hl7.fhir.utilities.xhtml.XhtmlNode; 039import org.hl7.fhir.utilities.xhtml.XhtmlParser; 040 041import ca.uhn.fhir.model.api.IElement; 042 043public abstract class Base implements Serializable, IBase, IElement { 044 045 /** 046 * User appended data items - allow users to add extra information to the class 047 */ 048private Map<String, Object> userData; 049 050 /** 051 * Round tracking xml comments for testing convenience 052 */ 053 private List<String> formatCommentsPre; 054 055 /** 056 * Round tracking xml comments for testing convenience 057 */ 058 private List<String> formatCommentsPost; 059 060 061 public Object getUserData(String name) { 062 if (userData == null) 063 return null; 064 return userData.get(name); 065 } 066 067 public void setUserData(String name, Object value) { 068 if (userData == null) 069 userData = new HashMap<String, Object>(); 070 userData.put(name, value); 071 } 072 073 public void clearUserData(String name) { 074 if (userData != null) 075 userData.remove(name); 076 } 077 078 public void setUserDataINN(String name, Object value) { 079 if (value == null) 080 return; 081 082 if (userData == null) 083 userData = new HashMap<String, Object>(); 084 userData.put(name, value); 085 } 086 087 public boolean hasUserData(String name) { 088 if (userData == null) 089 return false; 090 else 091 return userData.containsKey(name); 092 } 093 094 public String getUserString(String name) { 095 Object ud = getUserData(name); 096 if (ud == null) 097 return null; 098 if (ud instanceof String) 099 return (String) ud; 100 return ud.toString(); 101 } 102 103 public int getUserInt(String name) { 104 if (!hasUserData(name)) 105 return 0; 106 return (Integer) getUserData(name); 107 } 108 109 public boolean hasFormatComment() { 110 return (formatCommentsPre != null && !formatCommentsPre.isEmpty()) || (formatCommentsPost != null && !formatCommentsPost.isEmpty()); 111 } 112 113 public List<String> getFormatCommentsPre() { 114 if (formatCommentsPre == null) 115 formatCommentsPre = new ArrayList<String>(); 116 return formatCommentsPre; 117 } 118 119 public List<String> getFormatCommentsPost() { 120 if (formatCommentsPost == null) 121 formatCommentsPost = new ArrayList<String>(); 122 return formatCommentsPost; 123 } 124 125 // these 3 allow evaluation engines to get access to primitive values 126 public boolean isPrimitive() { 127 return false; 128 } 129 130 public boolean isBooleanPrimitive() { 131 return false; 132 } 133 134 public boolean hasPrimitiveValue() { 135 return isPrimitive(); 136 } 137 138 public String primitiveValue() { 139 return null; 140 } 141 142 public boolean isDateTime() { 143 return false; 144 } 145 146 public BaseDateTimeType dateTimeValue() { 147 return null; 148 } 149 150 public abstract String fhirType() ; 151 152 public boolean hasType(String... name) { 153 String t = fhirType(); 154 for (String n : name) 155 if (n.equalsIgnoreCase(t)) 156 return true; 157 return false; 158 } 159 160 protected abstract void listChildren(List<Property> result) ; 161 162 public Base setProperty(String name, Base value) throws FHIRException { 163 throw new FHIRException("Attempt to set unknown property "+name); 164 } 165 166 public Base addChild(String name) throws FHIRException { 167 throw new FHIRException("Attempt to add child with unknown name "+name); 168 } 169 170 /** 171 * Supports iterating the children elements in some generic processor or browser 172 * All defined children will be listed, even if they have no value on this instance 173 * 174 * Note that the actual content of primitive or xhtml elements is not iterated explicitly. 175 * To find these, the processing code must recognise the element as a primitive, typecast 176 * the value to a {@link Type}, and examine the value 177 * 178 * @return a list of all the children defined for this element 179 */ 180 public List<Property> children() { 181 List<Property> result = new ArrayList<Property>(); 182 listChildren(result); 183 return result; 184 } 185 186 public Property getChildByName(String name) { 187 List<Property> children = new ArrayList<Property>(); 188 listChildren(children); 189 for (Property c : children) 190 if (c.getName().equals(name)) 191 return c; 192 return null; 193 } 194 195 public List<Base> listChildrenByName(String name) throws FHIRException { 196 List<Base> result = new ArrayList<Base>(); 197 for (Base b : listChildrenByName(name, true)) 198 if (b != null) 199 result.add(b); 200 return result; 201 } 202 203 public Base[] listChildrenByName(String name, boolean checkValid) throws FHIRException { 204 if (name.equals("*")) { 205 List<Property> children = new ArrayList<Property>(); 206 listChildren(children); 207 List<Base> result = new ArrayList<Base>(); 208 for (Property c : children) 209 result.addAll(c.getValues()); 210 return result.toArray(new Base[result.size()]); 211 } 212 else 213 return getProperty(name.hashCode(), name, checkValid); 214 } 215 216 public boolean isEmpty() { 217 return true; // userData does not count 218 } 219 220 public boolean equalsDeep(Base other) { 221 return other != null; 222 } 223 224 public boolean equalsShallow(Base other) { 225 return other != null; 226 } 227 228 public static boolean compareDeep(String s1, String s2, boolean allowNull) { 229 if (allowNull) { 230 boolean noLeft = s1 == null || Utilities.noString(s1); 231 boolean noRight = s2 == null || Utilities.noString(s2); 232 if (noLeft && noRight) { 233 return true; 234 } 235 } 236 if (s1 == null || s2 == null) 237 return false; 238 return s1.equals(s2); 239 } 240 241 public static boolean compareDeep(List<? extends Base> e1, List<? extends Base> e2, boolean allowNull) { 242 if (noList(e1) && noList(e2) && allowNull) 243 return true; 244 if (noList(e1) || noList(e2)) 245 return false; 246 if (e1.size() != e2.size()) 247 return false; 248 for (int i = 0; i < e1.size(); i++) { 249 if (!compareDeep(e1.get(i), e2.get(i), allowNull)) 250 return false; 251 } 252 return true; 253 } 254 255 private static boolean noList(List<? extends Base> list) { 256 return list == null || list.isEmpty(); 257 } 258 259 public static boolean compareDeep(Base e1, Base e2, boolean allowNull) { 260 if (allowNull) { 261 boolean noLeft = e1 == null || e1.isEmpty(); 262 boolean noRight = e2 == null || e2.isEmpty(); 263 if (noLeft && noRight) { 264 return true; 265 } 266 } 267 if (e1 == null || e2 == null) 268 return false; 269 if (e2.isMetadataBased() && !e1.isMetadataBased()) // respect existing order for debugging consistency; outcome must be the same either way 270 return e2.equalsDeep(e1); 271 else 272 return e1.equalsDeep(e2); 273 } 274 275 public static boolean compareDeep(XhtmlNode div1, XhtmlNode div2, boolean allowNull) { 276 if (div1 == null && div2 == null && allowNull) 277 return true; 278 if (div1 == null || div2 == null) 279 return false; 280 return div1.equalsDeep(div2); 281 } 282 283 284 public static boolean compareValues(List<? extends PrimitiveType> e1, List<? extends PrimitiveType> e2, boolean allowNull) { 285 if (e1 == null && e2 == null && allowNull) 286 return true; 287 if (e1 == null || e2 == null) 288 return false; 289 if (e1.size() != e2.size()) 290 return false; 291 for (int i = 0; i < e1.size(); i++) { 292 if (!compareValues(e1.get(i), e2.get(i), allowNull)) 293 return false; 294 } 295 return true; 296 } 297 298 public static boolean compareValues(PrimitiveType e1, PrimitiveType e2, boolean allowNull) { 299 boolean noLeft = e1 == null || e1.isEmpty(); 300 boolean noRight = e2 == null || e2.isEmpty(); 301 if (noLeft && noRight && allowNull) { 302 return true; 303 } 304 if (noLeft != noRight) 305 return false; 306 return e1.equalsShallow(e2); 307 } 308 309 // -- converters for property setters 310 311 public Type castToType(Base b) throws FHIRException { 312 if (b instanceof Type) 313 return (Type) b; 314 else if (b.isMetadataBased()) 315 return ((org.hl7.fhir.r5.elementmodel.Element) b).asType(); 316 else 317 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 318 } 319 320 321 public BooleanType castToBoolean(Base b) throws FHIRException { 322 if (b instanceof BooleanType) 323 return (BooleanType) b; 324 else 325 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Boolean"); 326 } 327 328 public IntegerType castToInteger(Base b) throws FHIRException { 329 if (b instanceof IntegerType) 330 return (IntegerType) b; 331 else 332 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Integer"); 333 } 334 335 public DecimalType castToDecimal(Base b) throws FHIRException { 336 if (b instanceof DecimalType) 337 return (DecimalType) b; 338 else if (b.hasPrimitiveValue()) 339 return new DecimalType(b.primitiveValue()); 340 else 341 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Decimal"); 342 } 343 344 public Base64BinaryType castToBase64Binary(Base b) throws FHIRException { 345 if (b instanceof Base64BinaryType) 346 return (Base64BinaryType) b; 347 else 348 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Base64Binary"); 349 } 350 351 public InstantType castToInstant(Base b) throws FHIRException { 352 if (b instanceof InstantType) 353 return (InstantType) b; 354 else 355 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Instant"); 356 } 357 358 public StringType castToString(Base b) throws FHIRException { 359 if (b instanceof StringType) 360 return (StringType) b; 361 else if (b.hasPrimitiveValue()) 362 return new StringType(b.primitiveValue()); 363 else 364 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a String"); 365 } 366 367 public UriType castToUri(Base b) throws FHIRException { 368 if (b instanceof UriType) 369 return (UriType) b; 370 else if (b.hasPrimitiveValue()) 371 return new UriType(b.primitiveValue()); 372 else 373 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 374 } 375 376 public UrlType castToUrl(Base b) throws FHIRException { 377 if (b instanceof UrlType) 378 return (UrlType) b; 379 else if (b.hasPrimitiveValue()) 380 return new UrlType(b.primitiveValue()); 381 else 382 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 383 } 384 385 public CanonicalType castToCanonical(Base b) throws FHIRException { 386 if (b instanceof CanonicalType) 387 return (CanonicalType) b; 388 else if (b.hasPrimitiveValue()) 389 return new CanonicalType(b.primitiveValue()); 390 else 391 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Uri"); 392 } 393 394 public DateType castToDate(Base b) throws FHIRException { 395 if (b instanceof DateType) 396 return (DateType) b; 397 else if (b.hasPrimitiveValue()) 398 return new DateType(b.primitiveValue()); 399 else 400 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Date"); 401 } 402 403 public DateTimeType castToDateTime(Base b) throws FHIRException { 404 if (b instanceof DateTimeType) 405 return (DateTimeType) b; 406 else if (b.fhirType().equals("dateTime")) 407 return new DateTimeType(b.primitiveValue()); 408 else 409 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DateTime"); 410 } 411 412 public TimeType castToTime(Base b) throws FHIRException { 413 if (b instanceof TimeType) 414 return (TimeType) b; 415 else 416 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Time"); 417 } 418 419 public CodeType castToCode(Base b) throws FHIRException { 420 if (b instanceof CodeType) 421 return (CodeType) b; 422 else if (b.isPrimitive()) 423 return new CodeType(b.primitiveValue()); 424 else 425 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Code"); 426 } 427 428 public OidType castToOid(Base b) throws FHIRException { 429 if (b instanceof OidType) 430 return (OidType) b; 431 else 432 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Oid"); 433 } 434 435 public IdType castToId(Base b) throws FHIRException { 436 if (b instanceof IdType) 437 return (IdType) b; 438 else 439 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Id"); 440 } 441 442 public UnsignedIntType castToUnsignedInt(Base b) throws FHIRException { 443 if (b instanceof UnsignedIntType) 444 return (UnsignedIntType) b; 445 else 446 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UnsignedInt"); 447 } 448 449 public PositiveIntType castToPositiveInt(Base b) throws FHIRException { 450 if (b instanceof PositiveIntType) 451 return (PositiveIntType) b; 452 else 453 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a PositiveInt"); 454 } 455 456 public MarkdownType castToMarkdown(Base b) throws FHIRException { 457 if (b instanceof MarkdownType) 458 return (MarkdownType) b; 459 else if (b.hasPrimitiveValue()) 460 return new MarkdownType(b.primitiveValue()); 461 else 462 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Markdown"); 463 } 464 465 public Annotation castToAnnotation(Base b) throws FHIRException { 466 if (b instanceof Annotation) 467 return (Annotation) b; 468 else 469 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Annotation"); 470 } 471 472 public Dosage castToDosage(Base b) throws FHIRException { 473 if (b instanceof Dosage) 474 return (Dosage) b; 475 else throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an DosageInstruction"); 476 } 477 478 479 public Attachment castToAttachment(Base b) throws FHIRException { 480 if (b instanceof Attachment) 481 return (Attachment) b; 482 else 483 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Attachment"); 484 } 485 486 public Identifier castToIdentifier(Base b) throws FHIRException { 487 if (b instanceof Identifier) 488 return (Identifier) b; 489 else 490 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Identifier"); 491 } 492 493 public CodeableConcept castToCodeableConcept(Base b) throws FHIRException { 494 if (b instanceof CodeableConcept) 495 return (CodeableConcept) b; 496 else if (b instanceof Element) { 497 return ObjectConverter.readAsCodeableConcept((Element) b); 498 } else if (b instanceof CodeType) { 499 CodeableConcept cc = new CodeableConcept(); 500 cc.addCoding().setCode(((CodeType) b).asStringValue()); 501 return cc; 502 } else if(b instanceof StringType) { 503 CodeableConcept cc = new CodeableConcept(); 504 cc.addCoding().setCode(((StringType) b).asStringValue()); 505 return cc; 506 } else 507 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a CodeableConcept"); 508 } 509 510 public Population castToPopulation(Base b) throws FHIRException { 511 if (b instanceof Population) 512 return (Population) b; 513 else 514 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Population"); 515 } 516 517 518 public Coding castToCoding(Base b) throws FHIRException { 519 if (b instanceof Coding) 520 return (Coding) b; 521 else if (b instanceof Element) { 522 ICoding c = ((Element) b).getAsICoding(); 523 if (c == null) 524 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Coding"); 525 return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay()); 526 } else if (b instanceof ICoding) { 527 ICoding c = (ICoding) b; 528 return new Coding().setCode(c.getCode()).setSystem(c.getSystem()).setVersion(c.getVersion()).setDisplay(c.getDisplay()); 529 } else 530 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Coding"); 531 } 532 533 public Quantity castToQuantity(Base b) throws FHIRException { 534 if (b instanceof Quantity) 535 return (Quantity) b; 536 else 537 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Quantity"); 538 } 539 540 public Money castToMoney(Base b) throws FHIRException { 541 if (b instanceof Money) 542 return (Money) b; 543 else 544 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Money"); 545 } 546 547 public Duration castToDuration(Base b) throws FHIRException { 548 if (b instanceof Duration) 549 return (Duration) b; 550 else 551 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an Duration"); 552 } 553 554 public SimpleQuantity castToSimpleQuantity(Base b) throws FHIRException { 555 if (b instanceof SimpleQuantity) 556 return (SimpleQuantity) b; 557 else if (b instanceof Quantity) { 558 Quantity q = (Quantity) b; 559 SimpleQuantity sq = new SimpleQuantity(); 560 sq.setValueElement(q.getValueElement()); 561 sq.setComparatorElement(q.getComparatorElement()); 562 sq.setUnitElement(q.getUnitElement()); 563 sq.setSystemElement(q.getSystemElement()); 564 sq.setCodeElement(q.getCodeElement()); 565 return sq; 566 } else 567 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to an SimpleQuantity"); 568 } 569 570 public Range castToRange(Base b) throws FHIRException { 571 if (b instanceof Range) 572 return (Range) b; 573 else 574 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Range"); 575 } 576 577 public Period castToPeriod(Base b) throws FHIRException { 578 if (b instanceof Period) 579 return (Period) b; 580 else 581 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Period"); 582 } 583 584 public Ratio castToRatio(Base b) throws FHIRException { 585 if (b instanceof Ratio) 586 return (Ratio) b; 587 else 588 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Ratio"); 589 } 590 591 public SampledData castToSampledData(Base b) throws FHIRException { 592 if (b instanceof SampledData) 593 return (SampledData) b; 594 else 595 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a SampledData"); 596 } 597 598 public Signature castToSignature(Base b) throws FHIRException { 599 if (b instanceof Signature) 600 return (Signature) b; 601 else 602 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Signature"); 603 } 604 605 public HumanName castToHumanName(Base b) throws FHIRException { 606 if (b instanceof HumanName) 607 return (HumanName) b; 608 else 609 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a HumanName"); 610 } 611 612 public Address castToAddress(Base b) throws FHIRException { 613 if (b instanceof Address) 614 return (Address) b; 615 else 616 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Address"); 617 } 618 619 public ContactDetail castToContactDetail(Base b) throws FHIRException { 620 if (b instanceof ContactDetail) 621 return (ContactDetail) b; 622 else 623 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactDetail"); 624 } 625 626 public Contributor castToContributor(Base b) throws FHIRException { 627 if (b instanceof Contributor) 628 return (Contributor) b; 629 else 630 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Contributor"); 631 } 632 633 public UsageContext castToUsageContext(Base b) throws FHIRException { 634 if (b instanceof UsageContext) 635 return (UsageContext) b; 636 else 637 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a UsageContext"); 638 } 639 640 public RelatedArtifact castToRelatedArtifact(Base b) throws FHIRException { 641 if (b instanceof RelatedArtifact) 642 return (RelatedArtifact) b; 643 else 644 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a RelatedArtifact"); 645 } 646 647 public ContactPoint castToContactPoint(Base b) throws FHIRException { 648 if (b instanceof ContactPoint) 649 return (ContactPoint) b; 650 else 651 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ContactPoint"); 652 } 653 654 public Timing castToTiming(Base b) throws FHIRException { 655 if (b instanceof Timing) 656 return (Timing) b; 657 else 658 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Timing"); 659 } 660 661 public Reference castToReference(Base b) throws FHIRException { 662 if (b instanceof Reference) 663 return (Reference) b; 664 else if (b.isPrimitive() && Utilities.isURL(b.primitiveValue())) 665 return new Reference().setReference(b.primitiveValue()); 666 else if (b instanceof org.hl7.fhir.r5.elementmodel.Element && b.fhirType().equals("Reference")) { 667 org.hl7.fhir.r5.elementmodel.Element e = (org.hl7.fhir.r5.elementmodel.Element) b; 668 return new Reference().setReference(e.getChildValue("reference")).setDisplay(e.getChildValue("display")); 669 } else 670 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Reference"); 671 } 672 673 public Meta castToMeta(Base b) throws FHIRException { 674 if (b instanceof Meta) 675 return (Meta) b; 676 else 677 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Meta"); 678 } 679 680 681 public MarketingStatus castToMarketingStatus(Base b) throws FHIRException { 682 if (b instanceof MarketingStatus) 683 return (MarketingStatus) b; 684 else 685 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a MarketingStatus"); 686 } 687 688 public Statistic castToStatistic(Base b) throws FHIRException { 689 if (b instanceof Statistic) 690 return (Statistic) b; 691 else 692 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Statistic"); 693 } 694 695 696 public OrderedDistribution castToOrderedDistribution(Base b) throws FHIRException { 697 if (b instanceof OrderedDistribution) 698 return (OrderedDistribution) b; 699 else 700 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a OrderedDistribution"); 701 } 702 703 public ProductShelfLife castToProductShelfLife(Base b) throws FHIRException { 704 if (b instanceof ProductShelfLife) 705 return (ProductShelfLife) b; 706 else 707 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ProductShelfLife"); 708 } 709 710 public ProdCharacteristic castToProdCharacteristic(Base b) throws FHIRException { 711 if (b instanceof ProdCharacteristic) 712 return (ProdCharacteristic) b; 713 else 714 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ProdCharacteristic"); 715 } 716 717 718 public SubstanceAmount castToSubstanceAmount(Base b) throws FHIRException { 719 if (b instanceof SubstanceAmount) 720 return (SubstanceAmount) b; 721 else 722 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a SubstanceAmount"); 723 } 724 725 public Extension castToExtension(Base b) throws FHIRException { 726 if (b instanceof Extension) 727 return (Extension) b; 728 else 729 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Extension"); 730 } 731 732 public Resource castToResource(Base b) throws FHIRException { 733 if (b instanceof Resource) 734 return (Resource) b; 735 else 736 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Resource"); 737 } 738 739 public Narrative castToNarrative(Base b) throws FHIRException { 740 if (b instanceof Narrative) 741 return (Narrative) b; 742 else 743 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Narrative"); 744 } 745 746 747 public ElementDefinition castToElementDefinition(Base b) throws FHIRException { 748 if (b instanceof ElementDefinition) 749 return (ElementDefinition) b; 750 else 751 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ElementDefinition"); 752 } 753 754 public DataRequirement castToDataRequirement(Base b) throws FHIRException { 755 if (b instanceof DataRequirement) 756 return (DataRequirement) b; 757 else 758 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a DataRequirement"); 759 } 760 761 public Expression castToExpression(Base b) throws FHIRException { 762 if (b instanceof Expression) 763 return (Expression) b; 764 else 765 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a Expression"); 766 } 767 768 769 public ParameterDefinition castToParameterDefinition(Base b) throws FHIRException { 770 if (b instanceof ParameterDefinition) 771 return (ParameterDefinition) b; 772 else 773 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to a ParameterDefinition"); 774 } 775 776 public TriggerDefinition castToTriggerDefinition(Base b) throws FHIRException { 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 XhtmlNode castToXhtml(Base b) throws FHIRException { 784 if (b instanceof Element) { 785 return ((Element) b).getXhtml(); 786 } else if (b instanceof XhtmlType) { 787 return ((XhtmlType) b).getValue(); 788 } else if (b instanceof StringType) { 789 try { 790 return new XhtmlParser().parseFragment(((StringType) b).asStringValue()); 791 } catch (IOException e) { 792 throw new FHIRException(e); 793 } 794 } else 795 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml"); 796 } 797 798 public String castToXhtmlString(Base b) throws FHIRException { 799 if (b instanceof Element) { 800 return ((Element) b).getValue(); 801 } else if (b instanceof XhtmlType) { 802 try { 803 return new XhtmlComposer(true).compose(((XhtmlType) b).getValue()); 804 } catch (IOException e) { 805 return null; 806 } 807 } else if (b instanceof StringType) { 808 return ((StringType) b).asStringValue(); 809 } else 810 throw new FHIRException("Unable to convert a "+b.getClass().getName()+" to XHtml string"); 811 } 812 813 protected boolean isMetadataBased() { 814 return false; 815 } 816 817 public Base[] getProperty(int hash, String name, boolean checkValid) throws FHIRException { 818 if (checkValid) 819 throw new FHIRException("Attempt to read invalid property '"+name+"' on type "+fhirType()); 820 return null; 821 } 822 823 public Base setProperty(int hash, String name, Base value) throws FHIRException { 824 throw new FHIRException("Attempt to write to invalid property '"+name+"' on type "+fhirType()); 825 } 826 827 public Base makeProperty(int hash, String name) throws FHIRException { 828 throw new FHIRException("Attempt to make an invalid property '"+name+"' on type "+fhirType()); 829 } 830 831 public String[] getTypesForProperty(int hash, String name) throws FHIRException { 832 throw new FHIRException("Attempt to get types for an invalid property '"+name+"' on type "+fhirType()); 833 } 834 835 public static boolean equals(String v1, String v2) { 836 if (v1 == null && v2 == null) 837 return true; 838 else if (v1 == null || v2 == null) 839 return false; 840 else 841 return v1.equals(v2); 842 } 843 844 public boolean isResource() { 845 return false; 846 } 847 848 849 public abstract String getIdBase(); 850 public abstract void setIdBase(String value); 851 852 public Property getNamedProperty(String _name) throws FHIRException { 853 return getNamedProperty(_name.hashCode(), _name, false); 854 } 855 public Property getNamedProperty(int _hash, String _name, boolean _checkValid) throws FHIRException { 856 if (_checkValid) 857 throw new FHIRException("Attempt to read invalid property '"+_name+"' on type "+fhirType()); 858 return null; 859 } 860 861}