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.email.mgt.util.I18nEmailUtil.java

/**
 * @return//from w ww. j  av  a2  s.  c  om
 */
public static List<EmailTemplate> getDefaultEmailTemplates() {
    String configFilePath = CarbonUtils.getCarbonConfigDirPath() + File.separator
            + I18nMgtConstants.EMAIL_CONF_DIRECTORY + File.separator + I18nMgtConstants.EMAIL_ADMIN_CONF_FILE;

    List<EmailTemplate> defaultTemplates = new ArrayList<>();
    File configFile = new File(configFilePath);
    if (!configFile.exists()) {
        log.error("Email Configuration File is not present at: " + configFilePath);
    }

    XMLStreamReader xmlStreamReader = null;
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(configFile);
        xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        StAXOMBuilder builder = new StAXOMBuilder(xmlStreamReader);

        OMElement documentElement = builder.getDocumentElement();
        Iterator iterator = documentElement.getChildElements();
        while (iterator.hasNext()) {
            OMElement omElement = (OMElement) iterator.next();
            String type = omElement.getAttributeValue(new QName(I18nMgtConstants.TEMPLATE_TYPE));
            String displayName = omElement
                    .getAttributeValue(new QName(I18nMgtConstants.TEMPLATE_TYPE_DISPLAY_NAME));
            String locale = omElement.getAttributeValue(new QName(I18nMgtConstants.TEMPLATE_LOCALE));
            String contentType = omElement.getAttributeValue(new QName(I18nMgtConstants.TEMPLATE_CONTENT_TYPE));

            Map<String, String> emailContentMap = getEmailContent(omElement);
            String subject = emailContentMap.get(I18nMgtConstants.TEMPLATE_SUBJECT);
            String body = emailContentMap.get(I18nMgtConstants.TEMPLATE_BODY);
            String footer = emailContentMap.get(I18nMgtConstants.TEMPLATE_FOOTER);

            // create the DTO and add to list
            EmailTemplate emailTemplateDTO = new EmailTemplate();
            emailTemplateDTO.setTemplateType(type);
            emailTemplateDTO.setTemplateDisplayName(displayName);
            emailTemplateDTO.setLocale(locale);
            emailTemplateDTO.setEmailContentType(contentType);

            emailTemplateDTO.setSubject(subject);
            emailTemplateDTO.setBody(body);
            emailTemplateDTO.setFooter(footer);

            defaultTemplates.add(emailTemplateDTO);
        }
    } catch (XMLStreamException | FileNotFoundException e) {
        log.warn("Error while loading default templates to the registry.", e);
    } finally {
        try {
            if (xmlStreamReader != null) {
                xmlStreamReader.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (XMLStreamException e) {
            log.error("Error while closing XML stream", e);
        } catch (IOException e) {
            log.error("Error while closing input stream", e);
        }
    }

    return defaultTemplates;
}

From source file:org.wso2.carbon.email.verification.util.Util.java

/**
 * Loads the email configuration from the respective components.
 * @param configFilename, the file that contains the email configurations
 * @return EmailVerfierConfig//from  w w w.  j  a v  a2  s .c om
 */
public static EmailVerifierConfig loadeMailVerificationConfig(String configFilename) {
    EmailVerifierConfig config = new EmailVerifierConfig();
    File configfile = new File(configFilename);
    if (!configfile.exists()) {
        log.error("Email Configuration File is not present at: " + configFilename);
        return null;
    }
    try {
        XMLStreamReader parser = XMLInputFactory.newInstance()
                .createXMLStreamReader(new FileInputStream(configfile));
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator it = documentElement.getChildrenWithLocalName("configuration");

        //Assume the configuration file is in older format with just one email template
        if (it.hasNext() == false) {
            config = fillConfig(documentElement.getChildElements());

            //If the configuration file is in new format
        } else {
            while (it.hasNext()) {
                OMElement element = (OMElement) it.next();
                String configType = element.getAttributeValue(new QName("type"));
                if (configType.trim().equalsIgnoreCase(PASSWORD_RESET)) {
                    Iterator configValues = element.getChildElements();
                    config = fillConfig(configValues);
                }
            }
        }
        return config;
    } catch (Exception e) {
        String msg = "Error in loading configuration for email verification: " + configFilename + ".";
        log.error(msg, e);
        return null;
    }
}

From source file:org.wso2.carbon.identity.oauth2.util.OAuth2Util.java

private static Map<String, String> loadScopeConfigFile() {
    Map<String, String> scopes = new HashMap<>();
    String configDirPath = CarbonUtils.getCarbonConfigDirPath();
    String confXml = Paths.get(configDirPath, "identity", OAuthConstants.OIDC_SCOPE_CONFIG_PATH).toString();
    File configfile = new File(confXml);
    if (!configfile.exists()) {
        log.warn("OIDC scope-claim Configuration File is not present at: " + confXml);
    }/*from   w  w  w  .java  2s . co  m*/

    XMLStreamReader parser = null;
    InputStream stream = null;

    try {
        stream = new FileInputStream(configfile);
        parser = XMLInputFactory.newInstance().createXMLStreamReader(stream);
        StAXOMBuilder builder = new StAXOMBuilder(parser);
        OMElement documentElement = builder.getDocumentElement();
        Iterator iterator = documentElement.getChildElements();
        while (iterator.hasNext()) {
            OMElement omElement = (OMElement) iterator.next();
            String configType = omElement.getAttributeValue(new QName("id"));
            scopes.put(configType, loadClaimConfig(omElement));
        }
    } catch (XMLStreamException e) {
        log.warn("Error while loading scope config.", e);
    } catch (FileNotFoundException e) {
        log.warn("Error while loading email config.", e);
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
            if (stream != null) {
                IdentityIOStreamUtils.closeInputStream(stream);
            }
        } catch (XMLStreamException e) {
            log.error("Error while closing XML stream", e);
        }
    }
    return scopes;
}