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