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.apimgt.everywhere.interceptor.utils.ThrottleUtils.java

public static OMNode lookup(String key) {
    try {/*from ww  w  .  j a va 2  s  .  co  m*/
        Resource resource = getResource(key);

        if (resource instanceof Collection || resource == null) {
            return null;
        }

        ByteArrayInputStream inputStream = null;
        Object content = resource.getContent();
        if (content instanceof String) {
            inputStream = new ByteArrayInputStream(content.toString().getBytes());
        } else if (content instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) content);
        }

        OMNode result = null;
        try {
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            result = builder.getDocumentElement();

        } catch (OMException ignored) {
            // result = readNonXML(resource);

        } catch (XMLStreamException ignored) {
            //result = readNonXML(resource);

        } catch (Exception e) {
            // a more general exception(e.g. a Runtime exception if the XML doc has an
            // external DTD deceleration and if not connected to internet) which in case
            // just log for debugging
            log.error("Error while reading the resource '" + key + "'", e);
        } finally {
            try {
                resource.discard();
                if (result != null && result.getParent() != null) {
                    result.detach();
                    OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory());
                    parent.addChild(result);
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("Error while closing the input stream", e);
            }
        }
        return result;

    } catch (RegistryException e) {
        handleException("Error while fetching the resource " + key, e);
    }

    return null;
}

From source file:org.wso2.carbon.apimgt.hostobjects.AssetStoreHostObject.java

private static OMElement buildLifecycleConfiguration(Scriptable thisObj, String lifecycleName)
        throws RegistryException {
    OMElement configuration = null;//w w w  . j  a  va  2 s.  co m
    if (lifecycleName == null) {
        return null;
    }
    Object lifecycleContent = getRootRegistry(thisObj)
            .get(RegistryConstants.CONFIG_REGISTRY_BASE_PATH
                    + "/repository/components/org.wso2.carbon.governance/lifecycles/" + lifecycleName)
            .getContent();
    String temp;
    if (lifecycleContent instanceof String) {
        temp = (String) lifecycleContent;
    } else {
        temp = RegistryUtils.decodeBytes((byte[]) lifecycleContent);
    }
    try {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(temp));
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        AXIOMXPath xPath = new AXIOMXPath("//ns:scxml");
        xPath.addNamespace("ns", "http://www.w3.org/2005/07/scxml");
        configuration = (OMElement) ((ArrayList) xPath.evaluate(builder.getDocumentElement())).get(0);
    } catch (Exception e) {
        log.error("Error in parsing the lifecycle configuration for " + lifecycleName + ".", e);
    }
    return configuration;
}

From source file:org.wso2.carbon.apimgt.hostobjects.AssetStoreHostObject.java

private static OMElement buildLifecycleHistory(Scriptable thisObj, String path) throws RegistryException {
    if (!getRegistry(thisObj).resourceExists(path)) {
        return null;
    }/*from  w  ww . j  a  v a  2 s  .  c o m*/
    Object lifecycleContent = getRegistry(thisObj).get(path).getContent();
    String temp;
    if (lifecycleContent instanceof String) {
        temp = (String) lifecycleContent;
    } else {
        temp = RegistryUtils.decodeBytes((byte[]) lifecycleContent);
    }
    try {
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(temp));
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        return builder.getDocumentElement();
    } catch (Exception e) {
        log.error("Error in parsing the lifecycle history at " + path + ".", e);
    }
    return null;
}

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

private static OMElement buildOMElement(InputStream inputStream) throws APIManagementException {
    XMLStreamReader parser;//  ww  w . j  a v a 2 s .  co 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.interceptor.utils.ThrottleUtils.java

public static OMNode lookup(String key) {
    try {/*from  ww w.j  a v  a  2  s.  com*/
        Resource resource = getResource(key);

        if (resource instanceof Collection || resource == null) {
            return null;
        }

        ByteArrayInputStream inputStream = null;
        Object content = resource.getContent();
        if (content instanceof String) {
            inputStream = new ByteArrayInputStream(content.toString().getBytes(Charset.defaultCharset()));
        } else if (content instanceof byte[]) {
            inputStream = new ByteArrayInputStream((byte[]) content);
        }

        OMNode result = null;
        try {
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            result = builder.getDocumentElement();

        } catch (OMException ignored) {
            // result = readNonXML(resource);

        } catch (XMLStreamException ignored) {
            //result = readNonXML(resource);

        } catch (Exception e) {
            // a more general exception(e.g. a Runtime exception if the XML doc has an
            // external DTD deceleration and if not connected to internet) which in case
            // just log for debugging
            log.error("Error while reading the resource '" + key + "'", e);
        } finally {
            try {
                resource.discard();
                if (result != null && result.getParent() != null) {
                    result.detach();
                    OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory());
                    parent.addChild(result);
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                log.error("Error while closing the input stream", e);
            }
        }
        return result;

    } catch (RegistryException e) {
        handleException("Error while fetching the resource " + key, e);
    }

    return null;
}

From source file:org.wso2.carbon.apimgt.usage.client.impl.APIUsageStatisticsRdbmsClientImpl.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 va2 s  .c  om*/
 * @throws Exception throws generic exception
 */
public static OMElement buildOMElement(InputStream inputStream) throws Exception {
    XMLStreamReader parser;
    try {
        parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
    } catch (XMLStreamException e) {
        String msg = "Error in initializing the parser to build the OMElement.";
        throw new Exception(msg, e);
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    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/* w w w .j a va2 s.  c o m*/
 * @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//w  w w .  j av a2 s . c  o  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 w w .j  a  v  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 {//w ww  .ja  v  a 2s. com
        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;
}