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.wso2.carbon.humantask.core.mgt.services.HumanTaskPackageManagementSkeleton.java

private OMElement createTaskDefOMElement(File humanTaskDefFile) throws PackageManagementException {

    XMLStreamReader reader;//from   w  ww . j a  v  a  2  s .  c  o m
    FileInputStream fis = null;
    OMElement humanTaskDefinition;
    try {
        fis = new FileInputStream(humanTaskDefFile);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        reader = xif.createXMLStreamReader(fis);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        humanTaskDefinition = builder.getDocumentElement();
        humanTaskDefinition.build();
    } catch (XMLStreamException e) {
        String errMsg = "XML stream reader exception: " + humanTaskDefFile.getAbsolutePath();
        log.error(errMsg, e);
        throw new PackageManagementException(errMsg, e);
    } catch (FileNotFoundException e) {
        String errMsg = "HT File reading exception: " + humanTaskDefFile.getAbsolutePath();
        log.error(errMsg, e);
        throw new PackageManagementException(errMsg, e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.warn("Cannot close file input stream.", e);
            }
        }
    }

    return humanTaskDefinition;
}

From source file:org.wso2.carbon.localentry.service.LocalEntryAdmin.java

private OMElement nonCoalescingStringToOm(String xmlStr) throws XMLStreamException {
    StringReader strReader = new StringReader(xmlStr);
    XMLInputFactory xmlInFac = XMLInputFactory.newInstance();
    //Non-Coalescing parsing
    xmlInFac.setProperty("javax.xml.stream.isCoalescing", false);

    XMLStreamReader parser = xmlInFac.createXMLStreamReader(strReader);
    StAXOMBuilder builder = new StAXOMBuilder(parser);

    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.localentry.ui.client.LocalEntryAdminClient.java

public static OMElement nonCoalescingStringToOm(String xmlStr) throws XMLStreamException {
    StringReader strReader = new StringReader(xmlStr);
    XMLInputFactory xmlInFac = XMLInputFactory.newInstance();
    //Non-Coalescing parsing
    xmlInFac.setProperty("javax.xml.stream.isCoalescing", false);

    XMLStreamReader parser = xmlInFac.createXMLStreamReader(strReader);
    StAXOMBuilder builder = new StAXOMBuilder(parser);

    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.mediation.library.util.LocalEntryUtil.java

public static OMElement nonCoalescingStringToOm(String xmlStr) throws XMLStreamException {
    StringReader strReader = new StringReader(xmlStr);
    XMLInputFactory xmlInFac = XMLInputFactory.newInstance();
    // Non-Coalescing parsing
    xmlInFac.setProperty("javax.xml.stream.isCoalescing", false);

    XMLStreamReader parser = xmlInFac.createXMLStreamReader(strReader);
    StAXOMBuilder builder = new StAXOMBuilder(parser);

    return builder.getDocumentElement();
}

From source file:org.wso2.carbon.registry.sample.extensions.handlers.AssociationHandler.java

public void put(RequestContext requestContext) throws RegistryException {

    if (!CommonUtil.isUpdateLockAvailable()) {
        return;//from  w  ww.  ja  v  a  2s.c  o  m
    }
    CommonUtil.acquireUpdateLock();
    try {
        String resourceContent;
        String resourcePath = requestContext.getResourcePath().getPath();
        OMElement resourceElement;
        // Derive registry resource content
        Object resourceContentObj = requestContext.getResource().getContent();
        if (resourceContentObj instanceof String) {
            resourceContent = (String) resourceContentObj;
        } else {
            resourceContent = new String((byte[]) resourceContentObj);
        }
        try {
            // Initialize XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Reading xml content from string resourceContent and creating XMLStreamReader
            XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(resourceContent));
            StAXOMBuilder builder = new StAXOMBuilder(reader);
            resourceElement = builder.getDocumentElement();
        } catch (XMLStreamException e) {
            String msg = "An error occurred " + "while reading the resource at " + resourcePath + ".";
            throw new RegistryException(msg, e);
        }
        // Get the first OMElement child with name 'overview'
        OMElement elementOverview = getFirstChild(resourceElement, "overview");
        // Get the first OMElement child with name 'wsdl' and appending absolute path prefix.
        String wsdlPath = "/_system/governance" + getFirstChild(elementOverview, "wsdl").getText();
        Registry registry = requestContext.getRegistry();
        registry.put(resourcePath, requestContext.getResource());

        // Creating associations. wsdl DEPENDS on application
        registry.addAssociation(wsdlPath, requestContext.getResourcePath().getCompletePath(),
                CommonConstants.DEPENDS);
        // Creating associations. application USED_BY wsdl
        registry.addAssociation(requestContext.getResourcePath().getCompletePath(), wsdlPath,
                CommonConstants.USED_BY);

    } catch (RegistryException e) {
        String msg = "Error occurred creating associations for "
                + requestContext.getResourcePath().getCompletePath();
        log.error(msg, e);
    } finally {
        CommonUtil.releaseUpdateLock();
    }
}

From source file:org.wso2.carbon.registry.samples.taggingHandler.CustomTagServiceHandler.java

public void put(RequestContext requestContext) throws RegistryException {

    if (!CommonUtil.isUpdateLockAvailable()) {
        return;/*from  w w  w . java  2  s  .c  om*/
    }
    CommonUtil.acquireUpdateLock();
    try {
        String resourceContent;
        String resourcePath = requestContext.getResourcePath().getPath();
        OMElement resourceElement;
        // Derive registry resource content
        Object resourceContentObj = requestContext.getResource().getContent();
        if (resourceContentObj instanceof String) {
            resourceContent = (String) resourceContentObj;
        } else {
            resourceContent = new String((byte[]) resourceContentObj);
        }
        try {
            // Initialize XMLInputFactory
            XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            // Reading xml content from string resourceContent and creating XMLStreamReader
            XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(resourceContent));
            StAXOMBuilder builder = new StAXOMBuilder(reader);
            resourceElement = builder.getDocumentElement();
        } catch (XMLStreamException e) {
            String msg = "An error occurred " + "while reading the resource at " + resourcePath + ".";
            throw new RegistryException(msg, e);
        }
        // Get the first OMElement child with name 'overview'
        OMElement elementOverview = getFirstChild(resourceElement, "overview");
        // Get the first OMElement child with name 'namespace'
        String namespace = getFirstChild(elementOverview, "namespace").getText();
        Registry registry = requestContext.getRegistry();
        // Add the resource namespace as the tag
        registry.applyTag(resourcePath, namespace);
    } catch (RegistryException e) {
        String msg = "Error occurred in TaggingHandler sample when applying namespace tag to the service";
        log.error(msg, e);
    } finally {
        CommonUtil.releaseUpdateLock();
    }
}

From source file:org.wso2.carbon.reporting.core.services.DBReportingService.java

private String[] getSubreportNames(String template) throws XMLStreamException, JaxenException {
    InputStream is = new ByteArrayInputStream(template.getBytes());
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(is);
    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMDocument document = builder.getDocument();
    OMElement documentElement = document.getOMDocumentElement();

    AXIOMXPath xpathExpression = new AXIOMXPath("//a:subreport");
    xpathExpression.addNamespace("a", "http://jasperreports.sourceforge.net/jasperreports");
    List nodeList = xpathExpression.selectNodes(documentElement);

    ArrayList<String> repNames = new ArrayList<String>();
    if (nodeList == null | nodeList.size() < 1) {
        return null;
    } else {//w w  w .  j a v a  2 s . c  o  m
        for (Object obj : nodeList) {
            if (obj instanceof OMElement) {
                OMElement subReport = (OMElement) obj;
                Iterator iterator = subReport.getChildrenWithLocalName("subreportExpression");
                OMElement element = (OMElement) iterator.next();

                String reportName = element.getText();
                reportName = reportName.replaceAll("\\{", "");
                reportName = reportName.replaceAll("\\}", "");
                reportName = reportName.replaceAll("\\$P", "");
                reportName = reportName.replaceAll("\\$F", "");
                repNames.add(reportName);
            }
        }
        String[] names = new String[repNames.size()];
        names = repNames.toArray(names);
        return names;
    }
}

From source file:org.wso2.carbon.reporting.core.utils.CommonUtil.java

/**
 * @param componentName  name of the report requesting component
 * @param reportTemplate name of the template
 * @param registry       Registry//from w  w  w .  j ava 2s  .  c om
 * @return report template as string
 * @throws ReportingException if failed to get report template
 */
public static String getReportResources(String componentName, String reportTemplate, Registry registry)
        throws ReportingException, XMLStreamException {

    String jrXmlPath;
    if (reportTemplate != null && !"".equals(reportTemplate)) {
        jrXmlPath = ReportConstants.JRXML_PATH + RegistryConstants.PATH_SEPARATOR + reportTemplate + ".jrxml";
    } else {
        throw new ReportingException("Can't generate report without template ");
    }

    Resource resource;
    InputStream reportDefinitionOmStream;

    StAXOMBuilder stAXOMBuilder;
    OMElement reportJrXmlOmElement;
    try {

        resource = registry.get(RegistryUtils.getRelativePathToOriginal(jrXmlPath,
                RegistryConstants.CONFIG_REGISTRY_BASE_PATH));
        reportDefinitionOmStream = resource.getContentStream();

    } catch (RegistryException e) {
        throw new ReportingException(reportTemplate + " getting  failed from " + componentName, e);
    }
    XMLInputFactory xmlInputFactory;
    XMLStreamReader xmlStreamReader = null;
    xmlInputFactory = XMLInputFactory.newInstance();
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(reportDefinitionOmStream);
        stAXOMBuilder = new StAXOMBuilder(xmlStreamReader);
        reportJrXmlOmElement = stAXOMBuilder.getDocumentElement();
        return reportJrXmlOmElement.toString();
    } catch (XMLStreamException e) {
        throw new ReportingException(reportTemplate + " getting  failed from " + componentName, e);
    } finally {
        if (xmlStreamReader != null) {
            xmlStreamReader.close();
        }
    }

}

From source file:org.wso2.carbon.reporting.template.core.handler.metadata.AbstractMetaDataHandler.java

private void loadXML(Resource resource) throws ReportingException {

    InputStream reportDefinitionOmStream;

    StAXOMBuilder stAXOMBuilder;//from  w  ww  .  jav  a  2 s  .  com
    try {
        reportDefinitionOmStream = resource.getContentStream();
    } catch (RegistryException e) {
        throw new ReportingException("failed to get the metadata xml", e);
    }
    XMLInputFactory xmlInputFactory;
    XMLStreamReader xmlStreamReader = null;
    xmlInputFactory = XMLInputFactory.newInstance();
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(reportDefinitionOmStream);
        stAXOMBuilder = new StAXOMBuilder(xmlStreamReader);
        reportsElement = stAXOMBuilder.getDocumentElement();
        reportsElement.build();
    } catch (XMLStreamException e) {
        throw new ReportingException("failed to get the metadata xml", e);
    }

}

From source file:org.wso2.carbon.reporting.template.core.handler.metadata.MetadataFinder.java

private static void loadXML(Resource resource) throws ReportingException {

    InputStream reportDefinitionOmStream;

    StAXOMBuilder stAXOMBuilder;/*  w w  w  .  ja v  a  2 s . c  o  m*/
    try {
        reportDefinitionOmStream = resource.getContentStream();
    } catch (RegistryException e) {
        log.error("failed to get the metadata xml", e);
        throw new ReportingException("failed to get the metadata xml", e);
    }
    XMLInputFactory xmlInputFactory;
    XMLStreamReader xmlStreamReader = null;
    xmlInputFactory = XMLInputFactory.newInstance();
    try {
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(reportDefinitionOmStream);
        stAXOMBuilder = new StAXOMBuilder(xmlStreamReader);
        reportsElement = stAXOMBuilder.getDocumentElement();
        reportsElement.build();
    } catch (XMLStreamException e) {
        throw new ReportingException("failed to get the metadata xml", e);
    }

}