Example usage for javax.xml.stream XMLInputFactory newInstance

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

Introduction

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

Prototype

public static XMLInputFactory 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.cassandra.mgt.ui.CassandraAdminClientHelper.java

/**
 * Read cassandra-endpoint.xml/*from w  w  w.  j  a va 2 s  .c  o  m*/
 *
 * @return
 */
public static OMElement readCassandraEndpoints() {
    String carbonHome = System.getProperty(ServerConstants.CARBON_HOME);
    String path = carbonHome + File.separator + CASSANDRA_ENDPOINT_CONF;
    BufferedInputStream inputStream = null;
    try {
        File file = new File(path);
        if (!file.exists()) {
            log.info("There is no " + CASSANDRA_ENDPOINT_CONF + ". Using the default configuration");
            inputStream = new BufferedInputStream(new ByteArrayInputStream("<Cassandra/>".getBytes()));
        } else {
            inputStream = new BufferedInputStream(new FileInputStream(file));
        }
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        return builder.getDocumentElement();
    } catch (FileNotFoundException e) {
        log.error(CASSANDRA_ENDPOINT_CONF + "cannot be found in the path : " + path, e);
    } catch (XMLStreamException e) {
        log.error("Invalid XML for " + CASSANDRA_ENDPOINT_CONF + " located in " + "the path : " + path, e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException ingored) {
        }
    }
    return null;
}

From source file:org.wso2.carbon.cep.core.BucketDeployer.java

private OMElement getBucketOMElement(String path, File bucketFile) throws DeploymentException {
    OMElement bucketElement;/*from  w ww . j a va  2  s .  c  om*/
    BufferedInputStream inputStream = null;
    try {
        //read  and build the bucket from file
        inputStream = new BufferedInputStream(new FileInputStream(bucketFile));
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        bucketElement = builder.getDocumentElement();
        bucketElement.build();

    } catch (FileNotFoundException e) {
        String errorMessage = " .xml file cannot be found in the path : " + path;
        log.error(errorMessage, e);
        throw new DeploymentException(errorMessage, e);
    } catch (XMLStreamException e) {
        String errorMessage = "Invalid XML for " + bucketFile.getName() + " located in the path : " + path;
        log.error(errorMessage, e);
        throw new DeploymentException(errorMessage, e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            String errorMessage = "Can not close the input stream";
            log.error(errorMessage, e);
        }
    }
    return bucketElement;
}

From source file:org.wso2.carbon.cep.core.internal.builder.CEPServiceBuilder.java

/**
 * Helper method to load the event config
 *
 * @return OMElement representation of the event config
 *///w w w  . j  a va  2 s.  c  om
private static OMElement loadConfigXML() throws CEPConfigurationException {

    String carbonHome = System.getProperty(ServerConstants.CARBON_CONFIG_DIR_PATH);
    String path = carbonHome + File.separator + CEPConstants.CEP_CONF;

    // if the cep config file not exists then simply return null.
    File cepConfigFile = new File(path);
    if (!cepConfigFile.exists()) {
        return null;
    }

    BufferedInputStream inputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(new File(path)));
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement omElement = builder.getDocumentElement();
        omElement.build();
        return omElement;
    } catch (FileNotFoundException e) {
        String errorMessage = CEPConstants.CEP_CONF + "cannot be found in the path : " + path;
        log.error(errorMessage, e);
        throw new CEPConfigurationException(errorMessage, e);
    } catch (XMLStreamException e) {
        String errorMessage = "Invalid XML for " + CEPConstants.CEP_CONF + " located in the path : " + path;
        log.error(errorMessage, e);
        throw new CEPConfigurationException(errorMessage, e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            String errorMessage = "Can not close the input stream";
            log.error(errorMessage, e);
            throw new CEPConfigurationException(errorMessage, e);
        }
    }
}

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

/**
 * @param byteArrayOutStream/*from  w  w  w .  j  ava 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.databridge.core.internal.utils.DataBridgeCoreBuilder.java

public static OMElement loadXML(String path, String fileName) throws DataBridgeConfigurationException {
    BufferedInputStream inputStream = null;
    try {//from w w w. j  ava 2s  . c  o  m
        inputStream = new BufferedInputStream(new FileInputStream(new File(path)));
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement omElement = builder.getDocumentElement();
        omElement.build();
        return omElement;
    } catch (FileNotFoundException e) {
        String errorMessage = fileName + " cannot be found in the path : " + path;
        log.error(errorMessage, e);
        throw new DataBridgeConfigurationException(errorMessage, e);
    } catch (XMLStreamException e) {
        String errorMessage = "Invalid XML for " + fileName + " located in the path : " + path;
        log.error(errorMessage, e);
        throw new DataBridgeConfigurationException(errorMessage, e);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            String errorMessage = "Can not close the input stream";
            log.error(errorMessage, e);
        }
    }
}

From source file:org.wso2.carbon.datasource.MiscellaneousHelper.java

public static OMElement createOMElement(Properties properties) {
    if (log.isDebugEnabled()) {
        log.debug("Properties : " + properties);
    }/*w  ww . j ava  2  s.co m*/
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        properties.storeToXML(baos, "");
        String propertyS = new String(baos.toByteArray());
        String correctedS = propertyS.substring(propertyS.indexOf("<properties>"), propertyS.length());
        String inLined = "<!DOCTYPE properties   [\n" + "\n" + "<!ELEMENT properties ( comment?, entry* ) >\n"
                + "\n" + "<!ATTLIST properties version CDATA #FIXED \"1.0\">\n" + "\n"
                + "<!ELEMENT comment (#PCDATA) >\n" + "\n" + "<!ELEMENT entry (#PCDATA) >\n" + "\n"
                + "<!ATTLIST entry key CDATA #REQUIRED>\n" + "]>";
        XMLStreamReader reader = XMLInputFactory.newInstance()
                .createXMLStreamReader(new StringReader(inLined + correctedS));
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        return builder.getDocumentElement();

    } catch (XMLStreamException e) {
        handleException("Error Creating a OMElement from properties : " + properties, e);
    } catch (IOException e) {
        handleException("IOError Creating a OMElement from properties : " + properties, e);
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (IOException ignored) {
            }

        }
    }
    return null;
}

From source file:org.wso2.carbon.datasource.MiscellaneousHelper.java

public static OMElement getOMElement(InputStream input) {
    BufferedInputStream inputStream = new BufferedInputStream(input);
    try {/*from w  w  w . java  2s. com*/
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        return builder.getDocumentElement();

    } catch (Exception ignored) {
    } finally {
        try {
            inputStream.close();
        } catch (IOException ignored) {
        }

    }
    return null;

}

From source file:org.wso2.carbon.datasource.ui.DatasourceManagementClient.java

private static OMElement createOMElement(Properties properties) {

    if (log.isDebugEnabled()) {
        log.debug("Properties : " + properties);
    }//from   ww  w.  j  a  v  a  2  s .  c  o m
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        properties.storeToXML(baos, "");
        String propertyS = new String(baos.toByteArray());
        String correctedS = propertyS.substring(propertyS.indexOf("<properties>"), propertyS.length());
        String inLined = "<!DOCTYPE properties   [\n" + "\n" + "<!ELEMENT properties ( comment?, entry* ) >\n"
                + "\n" + "<!ATTLIST properties version CDATA #FIXED \"1.0\">\n" + "\n"
                + "<!ELEMENT comment (#PCDATA) >\n" + "\n" + "<!ELEMENT entry (#PCDATA) >\n" + "\n"
                + "<!ATTLIST entry key CDATA #REQUIRED>\n" + "]>";
        XMLStreamReader reader = XMLInputFactory.newInstance()
                .createXMLStreamReader(new StringReader(inLined + correctedS));
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        return builder.getDocumentElement();

    } catch (XMLStreamException e) {
        handleException("Error Creating a OMElement from properties : " + properties, e);
    } catch (IOException e) {
        handleException("IOError Creating a OMElement from properties : " + properties, e);
    } finally {
        if (baos != null) {
            try {
                baos.close();
            } catch (IOException ignored) {
            }

        }
    }
    return null;
}

From source file:org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl.java

@Override
public PlatformConfiguration getConfiguration(String resourcePath) throws ConfigurationManagementException {
    Resource resource;//from  www  .ja  va  2 s. co m
    try {
        resource = ConfigurationManagerUtil.getRegistryResource(resourcePath);
        if (resource != null) {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
            XMLStreamReader reader = factory
                    .createXMLStreamReader(new StringReader(new String((byte[]) resource.getContent(),
                            Charset.forName(ConfigurationManagerConstants.CharSets.CHARSET_UTF8))));

            JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (PlatformConfiguration) unmarshaller.unmarshal(reader);
        }
        return new PlatformConfiguration();
    } catch (JAXBException | XMLStreamException e) {
        throw new ConfigurationManagementException(
                "Error occurred while parsing the Tenant configuration : " + e.getMessage(), e);
    } catch (RegistryException e) {
        throw new ConfigurationManagementException(
                "Error occurred while retrieving the Registry resource of Tenant Configuration : "
                        + e.getMessage(),
                e);
    }
}

From source file:org.wso2.carbon.device.mgt.extensions.device.type.template.DeviceTypeManager.java

@Override
public PlatformConfiguration getConfiguration() throws DeviceManagementException {
    Resource resource;//from   ww w.  j a v a2  s . co  m
    try {
        resource = DeviceTypeUtils.getRegistryResource(deviceType);
        if (resource != null) {
            XMLInputFactory factory = XMLInputFactory.newInstance();
            factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
            factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
            XMLStreamReader reader = factory
                    .createXMLStreamReader(new StringReader(new String((byte[]) resource.getContent(),
                            Charset.forName(DeviceTypePluginConstants.CHARSET_UTF8))));

            JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (PlatformConfiguration) unmarshaller.unmarshal(reader);
        } else if (defaultPlatformConfiguration != null) {
            return defaultPlatformConfiguration;
        }
        return null;
    } catch (DeviceTypeMgtPluginException e) {
        throw new DeviceManagementException(
                "Error occurred while retrieving the Registry instance : " + e.getMessage(), e);
    } catch (JAXBException | XMLStreamException e) {
        throw new DeviceManagementException(
                "Error occurred while parsing the " + deviceType + " configuration : " + e.getMessage(), e);
    } catch (RegistryException e) {
        throw new DeviceManagementException("Error occurred while retrieving the Registry resource of "
                + deviceType + " Configuration : " + e.getMessage(), e);
    }
}