예제 #1
0
  @Override
  public void encodeTagListToWriter(TagList theTagList, Writer theWriter) throws IOException {
    try {
      XMLStreamWriter eventWriter = createXmlWriter(theWriter);

      eventWriter.writeStartElement(TagList.ELEMENT_NAME_LC);
      eventWriter.writeDefaultNamespace(FHIR_NS);

      for (Tag next : theTagList) {
        eventWriter.writeStartElement(TagList.ATTR_CATEGORY);

        if (isNotBlank(next.getTerm())) {
          eventWriter.writeAttribute(Tag.ATTR_TERM, next.getTerm());
        }
        if (isNotBlank(next.getLabel())) {
          eventWriter.writeAttribute(Tag.ATTR_LABEL, next.getLabel());
        }
        if (isNotBlank(next.getScheme())) {
          eventWriter.writeAttribute(Tag.ATTR_SCHEME, next.getScheme());
        }

        eventWriter.writeEndElement();
      }

      eventWriter.writeEndElement();
      eventWriter.close();
    } catch (XMLStreamException e) {
      throw new ConfigurationException("Failed to initialize STaX event factory", e);
    }
  }
예제 #2
0
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    try {
      String fileName = null;
      try {
        if (args[0].equals("-f")) {
          fileName = args[1];
        } else {
          printUsage();
          return;
        }
      } catch (Exception ex) {
        printUsage();
        return;
      }

      XMLOutputFactory xof = XMLOutputFactory.newInstance();
      XMLStreamWriter xtw = null;
      xtw = xof.createXMLStreamWriter(new FileOutputStream(fileName), "utf-8");
      xtw.writeStartDocument("utf-8", "1.0");
      xtw.writeComment("StAX Sample: writer.HelloWorld");
      xtw.writeStartElement("hello");
      xtw.writeDefaultNamespace("http://samples");
      xtw.writeCharacters("this crazy");
      xtw.writeEmptyElement("world");
      xtw.writeEndElement();
      xtw.writeEndDocument();
      xtw.flush();
      xtw.close();
    } catch (Exception ex) {
      ex.printStackTrace();
      System.err.println("Exception occurred while running Hello World samples");
    }
    System.out.println("Done");
  }
  public void append(
      final XMLStreamWriter writer,
      final EntityInfoAggregator eia,
      final Map<String, Object> data,
      final boolean isRootElement,
      final boolean isFeedPart)
      throws EntityProviderException {
    try {
      writer.writeStartElement(FormatXml.ATOM_ENTRY);

      if (isRootElement) {
        writer.writeDefaultNamespace(Edm.NAMESPACE_ATOM_2005);
        writer.writeNamespace(Edm.PREFIX_M, Edm.NAMESPACE_M_2007_08);
        writer.writeNamespace(Edm.PREFIX_D, Edm.NAMESPACE_D_2007_08);
      }
      if (!isFeedPart) {
        writer.writeAttribute(
            Edm.PREFIX_XML,
            Edm.NAMESPACE_XML_1998,
            "base",
            properties.getServiceRoot().toASCIIString());
      }

      etag = createETag(eia, data);
      if (etag != null) {
        writer.writeAttribute(Edm.NAMESPACE_M_2007_08, FormatXml.M_ETAG, etag);
      }

      // write all atom infos (mandatory and optional)
      appendAtomMandatoryParts(writer, eia, data);
      appendAtomOptionalParts(writer, eia, data);

      if (eia.getEntityType().hasStream()) {
        // write all links
        appendAtomEditLink(writer, eia, data);
        appendAtomContentLink(writer, eia, data, properties.getMediaResourceMimeType());
        appendAtomNavigationLinks(writer, eia, data);
        // write properties/content
        appendCustomProperties(writer, eia, data);
        appendAtomContentPart(writer, eia, data, properties.getMediaResourceMimeType());
        appendProperties(writer, eia, data);
      } else {
        // write all links
        appendAtomEditLink(writer, eia, data);
        appendAtomNavigationLinks(writer, eia, data);
        // write properties/content
        appendCustomProperties(writer, eia, data);
        writer.writeStartElement(FormatXml.ATOM_CONTENT);
        writer.writeAttribute(FormatXml.ATOM_TYPE, ContentType.APPLICATION_XML.toString());
        appendProperties(writer, eia, data);
        writer.writeEndElement();
      }

      writer.writeEndElement();

      writer.flush();
    } catch (Exception e) {
      throw new EntityProviderException(EntityProviderException.COMMON, e);
    }
  }
  private void appendSchema(final XMLStreamWriter writer, final EdmSchema schema)
      throws XMLStreamException {
    writer.writeStartElement(NS_EDM, SCHEMA);
    writer.writeDefaultNamespace(NS_EDM);
    writer.writeAttribute(XML_NAMESPACE, schema.getNamespace());
    if (schema.getAlias() != null) {
      writer.writeAttribute(XML_ALIAS, schema.getAlias());
      namespaceToAlias.put(schema.getNamespace(), schema.getAlias());
    }

    // EnumTypes
    appendEnumTypes(writer, schema.getEnumTypes());

    // TypeDefinitions
    appendTypeDefinitions(writer, schema.getTypeDefinitions());

    // EntityTypes
    appendEntityTypes(writer, schema.getEntityTypes());

    // ComplexTypes
    appendComplexTypes(writer, schema.getComplexTypes());

    // Actions
    appendActions(writer, schema.getActions());

    // Functions
    appendFunctions(writer, schema.getFunctions());

    // EntityContainer
    appendEntityContainer(writer, schema.getEntityContainer());

    writer.writeEndElement();
  }
 private void writeBlueprint() throws XMLStreamException {
   writer.writeStartElement("blueprint");
   writer.writeDefaultNamespace(NS_BLUEPRINT);
   writer.writeNamespace("ext", NS_EXT);
   writer.writeNamespace("jpa", NS_JPA);
   writer.writeNamespace("tx", NS_TX);
   writer.writeCharacters("\n");
 }
예제 #6
0
 private static String dto2ddi(DatasetDTO datasetDto) throws XMLStreamException {
   OutputStream outputStream = new ByteArrayOutputStream();
   XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
   xmlw.writeStartElement("codeBook");
   xmlw.writeDefaultNamespace("http://www.icpsr.umich.edu/DDI");
   writeAttribute(xmlw, "version", "2.0");
   createStdyDscr(xmlw, datasetDto);
   createdataDscr(xmlw, datasetDto.getDatasetVersion().getFiles());
   xmlw.writeEndElement(); // codeBook
   xmlw.flush();
   String xml = outputStream.toString();
   return XmlPrinter.prettyPrintXml(xml);
 }
예제 #7
0
 public void writeTo(XMLStreamWriter w) throws XMLStreamException {
   w.writeStartElement("", getLocalPart(), getNamespaceURI());
   w.writeDefaultNamespace(getNamespaceURI());
   w.writeStartElement(actionLocalName);
   w.writeCharacters(action);
   w.writeEndElement();
   if (soapAction != null) {
     w.writeStartElement(soapActionLocalName);
     w.writeCharacters(soapAction);
     w.writeEndElement();
   }
   w.writeEndElement();
 }
예제 #8
0
 public void write(OutputStream out) throws IOException {
   try {
     XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
     outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
     XMLStreamWriter xsw = outputFactory.createXMLStreamWriter(out);
     xsw.writeStartDocument();
     xsw.writeStartElement("membership");
     xsw.writeDefaultNamespace(XMLContants.EXO_NAMESPACE_URL);
     xsw.writeNamespace(XMLContants.XLINK_PREFIX, XMLContants.XLINK_NAMESPACE_URL);
     xsw.writeAttribute(
         XMLContants.XLINK_NAMESPACE_URL,
         XMLContants.XLINK_HREF,
         baseURI_
             + "/organization/membership/"
             + membership_.getId()
             + "/?output=xml&command=view");
     xsw.writeStartElement("id");
     xsw.writeCharacters(membership_.getId());
     xsw.writeEndElement();
     xsw.writeStartElement("group-id");
     xsw.writeCharacters(membership_.getGroupId());
     xsw.writeEndElement();
     xsw.writeStartElement("user-name");
     xsw.writeCharacters(membership_.getUserName());
     xsw.writeEndElement();
     xsw.writeStartElement("membership-type");
     xsw.writeCharacters(membership_.getMembershipType());
     xsw.writeEndElement();
     xsw.writeEndDocument();
     xsw.flush();
     xsw.close();
   } catch (Exception e) {
     e.printStackTrace();
     throw new IOException(e.getMessage());
   }
 }
예제 #9
0
  private void encodeResourceToXmlStreamWriter(
      IBaseResource theResource,
      XMLStreamWriter theEventWriter,
      boolean theContainedResource,
      String theResourceId)
      throws XMLStreamException {
    if (!theContainedResource) {
      super.containResourcesForEncoding(theResource);
    }

    RuntimeResourceDefinition resDef = myContext.getResourceDefinition(theResource);
    if (resDef == null) {
      throw new ConfigurationException("Unknown resource type: " + theResource.getClass());
    }

    theEventWriter.writeStartElement(resDef.getName());
    theEventWriter.writeDefaultNamespace(FHIR_NS);

    if (theResource instanceof IAnyResource) {

      // HL7.org Structures
      encodeCompositeElementToStreamWriter(
          theResource, theResource, theEventWriter, resDef, theContainedResource);

    } else {

      if (myContext.getVersion().getVersion().isNewerThan(FhirVersionEnum.DSTU1)) {

        // DSTU2+

        IResource resource = (IResource) theResource;
        writeOptionalTagWithValue(theEventWriter, "id", theResourceId);

        InstantDt updated =
            (InstantDt) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
        IdDt resourceId = resource.getId();
        String versionIdPart = resourceId.getVersionIdPart();
        if (isBlank(versionIdPart)) {
          versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
        }
        List<BaseCodingDt> securityLabels =
            extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.SECURITY_LABELS);
        List<IdDt> profiles =
            extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES);
        TagList tags = ResourceMetadataKeyEnum.TAG_LIST.get(resource);
        if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, profiles) == false) {
          theEventWriter.writeStartElement("meta");
          writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart);
          if (updated != null) {
            writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
          }

          for (IdDt profile : profiles) {
            theEventWriter.writeStartElement("profile");
            theEventWriter.writeAttribute("value", profile.getValue());
            theEventWriter.writeEndElement();
          }
          for (BaseCodingDt securityLabel : securityLabels) {
            theEventWriter.writeStartElement("security");
            BaseRuntimeElementCompositeDefinition<?> def =
                (BaseRuntimeElementCompositeDefinition<?>)
                    myContext.getElementDefinition(securityLabel.getClass());
            encodeCompositeElementChildrenToStreamWriter(
                resource, securityLabel, theEventWriter, def.getChildren(), theContainedResource);
            theEventWriter.writeEndElement();
          }
          if (tags != null) {
            for (Tag tag : tags) {
              theEventWriter.writeStartElement("tag");
              writeOptionalTagWithValue(theEventWriter, "system", tag.getScheme());
              writeOptionalTagWithValue(theEventWriter, "code", tag.getTerm());
              writeOptionalTagWithValue(theEventWriter, "display", tag.getLabel());
              theEventWriter.writeEndElement();
            }
          }
          theEventWriter.writeEndElement();
        }

        if (theResource instanceof IBaseBinary) {
          IBaseBinary bin = (IBaseBinary) theResource;
          writeOptionalTagWithValue(theEventWriter, "contentType", bin.getContentType());
          writeOptionalTagWithValue(theEventWriter, "content", bin.getContentAsBase64());
        } else {
          encodeResourceToStreamWriterInDstu2Format(
              resDef, theResource, theResource, theEventWriter, resDef, theContainedResource);
        }

      } else {

        // DSTU1
        if (theResourceId != null && theContainedResource) {
          theEventWriter.writeAttribute("id", theResourceId);
        }

        if (theResource instanceof IBaseBinary) {
          IBaseBinary bin = (IBaseBinary) theResource;
          if (bin.getContentType() != null) {
            theEventWriter.writeAttribute("contentType", bin.getContentType());
          }
          theEventWriter.writeCharacters(bin.getContentAsBase64());
        } else {
          encodeCompositeElementToStreamWriter(
              theResource, theResource, theEventWriter, resDef, theContainedResource);
        }
      }
    }

    theEventWriter.writeEndElement();
  }
예제 #10
0
  private void encodeBundleToWriterDstu2(Bundle theBundle, XMLStreamWriter theEventWriter)
      throws XMLStreamException {
    theEventWriter.writeStartElement("Bundle");
    theEventWriter.writeDefaultNamespace(FHIR_NS);

    writeOptionalTagWithValue(theEventWriter, "id", theBundle.getId().getIdPart());

    InstantDt updated =
        (InstantDt) theBundle.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED);
    IdDt bundleId = theBundle.getId();
    if (bundleId != null && isNotBlank(bundleId.getVersionIdPart())
        || (updated != null && !updated.isEmpty())) {
      theEventWriter.writeStartElement("meta");
      writeOptionalTagWithValue(theEventWriter, "versionId", bundleId.getVersionIdPart());
      if (updated != null) {
        writeOptionalTagWithValue(theEventWriter, "lastUpdated", updated.getValueAsString());
      }
      theEventWriter.writeEndElement();
    }

    String bundleBaseUrl = theBundle.getLinkBase().getValue();

    writeOptionalTagWithValue(theEventWriter, "type", theBundle.getType().getValue());
    writeOptionalTagWithValue(theEventWriter, "base", bundleBaseUrl);
    writeOptionalTagWithValue(
        theEventWriter, "total", theBundle.getTotalResults().getValueAsString());

    writeBundleResourceLink(theEventWriter, "first", theBundle.getLinkFirst());
    writeBundleResourceLink(theEventWriter, "previous", theBundle.getLinkPrevious());
    writeBundleResourceLink(theEventWriter, "next", theBundle.getLinkNext());
    writeBundleResourceLink(theEventWriter, "last", theBundle.getLinkLast());
    writeBundleResourceLink(theEventWriter, "self", theBundle.getLinkSelf());

    for (BundleEntry nextEntry : theBundle.getEntries()) {
      theEventWriter.writeStartElement("entry");

      boolean deleted = false;
      if (nextEntry.getDeletedAt() != null && nextEntry.getDeletedAt().isEmpty() == false) {
        deleted = true;
      }

      writeOptionalTagWithValue(
          theEventWriter, "base", determineResourceBaseUrl(bundleBaseUrl, nextEntry));

      IResource resource = nextEntry.getResource();
      if (resource != null && !resource.isEmpty() && !deleted) {
        theEventWriter.writeStartElement("resource");
        encodeResourceToXmlStreamWriter(resource, theEventWriter, false);
        theEventWriter.writeEndElement(); // content
      } else {
        ourLog.debug("Bundle entry contains null resource");
      }

      if (nextEntry.getSearchMode().isEmpty() == false || nextEntry.getScore().isEmpty() == false) {
        theEventWriter.writeStartElement("search");
        writeOptionalTagWithValue(
            theEventWriter, "mode", nextEntry.getSearchMode().getValueAsString());
        writeOptionalTagWithValue(theEventWriter, "score", nextEntry.getScore().getValueAsString());
        theEventWriter.writeEndElement();
        // IResource nextResource = nextEntry.getResource();
      }

      if (nextEntry.getTransactionMethod().isEmpty() == false
          || nextEntry.getLinkSearch().isEmpty() == false) {
        theEventWriter.writeStartElement("transaction");
        writeOptionalTagWithValue(
            theEventWriter, "method", nextEntry.getTransactionMethod().getValue());
        writeOptionalTagWithValue(theEventWriter, "url", nextEntry.getLinkSearch().getValue());
        theEventWriter.writeEndElement();
      }

      if (deleted) {
        theEventWriter.writeStartElement("deleted");
        writeOptionalTagWithValue(theEventWriter, "type", nextEntry.getId().getResourceType());
        writeOptionalTagWithValue(theEventWriter, "id", nextEntry.getId().getIdPart());
        writeOptionalTagWithValue(
            theEventWriter, "versionId", nextEntry.getId().getVersionIdPart());
        writeOptionalTagWithValue(
            theEventWriter, "instant", nextEntry.getDeletedAt().getValueAsString());
        theEventWriter.writeEndElement();
      }

      theEventWriter.writeEndElement(); // entry
    }

    theEventWriter.writeEndElement();
    theEventWriter.close();
  }
예제 #11
0
  private void encodeBundleToWriterDstu1(Bundle theBundle, XMLStreamWriter eventWriter)
      throws XMLStreamException {
    eventWriter.writeStartElement("feed");
    eventWriter.writeDefaultNamespace(ATOM_NS);

    writeTagWithTextNode(eventWriter, "title", theBundle.getTitle());
    writeTagWithTextNode(eventWriter, "id", theBundle.getBundleId());

    writeAtomLink(eventWriter, Constants.LINK_SELF, theBundle.getLinkSelf());
    writeAtomLink(eventWriter, Constants.LINK_FIRST, theBundle.getLinkFirst());
    writeAtomLink(eventWriter, Constants.LINK_PREVIOUS, theBundle.getLinkPrevious());
    writeAtomLink(eventWriter, Constants.LINK_NEXT, theBundle.getLinkNext());
    writeAtomLink(eventWriter, Constants.LINK_LAST, theBundle.getLinkLast());
    writeAtomLink(eventWriter, Constants.LINK_FHIR_BASE, theBundle.getLinkBase());

    if (theBundle.getTotalResults().getValue() != null) {
      eventWriter.writeStartElement("os", "totalResults", OPENSEARCH_NS);
      eventWriter.writeNamespace("os", OPENSEARCH_NS);
      eventWriter.writeCharacters(theBundle.getTotalResults().getValue().toString());
      eventWriter.writeEndElement();
    }

    writeOptionalTagWithTextNode(eventWriter, "updated", theBundle.getUpdated());

    if (StringUtils.isNotBlank(theBundle.getAuthorName().getValue())) {
      eventWriter.writeStartElement("author");
      writeTagWithTextNode(eventWriter, "name", theBundle.getAuthorName());
      writeOptionalTagWithTextNode(eventWriter, "uri", theBundle.getAuthorUri());
      eventWriter.writeEndElement();
    }

    writeCategories(eventWriter, theBundle.getCategories());

    for (BundleEntry nextEntry : theBundle.getEntries()) {
      boolean deleted = false;
      if (nextEntry.getDeletedAt() != null && nextEntry.getDeletedAt().isEmpty() == false) {
        deleted = true;
        eventWriter.writeStartElement("at", "deleted-entry", TOMBSTONES_NS);
        eventWriter.writeNamespace("at", TOMBSTONES_NS);

        if (nextEntry.getDeletedResourceId().isEmpty()) {
          writeOptionalAttribute(eventWriter, "ref", nextEntry.getId().getValueAsString());
        } else {
          writeOptionalAttribute(
              eventWriter, "ref", nextEntry.getDeletedResourceId().getValueAsString());
        }

        writeOptionalAttribute(eventWriter, "when", nextEntry.getDeletedAt().getValueAsString());
        if (nextEntry.getDeletedByEmail().isEmpty() == false
            || nextEntry.getDeletedByName().isEmpty() == false) {
          eventWriter.writeStartElement(TOMBSTONES_NS, "by");
          if (nextEntry.getDeletedByName().isEmpty() == false) {
            eventWriter.writeStartElement(TOMBSTONES_NS, "name");
            eventWriter.writeCharacters(nextEntry.getDeletedByName().getValue());
            eventWriter.writeEndElement();
          }
          if (nextEntry.getDeletedByEmail().isEmpty() == false) {
            eventWriter.writeStartElement(TOMBSTONES_NS, "email");
            eventWriter.writeCharacters(nextEntry.getDeletedByEmail().getValue());
            eventWriter.writeEndElement();
          }
          eventWriter.writeEndElement();
        }
        if (nextEntry.getDeletedComment().isEmpty() == false) {
          eventWriter.writeStartElement(TOMBSTONES_NS, "comment");
          eventWriter.writeCharacters(nextEntry.getDeletedComment().getValue());
          eventWriter.writeEndElement();
        }
      } else {
        eventWriter.writeStartElement("entry");
      }

      writeOptionalTagWithTextNode(eventWriter, "title", nextEntry.getTitle());
      if (!deleted) {
        if (nextEntry.getId().isEmpty() == false) {
          writeTagWithTextNode(eventWriter, "id", nextEntry.getId());
        } else {
          writeTagWithTextNode(eventWriter, "id", nextEntry.getResource().getId());
        }
      }
      writeOptionalTagWithTextNode(eventWriter, "updated", nextEntry.getUpdated());
      writeOptionalTagWithTextNode(eventWriter, "published", nextEntry.getPublished());

      writeCategories(eventWriter, nextEntry.getCategories());

      if (!nextEntry.getLinkSelf().isEmpty()) {
        writeAtomLink(eventWriter, "self", nextEntry.getLinkSelf());
      }

      if (!nextEntry.getLinkAlternate().isEmpty()) {
        writeAtomLink(eventWriter, "alternate", nextEntry.getLinkAlternate());
      }

      if (!nextEntry.getLinkSearch().isEmpty()) {
        writeAtomLink(eventWriter, "search", nextEntry.getLinkSearch());
      }

      IResource resource = nextEntry.getResource();
      if (resource != null && !resource.isEmpty() && !deleted) {
        eventWriter.writeStartElement("content");
        eventWriter.writeAttribute("type", "text/xml");
        encodeResourceToXmlStreamWriter(resource, eventWriter, false);
        eventWriter.writeEndElement(); // content
      } else {
        ourLog.debug("Bundle entry contains null resource");
      }

      if (!nextEntry.getSummary().isEmpty()) {
        eventWriter.writeStartElement("summary");
        eventWriter.writeAttribute("type", "xhtml");
        encodeXhtml(nextEntry.getSummary(), eventWriter);
        eventWriter.writeEndElement();
      }

      eventWriter.writeEndElement(); // entry
    }

    eventWriter.writeEndElement();
    eventWriter.close();
  }
예제 #12
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;
      }
    }
  }
 public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
   writer.writeDefaultNamespace(namespaceURI);
 }
 public void writeDefaultNamespace(java.lang.String namespaceURI) throws XMLStreamException {
   baseWriter.writeDefaultNamespace(namespaceURI);
 }
예제 #15
0
  public static void convert(XMLStreamWriter ch, Map<String, String> toc, String id, Reader in)
      throws XMLStreamException, IOException {
    ch.writeStartDocument("utf-8", "1.0");

    ch.writeStartElement("rdf:RDF");
    ch.writeDefaultNamespace(Data.PREFIX);
    // Base "" more generic
    // ch.writeAttribute("xml:base", "http://estatwrap.ontologycentral.com/");
    //		ch.writeAttribute("xml:base", "");
    ch.writeNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    ch.writeNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
    ch.writeNamespace("foaf", "http://xmlns.com/foaf/0.1/");
    ch.writeNamespace("qb", "http://purl.org/linked-data/cube#");
    ch.writeNamespace("sdmx-measure", "http://purl.org/linked-data/sdmx/2009/measure#");
    ch.writeNamespace("dcterms", "http://purl.org/dc/terms/");

    ch.writeStartElement("rdf:Description");
    ch.writeAttribute("rdf:about", "");

    ch.writeStartElement("rdfs:comment");
    ch.writeCharacters("No guarantee of correctness! USE AT YOUR OWN RISK!");
    ch.writeEndElement();

    ch.writeStartElement("dcterms:publisher");
    ch.writeCharacters(
        "Eurostat (http://epp.eurostat.ec.europa.eu/) via Linked Eurostat (http://estatwrap.ontologycentral.com/)");
    ch.writeEndElement();

    ch.writeStartElement("rdfs:seeAlso");
    ch.writeAttribute("rdf:resource", "http://estatwrap.ontologycentral.com/table_of_contents.rdf");
    ch.writeEndElement();

    ch.writeStartElement("foaf:maker");
    ch.writeAttribute(
        "rdf:resource", "http://cbasewrap.ontologycentral.com/company/ontologycentral#id");
    ch.writeEndElement();

    ch.writeStartElement("foaf:topic");
    ch.writeAttribute("rdf:resource", "#ds");
    ch.writeEndElement();

    Calendar cal = Calendar.getInstance();
    ch.writeStartElement("dcterms:date");
    ch.writeCharacters(Listener.ISO8601.format(cal.getTime()));
    ch.writeEndElement();
    ch.writeEndElement();

    ch.writeStartElement("qb:DataSet");
    // ch.writeAttribute("rdf:about", "/id/" + id  + "#ds");
    ch.writeAttribute("rdf:about", "/id/" + id + "#ds");
    if (toc.containsKey(id)) {
      ch.writeStartElement("rdfs:label");
      ch.writeAttribute("xml:lang", "en");
      ch.writeCharacters(toc.get(id));
      ch.writeEndElement();
    }
    ch.writeStartElement("rdfs:comment");
    ch.writeCharacters(
        "Source: Eurostat (http://epp.eurostat.ec.europa.eu/) via Linked Eurostat (http://estatwrap.ontologycentral.com/).");
    ch.writeEndElement();
    ch.writeStartElement("rdfs:seeAlso");
    ch.writeAttribute(
        "rdf:resource",
        "http://epp.eurostat.ec.europa.eu/portal/page/portal/about_eurostat/corporate/copyright_licence_policy");
    ch.writeEndElement();
    ch.writeStartElement("rdfs:seeAlso");
    ch.writeAttribute("rdf:resource", "http://ontologycentral.com/2009/01/eurostat/");
    ch.writeEndElement();
    ch.writeStartElement("foaf:page");
    ch.writeAttribute("rdf:resource", "");
    ch.writeEndElement();

    ch.writeStartElement("qb:structure");
    ch.writeAttribute("rdf:resource", "/dsd/" + id + "#dsd");
    ch.writeEndElement();

    ch.writeEndElement();

    Data d = new Data(in);

    d.convert(ch, id);

    ch.writeEndElement();
    ch.writeEndDocument();
  }