private void loadValueSet(String n) throws FileNotFoundException, Exception {
   XmlParser xml = new XmlParser();
   ValueSet vs =
       (ValueSet)
           xml.parse(
               new CSFileInputStream(
                   srcDir
                       + ini.getStringProperty("valuesets", n).replace('\\', File.separatorChar)));
   vs.setIdentifierSimple("http://hl7.org/fhir/vs/" + n);
   definitions.getExtraValuesets().put(n, vs);
 }
 public static String describeBinding(
     String prefix, ElementDefinitionBindingComponent def, PageProcessor page) throws Exception {
   if (!def.hasValueSet()) return def.getDescription();
   String ref =
       def.getValueSet() instanceof UriType
           ? ((UriType) def.getValueSet()).asStringValue()
           : ((Reference) def.getValueSet()).getReference();
   ValueSet vs = page.getValueSets().get(ref);
   if (vs != null) {
     String pp = (String) vs.getUserData("path");
     return def.getDescription()
         + "<br/>"
         + conf(def)
         + "<a href=\""
         + prefix
         + pp.replace(File.separatorChar, '/')
         + "\">"
         + vs.getName()
         + "</a>"
         + confTail(def);
   }
   if (ref.startsWith("http:") || ref.startsWith("https:"))
     return def.getDescription()
         + "<br/>"
         + conf(def)
         + " <a href=\""
         + ref
         + "\">"
         + ref
         + "</a>"
         + confTail(def);
   else
     return def.getDescription()
         + "<br/>"
         + conf(def)
         + " ?? Broken Reference to "
         + ref
         + " ??"
         + confTail(def);
 }
  private void validateQuestionAnswers(
      List<ValidationMessage> theErrors,
      QuestionComponent theQuestion,
      LinkedList<String> thePathStack,
      AnswerFormat type,
      org.hl7.fhir.instance.model.QuestionnaireResponse.QuestionComponent answerQuestion,
      QuestionnaireResponse theAnswers,
      boolean theValidateRequired) {

    String linkId = theQuestion.getLinkId();
    Set<Class<? extends Type>> allowedAnswerTypes = determineAllowedAnswerTypes(type);
    if (allowedAnswerTypes.isEmpty()) {
      for (QuestionAnswerComponent nextAnswer : answerQuestion.getAnswer()) {
        rule(
            theErrors,
            IssueType.BUSINESSRULE,
            thePathStack,
            ElementUtil.isEmpty(nextAnswer.getValue()),
            "Question with linkId[{0}] has no answer type but an answer was provided",
            linkId);
      }
    } else {
      rule(
          theErrors,
          IssueType.BUSINESSRULE,
          thePathStack,
          !(answerQuestion.getAnswer().size() > 1 && !theQuestion.getRepeats()),
          "Multiple answers to non repeating question with linkId[{0}]",
          linkId);
      if (theValidateRequired) {
        rule(
            theErrors,
            IssueType.BUSINESSRULE,
            thePathStack,
            !(theQuestion.getRequired() && answerQuestion.getAnswer().isEmpty()),
            "Missing answer to required question with linkId[{0}]",
            linkId);
      } else {
        hint(
            theErrors,
            IssueType.BUSINESSRULE,
            thePathStack,
            !(theQuestion.getRequired() && answerQuestion.getAnswer().isEmpty()),
            "Missing answer to required question with linkId[{0}]",
            linkId);
      }
    }

    int answerIdx = -1;
    for (QuestionAnswerComponent nextAnswer : answerQuestion.getAnswer()) {
      answerIdx++;
      try {
        thePathStack.add("answer[" + answerIdx + "]");
        Type nextValue = nextAnswer.getValue();
        if (nextValue == null) {
          continue;
        }
        if (!allowedAnswerTypes.contains(nextValue.getClass())) {
          rule(
              theErrors,
              IssueType.BUSINESSRULE,
              thePathStack,
              false,
              "Answer to question with linkId[{0}] found of type [{1}] but this is invalid for question of type [{2}]",
              linkId,
              nextValue.getClass().getSimpleName(),
              type.toCode());
          continue;
        }

        // Validate choice answers
        if (type == AnswerFormat.CHOICE || type == AnswerFormat.OPENCHOICE) {
          if (nextAnswer.getValue() instanceof StringType) {
            StringType answer = (StringType) nextAnswer.getValue();
            if (answer == null || isBlank(answer.getValueAsString())) {
              rule(
                  theErrors,
                  IssueType.BUSINESSRULE,
                  thePathStack,
                  false,
                  "Answer to question with linkId[{0}] is required but answer does not have a value",
                  linkId);
              continue;
            }
          } else {
            Coding coding = (Coding) nextAnswer.getValue();
            if (isBlank(coding.getCode())) {
              rule(
                  theErrors,
                  IssueType.BUSINESSRULE,
                  thePathStack,
                  false,
                  "Answer to question with linkId[{0}] is of type {1} but coding answer does not have a code",
                  linkId,
                  type.name());
              continue;
            }
            if (isBlank(coding.getSystem())) {
              rule(
                  theErrors,
                  IssueType.BUSINESSRULE,
                  thePathStack,
                  false,
                  "Answer to question with linkId[{0}] is of type {1} but coding answer does not have a system",
                  linkId,
                  type.name());
              continue;
            }

            String optionsRef = theQuestion.getOptions().getReference();
            if (isNotBlank(optionsRef)) {
              ValueSet valueSet = getValueSet(theAnswers, theQuestion.getOptions());
              if (valueSet == null) {
                rule(
                    theErrors,
                    IssueType.BUSINESSRULE,
                    thePathStack,
                    false,
                    "Question with linkId[{0}] has options ValueSet[{1}] but this ValueSet can not be found",
                    linkId,
                    optionsRef);
                continue;
              }

              boolean found = false;
              if (coding.getSystem().equals(valueSet.getCodeSystem().getSystem())) {
                for (ConceptDefinitionComponent next : valueSet.getCodeSystem().getConcept()) {
                  if (coding.getCode().equals(next.getCode())) {
                    found = true;
                    break;
                  }
                }
              }
              if (!found) {
                for (ConceptSetComponent nextCompose : valueSet.getCompose().getInclude()) {
                  if (coding.getSystem().equals(nextCompose.getSystem())) {
                    for (ConceptReferenceComponent next : nextCompose.getConcept()) {
                      if (coding.getCode().equals(next.getCode())) {
                        found = true;
                        break;
                      }
                    }
                  }
                  if (found) {
                    break;
                  }
                }
              }

              rule(
                  theErrors,
                  IssueType.BUSINESSRULE,
                  thePathStack,
                  found,
                  "Question with linkId[{0}] has answer with system[{1}] and code[{2}] but this is not a valid answer for ValueSet[{3}]",
                  linkId,
                  coding.getSystem(),
                  coding.getCode(),
                  optionsRef);
            }
          }
        }

      } finally {
        thePathStack.removeLast();
      }
    } // for answers
  }
 public static String describeBinding(String prefix, BindingSpecification cd, PageProcessor page)
     throws Exception {
   if (cd.getBinding() == BindingSpecification.BindingMethod.Unbound) return cd.getDefinition();
   if (cd.getBinding() == BindingSpecification.BindingMethod.Special) {
     if (cd.getValueSet().getName().equals("MessageEvent"))
       return "the <a href=\""
           + prefix
           + "valueset-message-events.html\">Event List in the messaging framework</a>";
     else if (cd.getValueSet().getName().equals("ResourceType"))
       return "<a href=\""
           + prefix
           + "valueset-resource-types.html\">Any defined Resource Type name</a>";
     else if (cd.getValueSet().getName().equals("DataType"))
       return "<a href=\"" + prefix + "valueset-data-types.html\">Any defined Data Type name</a>";
     else if (cd.getValueSet().getName().equals("FHIRDefinedType"))
       return "<a href=\""
           + prefix
           + "valueset-defined-types.html\">Any defined Resource or Data Type name</a>";
     else throw new Exception("Unknown special type " + cd.getValueSet().getName());
   }
   String bs =
       "<a href=\""
           + prefix
           + "terminologies.html#"
           + cd.getStrength().toCode()
           + "\">"
           + cd.getStrength().getDisplay()
           + "</a>";
   if (cd.getValueSet() != null) {
     ValueSet vs = cd.getValueSet();
     String pp = (String) vs.getUserData("path");
     return "<a href=\""
         + prefix
         + pp.replace(File.separatorChar, '/')
         + "\">"
         + cd.getDefinition()
         + "</a> ("
         + bs
         + ")";
   } else if (cd.getBinding() == BindingSpecification.BindingMethod.ValueSet) {
     if (Utilities.noString(cd.getReference())) return cd.getDescription();
     else if (cd.getValueSet() == null)
       return bs
           + ": <a href=\""
           + (cd.getReference().startsWith("http")
               ? cd.getReference()
               : prefix + cd.getReference() + ".html")
           + "\">See "
           + cd.getDescription()
           + "</a> ("
           + cd.getDefinition()
           + ")";
     else
       return bs
           + ": <a href=\""
           + prefix
           + cd.getReference()
           + ".html\">See "
           + cd.getValueSet().getUrl()
           + "</a> ("
           + cd.getDefinition()
           + ")";
   } else if (cd.getBinding() == BindingSpecification.BindingMethod.CodeList) {
     if (Utilities.noString(cd.getReference()))
       return bs + ": " + cd.getDescription() + " (" + cd.getDefinition() + ")";
     else
       return bs
           + ": <a href=\""
           + prefix
           + "valueset-"
           + cd.getReference().substring(1)
           + ".html\">http://hl7.org/fhir/"
           + cd.getReference().substring(1)
           + "</a> ("
           + cd.getDefinition()
           + ")";
   }
   if (cd.getBinding() == BindingSpecification.BindingMethod.Reference) {
     return bs
         + ": <a href=\""
         + prefix
         + cd.getReference()
         + "\">"
         + cd.getDescription()
         + "</a> ("
         + cd.getDefinition()
         + ")";
   }
   return "??";
 }
  private void gen(String prefix, Map<BindingSpecification, List<CDUsage>> txusages2)
      throws Exception {
    List<BindingSpecification> cds = new ArrayList<BindingSpecification>();
    cds.addAll(txusages.keySet());
    if (cds.size() == 0) return;

    Collections.sort(cds, new MyCompare());
    write("<h3>\r\nTerminology Bindings\r\n</h3>\r\n");
    // 1. new form
    write("<table class=\"grid\">\r\n");
    write(" <tr><th>Path</th><th>Definition</th><th>Type</th><th>Reference</th></tr>\r\n");
    for (BindingSpecification cd : cds) {
      String path;
      List<CDUsage> list = txusages.get(cd);
      for (int i = 2; i < list.size(); i++) {
        if (!list.get(i).element.typeCode().equals(list.get(1).element.typeCode()))
          throw new Exception(
              "Mixed types on one concept domain in one type - not yet supported by the build process for binding "
                  + cd.getName());
      }
      String name = cd.getValueSet() != null ? cd.getValueSet().getName() : cd.getName();
      write(" <tr><td valign=\"top\" title=\"" + name + "\">");
      boolean first = true;
      for (int i = 1; i < list.size(); i++) {
        if (!first) write("<br/>");
        first = false;
        write(list.get(i).path);
      }
      write(" </td>");
      write("<td valign=\"top\">" + Utilities.escapeXml(cd.getDefinition()) + "</td>");
      if (cd.getBinding() == BindingMethod.Unbound)
        write("<td>Unknown</td><td valign=\"top\">No details provided yet</td>");
      else {
        write(
            "<td><a href=\""
                + prefix
                + "terminologies.html#"
                + cd.getStrength().toCode()
                + "\">"
                + cd.getStrength().getDisplay()
                + "</a></td>");
        write("<td valign=\"top\">");
        if (cd.getBinding() == BindingSpecification.BindingMethod.Special) {
          if (name.equals("MessageEvent"))
            write(
                "<a href=\""
                    + prefix
                    + "valueset-message-events.html\">http://hl7.org/fhir/valueset/message-events</a>");
          else if (name.equals("ResourceType"))
            write(
                "<a href=\""
                    + prefix
                    + "valueset-resource-types.html\">http://hl7.org/fhir/valueset/resource-types</a>");
          else if (name.equals("DataType"))
            write(
                "<a href=\""
                    + prefix
                    + "valueset-data-types.html\">http://hl7.org/fhir/valueset/data-types</a>");
          else if (name.equals("FHIRDefinedType"))
            write(
                "<a href=\""
                    + prefix
                    + "valueset-defined-types.html\">http://hl7.org/fhir/valueset/defined-types</a>");
          else throw new Exception("Unknown special type " + name);
        }
        if (cd.getValueSet() != null) {
          ValueSet vs = cd.getValueSet();
          String pp = (String) vs.getUserData("path");
          if (pp == null) throw new Exception("unknown path on " + cd.getReference());
          write(
              "<a href=\""
                  + prefix
                  + pp.replace(File.separatorChar, '/')
                  + "\">"
                  + vs.getName()
                  + "</a><!-- b -->");
        } else if (cd.getBinding() == BindingSpecification.BindingMethod.ValueSet) {
          if (Utilities.noString(cd.getReference())) write("??");
          else if (cd.getReference().startsWith("valueset-"))
            write(
                "<a href=\""
                    + prefix
                    + cd.getReference()
                    + ".html\">http://hl7.org/fhir/ValueSet/"
                    + cd.getReference().substring(9)
                    + "</a><!-- a -->");
          else if (cd.getReference().startsWith("http://hl7.org/fhir")) {
            if (cd.getReference().startsWith("http://hl7.org/fhir/ValueSet/v3-")) {
              ValueSet vs = page.getValueSets().get(cd.getReference());
              String pp = (String) vs.getUserData("path");
              if (pp == null) throw new Exception("unknown path on " + cd.getReference());
              write(
                  "<a href=\""
                      + prefix
                      + pp.replace(File.separatorChar, '/')
                      + "\">"
                      + cd.getReference()
                      + "</a><!-- b -->");
            } else if (cd.getReference().startsWith("http://hl7.org/fhir/ValueSet/v2-")) {
              ValueSet vs = page.getValueSets().get(cd.getReference());
              String pp = (String) vs.getUserData("path");
              write(
                  "<a href=\""
                      + prefix
                      + pp.replace(File.separatorChar, '/')
                      + "\">"
                      + cd.getReference()
                      + "</a><!-- c -->");
            } else if (cd.getReference().startsWith("http://hl7.org/fhir/ValueSet/")) {
              String ref = getBindingLink(cd);
              write("<a href=\"" + prefix + ref + "\">" + cd.getReference() + "</a><!-- d -->");
              //              BindingSpecification bs1 =
              // page.getDefinitions().getBindingByURL(cd.getReference());
              //              if (bs1 != null)
              //                write("<a
              // href=\""+cd.getReference().substring(23)+".html\">"+cd.getReference()+"</a><!-- d
              // -->");
              //              else
              //                write("<a
              // href=\"valueset-"+cd.getReference().substring(23)+".html\">"+cd.getReference()+"</a><!-- d -->");
            } else
              throw new Exception("Internal reference " + cd.getReference() + " not handled yet");
          } else if (cd.getReference().startsWith("http:"))
            write("<a href=\"" + cd.getReference() + "\">" + cd.getReference() + "</a><!-- e -->");
          else
            write(
                "<a href=\""
                    + prefix
                    + "valueset-"
                    + cd.getReference()
                    + ".html\">http://hl7.org/fhir/"
                    + cd.getReference()
                    + "</a><!-- e -->");
        } else if (cd.getBinding() == BindingSpecification.BindingMethod.CodeList) {
          write(
              "<a href=\""
                  + prefix
                  + "valueset-"
                  + cd.getReference().substring(1)
                  + ".html\">http://hl7.org/fhir/"
                  + cd.getReference().substring(1)
                  + "</a><!-- f -->");
        } else if (cd.getBinding() == BindingSpecification.BindingMethod.Reference) {
          write(
              "<a href=\""
                  + prefix
                  + cd.getReference()
                  + "\">"
                  + cd.getDescription()
                  + "</a><!-- g -->");
        }

        write("</td>");
      }
      write(" </tr>\r\n");
    }
    write("</table>\r\n<p> </p>\r\n");
  }
  public void validate(ValueSet vs, boolean internal) throws Exception {

    if (internal && vs.getDefine() != null) {
      for (ValueSetDefineConceptComponent t : vs.getDefine().getConcept()) checkLowercase(t);
    }
  }