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.registry.core.jdbc.Repository.java

/**
 * Method to do a dump.//from w w  w  .  j ava2  s. com
 *
 * @param _path   the path to obtain the dump from.
 * @param writer the writer used.
 *
 * @throws RegistryException if the operation failed.
 */
public void dump(String _path, Writer writer) throws RegistryException {
    String path = _path;
    if (!path.equals("/") && path.endsWith("/")) {
        // remove the / suffix
        path = path.substring(0, path.length() - 1);
    }
    XMLStreamWriter xmlWriter = null;
    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursively(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RegistryException(msg, e);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (XMLStreamException e) {
            }
        }
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.Repository.java

protected void dumpLite(String _path, Writer writer) throws RegistryException {
    String path = _path;//from  w w w . j a  v  a2  s  .  c  o  m
    if (!path.equals("/") && path.endsWith("/")) {
        // remove the / suffix
        path = path.substring(0, path.length() - 1);
    }

    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursivelyLight(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RegistryException(msg, e);
    }
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213.java

@Test(groups = { "wso2.greg" }, description = "change registry.xml and restart server")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void editRegistryXML() throws Exception {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {/*from www  .j  av  a  2  s  .  co  m*/
        OMElement handlerConfig = AXIOMUtil.stringToOM("<property name=\"useOriginalSchema\">true</property>");
        OMElement registryXML = getRegistryXmlOmElement();

        OMElement om1;
        for (Iterator iterator = registryXML.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {
                om1 = om;
                om1.addChild(handlerConfig);
                registryXML.addChild(om1);
                registryXML.build();
                break;
            }

        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } catch (IOException e) {
        throw new IOException("IO exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

    restartServer();

    EnvironmentBuilder builder = new EnvironmentBuilder().greg(userId);
    ManageEnvironment environment = builder.build();

    //reinitialize environment after server restart
    resourceAdminServiceClient = new ResourceAdminServiceClient(environment.getGreg().getBackEndUrl(),
            environment.getGreg().getSessionCookie());
    serverAdminClient = new ServerAdminClient(environment.getGreg().getProductVariables().getBackendUrl(),
            environment.getGreg().getSessionCookie());
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213.java

@AfterClass(groups = { "wso2.greg" })
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void deleteSchema() throws Exception {
    resourceAdminServiceClient.deleteResource(schemaPath);
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {//from w w  w  .  j  a  va2  s. c om
        File srcFile = new File(registryXmlPath);
        OMElement element = getRegistryXmlOmElement();
        OMElement omElement = null;

        for (Iterator iterator = element.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {

                omElement = om;
                for (Iterator it = omElement.getChildrenWithName(new QName("property")); it.hasNext();) {
                    OMElement omRemove = (OMElement) it.next();
                    if (omRemove.getAttribute(new QName("name")).getAttributeValue()
                            .equals("useOriginalSchema")) {
                        it.remove();
                        element.build();
                        break;
                    }
                }
                break;
            }
        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);
        restartServer();

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213TestCase.java

@Test(groups = { "wso2.greg" }, description = "change registry.xml and restart server")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void editRegistryXML() throws Exception {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    File srcFile = new File(registryXmlPath);
    try {/*from w  w w  . j  av  a  2  s .co m*/
        OMElement handlerConfig = AXIOMUtil.stringToOM("<property name=\"useOriginalSchema\">true</property>");
        OMElement registryXML = getRegistryXmlOmElement();

        OMElement om1;
        for (Iterator iterator = registryXML.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {
                om1 = om;
                om1.addChild(handlerConfig);
                registryXML.addChild(om1);
                registryXML.build();
                break;
            }

        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
    }

    restartServer();

    String sessionCookie = getSessionCookie();

    //reinitialize environment after server restart
    resourceAdminServiceClient = new ResourceAdminServiceClient(backendURL, sessionCookie);
    serverAdminClient = new ServerAdminClient(backendURL, sessionCookie);
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12213TestCase.java

@AfterClass(groups = { "wso2.greg" })
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.STANDALONE })
public void deleteSchema() throws Exception {
    resourceAdminServiceClient.deleteResource(schemaPath);
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    FileOutputStream fileOutputStream = null;
    XMLStreamWriter writer = null;
    try {/* w w w . j a  v a  2  s  .c  o  m*/
        File srcFile = new File(registryXmlPath);
        OMElement element = getRegistryXmlOmElement();
        OMElement omElement = null;

        for (Iterator iterator = element.getChildrenWithName(new QName("handler")); iterator.hasNext();) {
            OMElement om = (OMElement) iterator.next();

            if (om.getAttribute(new QName("class")).getAttributeValue()
                    .equals("org.wso2.carbon.registry.extensions.handlers" + ".ZipWSDLMediaTypeHandler")) {

                omElement = om;
                for (Iterator it = omElement.getChildrenWithName(new QName("property")); it.hasNext();) {
                    OMElement omRemove = (OMElement) it.next();
                    if (omRemove.getAttribute(new QName("name")).getAttributeValue()
                            .equals("useOriginalSchema")) {
                        it.remove();
                        element.build();
                        break;
                    }
                }
                break;
            }
        }

        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);
        restartServer();

    } finally {
        if (writer != null) {
            writer.close();
        }
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }

    }
}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12924.java

@Test(groups = {
        "wso2.greg" }, description = "Restore registry.xml to the previous state", dependsOnMethods = "deleteService")
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void restoreRegistryXML()
        throws XMLStreamException, IOException, InterruptedException, RegistryException {

    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {/*from   w ww  . ja v  a 2s. c  o m*/
        OMElement element = getRegistryXmlOmElement();
        element.getChildrenWithName(new QName("aspect")).remove();
        element.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);
    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e);

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e);

    } catch (IOException e) {
        throw new IOException("IO exception" + e);

    } finally {
        writer.close();
        fileOutputStream.close();
    }

    String resourcePath = ProductConstant.SYSTEM_TEST_RESOURCE_LOCATION + "artifacts" + File.separator + "GREG"
            + File.separator + "xml" + File.separator + "lifeCycleConfig.xml";

    Resource lifecycleConfig = governance.newResource();
    lifecycleConfig.setMediaType("application/xml");
    lifecycleConfig.setContentStream(new FileInputStream(resourcePath));
    governance.put("trunk/lifeCycleConfig.xml", lifecycleConfig);

}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12924.java

@AfterClass(alwaysRun = true)
@SetEnvironment(executionEnvironments = { ExecutionEnvironment.integration_user })
public void deleteAndRestore() throws Exception {

    String workList = "<workList serverURL=\"" + frameworkProperties.getProductVariables().getBackendUrl()
            + "\" remote=\"false\">\n" + "        <username>" + userInfo.getUserName() + "</username>\n"
            + "        <password>" + userInfo.getPassword() + "</password>\n" + "        </workList>";

    ServiceManager serviceManager = new ServiceManager(governance);
    GovernanceUtils.loadGovernanceArtifacts((UserRegistry) governance);
    Service[] services = serviceManager.getAllServices();
    for (Service s : services) {
        if (s.getQName().getLocalPart().equals(serviceName)) {
            serviceManager.removeService(s.getId());
        }//from  w w w  . ja va  2 s .  co  m

    }

    if (governance.resourceExists("trunk/lifeCycleConfig.xml")) {
        governance.delete("trunk/lifeCycleConfig.xml");
    }
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {
        OMElement element = getRegistryXmlOmElement();
        element.getChildrenWithName(new QName("aspect")).remove();

        if (!element.getChildrenWithName(new QName("workList")).hasNext()) {
            element.addChild(AXIOMUtil.stringToOM(workList));

        }

        element.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        element.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e.getMessage());

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e.getMessage());

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
    restartServer();

    governance = null;
    registry = null;
    lifeCycleAdminServiceClient = null;
    lifeCycleManagementClient = null;
    serverAdminClient = null;
    environment = null;
    builder = null;

}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12924.java

public static void editRegistryXML() throws XMLStreamException, IOException, InterruptedException {
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {//from   w ww  .  j av  a 2s.c  om
        OMElement handlerConfig = getHandlerOmElement();
        OMElement registryXML = getRegistryXmlOmElement();
        if (registryXML.getChildrenWithName(new QName("workList")).hasNext()) {
            registryXML.getChildrenWithName(new QName("workList")).remove();

        }

        registryXML.addChild(handlerConfig);
        registryXML.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e.getMessage());

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e.getMessage());

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }

}

From source file:org.wso2.carbon.registry.jira.issues.test.Carbon12924.java

public static void editRegistryXMLWithNewConfig() throws XMLStreamException, IOException, InterruptedException {
    String registryXmlPath = CarbonUtils.getCarbonHome() + File.separator + "repository" + File.separator
            + "conf" + File.separator + "registry.xml";

    File srcFile = new File(registryXmlPath);
    try {//ww w. ja v a 2 s. c  o m
        OMElement handlerConfig = getHandlerOmElementNew();
        OMElement registryXML = getRegistryXmlOmElement();

        registryXML.addChild(handlerConfig);
        registryXML.build();
        fileOutputStream = new FileOutputStream(srcFile);
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(fileOutputStream);
        registryXML.serialize(writer);

    } catch (FileNotFoundException e) {
        throw new FileNotFoundException("Registry.xml file not found" + e.getMessage());

    } catch (XMLStreamException e) {
        throw new XMLStreamException("XML stream exception" + e.getMessage());

    } catch (IOException e) {
        throw new IOException("XML stream exception" + e.getMessage());

    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (writer != null) {
            writer.flush();
        }
    }
}