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.apache.stratos.account.mgt.ui.utils.Util.java

public static HttpServletRequest readIntermediateData(HttpServletRequest request, String data) {
    try {//from  w ww . j  a  v  a 2 s  . co  m
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(data));
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildElements();
        while (it.hasNext()) {
            OMElement element = (OMElement) it.next();
            if ("admin".equals(element.getLocalName())) {
                request.setAttribute("admin", element.getText());
            } else if ("email".equals(element.getLocalName())) {
                request.setAttribute("email", element.getText());
            } else if ("tenantDomain".equals(element.getLocalName())
                    && request.getAttribute("tenantDomain") == null) {
                request.setAttribute("tenantDomain", element.getText());
            } else if ("confirmationKey".equals(element.getLocalName())) {
                request.setAttribute("confirmationKey", element.getText());
            }
        }
    } catch (Exception e) {
        log.error("Error parsing xml", e);
    }
    return request;
}

From source file:org.apache.stratos.autoscaler.policy.deployers.AbstractPolicyReader.java

protected OMElement getDocument() throws Exception {
    fStream = new FileInputStream(file);
    XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(fStream);
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    return builder.getDocumentElement();
}

From source file:org.apache.stratos.common.util.CommonUtil.java

/**
 * builds the OMElement from the given inputStream
 *
 * @param inputStream, given input - inputStream
 * @return OMElement//from  w ww.java  2  s  .  c o m
 * @throws Exception, if building OMElement from the inputStream failed.
 */
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.";
        log.error(msg, e);
        throw new Exception(msg, e);
    }

    // create the builder
    StAXOMBuilder builder = new StAXOMBuilder(parser);
    // get the root element (in this case the envelope)

    return builder.getDocumentElement();
}

From source file:org.apache.stratos.common.util.CommonUtil.java

/**
 * Loads the given Stratos Configuration file.
 *
 * @param configFilename Name of the configuration file
 * @return the stratos configuration data.
 *///  w w  w.j av a  2s .  co m
private static StratosConfiguration loadStratosConfiguration(String configFilename) {
    StratosConfiguration config = new StratosConfiguration();
    File configFile = new File(configFilename);
    if (configFile.exists()) {
        FileInputStream inputStream = null;
        try {
            inputStream = new FileInputStream(configFile);
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement documentElement = builder.getDocumentElement();
            Iterator it = documentElement.getChildElements();
            while (it.hasNext()) {
                OMElement element = (OMElement) it.next();

                if ("DisableTenantManagementEmails".equals(element.getLocalName())) {
                    String disableEmails = element.getText();
                    // by default, make the email validation mandatory.
                    boolean isEmailsDisabled = true;
                    if (disableEmails.trim().equalsIgnoreCase("false")) {
                        isEmailsDisabled = false;
                    }
                    config.setEmailsDisabled(isEmailsDisabled);
                }
                // Checks whether Email Validation is mandatory to log in and use the registered
                // tenants.
                else if ("EmailValidationMandatoryForLogin".equals(element.getLocalName())) {
                    String emailValidation = element.getText();
                    //by default, make the email validation not mandatory.
                    boolean isEmailValidationRequired = false;
                    if (emailValidation.trim().equalsIgnoreCase("true")) {
                        isEmailValidationRequired = true;
                    }
                    config.setEmailValidationRequired(isEmailValidationRequired);
                } else if ("ChargeOnRegistration".equals(element.getLocalName())) {
                    String chargeOnRegistration = element.getText();
                    boolean isChargedOnRegistration = false;
                    if (chargeOnRegistration.trim().equalsIgnoreCase("true")) {
                        isChargedOnRegistration = true;
                    }
                    config.setChargeOnRegistration(isChargedOnRegistration);
                } else if ("NotificationEmail".equals(element.getLocalName())) {
                    config.setNotificationEmail(element.getText());
                } else if ("SuperAdminEmail".equals(element.getLocalName())) {
                    config.setSuperAdminEmail(element.getText());
                } else if ("TenantActivationModerated".equals(element.getLocalName())) {
                    String isTenantActivationModerated = element.getText();
                    boolean tenantActivationModerated = false;
                    if (isTenantActivationModerated.trim().equalsIgnoreCase("true")) {
                        tenantActivationModerated = true;
                    }
                    config.setTenantActivationModerated(tenantActivationModerated);
                }
                //Checks whether it is public cloud deployment.
                else if ("StratosPublicCloudSetup".equals(element.getLocalName())) {
                    String cloudSetup = element.getText();
                    //by default, make the email validation mandatory.
                    boolean isStratosPublicCloudSetup = true;
                    if (cloudSetup.trim().equalsIgnoreCase("false")) {
                        isStratosPublicCloudSetup = false;
                    }
                    config.setPublicCloudSetup(isStratosPublicCloudSetup);
                    //Setting the paypal url
                } else if ("PaypalUrl".equals(element.getLocalName())) {
                    String paypalUrl = element.getText();
                    config.setPaypalUrl(paypalUrl);
                } else if ("SkipSummaryGenerator".equals(element.getLocalName())) {
                    String summaryGenerator = element.getText();
                    boolean skipSummaryGenerator = false;
                    if (summaryGenerator.trim().equalsIgnoreCase("true")) {
                        skipSummaryGenerator = true;
                    }
                    config.setSkipSummaryGenerator(skipSummaryGenerator);
                } else if ("PaypalAPIUsername".equals(element.getLocalName())) {
                    config.setPaypalAPIUsername(element.getText());
                } else if ("PaypalAPIPassword".equals(element.getLocalName())) {
                    config.setPaypalAPIPassword(element.getText());
                } else if ("PaypalAPISignature".equals(element.getLocalName())) {
                    config.setPaypalAPISignature(element.getText());
                } else if ("PaypalEnvironment".equals(element.getLocalName())) {
                    config.setPaypalEnvironment(element.getText());
                } else if ("FinanceNotificationEmail".equals(element.getLocalName())) {
                    config.setFinanceNotificationEmail(element.getText());
                } else if ("UsagePlanUrl".equals(element.getLocalName())) {
                    config.setUsagePlanURL(element.getText());
                } else if ("PaidJIRAUrl".equals(element.getLocalName())) {
                    config.setPaidJIRAUrl(element.getText());
                } else if ("PaidJIRAProject".equals(element.getLocalName())) {
                    config.setPaidJIRAProject(element.getText());
                } else if ("ForumUrl".equals(element.getLocalName())) {
                    config.setForumUrl(element.getText());
                } else if ("PaidUserGroup".equals(element.getLocalName())) {
                    config.setPaidUserGroup(element.getText());
                } else if ("NonpaidUserGroup".equals(element.getLocalName())) {
                    config.setNonpaidUserGroup(element.getText());
                } else if ("SupportInfoUrl".equals(element.getLocalName())) {
                    config.setSupportInfoUrl(element.getText());
                } else if ("IncidentCustomFieldId".equals(element.getLocalName())) {
                    config.setIncidentCustomFieldId(element.getText());
                } else if ("IncidentImpactCustomFieldId".equals(element.getLocalName())) {
                    config.setIncidentImpactCustomFieldId(element.getText());
                } else if ("GoogleAnalyticsURL".equals(element.getLocalName())) {
                    config.setGoogleAnalyticsURL(element.getText());
                } else if ("StratosEventListener".equals(element.getLocalName())) {
                    populateEventListenerProperties(config, element);
                } else if ("managerServiceUrl".equals(element.getLocalName())) {
                    config.setManagerServiceUrl(element.getText());
                } else if ("adminUserName".equals(element.getLocalName())) {
                    config.setAdminUserName(element.getText());
                } else if ("adminPassword".equals(element.getLocalName())) {
                    config.setAdminPassword(element.getText());
                } else if ("SSOLoadingMessage".equals(element.getLocalName())) {
                    config.setSsoLoadingMessage(element.getText());
                }
            }
            return config;
        } catch (Exception e) {
            String msg = "Error in loading Stratos Configurations File: " + configFilename + ".";
            log.error(msg, e);
            return config; //returns the default configurations, if the file could not be loaded.
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Could not close the Configuration File " + configFilename);
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Unable to locate the stratos configurations file. " + "Default Settings will be used.");
    }
    return config; // return the default configuratiosn, if the file not found.
}

From source file:org.apache.stratos.common.util.CommonUtil.java

private static String loadTermsOfUsage(String eulaFile) {
    String eula = StratosConstants.STRATOS_EULA_DEFAULT_TEXT;
    File configFile = new File(eulaFile);
    if (configFile.exists()) {
        FileInputStream inputStream = null;
        try {/*  w w  w .j  a  v a  2s . c  om*/
            inputStream = new FileInputStream(configFile);
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement documentElement = builder.getDocumentElement();
            Iterator it = documentElement.getChildElements();
            while (it.hasNext()) {
                OMElement element = (OMElement) it.next();

                //Checks whether Email Validation is mandatory for tenant registration complete.
                if ("EULA".equalsIgnoreCase(element.getLocalName())) {
                    eula = element.getText();
                }
            }
            return eula;
        } catch (Exception e) {
            String msg = "Error in loading Stratos Terms and Conditions File.";
            log.error(msg, e);
            return eula; //returns the default text, if the file could not be loaded.
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error("Could not close the EULA File " + eulaFile);
                }
            }
        }
    }
    if (log.isDebugEnabled()) {
        log.debug("Unable to locate the stratos EULA file. Default value will be used.");
    }
    return eula; // return the default, if the file not found.
}

From source file:org.apache.stratos.email.sender.api.EmailSenderConfiguration.java

/**
 * Loads the email sender configuration//from  w  w  w .j  ava 2 s  .  c  o  m
 * @param configFilename configuration file name
 * @return EmailSenderConfiguration.
 */
public static EmailSenderConfiguration loadEmailSenderConfiguration(String configFilename) {
    File configFile = new File(configFilename);
    if (!configFile.exists()) {
        log.error("Email sender configuration File is not present at: " + configFilename);
        return null;
    }
    EmailSenderConfiguration config = new EmailSenderConfiguration();
    FileInputStream ip = null;
    try {
        ip = new FileInputStream(configFile);
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(ip);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildElements();
        while (it.hasNext()) {
            OMElement element = (OMElement) it.next();
            if ("subject".equals(element.getLocalName())) {
                config.setSubject(element.getText());
            } else if ("body".equals(element.getLocalName())) {
                config.setBody(element.getText());
            } else if ("customParameters".equals(element.getLocalName())) {
                Map<String, String> customParameters = new HashMap<String, String>();
                Iterator customParamIt = element.getChildElements();
                while (customParamIt.hasNext()) {
                    OMElement customElement = (OMElement) it.next();
                    customParameters.put(customElement.getLocalName(), customElement.getText());
                }
                config.setCustomParameters(customParameters);
            }
        }
        return config;
    } catch (Exception e) {
        String msg = "Error in loading configuration for email verification: " + configFilename + ".";
        log.error(msg, e);
        return null;
    } finally {
        if (ip != null) {
            try {
                ip.close();
            } catch (IOException e) {
                log.warn("Could not close InputStream for file " + configFile.getAbsolutePath());
            }
        }

    }
}

From source file:org.apache.stratos.register.ui.utils.TenantConfigUtil.java

public static HttpServletRequest readIntermediateData(HttpServletRequest request, String data) {
    try {// w w  w  .  j  a v a  2 s  .co m
        XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(data));
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildElements();
        while (it.hasNext()) {
            OMElement element = (OMElement) it.next();
            if ("admin".equals(element.getLocalName())) {
                request.setAttribute("admin", element.getText());
            } else if ("firstname".equals(element.getText())) {
                request.setAttribute("firstname", element.getText());
            } else if ("lastname".equals(element.getText())) {
                request.setAttribute("lastname", element.getText());
            } else if ("email".equals(element.getLocalName())) {
                request.setAttribute("email", element.getText());
            } else if ("tenantDomain".equals(element.getLocalName())) {
                request.setAttribute("tenantDomain", element.getText());
            } else if ("confirmationKey".equals(element.getLocalName())) {
                request.setAttribute("confirmationKey", element.getText());
            }
        }
    } catch (Exception e) {
        log.error("Error parsing xml", e);
    }
    return request;
}

From source file:org.apache.stratos.status.monitor.core.StatusMonitorConfigurationBuilder.java

private OMElement buildOMElement(InputStream inputStream) throws StatusMonitorException {
    XMLStreamReader parser;/*www  .  ja v  a2  s .  com*/
    try {
        parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
    } catch (XMLStreamException e) {
        String msg = "Error in initializing the parser to build the OMElement.";
        log.error(msg, e);
        throw new StatusMonitorException(msg, e);
    }

    StAXOMBuilder builder = new StAXOMBuilder(parser);
    return builder.getDocumentElement();
}

From source file:org.apache.synapse.config.SynapseConfigUtils.java

/**
 * Get an object from a given URL. Will first fetch the content from the
 * URL and depending on the content-type, a suitable XMLToObjectMapper
 * (if available) would be used to transform this content into an Object.
 * If a suitable XMLToObjectMapper cannot be found, the content would be
 * treated as XML and an OMNode would be returned
 *
 * @param url the URL to the resource/*ww  w.jav  a 2 s .com*/
 * @param properties bag of properties to pass in any information to the factory
 * @return an Object created from the given URL
 */
public static Object getObject(URL url, Properties properties) {
    try {
        if (url != null && "file".equals(url.getProtocol())) {
            try {
                url.openStream();
            } catch (IOException ignored) {
                String path = url.getPath();
                if (log.isDebugEnabled()) {
                    log.debug("Can not open a connection to the URL with a path :" + path);
                }
                String synapseHome = (String) properties.get(SynapseConstants.SYNAPSE_HOME);
                if (synapseHome != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("Trying  to resolve an absolute path of the "
                                + " URL using the synapse.home : " + synapseHome);
                    }
                    if (synapseHome.endsWith("/")) {
                        synapseHome = synapseHome.substring(0, synapseHome.lastIndexOf("/"));
                    }
                    url = new URL(url.getProtocol() + ":" + synapseHome + "/" + path);
                    try {
                        url.openStream();
                    } catch (IOException e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Failed to resolve an absolute path of the "
                                    + " URL using the synapse.home : " + synapseHome);
                        }
                        log.warn("IO Error reading from URL " + url.getPath() + e);
                    }
                }
            }
        }
        if (url == null) {
            return null;
        }
        URLConnection connection = getURLConnection(url);
        if (connection == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot create a URLConnection for given URL : " + url);
            }
            return null;
        }
        XMLToObjectMapper xmlToObject = getXmlToObjectMapper(connection.getContentType());
        InputStream inputStream = connection.getInputStream();
        try {
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement omElem = builder.getDocumentElement();

            // detach from URL connection and keep in memory
            // TODO remove this 
            omElem.build();

            if (xmlToObject != null) {
                return xmlToObject.getObjectFromOMNode(omElem, properties);
            } else {
                return omElem;
            }

        } catch (XMLStreamException e) {
            if (log.isDebugEnabled()) {
                log.debug("Content at URL : " + url + " is non XML..");
            }
            return readNonXML(url);
        } catch (OMException e) {
            if (log.isDebugEnabled()) {
                log.debug("Content at URL : " + url + " is non XML..");
            }
            return readNonXML(url);
        } finally {
            inputStream.close();
        }

    } catch (IOException e) {
        handleException("Error connecting to URL : " + url, e);
    }
    return null;
}

From source file:org.apache.synapse.config.Util.java

/**
 * Get an object from a given URL. Will first fetch the content from the
 * URL and depending on the content-type, a suitable XMLToObjectMapper
 * (if available) would be used to transform this content into an Object.
 * If a suitable XMLToObjectMapper cannot be found, the content would be
 * treated as XML and an OMNode would be returned
 * @param url the URL to the resource//from w  w  w.  j  av  a 2 s.  c o  m
 * @return an Object created from the given URL
 */
public static Object getObject(URL url) {
    try {
        URLConnection urlc = url.openConnection();
        XMLToObjectMapper xmlToObject = getXmlToObjectMapper(urlc.getContentType());

        try {
            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(urlc.getInputStream());
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            OMElement omElem = builder.getDocumentElement();

            // detach from URL connection and keep in memory
            // TODO remove this 
            omElem.build();

            if (xmlToObject != null) {
                return xmlToObject.getObjectFromOMNode(omElem);
            } else {
                return omElem;
            }

        } catch (XMLStreamException e) {
            log.warn("Content at URL : " + url + " is non XML..");
            return urlc.getContent();
        }

    } catch (IOException e) {
        handleException("Error connecting to URL : " + url, e);
    }
    return null;
}