예제 #1
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);
        }
      }
    }
  }
예제 #2
0
  private void encodeChildElementToStreamWriter(
      IBaseResource theResource,
      XMLStreamWriter theEventWriter,
      IBase nextValue,
      String childName,
      BaseRuntimeElementDefinition<?> childDef,
      String theExtensionUrl,
      boolean theIncludedResource)
      throws XMLStreamException, DataFormatException {
    if (nextValue.isEmpty()) {
      if (childDef.getChildType() == ChildTypeEnum.CONTAINED_RESOURCES
          && getContainedResources().isEmpty() == false
          && theIncludedResource == false) {
        // We still want to go in..
      } else {
        return;
      }
    }

    switch (childDef.getChildType()) {
      case PRIMITIVE_DATATYPE:
        {
          IPrimitiveType<?> pd = (IPrimitiveType<?>) nextValue;
          String value = pd.getValueAsString();
          if (value != null) {
            theEventWriter.writeStartElement(childName);
            theEventWriter.writeAttribute("value", value);
            encodeExtensionsIfPresent(theResource, theEventWriter, nextValue, theIncludedResource);
            theEventWriter.writeEndElement();
          }
          break;
        }
      case RESOURCE_BLOCK:
      case COMPOSITE_DATATYPE:
        {
          theEventWriter.writeStartElement(childName);
          if (isNotBlank(theExtensionUrl)) {
            theEventWriter.writeAttribute("url", theExtensionUrl);
          }
          BaseRuntimeElementCompositeDefinition<?> childCompositeDef =
              (BaseRuntimeElementCompositeDefinition<?>) childDef;
          encodeCompositeElementToStreamWriter(
              theResource, nextValue, theEventWriter, childCompositeDef, theIncludedResource);
          theEventWriter.writeEndElement();
          break;
        }
      case RESOURCE_REF:
        {
          BaseResourceReferenceDt ref = (BaseResourceReferenceDt) nextValue;
          if (!ref.isEmpty()) {
            theEventWriter.writeStartElement(childName);
            encodeResourceReferenceToStreamWriter(theEventWriter, ref);
            theEventWriter.writeEndElement();
          }
          break;
        }
      case CONTAINED_RESOURCES:
        {
          BaseContainedDt value = (BaseContainedDt) nextValue;
          /*
           * Disable per #103 for (IResource next : value.getContainedResources()) { if (getContainedResources().getResourceId(next) != null) { continue; }
           * theEventWriter.writeStartElement("contained"); encodeResourceToXmlStreamWriter(next, theEventWriter, true, fixContainedResourceId(next.getId().getValue()));
           * theEventWriter.writeEndElement(); }
           */
          for (IBaseResource next : getContainedResources().getContainedResources()) {
            IdDt resourceId = getContainedResources().getResourceId(next);
            theEventWriter.writeStartElement("contained");
            encodeResourceToXmlStreamWriter(
                next, theEventWriter, true, fixContainedResourceId(resourceId.getValue()));
            theEventWriter.writeEndElement();
          }
          break;
        }
      case RESOURCE:
        {
          theEventWriter.writeStartElement(childName);
          IBaseResource resource = (IBaseResource) nextValue;
          encodeResourceToXmlStreamWriter(resource, theEventWriter, false);
          theEventWriter.writeEndElement();
          break;
        }
      case PRIMITIVE_XHTML:
        {
          XhtmlDt dt = (XhtmlDt) nextValue;
          if (dt.hasContent()) {
            encodeXhtml(dt, theEventWriter);
          }
          break;
        }
      case EXTENSION_DECLARED:
      case UNDECL_EXT:
        {
          throw new IllegalStateException("state should not happen: " + childDef.getName());
        }
    }
  }