Example usage for javax.xml.stream XMLOutputFactory newInstance

List of usage examples for javax.xml.stream XMLOutputFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory newInstance.

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:eu.peppol.document.PayloadParserTest.java

/**
 * Takes a file holding an SBD/SBDH with an ASiC archive in base64 as payload and extracts the ASiC archive in binary format, while
 * calculating the message digest.//from w ww  . j  a v a2 s.  co m
 *
 * @throws Exception
 */
@Test
public void parseSampleSbdWithAsic() throws Exception {

    InputStream resourceAsStream = PayloadParserTest.class.getClassLoader()
            .getResourceAsStream("sample-sbd-with-asic.xml");
    assertNotNull(resourceAsStream);

    Path xmlFile = Files.createTempFile("unit-test", ".xml");

    XMLEventReader xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(resourceAsStream,
            "UTF-8");
    FileOutputStream outputStream = new FileOutputStream(xmlFile.toFile());
    XMLEventWriter xmlEventWriter = XMLOutputFactory.newInstance().createXMLEventWriter(outputStream, "UTF-8");

    Path asicFile = Files.createTempFile("unit-test", ".asice");
    OutputStream asicOutputStream = Files.newOutputStream(asicFile);
    MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");

    DigestOutputStream digestOutputStream = new DigestOutputStream(asicOutputStream, messageDigest);
    Base64OutputStream base64OutputStream = new Base64OutputStream(digestOutputStream, false);

    boolean insideAsicElement = false;

    while (xmlEventReader.hasNext()) {
        XMLEvent xmlEvent = xmlEventReader.nextEvent();

        switch (xmlEvent.getEventType()) {
        case XMLEvent.START_ELEMENT:
            String localPart = xmlEvent.asStartElement().getName().getLocalPart();
            if ("asic".equals(localPart)) {
                insideAsicElement = true;
            }
            break;
        case XMLEvent.END_ELEMENT:
            localPart = xmlEvent.asEndElement().getName().getLocalPart();
            if ("asic".equals(localPart)) {
                insideAsicElement = false;
            }
            break;

        case XMLEvent.CHARACTERS:
            // Whenever we are inside the ASiC XML element, spit
            // out the base64 encoded data into the base64 decoding output stream.
            if (insideAsicElement) {
                Characters characters = xmlEvent.asCharacters();
                base64OutputStream.write(characters.getData().getBytes("UTF-8"));
            }
            break;
        }
        xmlEventWriter.add(xmlEvent);
    }

    asicOutputStream.close();
    outputStream.close();
    log.debug("Wrote xml output to: " + xmlFile);
    log.debug("Wrote ASiC to:" + asicFile);
    log.debug("Digest: " + new String(Base64.getEncoder().encode(messageDigest.digest())));
}

From source file:com.predic8.membrane.integration.ProxyRuleTest.java

@Test
public void testWriteRuleToByteBuffer() throws Exception {
    rule1 = new ProxyRule(new ProxyRuleKey(8888));
    rule1.setName("Rule 1");
    rule1.setInboundTLS(true);// www . j  a  va  2  s  . co m
    rule1.setBlockResponse(true);
    rule1.setInterceptors(getInterceptors());

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(os, Constants.UTF_8);
    rule1.write(writer);
    writer.flush();
    buffer = os.toByteArray();
}

From source file:com.hp.mqm.clt.xml.TestResultXmlWriter.java

private void initialize(Settings settings) throws IOException, InterruptedException, XMLStreamException {
    if (outputStream == null) {
        outputStream = new FileOutputStream(targetPath);
        writer = possiblyCreateIndentingWriter(
                XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream));
        writer.writeStartDocument();// w w  w.j  av  a 2  s. co m

        writer.writeStartElement("test_result");
        writeFields(settings);
        writer.writeStartElement("test_runs");
    }
}

From source file:eionet.cr.web.util.VoIDXmlWriter.java

/**
 *
 * Class constructor.//  w w w.j  a  va 2  s . c  o  m
 *
 * @param out
 * @param contextRoot
 * @throws XMLStreamException
 */
public VoIDXmlWriter(OutputStream out, String contextRoot) throws XMLStreamException {
    writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out, ENCODING);
    this.contextRoot = contextRoot;
}

From source file:com.google.code.activetemplates.impl.TemplateCompilerImpl.java

public TemplateCompilerImpl() {

    outFactory = XMLOutputFactory.newInstance();
    outFactory.setProperty(XMLOutputFactory2.IS_REPAIRING_NAMESPACES, true);
    inFactory = XMLInputFactory.newInstance();
    eFactory = XMLEventFactory.newInstance();

    h = new Handlers();

    excludedNamespaces = new HashSet<String>();
    List<HandlerSPI> spis = Providers.getHandlerSPIs();

    for (HandlerSPI spi : spis) {
        Set<String> s = spi.getExcludedNamespaces();
        if (s != null) {
            excludedNamespaces.addAll(s);
        }/*from w  w  w.j a va 2s  .c o  m*/
    }

    eComponentFactory = new EventComponentFactory();
    expressionParser = new SpelExpressionParser();
}

From source file:com.norconex.jefmon.model.ConfigurationDAO.java

public static void saveConfig(JEFMonConfig config) throws IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Saving JEF config to: " + CONFIG_FILE);
    }/*from   w  ww. ja va2  s  . co m*/

    if (!CONFIG_FILE.exists()) {
        File configDir = new File(FilenameUtils.getFullPath(CONFIG_FILE.getAbsolutePath()));
        if (!configDir.exists()) {
            LOG.debug("Creating JEF Monitor config directory for: " + CONFIG_FILE);
            configDir.mkdirs();
        }
    }

    OutputStream out = new FileOutputStream(CONFIG_FILE);
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter xml = factory.createXMLStreamWriter(out);
        xml.writeStartDocument();
        xml.writeStartElement("jefmon-config");

        xml.writeStartElement("instance-name");
        xml.writeCharacters(config.getInstanceName());
        xml.writeEndElement();

        xml.writeStartElement("default-refresh-interval");
        xml.writeCharacters(Integer.toString(config.getDefaultRefreshInterval()));
        xml.writeEndElement();

        saveRemoteUrls(xml, config.getRemoteInstanceUrls());
        saveMonitoredPaths(xml, config.getMonitoredPaths());
        saveJobActions(xml, config.getJobActions());

        xml.writeEndElement();
        xml.writeEndDocument();
        xml.flush();
        xml.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
    out.close();
}

From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolverFactory.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//w  w  w . j  a  va 2  s. c o  m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("sitemapResolverFactory");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeAttribute("lenient", Boolean.toString(lenient));
        if (sitemapLocations != null) {
            for (String location : sitemapLocations) {
                writer.writeStartElement("location");
                writer.writeCharacters(location);
                writer.writeEndElement();
            }
        }
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.predic8.membrane.core.config.AbstractXmlElement.java

public String toXml() throws Exception {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
    w.writeStartDocument();//from w  w  w. ja  v a  2  s . c o m
    write(w);
    w.writeEndDocument();
    return sw.toString();
}

From source file:com.hp.application.automation.tools.octane.tests.xml.TestResultXmlWriter.java

private void initialize(ResultFields resultFields)
        throws IOException, InterruptedException, XMLStreamException {
    if (outputStream == null) {
        outputStream = targetPath.write();
        writer = possiblyCreateIndentingWriter(
                XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream));
        writer.writeStartDocument();/*from w  w w.  ja v  a 2s . co  m*/

        writer.writeStartElement("test_result");
        writer.writeStartElement("build");
        writer.writeAttribute("server_id", ConfigurationService.getModel().getIdentity());
        BuildDescriptor descriptor = this.buildDescriptor;
        writer.writeAttribute("job_id", descriptor.getJobId());
        writer.writeAttribute("job_name", descriptor.getJobName());
        writer.writeAttribute("build_id", descriptor.getBuildId());
        writer.writeAttribute("build_name", descriptor.getBuildName());
        if (!StringUtils.isEmpty(descriptor.getSubType())) {
            writer.writeAttribute("sub_type", descriptor.getSubType());
        }
        writer.writeEndElement(); // build
        writeFields(resultFields);
        writer.writeStartElement("test_runs");
    }
}

From source file:com.predic8.membrane.core.ws.relocator.Relocator.java

public Relocator(Writer w, String protocol, String host, int port, PathRewriter pathRewriter) throws Exception {
    this.writer = XMLOutputFactory.newInstance().createXMLEventWriter(w);
    this.host = host;
    this.port = port;
    this.protocol = protocol;
    this.pathRewriter = pathRewriter;
}