示例#1
0
  private void encodeResourceToStreamWriterInDstu2Format(
      RuntimeResourceDefinition theResDef,
      IBaseResource theResource,
      IBase theElement,
      XMLStreamWriter theEventWriter,
      BaseRuntimeElementCompositeDefinition<?> resDef,
      boolean theIncludedResource)
      throws XMLStreamException, DataFormatException {
    /*
     * DSTU2 requires extensions to come in a specific spot within the encoded content - This is a bit of a messy way to make that happen, but hopefully this won't matter as much once we use the
     * HL7 structures
     */

    List<BaseRuntimeChildDefinition> preExtensionChildren =
        new ArrayList<BaseRuntimeChildDefinition>();
    List<BaseRuntimeChildDefinition> postExtensionChildren =
        new ArrayList<BaseRuntimeChildDefinition>();
    List<BaseRuntimeChildDefinition> children = resDef.getChildren();
    for (BaseRuntimeChildDefinition next : children) {
      if (next.getElementName().equals("text")) {
        preExtensionChildren.add(next);
      } else if (next.getElementName().equals("contained")) {
        preExtensionChildren.add(next);
      } else {
        postExtensionChildren.add(next);
      }
    }
    encodeCompositeElementChildrenToStreamWriter(
        theResource, theElement, theEventWriter, preExtensionChildren, theIncludedResource);

    encodeExtensionsIfPresent(theResource, theEventWriter, theElement, theIncludedResource);
    encodeCompositeElementChildrenToStreamWriter(
        theResource, theElement, theEventWriter, resDef.getExtensions(), theIncludedResource);

    encodeCompositeElementChildrenToStreamWriter(
        theResource, theElement, theEventWriter, postExtensionChildren, theIncludedResource);
  }
示例#2
0
  private void encodeCompositeElementChildrenToStreamWriter(
      IBaseResource theResource,
      IBase theElement,
      XMLStreamWriter theEventWriter,
      List<? extends BaseRuntimeChildDefinition> children,
      boolean theIncludedResource)
      throws XMLStreamException, DataFormatException {
    for (BaseRuntimeChildDefinition nextChild : children) {
      if (nextChild.getElementName().equals("extension")
          || nextChild.getElementName().equals("modifierExtension")) {
        continue;
      }

      if (nextChild instanceof RuntimeChildNarrativeDefinition && !theIncludedResource) {
        INarrativeGenerator gen = myContext.getNarrativeGenerator();
        if (theResource instanceof IResource) {
          BaseNarrativeDt<?> narr = ((IResource) theResource).getText();
          if (gen != null && narr.isEmpty()) {
            String resourceProfile =
                myContext.getResourceDefinition(theResource).getResourceProfile();
            gen.generateNarrative(resourceProfile, theResource, narr);
          }
          if (narr.isEmpty() == false) {
            RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
            String childName = nextChild.getChildNameByDatatype(child.getDatatype());
            BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
            encodeChildElementToStreamWriter(
                theResource, theEventWriter, narr, childName, type, null, theIncludedResource);
            continue;
          }
        } else {
          INarrative narr1 = ((IDomainResource) theResource).getText();
          BaseNarrativeDt<?> narr2 = null;
          if (gen != null && narr1.isEmpty()) {
            // TODO: need to implement this
            String resourceProfile =
                myContext.getResourceDefinition(theResource).getResourceProfile();
            gen.generateNarrative(resourceProfile, theResource, null);
          }
          if (narr2 != null) {
            RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
            String childName = nextChild.getChildNameByDatatype(child.getDatatype());
            BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
            encodeChildElementToStreamWriter(
                theResource, theEventWriter, narr2, childName, type, null, theIncludedResource);
            continue;
          }
        }
      }

      List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
      if (values == null || values.isEmpty()) {
        continue;
      }

      for (IBase nextValue : values) {
        if ((nextValue == null || nextValue.isEmpty()) && !(nextValue instanceof BaseContainedDt)) {
          continue;
        }
        Class<? extends IBase> type = nextValue.getClass();
        String childName = nextChild.getChildNameByDatatype(type);
        String extensionUrl = nextChild.getExtensionUrl();
        BaseRuntimeElementDefinition<?> childDef =
            nextChild.getChildElementDefinitionByDatatype(type);
        if (childDef == null) {
          super.throwExceptionForUnknownChildType(nextChild, type);
        }

        if (nextValue instanceof IBaseExtension
            && myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) {
          // This is called for the Query resource in DSTU1 only
          extensionUrl = ((IBaseExtension<?>) nextValue).getUrl();
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              extensionUrl,
              theIncludedResource);

        } else if (extensionUrl != null && childName.equals("extension") == false) {
          RuntimeChildDeclaredExtensionDefinition extDef =
              (RuntimeChildDeclaredExtensionDefinition) nextChild;
          if (extDef.isModifier()) {
            theEventWriter.writeStartElement("modifierExtension");
          } else {
            theEventWriter.writeStartElement("extension");
          }

          theEventWriter.writeAttribute("url", extensionUrl);
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              null,
              theIncludedResource);
          theEventWriter.writeEndElement();
        } else if (nextChild instanceof RuntimeChildNarrativeDefinition && theIncludedResource) {
          // suppress narratives from contained resources
        } else {
          encodeChildElementToStreamWriter(
              theResource,
              theEventWriter,
              nextValue,
              childName,
              childDef,
              extensionUrl,
              theIncludedResource);
        }
      }
    }
  }