Java Code Examples for javax.xml.stream.XMLStreamWriter#writeDTD()

The following examples show how to use javax.xml.stream.XMLStreamWriter#writeDTD() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlDocumentationWriter.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final ConfigurableComponent configurableComponent, final OutputStream streamToWriteTo,
        final boolean includesAdditionalDocumentation) throws IOException {

    try {
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
                streamToWriteTo, "UTF-8");
        xmlStreamWriter.writeDTD("<!DOCTYPE html>");
        xmlStreamWriter.writeStartElement("html");
        xmlStreamWriter.writeAttribute("lang", "en");
        writeHead(configurableComponent, xmlStreamWriter);
        writeBody(configurableComponent, xmlStreamWriter, includesAdditionalDocumentation);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.close();
    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new IOException("Unable to create XMLOutputStream", e);
    }
}
 
Example 2
Source File: HtmlExtensionDocWriter.java    From nifi-registry with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final ExtensionMetadata extensionMetadata, final Extension extension, final OutputStream outputStream) throws IOException {
    try {
        final XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream, "UTF-8");
        xmlStreamWriter.writeDTD("<!DOCTYPE html>");
        xmlStreamWriter.writeStartElement("html");
        xmlStreamWriter.writeAttribute("lang", "en");
        writeHead(extensionMetadata, xmlStreamWriter);
        writeBody(extensionMetadata, extension, xmlStreamWriter);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.close();
        outputStream.flush();
    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new IOException("Unable to create XMLOutputStream", e);
    }
}
 
Example 3
Source File: PseudoRdfXmlWriter.java    From owltools with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Write a pseudo RDF XML for the given ontology and gene annotations.
 * 
 * @param stream
 * @param graph the ontology
 * @param gafs (optional) list of gaf documents or null
 * @throws IOException
 */
public void write(OutputStream stream, OWLGraphWrapper graph, List<GafDocument> gafs) throws IOException {
	try {
		XMLStreamWriter writer = createWriter(stream);
		writer.writeStartDocument();
		writer.writeDTD("\n<!DOCTYPE go:go PUBLIC \"-//Gene Ontology//Custom XML/RDF Version 2.0//EN\" \""+GO_RDF_XML_DTD+"\">\n");
		
		writer.writeStartElement("go:go");
		writer.writeNamespace("go", GO_NAMESPACE_URI);
		writer.writeNamespace("rdf", RDF_NAMESPACE_URI);
		
		writer.writeStartElement(RDF_NAMESPACE_URI, "RDF");
		
		writeTerms(writer, graph, gafs);
		
		writer.writeEndElement(); // RDF
		writer.writeEndElement(); // go:go
		
		writer.writeEndDocument();
		writer.flush();
	} catch (XMLStreamException e) {
		throw new IOException(e);
	}
}
 
Example 4
Source File: HtmlDocumentationWriter.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public void write(final ConfigurableComponent configurableComponent, final OutputStream streamToWriteTo,
        final boolean includesAdditionalDocumentation) throws IOException {

    try {
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(
                streamToWriteTo, "UTF-8");
        xmlStreamWriter.writeDTD("<!DOCTYPE html>");
        xmlStreamWriter.writeStartElement("html");
        xmlStreamWriter.writeAttribute("lang", "en");
        writeHead(configurableComponent, xmlStreamWriter);
        writeBody(configurableComponent, xmlStreamWriter, includesAdditionalDocumentation);
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.close();
    } catch (XMLStreamException | FactoryConfigurationError e) {
        throw new IOException("Unable to create XMLOutputStream", e);
    }
}
 
Example 5
Source File: SectDBSynchronizer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected synchronized void initErrorFile() throws Exception {
  if (this.errorFile != null && this.errorWriter == null) {
    // first create the top-level XML file that sources the actual errors XML
    int dotIndex = this.errorFile.lastIndexOf('.');
    if (dotIndex <= 0
        || !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
      this.errorFile = this.errorFile.concat(".xml");
    }
    String errorFileName = this.errorFile.substring(0,
        this.errorFile.length() - 4);
    String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
    String errorRootFile = this.errorFile;
    this.errorFile = errorEntriesFile;
    errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
    FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
    final String encoding = "UTF-8";
    final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
    XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
        encoding);
    // write the XML header
    xmlWriter.writeStartDocument(encoding, "1.0");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
        + ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
        + new File(errorEntriesFile).getName() + "\"> ]>");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeStartElement(ERR_XML_ROOT);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndElement();
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
    xmlStream.flush();
    xmlStream.close();

    this.errorStream = new BufferedOutputStream(new FileOutputStream(
        this.errorFile));
    // disable basic output structure validation done by Woodstox since
    // this XML has multiple root elements by design
    if (xmlFactory
        .isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
      xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
          Boolean.FALSE);
    }
    this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
        encoding);
  }
}
 
Example 6
Source File: EventErrorLogger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected synchronized void initErrorFile() throws Exception {
  if (this.errorFile != null && this.errorWriter == null) {
    // first create the top-level XML file that sources the actual errors XML
    int dotIndex = this.errorFile.lastIndexOf('.');
    if (dotIndex <= 0
        || !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
      this.errorFile = this.errorFile.concat(".xml");
    }
    String errorFileName = this.errorFile.substring(0,
        this.errorFile.length() - 4);
    String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
    
    String errorRootFile = this.errorFile;
    this.errorFile = errorEntriesFile;
    errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
    
    FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
    
    final String encoding = "UTF-8";
    final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
    XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
        encoding);
    // write the XML header
    xmlWriter.writeStartDocument(encoding, "1.0");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
        + ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
        + new File(errorEntriesFile).getName() + "\"> ]>");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeStartElement(ERR_XML_ROOT);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndElement();
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
    xmlStream.flush();
    xmlStream.close();

    this.errorStream = new BufferedOutputStream(new FileOutputStream(
        this.errorFile));
    // disable basic output structure validation done by Woodstox since
    // this XML has multiple root elements by design
    if (xmlFactory
        .isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
      xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
          Boolean.FALSE);
    }
    this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
        encoding);
  }
}
 
Example 7
Source File: SectDBSynchronizer.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected synchronized void initErrorFile() throws Exception {
  if (this.errorFile != null && this.errorWriter == null) {
    // first create the top-level XML file that sources the actual errors XML
    int dotIndex = this.errorFile.lastIndexOf('.');
    if (dotIndex <= 0
        || !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
      this.errorFile = this.errorFile.concat(".xml");
    }
    String errorFileName = this.errorFile.substring(0,
        this.errorFile.length() - 4);
    String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
    String errorRootFile = this.errorFile;
    this.errorFile = errorEntriesFile;
    errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
    FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
    final String encoding = "UTF-8";
    final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
    XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
        encoding);
    // write the XML header
    xmlWriter.writeStartDocument(encoding, "1.0");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
        + ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
        + new File(errorEntriesFile).getName() + "\"> ]>");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeStartElement(ERR_XML_ROOT);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndElement();
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
    xmlStream.flush();
    xmlStream.close();

    this.errorStream = new BufferedOutputStream(new FileOutputStream(
        this.errorFile));
    // disable basic output structure validation done by Woodstox since
    // this XML has multiple root elements by design
    if (xmlFactory
        .isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
      xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
          Boolean.FALSE);
    }
    this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
        encoding);
  }
}
 
Example 8
Source File: EventErrorLogger.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
protected synchronized void initErrorFile() throws Exception {
  if (this.errorFile != null && this.errorWriter == null) {
    // first create the top-level XML file that sources the actual errors XML
    int dotIndex = this.errorFile.lastIndexOf('.');
    if (dotIndex <= 0
        || !"xml".equalsIgnoreCase(this.errorFile.substring(dotIndex + 1))) {
      this.errorFile = this.errorFile.concat(".xml");
    }
    String errorFileName = this.errorFile.substring(0,
        this.errorFile.length() - 4);
    String errorEntriesFile = errorFileName + ERROR_ENTRIES_SUFFIX + ".xml";
    
    String errorRootFile = this.errorFile;
    this.errorFile = errorEntriesFile;
    errorEntriesFile = rollFileIfRequired(errorEntriesFile, this.logger2);
    
    FileOutputStream xmlStream = new FileOutputStream(errorRootFile);
    
    final String encoding = "UTF-8";
    final XMLOutputFactory xmlFactory = XMLOutputFactory.newFactory();
    XMLStreamWriter xmlWriter = xmlFactory.createXMLStreamWriter(xmlStream,
        encoding);
    // write the XML header
    xmlWriter.writeStartDocument(encoding, "1.0");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeDTD("<!DOCTYPE staticinc [ <!ENTITY "
        + ERR_XML_ENTRIES_ENTITY + " SYSTEM \""
        + new File(errorEntriesFile).getName() + "\"> ]>");
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeStartElement(ERR_XML_ROOT);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEntityRef(ERR_XML_ENTRIES_ENTITY);
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndElement();
    xmlWriter.writeCharacters("\n");
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
    xmlStream.flush();
    xmlStream.close();

    this.errorStream = new BufferedOutputStream(new FileOutputStream(
        this.errorFile));
    // disable basic output structure validation done by Woodstox since
    // this XML has multiple root elements by design
    if (xmlFactory
        .isPropertySupported("com.ctc.wstx.outputValidateStructure")) {
      xmlFactory.setProperty("com.ctc.wstx.outputValidateStructure",
          Boolean.FALSE);
    }
    this.errorWriter = xmlFactory.createXMLStreamWriter(this.errorStream,
        encoding);
  }
}
 
Example 9
Source File: StaxUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static void writeNode(Node n, XMLStreamWriter writer, boolean repairing)
    throws XMLStreamException {

    switch (n.getNodeType()) {
    case Node.ELEMENT_NODE:
        writeElement((Element)n, writer, repairing);
        break;
    case Node.TEXT_NODE:
        writer.writeCharacters(((Text)n).getNodeValue());
        break;
    case Node.COMMENT_NODE:
        writer.writeComment(((Comment)n).getData());
        break;
    case Node.CDATA_SECTION_NODE:
        writer.writeCData(((CDATASection)n).getData());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writer.writeEntityRef(((EntityReference)n).getNodeValue());
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction pi = (ProcessingInstruction)n;
        writer.writeProcessingInstruction(pi.getTarget(), pi.getData());
        break;
    case Node.DOCUMENT_NODE:
        writeDocument((Document)n, writer, repairing);
        break;
    case Node.DOCUMENT_FRAGMENT_NODE: {
        DocumentFragment frag = (DocumentFragment)n;
        Node child = frag.getFirstChild();
        while (child != null) {
            writeNode(child, writer, repairing);
            child = child.getNextSibling();
        }
        break;
    }
    case Node.DOCUMENT_TYPE_NODE:
        try {
            if (((DocumentType)n).getTextContent() != null) {
                writer.writeDTD(((DocumentType)n).getTextContent());
            }
        } catch (UnsupportedOperationException ex) {
            //can we ignore?  DOM writers really don't allow this
            //as there isn't a way to write a DTD in dom
        }
        break;
    default:
        throw new IllegalStateException("Found type: " + n.getClass().getName());
    }
}
 
Example 10
Source File: StaxUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static void writeEvent(XMLEvent event, XMLStreamWriter writer)
    throws XMLStreamException {

    switch (event.getEventType()) {
    case XMLStreamConstants.START_ELEMENT:
        writeStartElementEvent(event, writer);
        break;
    case XMLStreamConstants.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLStreamConstants.ATTRIBUTE:
        writeAttributeEvent(event, writer);
        break;
    case XMLStreamConstants.ENTITY_REFERENCE:
        writer.writeEntityRef(((javax.xml.stream.events.EntityReference)event).getName());
        break;
    case XMLStreamConstants.DTD:
        writer.writeDTD(((DTD)event).getDocumentTypeDeclaration());
        break;
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        if (((javax.xml.stream.events.ProcessingInstruction)event).getData() != null) {
            writer.writeProcessingInstruction(
                ((javax.xml.stream.events.ProcessingInstruction)event).getTarget(),
                ((javax.xml.stream.events.ProcessingInstruction)event).getData());
        } else {
            writer.writeProcessingInstruction(
                ((javax.xml.stream.events.ProcessingInstruction)event).getTarget());
        }
        break;
    case XMLStreamConstants.NAMESPACE:
        if (((Namespace)event).isDefaultNamespaceDeclaration()) {
            writer.writeDefaultNamespace(((Namespace)event).getNamespaceURI());
            writer.setDefaultNamespace(((Namespace)event).getNamespaceURI());
        } else {
            writer.writeNamespace(((Namespace)event).getPrefix(),
                                  ((Namespace)event).getNamespaceURI());
            writer.setPrefix(((Namespace)event).getPrefix(),
                             ((Namespace)event).getNamespaceURI());
        }
        break;
    case XMLStreamConstants.COMMENT:
        writer.writeComment(((javax.xml.stream.events.Comment)event).getText());
        break;
    case XMLStreamConstants.CHARACTERS:
    case XMLStreamConstants.SPACE:
        writer.writeCharacters(event.asCharacters().getData());
        break;
    case XMLStreamConstants.CDATA:
        writer.writeCData(event.asCharacters().getData());
        break;
    case XMLStreamConstants.START_DOCUMENT:
        if (((StartDocument)event).encodingSet()) {
            writer.writeStartDocument(((StartDocument)event).getCharacterEncodingScheme(),
                                      ((StartDocument)event).getVersion());

        } else {
            writer.writeStartDocument(((StartDocument)event).getVersion());
        }
        break;
    case XMLStreamConstants.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    default:
        //shouldn't get here
    }
}
 
Example 11
Source File: RestServicesList.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@GET
@Produces({MediaType.TEXT_HTML})
public byte[] listHTML() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        XMLStreamWriter xsw = factory.createXMLStreamWriter(output, "UTF-8");
        xsw.writeStartDocument("UTF-8", "1.0");
        xsw.writeDTD("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
                     + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
        xsw.writeCharacters("\n");
        xsw.writeStartElement("html");
        xsw.writeDefaultNamespace("http://www.w3.org/1999/xhtml");
        xsw.writeStartElement("head");
        xsw.writeStartElement("title");
        xsw.writeCharacters("eXo JAXRS Implementation");
        xsw.writeEndElement(); // </title>
        xsw.writeEndElement(); // </head>
        xsw.writeStartElement("body");
        //
        xsw.writeStartElement("h3");
        xsw.writeAttribute("style", "text-align:center;");
        xsw.writeCharacters("Root resources");
        xsw.writeEndElement();
        // table
        xsw.writeStartElement("table");
        xsw.writeAttribute("width", "90%");
        xsw.writeAttribute("style", "table-layout:fixed;");
        // table header
        xsw.writeStartElement("tr");
        xsw.writeStartElement("th");
        xsw.writeCharacters("Path");
        xsw.writeEndElement(); // </th>
        xsw.writeStartElement("th");
        xsw.writeCharacters("Regex");
        xsw.writeEndElement(); // </th>
        xsw.writeStartElement("th");
        xsw.writeCharacters("FQN");
        xsw.writeEndElement(); // </th>
        xsw.writeEndElement(); // </tr>
        // end table header
        for (RootResource r : rootResources().getRootResources()) {
            xsw.writeStartElement("tr");
            xsw.writeStartElement("td");
            xsw.writeCharacters(r.getPath());
            xsw.writeEndElement(); // </td>
            xsw.writeStartElement("td");
            xsw.writeCharacters(r.getRegex());
            xsw.writeEndElement(); // </td>
            xsw.writeStartElement("td");
            xsw.writeCharacters(r.getFqn());
            xsw.writeEndElement(); // </td>
            xsw.writeEndElement(); // </tr>
        }
        xsw.writeEndElement(); // </table>
        xsw.writeEndElement(); // </body>
        xsw.writeEndDocument();
    } catch (XMLStreamException xmle) {
        throw new WebApplicationException(xmle, //
                                          Response.status(500) //
                                                  .entity("Unable write to output stream. " + xmle.getMessage()) //
                                                  .type(MediaType.TEXT_PLAIN) //
                                                  .build());
    }
    return output.toByteArray();
}