Example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

List of usage examples for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI

Introduction

In this page you can find the example usage for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Prototype

String W3C_XML_SCHEMA_NS_URI

To view the source code for javax.xml XMLConstants W3C_XML_SCHEMA_NS_URI.

Click Source Link

Document

W3C XML Schema Namespace URI.

Usage

From source file:org.wso2.carbon.apimgt.gateway.mediators.XMLSchemaValidator.java

/**
 * This method validates the request payload xml with the relevant xsd.
 *
 * @param messageContext      This message context contains the request message properties of the relevant
 *                            API which was enabled the XML_Validator message mediation in flow.
 * @param bufferedInputStream Buffered input stream to be validated.
 * @throws APIMThreatAnalyzerException Exception might be occurred while parsing the xml payload.
 */// ww w  . ja  v a  2s  . c om
private boolean validateSchema(MessageContext messageContext, BufferedInputStream bufferedInputStream)
        throws APIMThreatAnalyzerException {
    String xsdURL;
    Schema schema;
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    try {
        Object messageProperty = messageContext.getProperty(APIMgtGatewayConstants.XSD_URL);
        if (messageProperty == null) {
            return true;
        } else {
            if (String.valueOf(messageProperty).isEmpty()) {
                return true;
            } else {
                xsdURL = String.valueOf(messageProperty);
                URL schemaFile = new URL(xsdURL);
                schema = schemaFactory.newSchema(schemaFile);
                Source xmlFile = new StreamSource(bufferedInputStream);
                Validator validator = schema.newValidator();
                validator.validate(xmlFile);
            }
        }
    } catch (SAXException | IOException e) {
        throw new APIMThreatAnalyzerException("Error occurred while parsing XML payload : " + e);
    }
    return true;
}

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 {/*from   w  w w  .  j a v  a  2  s  . 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 {//from w  ww.  j  a v  a 2s .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 {//  w  w w  .jav a 2s  . c  om
        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 ava 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  ww  w .  j  a  v  a  2s  . 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 {/*from   w w w  .  j a  v a2s.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 {//w  w w.j  av a2  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 . j a  v  a  2  s .c om
        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 {//www. jav a  2 s  .  c  o  m
        schema = factory.newSchema(deviceManagementSchemaConfig);
    } catch (SAXException e) {
        Assert.fail("Invalid schema found", e);
    }
}