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

The following examples show how to use javax.xml.stream.XMLOutputFactory#createXMLStreamWriter() . 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: TemplateSerializer.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * This method when called assumes the Framework Nar ClassLoader is in the
 * classloader hierarchy of the current context class loader.
 * @param dto the template dto to serialize
 * @return serialized representation of the DTO
 */
public static byte[] serialize(final TemplateDTO dto) {
    try {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final BufferedOutputStream bos = new BufferedOutputStream(baos);

        JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
        Marshaller marshaller = context.createMarshaller();
        XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = new IndentingXMLStreamWriter(xmlof.createXMLStreamWriter(bos));
        marshaller.marshal(dto, writer);

        bos.flush();
        return baos.toByteArray(); //Note: For really large templates this could use a lot of heap space
    } catch (final IOException | JAXBException | XMLStreamException e) {
        throw new FlowSerializationException(e);
    }
}
 
Example 3
Source File: UnprefixedNameTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testUnboundPrefix() throws Exception {

    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        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();

        // Unexpected success
        String FAIL_MSG = "Unexpected success.  Expected: " + "XMLStreamException - " + "if the namespace URI has not been bound to a prefix "
                + "and javax.xml.stream.isPrefixDefaulting has not been " + "set to true";
        System.err.println(FAIL_MSG);
        Assert.fail(FAIL_MSG);
    } catch (XMLStreamException xmlStreamException) {
        // Expected Exception
        System.out.println("Expected XMLStreamException: " + xmlStreamException.toString());
    }
}
 
Example 4
Source File: StaxUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void isStaxResult() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	Result result = StaxUtils.createCustomStaxResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example 5
Source File: CfgLog.java    From aion_api with MIT License 5 votes vote down vote up
String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("log");
        xmlWriter.writeCharacters("\r\n");
        for (Map.Entry<String, String> module : this.modules.entrySet()) {
            xmlWriter.writeCharacters("\t\t");
            xmlWriter.writeStartElement(module.getKey().toUpperCase());
            xmlWriter.writeCharacters(module.getValue().toUpperCase());
            xmlWriter.writeEndElement();
            xmlWriter.writeCharacters("\r\n");
        }
        xmlWriter.writeCharacters("\t");
        xmlWriter.writeEndElement();
        xml = strWriter.toString();
        strWriter.flush();
        strWriter.close();
        xmlWriter.flush();
        xmlWriter.close();
        return xml;
    } catch (IOException | XMLStreamException e) {
        return "";
    }
}
 
Example 6
Source File: XStreamMarshallerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void marshalStaxResultXMLStreamWriter() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	StringWriter writer = new StringWriter();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
	Result result = StaxUtils.createStaxResult(streamWriter);
	marshaller.marshal(flight, result);
	assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
 
Example 7
Source File: StaxUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void isStaxResultJaxp14() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	StAXResult result = new StAXResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example 8
Source File: CfgSsl.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 {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t\t\t");
        xmlWriter.writeStartElement("ssl");

        xmlWriter.writeCharacters("\r\n\t\t\t\t");
        xmlWriter.writeComment("toggle ssl on/off " + "(if you to access json-rpc over https)");
        xmlWriter.writeCharacters("\r\n\t\t\t\t");
        xmlWriter.writeStartElement(ENABLED_TAG);
        xmlWriter.writeCharacters(this.enabled ? "true" : "false");
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t\t\t");
        xmlWriter.writeComment("path to jks or pkcs12 ssl certificate");
        xmlWriter.writeCharacters("\r\n\t\t\t\t");
        xmlWriter.writeStartElement(CERTIFICATE_TAG);
        xmlWriter.writeCharacters(this.cert);
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\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 9
Source File: StaxUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void isStaxResultJaxp14() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	StAXResult result = new StAXResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example 10
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 11
Source File: StaxUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void isStaxResultJaxp14() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(new StringWriter());
	StAXResult result = new StAXResult(streamWriter);

	assertTrue("Not a StAX Result", StaxUtils.isStaxResult(result));
}
 
Example 12
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 13
Source File: WSDLFetcher.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {

        DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
        WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
            @Override
            public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
                return null;
            }
        }, docLocator);

        XMLStreamReader xsr = null;
        XMLStreamWriter xsw = null;
        OutputStream os = null;
        String resolvedRootWsdl = null;
        try {
            XMLOutputFactory writerfactory;
            xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
            writerfactory = XMLOutputFactory.newInstance();
            resolvedRootWsdl = docLocator.getLocationFor(null, doc);
            File outFile = new File(destDir, resolvedRootWsdl);
            os = new FileOutputStream(outFile);
            if(options.verbose) {
                listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
            }
            xsw = writerfactory.createXMLStreamWriter(os);
            //DOMForest eats away the whitespace loosing all the indentation, so write it through
            // indenting writer for better readability of fetched documents
            IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
            wsdlPatcher.bridge(xsr, indentingWriter);
            options.addGeneratedFile(outFile);
        } finally {
            try {
                if (xsr != null) {xsr.close();}
                if (xsw != null) {xsw.close();}
            } finally {
                if (os != null) {os.close();}
            }
        }
        return resolvedRootWsdl;


    }
 
Example 14
Source File: WSDLFetcher.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {

        DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
        WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
            @Override
            public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
                return null;
            }
        }, docLocator);

        XMLStreamReader xsr = null;
        XMLStreamWriter xsw = null;
        OutputStream os = null;
        String resolvedRootWsdl = null;
        try {
            XMLOutputFactory writerfactory;
            xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
            writerfactory = XMLOutputFactory.newInstance();
            resolvedRootWsdl = docLocator.getLocationFor(null, doc);
            File outFile = new File(destDir, resolvedRootWsdl);
            os = new FileOutputStream(outFile);
            if(options.verbose) {
                listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
            }
            xsw = writerfactory.createXMLStreamWriter(os);
            //DOMForest eats away the whitespace loosing all the indentation, so write it through
            // indenting writer for better readability of fetched documents
            IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
            wsdlPatcher.bridge(xsr, indentingWriter);
            options.addGeneratedFile(outFile);
        } finally {
            try {
                if (xsr != null) {xsr.close();}
                if (xsw != null) {xsw.close();}
            } finally {
                if (os != null) {os.close();}
            }
        }
        return resolvedRootWsdl;


    }
 
Example 15
Source File: StaxStreamHandlerTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
	XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(result);
	return new StaxStreamHandler(streamWriter);
}
 
Example 16
Source File: CfgTx.java    From aion with MIT License 4 votes vote down vote up
public String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("tx");

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("Sets max TransactionPoolCaching size by 0.1MB incremental unit");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("cacheMax");
        xmlWriter.writeCharacters(String.valueOf(this.getCacheMax()));
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("Sets pending transaction timeout threshold in the transaction pool");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("txPendingTimeout");
        xmlWriter.writeCharacters(String.valueOf(this.getTxPendingTimeout()));
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("Sets the pending transactions backup to the Database");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("poolBackup");
        xmlWriter.writeCharacters(String.valueOf(this.getPoolBackup()));
        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: BpmnXMLConverter.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
public byte[] convertToXML(BpmnModel model, String encoding) {
  try {

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);

    XMLStreamWriter writer = xof.createXMLStreamWriter(out);
    XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer);

    DefinitionsRootExport.writeRootElement(model, xtw, encoding);
    CollaborationExport.writePools(model, xtw);
    DataStoreExport.writeDataStores(model, xtw);
    SignalAndMessageDefinitionExport.writeSignalsAndMessages(model, xtw);

    for (Process process : model.getProcesses()) {

      if (process.getFlowElements().isEmpty() && process.getLanes().isEmpty()) {
        // empty process, ignore it
        continue;
      }

      ProcessExport.writeProcess(process, xtw);

      for (FlowElement flowElement : process.getFlowElements()) {
        createXML(flowElement, model, xtw);
      }

      for (Artifact artifact : process.getArtifacts()) {
        createXML(artifact, model, xtw);
      }

      // end process element
      xtw.writeEndElement();
    }

    BPMNDIExport.writeBPMNDI(model, xtw);

    // end definitions root element
    xtw.writeEndElement();
    xtw.writeEndDocument();

    xtw.flush();

    outputStream.close();

    xtw.close();

    return outputStream.toByteArray();

  } catch (Exception e) {
    LOGGER.error("Error writing BPMN XML", e);
    throw new XMLException("Error writing BPMN XML", e);
  }
}
 
Example 18
Source File: CfgDb.java    From aion with MIT License 4 votes vote down vote up
public String toXML() {
    final XMLOutputFactory output = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlWriter;
    String xml;
    try {
        Writer strWriter = new StringWriter();
        xmlWriter = output.createXMLStreamWriter(strWriter);
        xmlWriter.writeCharacters("\r\n\t");
        xmlWriter.writeStartElement("db");

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("Sets the physical location on disk where data will be stored.");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("path");
        xmlWriter.writeCharacters(this.getPath());
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "Boolean value. Enable/disable database integrity check run at startup.");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("check_integrity");
        xmlWriter.writeCharacters(String.valueOf(this.check_integrity));
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "Data pruning behavior for the state database. Options: FULL, TOP, SPREAD.");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("FULL: the state is not pruned");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "TOP: the state is kept only for the top K blocks; limits sync to branching only within the stored blocks");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment(
                "SPREAD: the state is kept for the top K blocks and at regular block intervals");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("state-storage");
        xmlWriter.writeCharacters(this.prune_option.toString());
        xmlWriter.writeEndElement();

        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeComment("Data storage behavior for the transaction database. Boolean value used to enable/disable internal transaction storage.");
        xmlWriter.writeCharacters("\r\n\t\t");
        xmlWriter.writeStartElement("internal-tx-storage");
        xmlWriter.writeCharacters(String.valueOf(internalTxStorage));
        xmlWriter.writeEndElement();

        if (!expert) {
            xmlWriter.writeCharacters("\r\n\t\t");
            xmlWriter.writeComment(
                    "Database implementation used to store data; supported options: leveldb, h2, rocksdb.");
            xmlWriter.writeCharacters("\r\n\t\t");
            xmlWriter.writeComment(
                    "Caution: changing implementation requires re-syncing from genesis!");
            xmlWriter.writeCharacters("\r\n\t\t");
            xmlWriter.writeStartElement("vendor");
            xmlWriter.writeCharacters(this.vendor);
            xmlWriter.writeEndElement();

            xmlWriter.writeCharacters("\r\n\t\t");
            xmlWriter.writeComment(
                    "Boolean value. Enable/disable database compression to trade storage space for execution time.");
            xmlWriter.writeCharacters("\r\n\t\t");
            xmlWriter.writeStartElement(Props.ENABLE_DB_COMPRESSION);
            xmlWriter.writeCharacters(String.valueOf(this.compression));
            xmlWriter.writeEndElement();
        } else {
            for (Map.Entry<String, CfgDbDetails> entry : specificConfig.entrySet()) {
                entry.getValue().toXML(entry.getKey(), xmlWriter, expert);
            }
        }

        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 19
Source File: XmlBuilder.java    From geoportal-server-harvester with Apache License 2.0 4 votes vote down vote up
/**
 * Initialize the builder.
 * @throws Exception if initialization fails
 */
public void init() throws Exception {
  XMLOutputFactory factory = XMLOutputFactory.newInstance();
  xml = new StringWriter();
  writer = factory.createXMLStreamWriter(xml);
}
 
Example 20
Source File: WSDLFetcher.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private String fetchFile(final String doc, DOMForest forest, final Map<String, String> documentMap, File destDir) throws IOException, XMLStreamException {

        DocumentLocationResolver docLocator = createDocResolver(doc, forest, documentMap);
        WSDLPatcher wsdlPatcher = new WSDLPatcher(new PortAddressResolver() {
            @Override
            public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) {
                return null;
            }
        }, docLocator);

        XMLStreamReader xsr = null;
        XMLStreamWriter xsw = null;
        OutputStream os = null;
        String resolvedRootWsdl = null;
        try {
            XMLOutputFactory writerfactory;
            xsr = SourceReaderFactory.createSourceReader(new DOMSource(forest.get(doc)), false);
            writerfactory = XMLOutputFactory.newInstance();
            resolvedRootWsdl = docLocator.getLocationFor(null, doc);
            File outFile = new File(destDir, resolvedRootWsdl);
            os = new FileOutputStream(outFile);
            if(options.verbose) {
                listener.message(WscompileMessages.WSIMPORT_DOCUMENT_DOWNLOAD(doc,outFile));
            }
            xsw = writerfactory.createXMLStreamWriter(os);
            //DOMForest eats away the whitespace loosing all the indentation, so write it through
            // indenting writer for better readability of fetched documents
            IndentingXMLStreamWriter indentingWriter = new IndentingXMLStreamWriter(xsw);
            wsdlPatcher.bridge(xsr, indentingWriter);
            options.addGeneratedFile(outFile);
        } finally {
            try {
                if (xsr != null) {xsr.close();}
                if (xsw != null) {xsw.close();}
            } finally {
                if (os != null) {os.close();}
            }
        }
        return resolvedRootWsdl;


    }