Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

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

Introduction

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

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:org.springmodules.remoting.xmlrpc.stax.AbstractStaxXmlRpcParser.java

/**
 * Creates a new XML stream reader from the given InputStream.
 * //from   www  . j a v a2s.co m
 * @param inputStream
 *          the InputStream used as source.
 * @return the created XML stream reader.
 * @throws XmlRpcParsingException
 *           if there are any errors during the parsing.
 */
protected final XMLStreamReader loadXmlReader(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    if (logger.isDebugEnabled()) {
        logger.debug("Using StAX implementation [" + factory + "]");
    }

    XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
    return reader;
}

From source file:org.sweble.wikitext.dumpreader.DumpReader.java

/**
 * The xerces UTF8Reader is broken. If the xerces XML parser is given an
 * input stream, it will instantiate a reader for the encoding found in the
 * XML file, a UTF8Reader in case of an UTF8 encoded XML file. Sadly, this
 * UTF8Reader crashes for certain input (not sure why exactly).
 * //from www .j  a v  a 2  s.  c  o m
 * On the other hand, when given a reader, which is forced to work with a
 * certain encoding, the xerces XML parser does not have this freedom and
 * will apparently process Wikipedia dumps just fine.
 * 
 * Therefore, in case you have trouble to parse a XML file, by specifying an
 * encoding, you force the use of a Reader and can circumvent the crash.
 */
private XMLStreamReader getXmlStreamReader(Charset encoding)
        throws FactoryConfigurationError, XMLStreamException, UnsupportedEncodingException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

    if (encoding != null) {
        InputStreamReader isr = new InputStreamReader(decompressedInputStream, encoding);
        return xmlInputFactory.createXMLStreamReader(isr);
    } else {
        return xmlInputFactory.createXMLStreamReader(decompressedInputStream);
    }
}

From source file:org.wso2.carbon.apimgt.impl.APIProviderImplTest.java

private static OMElement buildOMElement(InputStream inputStream) throws APIManagementException {
    XMLStreamReader parser;// ww w .  ja  v a 2  s. c  o  m
    StAXOMBuilder builder;
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        parser = factory.createXMLStreamReader(inputStream);
        builder = new StAXOMBuilder(parser);
    } catch (XMLStreamException e) {
        String msg = "Error in initializing the parser.";
        throw new APIManagementException(msg, e);
    }

    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRestClientImpl.java

/**
 * This method read XML content from the given stream
 *
 * @param inputStream Stream to read XML
 * @return XML represented by OMElement/*from  w  w w. j  a v a 2 s. c  om*/
 * @throws Exception throws generic exception
 */
public static OMElement buildOMElement(InputStream inputStream) throws Exception {
    XMLStreamReader parser;
    try {
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        parser = xmlInputFactory.createXMLStreamReader(inputStream);
    } catch (XMLStreamException e) {
        String msg = "Error in initializing the parser to build the OMElement.";
        log.error(msg, e);
        throw new Exception(msg, e);
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.appfactory.core.dao.ApplicationDAO.java

/**
 * Method to add an application artifact to registry
 *
 * @param info               information of application, that need to be added to the registry
 * @param lifecycleAttribute the name of lifecycle, that should be associated with the application
 * @return registry path of the application artifact
 * @throws AppFactoryException//from w  w w .  j av a  2 s. co  m
 */
public String addApplicationArtifact(String info, String lifecycleAttribute) throws AppFactoryException {
    RegistryUtils.recordStatistics(AppFactoryConstants.RXT_KEY_APPINFO_APPLICATION, info, lifecycleAttribute);
    String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
    try {
        UserRegistry userRegistry = GovernanceUtil.getUserRegistry();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, true);

        XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(info));
        GenericArtifactManager manager = new GenericArtifactManager(userRegistry,
                AppFactoryConstants.RXT_KEY_APPINFO_APPLICATION);
        GenericArtifact artifact = manager
                .newGovernanceArtifact(new StAXOMBuilder(reader).getDocumentElement());

        // want to save original content, so set content here
        artifact.setContent(info.getBytes());

        manager.addGenericArtifact(artifact);
        if (lifecycleAttribute != null) {
            String lifecycle = artifact.getAttribute(lifecycleAttribute);
            if (lifecycle != null) {
                artifact.attachLifecycle(lifecycle);
            }
        }
        return AppFactoryConstants.REGISTRY_GOVERNANCE_PATH + artifact.getPath();
    } catch (Exception e) {
        log.error("Error while adding application artifact in tenant : " + tenantDomain);
        throw new AppFactoryException(e);
    }
}

From source file:org.wso2.carbon.appfactory.core.governance.RxtManager.java

public String addArtifact(String key, String info, String lifecycleAttribute) throws AppFactoryException {
    RegistryUtils.recordStatistics(key, info, lifecycleAttribute);
    try {//from w  ww  .  j av a  2 s .  c  o m
        UserRegistry userRegistry = getUserRegistry();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        factory.setProperty(XMLInputFactory.IS_COALESCING, true);
        XMLStreamReader reader = null;
        reader = factory.createXMLStreamReader(new StringReader(info));
        GenericArtifactManager manager = new GenericArtifactManager(userRegistry, key);
        GenericArtifact artifact = manager
                .newGovernanceArtifact(new StAXOMBuilder(reader).getDocumentElement());

        // want to save original content, so set content here
        artifact.setContent(info.getBytes());
        manager.addGenericArtifact(artifact);
        if (lifecycleAttribute != null) {
            String lifecycle = artifact.getAttribute(lifecycleAttribute);
            if (lifecycle != null) {
                artifact.attachLifecycle(lifecycle);
            }
        }
        return "/_system/governance" + artifact.getPath();
    } catch (Exception e) {
        String errorMsg = "Error adding artifact";
        throw new AppFactoryException(errorMsg, e);
    }
}

From source file:org.wso2.carbon.appfactory.repository.provider.common.AbstractRepositoryProvider.java

protected Repository getRepositoryFromStream(InputStream responseBodyAsStream) throws RepositoryMgtException {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = null;
    Repository repository = new Repository();
    try {//from  w  w  w.  j a v  a2  s .  c  o  m
        reader = xif.createXMLStreamReader(responseBodyAsStream);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        OMElement rootElement = builder.getDocumentElement();
        if (REPOSITORY_XML_ROOT_ELEMENT.equals(rootElement.getLocalName())) {
            Iterator elements = rootElement.getChildElements();
            while (elements.hasNext()) {
                Object object = elements.next();
                if (object instanceof OMElement) {
                    OMElement element = (OMElement) object;
                    if (REPOSITORY_URL_ELEMENT.equals(element.getLocalName())) {
                        repository.setUrl(element.getText());
                        break;
                    }
                }
            }
        } else {
            String msg = "In the payload no repository information is found";
            log.error(msg);
            throw new RepositoryMgtException(msg);
        }
    } catch (XMLStreamException e) {
        String msg = "Error while reading the stream";
        log.error(msg, e);
        throw new RepositoryMgtException(msg, e);
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (XMLStreamException e) {
            String msg = "Error while serializing the payload";
            log.error(msg, e);
        }
    }
    return repository;
}

From source file:org.wso2.carbon.bam.service.data.publisher.internal.StatisticsServiceComponent.java

private OMElement getPublishingConfig() {
    String bamConfigPath = CarbonUtils.getEtcCarbonConfigDirPath() + File.separator
            + CommonConstants.BAM_CONFIG_XML;

    File bamConfigFile = new File(bamConfigPath);
    try {// w ww . ja v a2 s. c  o  m
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStream inputStream = new FileInputStream(bamConfigFile);
        XMLStreamReader reader = xif.createXMLStreamReader(inputStream);
        xif.setProperty("javax.xml.stream.isCoalescing", false);

        StAXOMBuilder builder = new StAXOMBuilder(reader);

        return builder.getDocument().getOMDocumentElement();
    } catch (FileNotFoundException e) {
        log.warn("No " + CommonConstants.BAM_CONFIG_XML + " found in " + bamConfigPath);
        return null;
    } catch (XMLStreamException e) {
        log.error("Incorrect format " + CommonConstants.BAM_CONFIG_XML + " file", e);
        return null;
    }
}

From source file:org.wso2.carbon.bpel.core.ode.integration.mgt.services.ProcessManagementServiceSkeleton.java

private OMElement getProcessDefinition(ProcessConf pConf) throws ProcessManagementException {
    if (pConf == null) {
        String errMsg = "Process configuration cannot be null.";
        log.error(errMsg);/*  ww  w  .  j  a  v a2  s . c  o  m*/
        throw new ProcessManagementException(errMsg);
    }

    String bpelDoc = pConf.getBpelDocument();
    List<File> files = pConf.getFiles();

    for (final File file : files) {
        if (file.getPath().endsWith(bpelDoc) || file.getPath().endsWith(bpelDoc.replaceAll("/", "\\\\"))) {
            XMLStreamReader reader;
            FileInputStream fis = null;
            OMElement bpelDefinition;
            try {
                fis = new FileInputStream(file);
                XMLInputFactory xif = XMLInputFactory.newInstance();
                reader = xif.createXMLStreamReader(fis);
                StAXOMBuilder builder = new StAXOMBuilder(reader);
                bpelDefinition = builder.getDocumentElement();
                bpelDefinition.build();
            } catch (XMLStreamException e) {
                String errMsg = "XML stream reader exception: " + file.getAbsolutePath();
                log.error(errMsg, e);
                throw new ProcessManagementException(errMsg, e);
            } catch (FileNotFoundException e) {
                String errMsg = "BPEL File reading exception: " + file.getAbsolutePath();
                log.error(errMsg, e);
                throw new ProcessManagementException(errMsg, e);
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        log.warn("Cannot close file input stream.", e);
                    }
                }
            }

            return bpelDefinition;
        }
    }

    String errMsg = "Process Definition for: " + pConf.getProcessId() + " not found";
    log.error(errMsg);
    throw new ProcessManagementException(errMsg);
}

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

@Override
public PlatformConfiguration getConfiguration(String resourcePath) throws ConfigurationManagementException {
    Resource resource;/*from w  ww  .java 2  s .  c om*/
    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);
    }
}