Beispiel #1
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());
        }
    }
  }
Beispiel #2
0
  private void encodeXhtml(XhtmlDt theDt, XMLStreamWriter theEventWriter)
      throws XMLStreamException {
    if (theDt == null || theDt.getValue() == null || getSuppressNarratives()) {
      return;
    }

    boolean firstElement = true;
    for (XMLEvent event : theDt.getValue()) {
      switch (event.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
          Attribute attr = (Attribute) event;
          if (isBlank(attr.getName().getPrefix())) {
            if (isBlank(attr.getName().getNamespaceURI())) {
              theEventWriter.writeAttribute(attr.getName().getLocalPart(), attr.getValue());
            } else {
              theEventWriter.writeAttribute(
                  attr.getName().getNamespaceURI(), attr.getName().getLocalPart(), attr.getValue());
            }
          } else {
            theEventWriter.writeAttribute(
                attr.getName().getPrefix(),
                attr.getName().getNamespaceURI(),
                attr.getName().getLocalPart(),
                attr.getValue());
          }

          break;
        case XMLStreamConstants.CDATA:
          theEventWriter.writeCData(((Characters) event).getData());
          break;
        case XMLStreamConstants.CHARACTERS:
        case XMLStreamConstants.SPACE:
          String data = ((Characters) event).getData();
          theEventWriter.writeCharacters(data);
          break;
        case XMLStreamConstants.COMMENT:
          theEventWriter.writeComment(((Comment) event).getText());
          break;
        case XMLStreamConstants.END_ELEMENT:
          theEventWriter.writeEndElement();
          break;
        case XMLStreamConstants.ENTITY_REFERENCE:
          EntityReference er = (EntityReference) event;
          theEventWriter.writeEntityRef(er.getName());
          break;
        case XMLStreamConstants.NAMESPACE:
          Namespace ns = (Namespace) event;
          theEventWriter.writeNamespace(ns.getPrefix(), ns.getNamespaceURI());
          break;
        case XMLStreamConstants.START_ELEMENT:
          StartElement se = event.asStartElement();
          if (firstElement) {
            if (StringUtils.isBlank(se.getName().getPrefix())) {
              String namespaceURI = se.getName().getNamespaceURI();
              if (StringUtils.isBlank(namespaceURI)) {
                namespaceURI = "http://www.w3.org/1999/xhtml";
              }
              theEventWriter.writeStartElement(se.getName().getLocalPart());
              theEventWriter.writeDefaultNamespace(namespaceURI);
            } else {
              String prefix = se.getName().getPrefix();
              String namespaceURI = se.getName().getNamespaceURI();
              theEventWriter.writeStartElement(prefix, se.getName().getLocalPart(), namespaceURI);
              theEventWriter.writeNamespace(prefix, namespaceURI);
            }
            firstElement = false;
          } else {
            if (isBlank(se.getName().getPrefix())) {
              if (isBlank(se.getName().getNamespaceURI())) {
                theEventWriter.writeStartElement(se.getName().getLocalPart());
              } else {
                if (StringUtils.isBlank(se.getName().getPrefix())) {
                  theEventWriter.writeStartElement(se.getName().getLocalPart());
                  // theEventWriter.writeDefaultNamespace(se.getName().getNamespaceURI());
                } else {
                  theEventWriter.writeStartElement(
                      se.getName().getNamespaceURI(), se.getName().getLocalPart());
                }
              }
            } else {
              theEventWriter.writeStartElement(
                  se.getName().getPrefix(),
                  se.getName().getLocalPart(),
                  se.getName().getNamespaceURI());
            }
            for (Iterator<?> attrIter = se.getAttributes(); attrIter.hasNext(); ) {
              Attribute next = (Attribute) attrIter.next();
              theEventWriter.writeAttribute(next.getName().getLocalPart(), next.getValue());
            }
          }
          break;
        case XMLStreamConstants.DTD:
        case XMLStreamConstants.END_DOCUMENT:
        case XMLStreamConstants.ENTITY_DECLARATION:
        case XMLStreamConstants.NOTATION_DECLARATION:
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
        case XMLStreamConstants.START_DOCUMENT:
          break;
      }
    }
  }