Example usage for javax.xml.validation SchemaFactory newInstance

List of usage examples for javax.xml.validation SchemaFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.validation SchemaFactory newInstance.

Prototype

public static SchemaFactory newInstance(String schemaLanguage) 

Source Link

Document

Lookup an implementation of the SchemaFactory that supports the specified schema language and return it.

Usage

From source file:org.wso2.carbon.device.mgt.iot.common.config.devicetype.IotDeviceTypeConfigurationManager.java

public synchronized void initConfig() throws DeviceManagementException {

    try {/*from w  w  w.j a  v  a  2 s . c om*/
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(iotDeviceMgtConfigXSDPath));

        File iotDeviceMgtConfig = new File(iotDeviceMgtConfigXMLPath);
        Document doc = IotDeviceManagementUtil.convertToDocument(iotDeviceMgtConfig);
        JAXBContext iotDeviceMgmtContext = JAXBContext.newInstance(IoTDeviceTypeConfigManager.class);
        Unmarshaller unmarshaller = iotDeviceMgmtContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new IotConfigValidationEventHandler());
        this.currentIoTDeviceTypeConfig = (IoTDeviceTypeConfigManager) unmarshaller.unmarshal(doc);

        List<IotDeviceTypeConfig> iotDeviceTypeConfigList = currentIoTDeviceTypeConfig.getIotDeviceTypeConfig();
        for (IotDeviceTypeConfig iotDeviceTypeConfig : iotDeviceTypeConfigList) {
            String applicationName = iotDeviceTypeConfig.getApiApplicationName();

            if (applicationName == null || applicationName.isEmpty()) {
                iotDeviceTypeConfig.setApiApplicationName(iotDeviceTypeConfig.getType());
            }
            iotDeviceTypeConfigMap.put(iotDeviceTypeConfig.getType(), iotDeviceTypeConfig);

        }
        ApisAppClient.getInstance().setBase64EncodedConsumerKeyAndSecret(iotDeviceTypeConfigList);

    } catch (Exception e) {
        String error = "Error occurred while initializing device configurations";
        log.error(error);
    }
}

From source file:org.wso2.carbon.device.mgt.iot.config.server.DeviceManagementConfigurationManager.java

public void initConfig() throws DeviceControllerException {
    try {//  w  w w  .  ja v a  2s .  c  o m
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(XSDCONFIGS_FILE_LOCATION));

        File deviceCloudMgtConfig = new File(XMLCONFIGS_FILE_LOCATION);
        Document doc = IotDeviceManagementUtil.convertToDocument(deviceCloudMgtConfig);
        JAXBContext deviceCloudContext = JAXBContext.newInstance(DeviceManagementConfiguration.class);
        Unmarshaller unmarshaller = deviceCloudContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new IotConfigValidationEventHandler());
        this.currentDeviceManagementConfiguration = (DeviceManagementConfiguration) unmarshaller.unmarshal(doc);
    } catch (Exception e) {
        String error = "Error occurred while initializing DeviceController configurations";
        log.error(error);
        throw new DeviceControllerException(error, e);
    }
}

From source file:org.wso2.carbon.device.mgt.mobile.impl.MobileDeviceManagementConfigTests.java

@BeforeClass
private void initSchema() {
    File deviceManagementSchemaConfig = new File(MobileDeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {/*from   www  . ja va 2 s .  c  om*/
        schema = factory.newSchema(deviceManagementSchemaConfig);
    } catch (SAXException e) {
        Assert.fail("Invalid schema found", e);
    }
}

From source file:org.wso2.carbon.governance.generic.util.Util.java

public static Validator getSchemaValidator(String schemaPath) {

    if (serviceSchemaValidator == null) {
        try {//from   w ww . j  a  v  a  2 s  . c  o  m
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(new File(schemaPath));
            serviceSchemaValidator = schema.newValidator();
        } catch (SAXException e) {
            log.error("Unable to get a schema validator from the given file path : " + schemaPath);
        }
    }
    return serviceSchemaValidator;
}

From source file:org.wso2.carbon.governance.lcm.util.CommonUtil.java

public static Validator getLifecycleSchemaValidator(String schemaPath) {

    if (lifecycleSchemaValidator == null) {
        try {//from  w ww.j ava2s.com
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = schemaFactory.newSchema(new File(schemaPath));
            lifecycleSchemaValidator = schema.newValidator();
        } catch (SAXException e) {
            log.error("Unable to get a schema validator from the given file path : " + schemaPath);
        }
    }
    return lifecycleSchemaValidator;
}

From source file:org.wso2.carbon.governance.list.util.CommonUtil.java

private static boolean validateRXTContent(String rxtContent, String xsdPath) throws RegistryException {
    try {/*from  w  w  w. j a  va2  s  .  c o  m*/
        OMElement rxt = getRXTContentOMElement(rxtContent);
        AXIOMXPath xpath = new AXIOMXPath("//artifactType");
        OMElement c1 = (OMElement) xpath.selectSingleNode(rxt);
        InputStream is = new ByteArrayInputStream(c1.toString().getBytes());
        Source xmlFile = new StreamSource(is);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new File(xsdPath));
        Validator validator = schema.newValidator();
        validator.validate(xmlFile);
    } catch (Exception e) {
        log.error("RXT validation fails due to: " + e.getMessage());
        return false;
    }
    return true;
}

From source file:org.wso2.carbon.identity.entitlement.internal.SchemaBuilder.java

/**
 * Builds the policy schema map. There are three schemas.
 *
 * @param configHolder holder EntitlementConfigHolder
 * @throws SAXException if fails//from w  ww .ja v  a 2s.com
 */
public void buildPolicySchema() throws SAXException {

    if (!"true".equalsIgnoreCase((String) configHolder.getEngineProperties()
            .get(EntitlementExtensionBuilder.PDP_SCHEMA_VALIDATION))) {
        log.warn("PDP schema validation disabled.");
        return;
    }

    String[] schemaNSs = new String[] { PDPConstants.XACML_1_POLICY_XMLNS, PDPConstants.XACML_2_POLICY_XMLNS,
            PDPConstants.XACML_3_POLICY_XMLNS };

    for (String schemaNS : schemaNSs) {

        String schemaFile;

        if (PDPConstants.XACML_1_POLICY_XMLNS.equals(schemaNS)) {
            schemaFile = PDPConstants.XACML_1_POLICY_SCHEMA_FILE;
        } else if (PDPConstants.XACML_2_POLICY_XMLNS.equals(schemaNS)) {
            schemaFile = PDPConstants.XACML_2_POLICY_SCHEMA_FILE;
        } else {
            schemaFile = PDPConstants.XACML_3_POLICY_SCHEMA_FILE;
        }

        InputStream schemaFileStream = EntitlementExtensionBuilder.class.getResourceAsStream("/" + schemaFile);
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new StreamSource(schemaFileStream));
        configHolder.getPolicySchemaMap().put(schemaNS, schema);
    }
}

From source file:org.wso2.carbon.integration.common.tests.utils.DistributionValidationTestUtils.java

public static void validateXml(HashMap<String, Exception> xsdValidateMap, String distributionXml,
        String xsdFile) throws IOException {
    Source schemaFile = new StreamSource(new File(xsdFile));
    Source xmlFile = new StreamSource(new File(distributionXml));
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;//w  w  w .j av a2s .  c om
    try {
        schema = schemaFactory.newSchema(schemaFile);
    } catch (SAXException e) {
        log.error(distributionXml, e);
        xsdValidateMap.put(distributionXml, e);
    }
    if (schema != null) {
        Validator validator = schema.newValidator();
        try {
            validator.validate(xmlFile);
        } catch (SAXException e) {
            log.error(distributionXml, e);
            xsdValidateMap.put(distributionXml, e);
        }
    }
}

From source file:org.wso2.carbon.lcm.core.util.LifecycleUtils.java

/**
 * Method used to get schema validator object for lifecycle configurations.
 * @param schemaPath Schema path in the server extracted directory.
 * @return schema validator object// w  w w . ja v a  2 s .c om
 */
public static synchronized Validator getLifecycleSchemaValidator(String schemaPath) {
    if (lifecycleSchemaValidator != null) {
        return lifecycleSchemaValidator;
    }
    try {
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = schemaFactory.newSchema(new File(schemaPath));
        lifecycleSchemaValidator = schema.newValidator();
    } catch (SAXException e) {
        log.error("Unable to get a schema validator from the given file path : " + schemaPath);
    }
    return lifecycleSchemaValidator;
}

From source file:org.xsystem.bpmn2.formats.xml.XMLParser3.java

Schema buildSxema() throws SAXException, IOException {
    final ClassLoader classLoader = this.getClass().getClassLoader();
    //Thread.currentThread().getContextClassLoader();

    try (InputStream is = classLoader.getResourceAsStream("BPMN20/BPMN20.xsd")) {

        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);//"http://www.w3.org/2001/XMLSchema");
        StreamSource ss = new StreamSource(is);
        factory.setResourceResolver(//  www . j av a 2 s .  c  om
                (String type, String namespaceURI, String publicId, String systemId, String baseURI) -> {
                    InputStream resourceAsStream = classLoader.getResourceAsStream("BPMN20/" + systemId);
                    LSInput ret = new LSInputImpl(publicId, systemId, resourceAsStream);
                    return ret;
                });
        Schema shema = factory.newSchema(ss);
        return shema;
    }
}