Java Code Examples for javax.xml.stream.XMLOutputFactory#setProperty()

The following examples show how to use javax.xml.stream.XMLOutputFactory#setProperty() . 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: CfgApi.java    From aion with MIT License 8 votes vote down vote up
public String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    output.setProperty("escapeCharacters", false);
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("api");

        xmlWriter.writeCharacters(this.rpc.toXML());
        xmlWriter.writeCharacters(this.zmq.toXML());
        xmlWriter.writeCharacters(this.nrg.toXML());

        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeEndElement();
        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 2
Source File: DuplicateNSDeclarationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testDuplicateNSDeclaration() {

    // expect only 1 Namespace Declaration
    final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<ns1:foo" + " xmlns:ns1=\"http://example.com/\">" + "</ns1:foo>";

    // have XMLOutputFactory repair Namespaces
    XMLOutputFactory ofac = XMLOutputFactory.newInstance();
    ofac.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true));

    // send output to a Stream
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    StreamResult sr = new StreamResult(buffer);
    XMLStreamWriter w = null;

    // write a duplicate Namespace Declaration
    try {
        w = ofac.createXMLStreamWriter(sr);
        w.writeStartDocument();
        w.writeStartElement("ns1", "foo", "http://example.com/");
        w.writeNamespace("ns1", "http://example.com/");
        w.writeNamespace("ns1", "http://example.com/");
        w.writeEndElement();
        w.writeEndDocument();
        w.close();
    } catch (XMLStreamException xmlStreamException) {
        xmlStreamException.printStackTrace();
        Assert.fail(xmlStreamException.toString());
    }

    // debugging output for humans
    System.out.println();
    System.out.println("actual:   \"" + buffer.toString() + "\"");
    System.out.println("expected: \"" + EXPECTED_OUTPUT + "\"");

    // are results as expected?
    Assert.assertEquals(EXPECTED_OUTPUT, buffer.toString());
}
 
Example 3
Source File: UnprefixedNameTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testRepairingPrefix() throws Exception {

    try {

        // repair namespaces
        // use new XMLOutputFactory as changing its property settings
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        xof.setProperty(xof.IS_REPAIRING_NAMESPACES, new Boolean(true));
        XMLStreamWriter w = xof.createXMLStreamWriter(System.out);

        // here I'm trying to write
        // <bar xmlns="foo" />
        w.writeStartDocument();
        w.writeStartElement("foo", "bar");
        w.writeDefaultNamespace("foo");
        w.writeCharacters("---");
        w.writeEndElement();
        w.writeEndDocument();
        w.close();

        // Expected success
        System.out.println("Expected success.");
    } catch (Exception exception) {
        // Unexpected Exception
        String FAIL_MSG = "Unexpected Exception: " + exception.toString();
        System.err.println(FAIL_MSG);
        Assert.fail(FAIL_MSG);
    }
}
 
Example 4
Source File: NullUriDetectionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test1() throws Exception {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    xof.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);

    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xof.createXMLStreamWriter(sw);
    w.writeStartDocument();
    w.writeStartElement("foo", "bar", "zot");
    w.writeDefaultNamespace(null);
    w.writeCharacters("---");
}
 
Example 5
Source File: CfgApiZmq.java    From aion with MIT License 5 votes vote down vote up
String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    output.setProperty("escapeCharacters", false);
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        // <rpc active="false" ip="127.0.0.1" port="8545"/>

        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("java");

        xmlWriter.writeAttribute("active", this.active ? "true" : "false");
        xmlWriter.writeAttribute("ip", this.ip);
        xmlWriter.writeAttribute("port", this.port + "");

        xmlWriter.writeCharacters("\r\n\t\t\t");
        xmlWriter.writeStartElement("secure-connect");
        xmlWriter.writeCharacters(String.valueOf(this.secureConnectEnabled));
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeEndElement();
        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 6
Source File: StaxHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static void trySetProperty(XMLOutputFactory factory, String feature, boolean flag) {
    try {
        factory.setProperty(feature, flag);
    } catch (Exception e) {
        logger.log(POILogger.WARN, "StAX Property unsupported", feature, e);
    } catch (AbstractMethodError ame) {
        logger.log(POILogger.WARN, "Cannot set StAX property because outdated StAX parser in classpath", feature, ame);
    }
}
 
Example 7
Source File: JAXP15RegTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 8
Source File: JAXP15RegTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 9
Source File: JAXP15RegTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 10
Source File: JAXP15RegTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 11
Source File: JAXP15RegTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 12
Source File: JAXP15RegTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 13
Source File: JAXP15RegTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 14
Source File: JAXP15RegTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 15
Source File: JAXP15RegTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 16
Source File: CfgConsensusUnity.java    From aion with MIT License 4 votes vote down vote up
String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    output.setProperty("escapeCharacters", false);
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("consensus");

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("enable/disable the internal PoW block miner");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("mining");
        xmlWriter.writeCharacters(this.getMining() + "");
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("miner's desired coinbase (no 0x prefix) required by the internal PoW block miner, the block reward will been sent to this address");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("miner-address");
        xmlWriter.writeCharacters(this.getMinerAddress());
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("cpu threads been use in the internal PoW miner");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("cpu-mine-threads");
        xmlWriter.writeCharacters(this.getCpuMineThreads() + "");
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("extra-data");
        xmlWriter.writeCharacters(this.getExtraData());
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("nrg-strategy");
        xmlWriter.writeCharacters(this.cfgEnergyStrategy.toXML());
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeEndElement();
        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 17
Source File: JAXP15RegTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
 
Example 18
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 19
Source File: CfgApiRpc.java    From aion with MIT License 4 votes vote down vote up
String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    output.setProperty("escapeCharacters", false);
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        // <rpc active="false" ip="127.0.0.1" port="8545"/>

        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "rpc config docs: https://github.com/aionnetwork/aion/wiki/JSON-RPC-API-Docs");

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("rpc");

        xmlWriter.writeAttribute("active", this.active ? "true" : "false");
        xmlWriter.writeAttribute("ip", this.ip);
        xmlWriter.writeAttribute("port", this.port + "");

        xmlWriter.writeCharacters("\r\n\t\t\t");
        xmlWriter.writeStartElement("cors-enabled");
        xmlWriter.writeCharacters(String.valueOf(this.isCorsEnabled()));
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t\t");
        xmlWriter.writeComment(
                "comma-separated list, APIs available: web3,net,debug,personal,eth,stratum");
        xmlWriter.writeCharacters("\r\n\t\t\t");
        xmlWriter.writeStartElement("apis-enabled");
        xmlWriter.writeCharacters(String.join(",", this.getEnabled()));
        xmlWriter.writeEndElement();

        if (this.getEnabledMethods() != null) {
            xmlWriter.writeStartElement("api-methods-enabled");
            xmlWriter.writeCharacters(String.join(",", this.getEnabledMethods()));
            xmlWriter.writeEndElement();
        }

        if (this.getDisabledMethods() != null) {
            xmlWriter.writeStartElement("api-methods-disabled");
            xmlWriter.writeCharacters(String.join(",", this.getDisabledMethods()));
            xmlWriter.writeEndElement();
        }

        // don't write-back ssl. (keep it hidden for now)
        // xmlWriter.writeCharacters(this.ssl.toXML());

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeEndElement();
        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 20
Source File: JAXP15RegTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}