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:org.openhealthtools.openatna.archive.Archiver.java

private File archiveMessages(File directory) throws Exception {
    MessageDao dao = AtnaFactory.messageDao();
    MessageWriter writer = new MessageWriter();
    File ret = new File(directory, MESSAGES);
    FileOutputStream fout = new FileOutputStream(ret);
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(fout);
    int offset = 0;
    List<? extends MessageEntity> msgs = dao.getAll(offset, pageSize);
    writer.begin(w);/*from   www .  ja v a 2 s  .  com*/
    writer.writeMessages(msgs, w);
    offset += msgs.size();
    while (msgs.size() >= pageSize) {
        msgs = dao.getAll(offset, pageSize);
        writer.writeMessages(msgs, w);
        offset += msgs.size();
    }
    writer.finish(w);
    log.info("written " + offset + " messages");
    return ret;
}

From source file:org.openhealthtools.openatna.archive.Archiver.java

/**
 * order is important://from  w  w w .jav  a2  s  .c o m
 * <p/>
 * 1. codes
 * 2. naps
 * 3. sources
 * 4. participants
 * 5. objects
 *
 * @param directory
 * @return
 * @throws Exception
 */
private File archiveEntities(File directory) throws Exception {
    File ret = new File(directory, ENTITIES);
    FileOutputStream fout = new FileOutputStream(ret);
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(fout);
    EntityWriter writer = new EntityWriter();
    writer.begin(w);
    archiveCodes(writer, w, pageSize);
    archiveNaps(writer, w, pageSize);
    archiveSources(writer, w, pageSize);
    archiveParticipants(writer, w, pageSize);
    archiveObjects(writer, w, pageSize);
    writer.finish(w);
    return ret;
}

From source file:org.openhealthtools.openatna.archive.Archiver.java

private File archiveErrors(File directory) throws Exception {
    ErrorDao dao = AtnaFactory.errorDao();
    ErrorWriter writer = new ErrorWriter();
    File ret = new File(directory, ERRORS);
    FileOutputStream fout = new FileOutputStream(ret);
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(fout);
    int offset = 0;
    List<? extends ErrorEntity> msgs = dao.getAll(offset, pageSize);
    writer.begin(w);//from w w  w .j av  a2  s .  c  o m
    writer.writeErrors(msgs, w);
    offset += msgs.size();
    while (msgs.size() >= pageSize) {
        msgs = dao.getAll(offset, pageSize);
        writer.writeErrors(msgs, w);
        offset += msgs.size();
    }
    writer.finish(w);
    log.info("written " + offset + " errors");
    return ret;
}

From source file:org.openhim.mediator.denormalization.EnrichRegistryStoredQueryActor.java

private String enrichStoredQueryXML(Identifier id, InputStream xml) throws XMLStreamException {
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(xml);
    StringWriter output = new StringWriter();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);
    XMLEventFactory eventFactory = XMLEventFactory.newFactory();

    String curSlot = null;//from   w  w w. j av  a  2s.c  om
    boolean patientIdSlot = false;

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();

        if (event.getEventType() == XMLEvent.START_ELEMENT) {
            StartElement elem = event.asStartElement();
            if ("Slot".equals(elem.getName().getLocalPart())) {
                curSlot = elem.getAttributeByName(new QName("name")).getValue();
            } else if ("Value".equals(elem.getName().getLocalPart())
                    && ParseRegistryStoredQueryActor.PATIENT_ID_SLOT_TYPE.equals(curSlot)) {
                patientIdSlot = true;
                writer.add(event);
            }
        } else if (event.getEventType() == XMLEvent.END_ELEMENT) {
            EndElement elem = event.asEndElement();
            if (patientIdSlot && "Value".equals(elem.getName().getLocalPart())) {
                XMLEvent ecidEvent = eventFactory.createCharacters("'" + id.toString() + "'");
                writer.add(ecidEvent);
                patientIdSlot = false;
            }
        }

        if (!patientIdSlot) {
            writer.add(event);
        }
    }

    writer.close();
    return output.toString();
}

From source file:org.openhim.mediator.enrichers.IdentityEnricher.java

public String enrich(String xml) throws XMLStreamException {
    InputStream in = IOUtils.toInputStream(xml);
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(in);
    StringWriter output = new StringWriter();
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        processXMLEvent(writer, event);//from w  w w  .jav a  2  s. co  m
    }

    writer.close();
    return output.toString();
}

From source file:org.ops4j.pax.exam.karaf.container.internal.DependenciesDeployer.java

/**
 * Write a feature xml structure for test dependencies specified as ProvisionOption
 * in system to the given writer/*from w  w w  .j a v  a 2s  . co m*/
 * 
 * @param writer where to write the feature xml
 * @param provisionOptions dependencies
 */
static void writeDependenciesFeature(Writer writer, ProvisionOption<?>... provisionOptions) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    xof.setProperty("javax.xml.stream.isRepairingNamespaces", true);
    XMLStreamWriter sw = null;
    try {
        sw = xof.createXMLStreamWriter(writer);
        sw.writeStartDocument("UTF-8", "1.0");
        sw.setDefaultNamespace(KARAF_FEATURE_NS);
        sw.writeCharacters("\n");
        sw.writeStartElement("features");
        sw.writeAttribute("name", "test-dependencies");
        sw.writeCharacters("\n");
        sw.writeStartElement("feature");
        sw.writeAttribute("name", "test-dependencies");
        sw.writeCharacters("\n");
        for (ProvisionOption<?> provisionOption : provisionOptions) {
            if (provisionOption.getURL().startsWith("link")
                    || provisionOption.getURL().startsWith("scan-features")) {
                // well those we've already handled at another location...
                continue;
            }
            sw.writeStartElement("bundle");
            if (provisionOption.getStartLevel() != null) {
                sw.writeAttribute("start-level", provisionOption.getStartLevel().toString());
            }
            sw.writeCharacters(provisionOption.getURL());
            endElement(sw);
        }
        endElement(sw);
        endElement(sw);
        sw.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new RuntimeException("Error writing feature " + e.getMessage(), e);
    } finally {
        close(sw);
    }
}

From source file:org.plasma.sdo.xml.StreamMarshaller.java

private void construct() {
    this.factory = XMLOutputFactory.newInstance();
    // Set namespace prefix defaulting for all created writers
    //this.factory.setProperty("javax.xml.stream.isPrefixDefaulting",Boolean.TRUE);
}

From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java

private void exporterMain() {
    XMLStreamWriter wrt = null;//  w w  w.  jav a  2  s  .  co m
    OutputStream out = null;
    try {
        XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
        ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

        try {
            out = exportOutput;
            if (zipOutput) {
                out = new GZIPOutputStream(out);
            }
            wrt = new IndentingXMLStreamWriter(ofactory.createXMLStreamWriter(out, "UTF-8"));
            //wrt = ofactory.createXMLStreamWriter(out, "UTF-8");
        } catch (XMLStreamException e) {
            LOG.error("Failed to create the XML stream writer to output the export file to.", e);
            return;
        }

        exportPrologue(wrt);

        for (Synchronizer<?, ?> exp : synchronizers) {
            exportSingle(wrt, exp);
        }

        exportEpilogue(wrt);

        wrt.flush();
    } catch (Exception e) {
        LOG.error("Error while exporting.", e);
        unexpectedExporterException = e;
    } finally {
        if (wrt != null) {
            try {
                wrt.close();
            } catch (XMLStreamException e) {
                LOG.warn("Failed to close the exporter XML stream.", e);
            }
        }
        safeClose(out);
    }
}

From source file:org.rhq.enterprise.server.sync.test.DeployedAgentPluginsValidatorTest.java

public void testCanExportAndImportState() throws Exception {
    final PluginManagerLocal pluginManager = context.mock(PluginManagerLocal.class);

    final DeployedAgentPluginsValidator validator = new DeployedAgentPluginsValidator(pluginManager);

    context.checking(new Expectations() {
        {//w  ww  .  j a v  a 2  s.  c o m
            oneOf(pluginManager).getInstalledPlugins();
            will(returnValue(new ArrayList<Plugin>(getDeployedPlugins())));
        }
    });

    validator.initialize(null, null);

    StringWriter output = new StringWriter();
    try {
        XMLOutputFactory ofactory = XMLOutputFactory.newInstance();

        XMLStreamWriter wrt = ofactory.createXMLStreamWriter(output);
        //wrap the exported plugins in "something" so that we produce
        //a valid xml
        wrt.writeStartDocument();
        wrt.writeStartElement("root");

        validator.exportState(new ExportWriter(wrt));

        wrt.writeEndDocument();

        wrt.close();

        StringReader input = new StringReader(output.toString());

        try {
            XMLInputFactory ifactory = XMLInputFactory.newInstance();
            XMLStreamReader rdr = ifactory.createXMLStreamReader(input);

            //push the reader to the start of the plugin elements
            //this is what is expected by the validators
            rdr.nextTag();

            validator.initializeExportedStateValidation(new ExportReader(rdr));

            rdr.close();

            assertEquals(validator.getPluginsToValidate(), getDeployedPlugins());
        } finally {
            input.close();
        }
    } catch (Exception e) {
        LOG.error("Test failed. Output generated so far:\n" + output, e);
        throw e;
    } finally {
        output.close();
    }
}

From source file:org.simbasecurity.core.saml.SAMLServiceImpl.java

@Override
public String createAuthRequest(String authRequestId, Date issueInstant)
        throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "AuthnRequest", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);

    writer.writeAttribute("ID", "_" + authRequestId);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", SAML_DATE_FORMAT.format(issueInstant));
    writer.writeAttribute("ForceAuthn", "false");
    writer.writeAttribute("IsPassive", "false");
    writer.writeAttribute("ProtocolBinding", BINDING_HTTP_POST);
    writer.writeAttribute("AssertionConsumerServiceURL",
            configurationService.getValue(SimbaConfigurationParameter.SAML_ASSERTION_CONSUMER_SERVICE_URL));

    writer.writeStartElement("saml", "Issuer", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters(configurationService.getValue(SimbaConfigurationParameter.SAML_ISSUER));
    writer.writeEndElement();//from  w  ww  .  ja v  a  2s .c o m

    writer.writeStartElement("samlp", "NameIDPolicy", NS_SAMLP);

    writer.writeAttribute("Format", NAMEID_TRANSIENT);
    writer.writeAttribute("SPNameQualifier",
            configurationService.getValue(SimbaConfigurationParameter.SAML_ISSUER));
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();

    writer.writeStartElement("samlp", "RequestedAuthnContext", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);
    writer.writeAttribute("Comparison", "exact");

    writer.writeStartElement("saml", "AuthnContextClassRef", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters(AC_FAS_EID);
    writer.writeEndElement();

    writer.writeEndElement();
    writer.writeEndElement();
    writer.flush();

    return encodeSAMLRequest(baos.toByteArray());
}