Example usage for javax.xml.validation SchemaFactory newSchema

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

Introduction

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

Prototype

public abstract Schema newSchema(Source[] schemas) throws SAXException;

Source Link

Document

Parses the specified source(s) as a schema and returns it as a schema.

Usage

From source file:org.wso2.carbon.automation.engine.test.configuration.ConfigurationXSDValidatorTest.java

@Test(groups = "context.unit.test", description = "Upload aar service and verify deployment")
public void validateAutomationXml() throws IOException, SAXException {
    boolean validated = false;
    URL schemaFile = validateXsdFile.toURI().toURL();
    Source xmlFile = new StreamSource(configXmlFile);
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaFile);
    Validator validator = schema.newValidator();
    try {//  w w  w. ja va2s.  c om
        validator.validate(xmlFile);
        validated = true;

    } catch (SAXException e) {
        log.error(e.getStackTrace());
        throw new SAXException(e);
    }
    Assert.assertTrue(validated);

}

From source file:org.wso2.carbon.device.mgt.core.api.mgt.config.APIPublisherConfig.java

private static Schema getSchema() throws DeviceManagementException {
    try {//  w  w w. j  av  a2 s  . c o  m
        File deviceManagementSchemaConfig = new File(APIPublisherConfig.USER_DEFINED_API_CONFIG_SCHEMA_PATH);
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        return factory.newSchema(deviceManagementSchemaConfig);
    } catch (SAXException e) {
        throw new DeviceManagementException(
                "Error occurred while initializing the schema of " + "user-api-publisher-config.xml", e);
    }
}

From source file:org.wso2.carbon.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest.java

@BeforeClass
private void initSchema() {
    File deviceManagementSchemaConfig = new File(
            AppManagementConfigurationManagerTest.TEST_CONFIG_SCHEMA_LOCATION);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {/*from w w w. j  ava 2 s.  c  o m*/
        schema = factory.newSchema(deviceManagementSchemaConfig);
    } catch (SAXException e) {
        Assert.fail("Invalid schema found", e);
    }
}

From source file:org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests.java

@BeforeClass
private void initSchema() {
    File deviceManagementSchemaConfig = new File(DeviceManagementConfigTests.TEST_CONFIG_SCHEMA_LOCATION);
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {//from   ww  w  .  j  a v a  2s.c o m
        schema = factory.newSchema(deviceManagementSchemaConfig);
    } catch (SAXException e) {
        Assert.fail("Invalid schema found", e);
    }
}

From source file:org.wso2.carbon.device.mgt.etc.config.devicetype.DeviceTypeConfigurationManager.java

public synchronized void initConfig() throws DeviceManagementException {
    try {/*from   w  w  w.  j av  a2 s  . c o  m*/
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = sf.newSchema(new File(deviceMgtConfigXSDPath));

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

        List<DeviceTypeConfig> iotDeviceTypeConfigList = currentDeviceTypeConfig.getDeviceTypeConfigs();
        for (DeviceTypeConfig iotDeviceTypeConfig : iotDeviceTypeConfigList) {
            String applicationName = iotDeviceTypeConfig.getApiApplicationName();

            if (applicationName == null || applicationName.isEmpty()) {
                iotDeviceTypeConfig.setApiApplicationName(iotDeviceTypeConfig.getType());
            }
            deviceTypeConfigMap.put(iotDeviceTypeConfig.getType(), iotDeviceTypeConfig);
        }
        ApisAppClient.getInstance().setBase64EncodedConsumerKeyAndSecret(iotDeviceTypeConfigList);
    } catch (Exception e) {
        String error = "Error occurred while initializing device configurations";
        log.error(error, e);
    }
}

From source file:org.wso2.carbon.device.mgt.etc.config.server.DeviceCloudConfigManager.java

public void initConfig() throws DeviceControllerException {
    try {/*  w  w  w.  java  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(DeviceCloudConfig.class);
        Unmarshaller unmarshaller = deviceCloudContext.createUnmarshaller();
        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new IotConfigValidationEventHandler());
        this.currentDeviceCloudConfig = (DeviceCloudConfig) 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.iot.common.config.devicetype.IotDeviceTypeConfigurationManager.java

public synchronized void initConfig() throws DeviceManagementException {

    try {/*from ww  w . j ava  2  s  . co m*/
        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 {//from  w  ww  . j a  v  a2  s .com
        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 {// w w  w. j av  a  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 {// ww  w . ja va  2  s . c  om
            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;
}