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.appmgt.mobile.utils.MobileConfigurations.java

private MobileConfigurations() {
    XMLStreamReader parser = null;
    try {/*from w w  w . ja v a  2  s .  c  o  m*/
        parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(CONFIG_FILE_PATH));
    } catch (XMLStreamException e) {
        log.error("XML Parsing issue :" + e.getMessage());
    } catch (FileNotFoundException e) {
        log.error("App Manager XML not found :" + e.getMessage());
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    documentElement = builder.getDocumentElement();
    mobileConfElement = new QName("MobileAppsConfiguration");

}

From source file:org.wso2.carbon.appmgt.services.api.v1.apps.common.ServicesApiConfigurations.java

private ServicesApiConfigurations() {
    XMLStreamReader parser = null;
    try {//from  w  ww . j  a v a  2s  .c  o  m
        parser = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(CONFIG_FILE_PATH));
    } catch (XMLStreamException e) {
        log.error("XML Parsing issue :" + e.getMessage());
    } catch (FileNotFoundException e) {
        log.error("App Manager XML not found :" + e.getMessage());
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    documentElement = builder.getDocumentElement();
    mobileConfElement = new QName("ServicesAPI");

}

From source file:org.wso2.carbon.appmgt.usage.client.APIUsageStatisticsClient.java

public static OMElement buildOMElement(InputStream inputStream) throws Exception {
    XMLStreamReader parser;/*from  ww w .j  a  v a  2  s  . co m*/
    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);
    } finally {
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.appmgt.usage.client.impl.AppUsageStatisticsRdbmsClient.java

private static OMElement buildOMElement(InputStream inputStream) throws Exception {
    XMLStreamReader parser;//w w  w. ja va2  s. c  om
    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);
    } finally {
    }
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.artifact.search.client.ArtifactSearchService.java

public String[] getResultArtifacts(final String serviceName, final String version, final String mediaType)
        throws AxisFault {

    List<String> endpoints = new ArrayList<String>();
    try {/*from   w  ww . j  a  v a  2 s .co m*/

        Map<String, String> listMap = new HashMap<String, String>();
        // Create the search attribute map
        if (serviceName != null) {
            listMap.put(SearchConstants.SERVICE_NAME, serviceName.toLowerCase());
        }
        if (version != null) {
            listMap.put(SearchConstants.SERVICE_VERSION, version.toLowerCase());
        }
        if (mediaType != null && !("".equals(mediaType))) {
            listMap.put(SearchConstants.MEDIA_TYPE, mediaType.toLowerCase());
        } else {
            listMap.put(SearchConstants.MEDIA_TYPE, SearchConstants.DEFAULT_SERVICE_MEDIA_TYPE);
        }

        for (ResourceData resourceData : searchHolder.getAttributeIndexingService().search(listMap)) {
            Registry govRegistry = CarbonContext.getThreadLocalCarbonContext()
                    .getRegistry(RegistryType.USER_GOVERNANCE);
            String path = resourceData.getResourcePath()
                    .substring(RegistryConstants.GOVERNANCE_REGISTRY_BASE_PATH.length());
            Resource resource = govRegistry.get(path);

            XMLStreamReader streamReader = XMLInputFactory.newInstance()
                    .createXMLStreamReader(resource.getContentStream());
            StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(streamReader);
            OMElement documentElement = stAXOMBuilder.getDocumentElement();
            OMElement endpointsList = documentElement
                    .getFirstChildWithName(new QName(SearchConstants.ENDPOINT_NAMESPACE,
                            SearchConstants.ENDPOINT_LOCALNAME, SearchConstants.ENDPOINT_PREFIX));
            Iterator endpointIter = endpointsList.getChildElements();
            while (endpointIter.hasNext()) {
                OMElement endpoint = (OMElement) endpointIter.next();
                endpoints.add(endpoint.getText());
            }
        }
    } catch (RegistryException ex) {
        log.error("Failed to returnServices ", ex);
    } catch (XMLStreamException e) {
        log.error("Failed to returnServices ", e);
    }

    return endpoints.toArray(new String[endpoints.size()]);
}

From source file:org.wso2.carbon.automation.extensions.jmeter.JMeterTestManager.java

private JMeterResult resultValidator(String fileName) throws Exception {

    JMeterResult result = null;/*ww  w.  j a  v a2  s . c  om*/
    XMLStreamReader parser = null;
    FileInputStream inputStream = null;
    File file = new File(fileName);
    List<String> assertionFailureList;
    List<String> errorList;

    if (file.exists()) {
        try {
            inputStream = new FileInputStream(file);
            parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(file);
            doc.getDocumentElement().normalize();

            errorList = new ArrayList<String>();
            result = new JMeterResult();
            assertionFailureList = new ArrayList<String>();

            NodeList nodeList = doc.getElementsByTagName("httpSample");

            for (int temp = 0; temp < nodeList.getLength(); temp++) {

                String responseMessage;
                String label;
                String name = null;
                String error = null;
                String failure = null;
                String failureMessage = null;

                Node node = nodeList.item(temp);

                responseMessage = node.getAttributes().getNamedItem("s").getTextContent();
                label = node.getAttributes().getNamedItem("lb").getTextContent();

                if ("false".equalsIgnoreCase(responseMessage)) {
                    result.increaseErrorCount();
                    String errorMessage = label + " > " + responseMessage;
                    if (!errorList.contains(errorMessage)) {
                        errorList.add(errorMessage);
                    }
                }

                if (node.getNodeType() == Node.ELEMENT_NODE) {

                    Element element = (Element) node;

                    if (element.getElementsByTagName("name").getLength() != 0) {
                        name = element.getElementsByTagName("name").item(0).getTextContent();
                    }
                    if (element.getElementsByTagName("error").getLength() != 0) {
                        error = element.getElementsByTagName("error").item(0).getTextContent();
                    }
                    if (element.getElementsByTagName("failure").getLength() != 0) {
                        failure = element.getElementsByTagName("failure").item(0).getTextContent();
                    }
                    if (element.getElementsByTagName("failureMessage").getLength() != 0) {
                        failureMessage = element.getElementsByTagName("failureMessage").item(0)
                                .getTextContent();
                    }

                    if ("true".equalsIgnoreCase(failure) || "true".equalsIgnoreCase(error)) {
                        result.increaseFailureCount();
                        String assertionFailure = label + " : " + name + " > " + failureMessage;
                        if (!assertionFailureList.contains(assertionFailure)) {
                            assertionFailureList.add(assertionFailure);
                        }
                    }
                }
            }

            result.setErrorList(errorList);
            result.setAssertList(assertionFailureList);
        } finally {

            if (parser != null) {
                parser.close();
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    ///ignore
                }
            }
        }
    } else {
        throw new FileNotFoundException("Result File is not Created");
    }
    return result;
}

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  w  w.j av  a2s .com*/
        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);/*from  w  w w  .  j a  v  a  2  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.bpel.ui.bpel2svg.impl.BPELImpl.java

/**
 * Converts the bpel process definition to an omElement which is how the AXIS2 Object Model (AXIOM) represents an
 * XML//from  w  ww . java 2 s  . c  om
 * element
 *
 * @param bpelStr bpel process definition needed to create the SVG
 * @return omElement
 */
public OMElement load(String bpelStr) {
    try {
        /*Creates a new instance of the XmlStreamReader class for the specified String input i.e. the bpel
        process definition
          using the StringReader class which enables you to turn an ordinary String into a Reader.
          This is useful if you have data as a String but need to pass that String to a component that only
          accepts a Reader.
        */
        parser = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(bpelStr));
        /*The instance of XmlStreamReader created is passed to the StAXOMBuilder which produces a pure XML
        infoset compliant object
         model which conatins the bpel process definition
         */
        builder = new StAXOMBuilder(parser);
        //The XML object created by the StAXOMBuilder is used to build an OMElement that is added to an existing
        // OM tree
        bpelElement = builder.getDocumentElement();
        //OmElement containing the bpel process definition is returned
        return bpelElement;
    } catch (XMLStreamException e) {
        log.error("XMLStreamReader creation failed", e);
        throw new NullPointerException("Document Element is NULL");
    }
}

From source file:org.wso2.carbon.cassandra.dataaccess.internal.DataAccessServiceDSComponent.java

/**
 * Helper method to load the cassandra server config
 *
 * @return OMElement representation of the cep config
 *//*from  w ww.  j av  a  2 s. c o m*/
private OMElement loadConfigXML() {
    BufferedInputStream inputStream = null;
    try {
        File file = new File(HECTOR_CONFIG);
        if (!file.exists()) {
            log.info("Cannot locate '" + HECTOR_CONFIG + "'. Using the default " + "configuration");
            inputStream = new BufferedInputStream(
                    new ByteArrayInputStream("<HectorConfiguration/>".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) {
        throw new DataAccessComponentException("Cannot locate '" + HECTOR_CONFIG + "'", e, log);
    } catch (XMLStreamException e) {
        throw new DataAccessComponentException("Invalid XML configuration found for " + HECTOR_CONFIG + "", e,
                log);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException ignored) {
        }
    }
}