Example usage for javax.xml.bind Unmarshaller setSchema

List of usage examples for javax.xml.bind Unmarshaller setSchema

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller setSchema.

Prototype

public void setSchema(javax.xml.validation.Schema schema);

Source Link

Document

Specify the JAXP 1.3 javax.xml.validation.Schema Schema object that should be used to validate subsequent unmarshal operations against.

Usage

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

public void initConfig() throws DeviceControllerException {
    try {//  w ww .j a v a  2s  . co  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

/**
 * Validates a given malformed-configuration file.
 *//*from   w w  w . java 2  s .c  om*/
private void validateMalformedConfig(File malformedConfig) {
    try {
        JAXBContext ctx = JAXBContext.newInstance(MobileDeviceManagementConfig.class);
        Unmarshaller um = ctx.createUnmarshaller();
        um.setSchema(this.getSchema());
        um.unmarshal(malformedConfig);
        Assert.assertTrue(false);
    } catch (JAXBException e) {
        Throwable linkedException = e.getLinkedException();
        if (!(linkedException instanceof SAXParseException)) {
            log.error("Unexpected error occurred while unmarshalling mobile device management config", e);
            Assert.assertTrue(false);
        }
        Assert.assertTrue(true);
    }
}

From source file:pl.nask.hsn2.workflow.parser.HWLParser.java

private Unmarshaller createUnmarshaller() throws JAXBException {
    Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setSchema(schema);
    return unmarshaller;
}

From source file:pl.psnc.synat.wrdz.zmkd.plan.MigrationPlansBean.java

/**
 * Invoke when a new migration plan is uploaded by a user.
 * /*from  w ww.j ava2 s .c om*/
 * @param event
 *            file upload event
 * @throws Exception
 *             when something went wrong
 */
public void uploadPlanListener(FileUploadEvent event) throws Exception {
    UploadedFile file = event.getUploadedFile();
    InputStream is = file.getInputStream();

    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setSchema(schema);

    pl.psnc.darceo.migration.MigrationPlan plan;
    try {
        plan = (pl.psnc.darceo.migration.MigrationPlan) unmarshaller.unmarshal(is);
    } catch (UnmarshalException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.invalid_xml"));
        return;
    } finally {
        IOUtils.closeQuietly(is);
    }

    try {
        migrationPlanManager.createMigrationPlan(plan);
    } catch (InvalidFormatException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.invalid_format", e.getPuid()));
    } catch (InvalidObjectException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.invalid_object", e.getObjectIdentifier()));
    } catch (InvalidPathException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.invalid_path"));
    } catch (NoObjectsException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.no_objects"));
    } catch (NoPathException e) {
        reportUploadError(ZmkdMessageUtils.getMessage("plans.error.no_path"));
    }
}

From source file:sernet.verinice.service.XmlRightsService.java

/**
 * Loads the configuration by merging the files
 * 'verinice-auth-default.xml' and 'verinice-auth.xml'
 * /*from www.j a  v  a2 s  .co  m*/
 * @return The authorization configuration
 * @throws IllegalAuthConfTypeException if a different configurationType is detected in 'verinice-auth-default.xml' and 'verinice-auth.xml'
 */
private Auth loadConfiguration() {
    try {
        Unmarshaller unmarshaller = getContext().createUnmarshaller();
        unmarshaller.setSchema(getSchema());

        // read default configuration
        Auth authDefault = (Auth) unmarshaller.unmarshal(getAuthConfigurationDefault().getInputStream());
        Auth authUser = null;

        // check if configuration exists
        if (getAuthConfiguration().exists()) {
            if (log.isDebugEnabled()) {
                log.debug("Reading authorization configuration from file: "
                        + getAuthConfiguration().getFile().getPath());
            }

            authUser = (Auth) unmarshaller.unmarshal(getAuthConfiguration().getInputStream());

            // check configuration type of both files
            // throw an exception if a different type is detected
            if (!authDefault.getType().equals(authUser.getType())) {
                final String message = "You must use the same configurationType in 'verinice-auth-default.xml' and 'verinice-auth.xml'";
                throw new IllegalAuthConfTypeException(message);
            }
            // merge both configurations
            authDefault = AuthHelper.merge(new Auth[] { authUser, authDefault });
        }

        return authDefault;
    } catch (RuntimeException e) {
        log.error("Error while reading verinice authorization definition from file: "
                + getAuthConfiguration().getFilename(), e);
        throw e;
    } catch (Exception e) {
        log.error("Error while reading verinice authorization definition from file: "
                + getAuthConfiguration().getFilename(), e);
        throw new RuntimeException(e);
    }
}

From source file:system.handlers.admincommands.Reload.java

@Override
public void executeCommand(Player admin, String[] params) {
    if (admin.getAccessLevel() < AdminConfig.COMMAND_RELOAD) {
        PacketSendUtility.sendMessage(admin, "You dont have enough rights to execute this command");
        return;/* www.  java 2s  .c  om*/
    }

    if (params == null || params.length != 1) {
        PacketSendUtility.sendMessage(admin, "syntax //reload <quest | skill | portal | spawn>");
        return;
    }
    if (params[0].equals("quest")) {
        File xml = new File("./data/static_data/quest_data/quest_data.xml");
        File dir = new File("./data/static_data/quest_script_data");
        try {
            QuestEngine.getInstance().shutdown();
            JAXBContext jc = JAXBContext.newInstance(StaticData.class);
            Unmarshaller un = jc.createUnmarshaller();
            un.setSchema(getSchema("./data/static_data/static_data.xsd"));
            QuestsData newQuestData = (QuestsData) un.unmarshal(xml);
            QuestsData questsData = DataManager.QUEST_DATA;
            questsData.setQuestsData(newQuestData.getQuestsData());
            QuestScriptsData questScriptsData = DataManager.QUEST_SCRIPTS_DATA;
            questScriptsData.getData().clear();
            for (File file : listFiles(dir, true)) {
                QuestScriptsData data = ((QuestScriptsData) un.unmarshal(file));
                if (data != null)
                    if (data.getData() != null)
                        questScriptsData.getData().addAll(data.getData());
            }
            QuestEngine.getInstance().load();
        } catch (Exception e) {
            PacketSendUtility.sendMessage(admin, "Quest reload failed!");
            log.error(e);
        } finally {
            PacketSendUtility.sendMessage(admin, "Quest reload Success!");
        }
    } else if (params[0].equals("skill")) {
        File dir = new File("./data/static_data/skills");
        try {
            JAXBContext jc = JAXBContext.newInstance(StaticData.class);
            Unmarshaller un = jc.createUnmarshaller();
            un.setSchema(getSchema("./data/static_data/static_data.xsd"));
            List<SkillTemplate> newTemplates = new ArrayList<SkillTemplate>();
            for (File file : listFiles(dir, true)) {
                SkillData data = (SkillData) un.unmarshal(file);
                if (data != null)
                    newTemplates.addAll(data.getSkillTemplates());
            }
            DataManager.SKILL_DATA.setSkillTemplates(newTemplates);
        } catch (Exception e) {
            PacketSendUtility.sendMessage(admin, "Skill reload failed!");
            log.error(e);
        } finally {
            PacketSendUtility.sendMessage(admin, "Skill reload Success!");
        }
    } else if (params[0].equals("portal")) {
        File dir = new File("./data/static_data/portals");
        try {
            JAXBContext jc = JAXBContext.newInstance(StaticData.class);
            Unmarshaller un = jc.createUnmarshaller();
            un.setSchema(getSchema("./data/static_data/static_data.xsd"));
            List<PortalTemplate> newTemplates = new ArrayList<PortalTemplate>();
            for (File file : listFiles(dir, true)) {
                PortalData data = (PortalData) un.unmarshal(file);
                if (data != null && data.getPortals() != null)
                    newTemplates.addAll(data.getPortals());
            }
            DataManager.PORTAL_DATA.setPortals(newTemplates);
        } catch (Exception e) {
            PacketSendUtility.sendMessage(admin, "Portal reload failed!");
            log.error(e);
        } finally {
            PacketSendUtility.sendMessage(admin, "Portal reload Success!");
        }
    } else
        PacketSendUtility.sendMessage(admin, "syntax //reload <quest | skill | portal | spawn>");

}