Java Code Examples for org.hl7.fhir.dstu3.model.ElementDefinition#getType()

The following examples show how to use org.hl7.fhir.dstu3.model.ElementDefinition#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 6 votes vote down vote up
/**
 * Finds internal references in an Element's Binding and StructureDefinition references (in TypeRef) and bases them on the given url
 * @param url - the base url to use to turn internal references into absolute references
 * @param element - the Element to update
 * @return - the updated Element
 */
private ElementDefinition updateURLs(String url, ElementDefinition element) {
  if (element != null) {
    ElementDefinition defn = element;
    if (defn.hasBinding() && defn.getBinding().getValueSet() instanceof Reference && ((Reference)defn.getBinding().getValueSet()).getReference().startsWith("#"))
      ((Reference)defn.getBinding().getValueSet()).setReference(url+((Reference)defn.getBinding().getValueSet()).getReference());
    for (TypeRefComponent t : defn.getType()) {
      if (t.hasProfile()) {
        if (t.getProfile().startsWith("#"))
          t.setProfile(url+t.getProfile());
      }
      if (t.hasTargetProfile()) {
        if (t.getTargetProfile().startsWith("#"))
          t.setTargetProfile(url+t.getTargetProfile());
      }
    }
  }
  return element;
}
 
Example 2
Source File: Property.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public String getType(String elementName) {
   if (!definition.getPath().contains("."))
     return definition.getPath();
   ElementDefinition ed = definition;
   if (definition.hasContentReference()) {
     if (!definition.getContentReference().startsWith("#"))
       throw new Error("not handled yet");
     boolean found = false;
     for (ElementDefinition d : structure.getSnapshot().getElement()) {
       if (d.hasId() && d.getId().equals(definition.getContentReference().substring(1))) {
         found = true;
         ed = d;
       }
     }
     if (!found)
       throw new Error("Unable to resolve "+definition.getContentReference()+" at "+definition.getPath()+" on "+structure.getUrl());
   }
   if (ed.getType().size() == 0)
		return null;
   else if (ed.getType().size() > 1) {
     String t = ed.getType().get(0).getCode();
		boolean all = true;
     for (TypeRefComponent tr : ed.getType()) {
			if (!t.equals(tr.getCode()))
				all = false;
		}
		if (all)
			return t;
     String tail = ed.getPath().substring(ed.getPath().lastIndexOf(".")+1);
     if (tail.endsWith("[x]") && elementName != null && elementName.startsWith(tail.substring(0, tail.length()-3))) {
			String name = elementName.substring(tail.length()-3);
       return isPrimitive(lowFirst(name)) ? lowFirst(name) : name;        
		} else
       throw new Error("logic error, gettype when types > 1, name mismatch for "+elementName+" on at "+ed.getPath());
   } else if (ed.getType().get(0).getCode() == null) {
     return structure.getId();
	} else
     return ed.getType().get(0).getCode();
}
 
Example 3
Source File: Property.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
protected List<Property> getChildProperties(TypeDetails type) throws DefinitionException {
  ElementDefinition ed = definition;
  StructureDefinition sd = structure;
  List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed);
  if (children.isEmpty()) {
    // ok, find the right definitions
    String t = null;
    if (ed.getType().size() == 1)
      t = ed.getType().get(0).getCode();
    else if (ed.getType().size() == 0)
      throw new Error("types == 0, and no children found");
    else {
      t = ed.getType().get(0).getCode();
      boolean all = true;
      for (TypeRefComponent tr : ed.getType()) {
        if (!tr.getCode().equals(t)) {
          all = false;
          break;
        }
      }
      if (!all) {
        // ok, it's polymorphic
        t = type.getType();
      }
    }
    if (!"xhtml".equals(t)) {
      sd = context.fetchResource(StructureDefinition.class, t);
      if (sd == null)
        throw new DefinitionException("Unable to find class '"+t+"' for name '"+ed.getPath()+"' on property "+definition.getPath());
      children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0));
    }
  }
  List<Property> properties = new ArrayList<Property>();
  for (ElementDefinition child : children) {
    properties.add(new Property(context, child, sd));
  }
  return properties;
}
 
Example 4
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private String typeSummary(ElementDefinition ed) {
  StringBuilder b = new StringBuilder();
  boolean first = true;
  for (TypeRefComponent tr : ed.getType()) {
    if (first) 
      first = false;
    else
      b.append("|");
    b.append(tr.getCode());
  }
  return b.toString();
}
 
Example 5
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private boolean hasBindableType(ElementDefinition ed) {
  for (TypeRefComponent tr : ed.getType()) {
    if (Utilities.existsInList(tr.getCode(), "Coding", "CodeableConcept", "Quantity", "url", "string", "code"))
      return true;
  }
  return false;
}
 
Example 6
Source File: ProfileUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
private List<String> listReferenceProfiles(ElementDefinition ed) {
  List<String> res = new ArrayList<String>();
  for (TypeRefComponent tr : ed.getType()) {
    // code is null if we're dealing with "value" and profile is null if we just have Reference()
    if (tr.getCode()!= null && "Reference".equals(tr.getCode()) && tr.getTargetProfile() != null)
      res.add(tr.getTargetProfile());
  }
  return res ;
}
 
Example 7
Source File: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a set of alternative shapes
 * @param ed Containing element definition
 * @param id Element definition identifier
 * @param shortId id to use in the actual definition
 * @return ShEx list of alternative anonymous shapes separated by "OR"
 */
private ST genAlternativeTypes(ElementDefinition ed, String id, String shortId) {
  ST shex_alt = tmplt(ALTERNATIVE_SHAPES_TEMPLATE);
  List<String> altEntries = new ArrayList<String>();


  for(ElementDefinition.TypeRefComponent typ : ed.getType())  {
    altEntries.add(genAltEntry(id, typ));
  }
  shex_alt.add("altEntries", StringUtils.join(altEntries, " OR\n    "));
  return shex_alt;
}
 
Example 8
Source File: ShExGenerator.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
/**
 * Generate a list of type choices for a "name[x]" style id
 * @param sd Structure containing ed
 * @param ed element definition
 * @param id choice identifier
 * @return ShEx fragment for the set of choices
 */
private String genChoiceTypes(StructureDefinition sd, ElementDefinition ed, String id) {
  List<String> choiceEntries = new ArrayList<String>();
  String base = id.replace("[x]", "");

  for(ElementDefinition.TypeRefComponent typ : ed.getType())
    choiceEntries.add(genChoiceEntry(sd, ed, id, base, typ));

  return StringUtils.join(choiceEntries, " |\n");
}