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.wso2.carbon.core.persistence.PersistenceUtils.java

/**
 * Creates a registry Resource for a given Policy
 *
 * @param configRegistry config registry
 * @param policy         - Policy instance
 * @param policyId       - policy uuid/*from  w w  w  .  java2s . c  o m*/
 * @param policyType     - policy type
 * @return - created policy resource
 * @throws Exception - error on serialization
 */
public static Resource createPolicyResource(Registry configRegistry, Policy policy, String policyId,
        String policyType) throws RegistryException {
    try {
        Resource policyResource = configRegistry.newResource();
        policyResource.setProperty(RegistryResources.ServiceProperties.POLICY_UUID, policyId);

        // Set the policy as a string in the resource
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
        policy.serialize(writer);
        writer.flush();
        policyResource.setContent(outputStream.toString());

        policyResource.setProperty(RegistryResources.ServiceProperties.POLICY_TYPE, policyType);
        policyResource.setMediaType("application/policy+xml");
        return policyResource;
    } catch (XMLStreamException e) {
        log.error("Error creating the registry resource for " + policyId, e);
        throw new RegistryException("Error creating the registry resource for " + policyId, e);
    }
}

From source file:org.wso2.carbon.core.transports.util.AtomProcessor.java

public void process(HttpServletRequest request, HttpServletResponse response,
        ConfigurationContext configurationContext) throws Exception {
    try {/*  w ww.  ja v  a2  s . c o  m*/
        response.setContentType("text/xml; charset=utf-8");
        AtomFeed atomFeed = FeedFactory.getAtomFeed(FeedConstants.WSO2WSAS_ATOM_FEED,
                configurationContext.getAxisConfiguration());
        if (atomFeed != null) {
            XMLStreamWriter writer = XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(response.getOutputStream());
            writer.writeProcessingInstruction("xml-stylesheet",
                    "  type=\"text/xsl\" href=\"" + (configurationContext.getContextRoot().equals("/") ? ""
                            : configurationContext.getContextRoot()) + "/styles/atom.xsl\"");
            OMElement feedElement = atomFeed.getFeedElement(configurationContext.getServiceContextPath());
            feedElement.serialize(writer);
            writer.flush();
        }
    } catch (Exception e) {
        log.error("Could not process ATOM feed", e);
    }
}

From source file:org.wso2.carbon.core.transports.util.RequestProcessorUtil.java

/**
 * @param byteArrayOutStream//from  w  w  w .  jav a 2  s. c om
 * @param out
 * @param annotatedXsl
 * @param contextRoot
 * @param annotation         : If annotation is false PI would not be attached.
 */
public static void writeDocument(ByteArrayOutputStream byteArrayOutStream, OutputStream out,
        String annotatedXsl, String contextRoot, boolean annotation) {
    XMLStreamWriter writer;
    ByteArrayInputStream bais = null;
    XMLStreamReader reader = null;
    try {
        bais = new ByteArrayInputStream(byteArrayOutStream.toByteArray());
        reader = XMLInputFactory.newInstance().createXMLStreamReader(bais);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        OMElement docElem = builder.getDocumentElement();
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
        if (annotatedXsl != null && annotation) {
            writer.writeProcessingInstruction("xml-stylesheet", "  type=\"text/xsl\" href=\""
                    + (contextRoot.equals("/") ? "" : contextRoot) + "/styles/" + annotatedXsl + "\"");
        }
        docElem.serialize(writer);
        writer.flush();
    } catch (XMLStreamException e) {
        log.error("Error occurred while trying to write processing instruction for attaching "
                + "annotated style sheet", e);
    } finally {
        try {
            if (bais != null) {
                bais.close();
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (XMLStreamException e) {
            log.error(e.getMessage(), e);
        }

    }
}

From source file:org.wso2.carbon.core.transports.util.RssProcessor.java

public void process(HttpServletRequest request, HttpServletResponse response,
        ConfigurationContext configurationContext) throws Exception {
    try {// w ww  .  j a  v  a  2 s.co m
        response.setContentType("text/xml; charset=utf-8");
        RSSFeed rssFeed = FeedFactory.getRSSFeed(FeedConstants.WSO2WSAS_RSS_FEED,
                configurationContext.getAxisConfiguration());
        if (rssFeed != null) {
            XMLStreamWriter writer = XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(response.getOutputStream());
            writer.writeProcessingInstruction("xml-stylesheet",
                    "  type=\"text/xsl\" href=\"" + (configurationContext.getContextRoot().equals("/") ? ""
                            : configurationContext.getContextRoot()) + "/styles/rss.xsl\"");
            OMElement feedElement = rssFeed.getFeedElement(configurationContext.getServiceContextPath());
            feedElement.serialize(writer);
            writer.flush();
        }
    } catch (Exception e) {
        log.error("Could not process RSS feed", e);
    }
}

From source file:org.wso2.carbon.dss.samples.test.EventingSampleTestCase.java

private void updateAxis2_ClientXML() throws Exception {
    String axis2_client_path = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "axis2" + File.separator + "axis2_client.xml";

    String mail_transport_config = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" + File.separator
            + "DSS" + File.separator + "resources" + File.separator + "mailTransport.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;

    try {/*from w w  w  . ja v a 2 s.c  o m*/
        OMElement axis2_client_xml = AXIOMUtil.stringToOM(FileManager.readFile(axis2_client_path));

        axis2_client_xml.addChild(AXIOMUtil.stringToOM(FileManager.readFile(mail_transport_config)));
        axis2_client_xml.build();
        fileOutputStream = new FileOutputStream(axis2_client_path);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        axis2_client_xml.serialize(writer);

    } catch (Exception e) {
        throw new Exception("axis2_client.xml update fails");
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void increaseSearchIndexStartTimeDelay() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {/* ww w.  ja v  a 2s  . c  om*/
        AXIOMXPath xpathExpression = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/startingDelayInSeconds");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        indexConfigNode.setText("60");

        AXIOMXPath xpathExpression1 = new AXIOMXPath(
                "/wso2registry/indexingConfiguration/indexingFrequencyInSeconds");
        OMElement indexConfigNode1 = (OMElement) xpathExpression1.selectSingleNode(documentElement);
        indexConfigNode1.setText("5");

        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.build();
        Thread.sleep(2000);

    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        assert fileOutputStream != null;
        fileOutputStream.close();
        assert writer != null;
        writer.flush();
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void enableJmxManagement() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    OMElement documentElement = getRegistryXmlOmElement();
    try {//from  w  w  w . j  av  a2s.co  m
        AXIOMXPath xpathExpression = new AXIOMXPath("/wso2registry/jmx");
        OMElement indexConfigNode = (OMElement) xpathExpression.selectSingleNode(documentElement);
        OMAttribute omAttribute = indexConfigNode.getAttribute(new QName("enabled"));
        omAttribute.setAttributeValue("true");

        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        documentElement.serialize(writer);
        documentElement.build();
        Thread.sleep(2000);

    } catch (Exception e) {
        log.error("registry.xml edit fails" + e.getMessage());
        throw new Exception("registry.xml edit fails" + e.getMessage());
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}

From source file:org.wso2.carbon.greg.server.mgt.RegistryConfiguratorTestCase.java

private void enableWorkList() throws Exception {
    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;

    String workList;// w w  w . j  a  v a  2 s  .co m

    workList = "<workList serverURL=\"local://services/\" remote=\"false\">\n" + "        <username>"
            + userNameWithoutDomain + "</username>\n" + "        <password>"
            + automationContext.getContextTenant().getTenantAdmin().getPassword() + "</password>\n"
            + "    </workList>";

    try {
        OMElement registryXML = getRegistryXmlOmElement();

        registryXML.addChild(AXIOMUtil.stringToOM(workList));
        registryXML.build();
        fileOutputStream = new FileOutputStream(getRegistryXMLPath());
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (Exception e) {
        throw new Exception("registry.xml update fails");
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }

}

From source file:org.wso2.carbon.mediation.registry.WSO2Registry.java

/**
 * Updates the content of a resource in the given path with given content
 *
 * @param path  The resource path/*from www .  j ava 2  s . com*/
 * @param value The resource content to be set
 */
public void updateResource(String path, Object value) {

    setTenantInfo();

    Resource resource = getResource(path);
    if (resource != null) {
        Registry registry = getRegistry(path);
        if (value instanceof OMNode) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(baos);
                ((OMNode) value).serialize(xmlStreamWriter);
                resource.setContent(baos.toByteArray());
            } catch (XMLStreamException e) {
                handleException("Error when serializing OMNode " + value, e);
            } catch (RegistryException e) {
                handleException("Error when setting content " + value, e);
            }

        } else {
            try {
                resource.setContent(value);
            } catch (RegistryException e) {
                handleException("Error when setting content " + value, e);
            }
        }

        try {
            registry.put(resource.getPath(), resource);
            resource.discard();
        } catch (RegistryException e) {
            handleException("Error when setting a resource in the path : " + path, e);
        }
    }
}

From source file:org.wso2.carbon.mediator.datamapper.engine.output.writers.XMLWriter.java

private void init(Schema outputSchema) throws SchemaException, WriterException {
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try {/* ww  w .ja va2s  .c o m*/
        xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
        //creating root element of the xml message
        namespaceMap = outputSchema.getNamespaceMap();
        writeStartElement(outputSchema.getName(), xmlStreamWriter);
        Iterator<Map.Entry<String, String>> namespaceEntryIterator = namespaceMap.entrySet().iterator();
        while (namespaceEntryIterator.hasNext()) {
            Map.Entry<String, String> entry = namespaceEntryIterator.next();
            xmlStreamWriter.writeNamespace(entry.getValue(), entry.getKey());
        }
    } catch (XMLStreamException e) {
        throw new WriterException("Error while creating xml output factory. " + e.getMessage());
    }
}