List of usage examples for javax.xml.bind Unmarshaller setSchema
public void setSchema(javax.xml.validation.Schema schema);
From source file:org.rhq.enterprise.server.xmlschema.ServerPluginDescriptorUtil.java
/** * This will return a JAXB unmarshaller that will enable the caller to parse a server plugin * descriptor. The returned unmarshaller will have a {@link ValidationEventCollector} * installed as an {@link Unmarshaller#getEventHandler() event handler} which can be used * to obtain error messages if the unmarshaller fails to parse an XML document. * /*w w w . j a v a 2s . co m*/ * @return a JAXB unmarshaller enabling the caller to parse server plugin descriptors * * @throws Exception if an unmarshaller could not be created */ public static Unmarshaller getServerPluginDescriptorUnmarshaller() throws Exception { // create the JAXB context with all the generated plugin packages in it JAXBContext jaxbContext; try { jaxbContext = JAXBContext.newInstance(PLUGIN_CONTEXT_PATH); } catch (Exception e) { throw new Exception("Failed to create JAXB Context.", e); } // create the unmarshaller that can be used to parse XML documents containing server plugin descriptors Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); // enable schema validation to ensure the XML documents parsed by the unmarshaller are valid descriptors ClassLoader cl = ServerPluginDescriptorUtil.class.getClassLoader(); SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); StreamSource[] schemaSources = new StreamSource[PLUGIN_SCHEMA_PACKAGES.size()]; int i = 0; for (String schemaPath : PLUGIN_SCHEMA_PACKAGES.keySet()) { URL schemaURL = cl.getResource(schemaPath); schemaSources[i++] = new StreamSource(schemaURL.toExternalForm()); } Schema pluginSchema = schemaFactory.newSchema(schemaSources); unmarshaller.setSchema(pluginSchema); ValidationEventCollector vec = new ValidationEventCollector(); unmarshaller.setEventHandler(vec); return unmarshaller; }
From source file:org.silverpeas.core.importexport.control.ImportExport.java
/** * Mthode retournant l'arbre des objets mapps sur le fichier xml pass en paramtre. * @param xmlFileName le fichier xml interprt par JAXB * @return Un objet SilverPeasExchangeType contenant le mapping d'un fichier XML * @throws ImportExportException//w ww . j a va2 s .c o m */ private SilverPeasExchangeType loadSilverpeasExchange(String xmlFileName) throws ImportExportException { try { File xmlInputSource = new File(xmlFileName); String xsdSystemId = settings.getString("xsdDefaultSystemId"); SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(new URL(xsdSystemId)); // Unmarshall the import model Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new ImportExportErrorHandler()); SilverPeasExchangeType silverpeasExchange = (SilverPeasExchangeType) unmarshaller .unmarshal(xmlInputSource); return silverpeasExchange; } catch (JAXBException me) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_UNMARSHALLING_FAILED", "XML Filename " + xmlFileName + ": " + me.getLocalizedMessage(), me); } catch (MalformedURLException ue) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_UNMARSHALLING_FAILED", "XML Filename " + xmlFileName + ": " + ue.getLocalizedMessage(), ue); } catch (SAXException ve) { throw new ImportExportException("ImportExport.loadSilverpeasExchange", "importExport.EX_PARSING_FAILED", "XML Filename " + xmlFileName + ": " + ve.getLocalizedMessage(), ve); } }
From source file:org.springframework.oxm.jaxb.Jaxb2Marshaller.java
/** * Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. * Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set. * <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link * #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[]) * schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and * {@link #setAdapters(XmlAdapter[]) adapters}. *//*w ww. jav a 2 s. c o m*/ protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException { if (this.unmarshallerProperties != null) { for (String name : this.unmarshallerProperties.keySet()) { unmarshaller.setProperty(name, this.unmarshallerProperties.get(name)); } } if (this.unmarshallerListener != null) { unmarshaller.setListener(this.unmarshallerListener); } if (this.validationEventHandler != null) { unmarshaller.setEventHandler(this.validationEventHandler); } if (this.adapters != null) { for (XmlAdapter<?, ?> adapter : this.adapters) { unmarshaller.setAdapter(adapter); } } if (this.schema != null) { unmarshaller.setSchema(this.schema); } }
From source file:org.voltdb.compiler.VoltCompiler.java
/** * Read the project file and get the database object. * @param projectFileURL project file URL/path * @return database for project or null *//*from w w w . ja v a 2 s . c o m*/ private DatabaseType getProjectDatabase(final VoltCompilerReader projectReader) { DatabaseType database = null; if (projectReader != null) { m_currentFilename = projectReader.getName(); try { JAXBContext jc = JAXBContext.newInstance("org.voltdb.compiler.projectfile"); // This schema shot the sheriff. SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(this.getClass().getResource("ProjectFileSchema.xsd")); Unmarshaller unmarshaller = jc.createUnmarshaller(); // But did not shoot unmarshaller! unmarshaller.setSchema(schema); @SuppressWarnings("unchecked") JAXBElement<ProjectType> result = (JAXBElement<ProjectType>) unmarshaller.unmarshal(projectReader); ProjectType project = result.getValue(); database = project.getDatabase(); } catch (JAXBException e) { // Convert some linked exceptions to more friendly errors. if (e.getLinkedException() instanceof java.io.FileNotFoundException) { addErr(e.getLinkedException().getMessage()); compilerLog.error(e.getLinkedException().getMessage()); } else { DeprecatedProjectElement deprecated = DeprecatedProjectElement.valueOf(e); if (deprecated != null) { addErr("Found deprecated XML element \"" + deprecated.name() + "\" in project.xml file, " + deprecated.getSuggestion()); addErr("Error schema validating project.xml file. " + e.getLinkedException().getMessage()); compilerLog.error( "Found deprecated XML element \"" + deprecated.name() + "\" in project.xml file"); compilerLog.error(e.getMessage()); compilerLog.error(projectReader.getPath()); } else if (e.getLinkedException() instanceof org.xml.sax.SAXParseException) { addErr("Error schema validating project.xml file. " + e.getLinkedException().getMessage()); compilerLog.error( "Error schema validating project.xml file: " + e.getLinkedException().getMessage()); compilerLog.error(e.getMessage()); compilerLog.error(projectReader.getPath()); } else { throw new RuntimeException(e); } } } catch (SAXException e) { addErr("Error schema validating project.xml file. " + e.getMessage()); compilerLog.error("Error schema validating project.xml file. " + e.getMessage()); } } else { // No project.xml - create a stub object. database = new DatabaseType(); } return database; }
From source file:org.voltdb.utils.CatalogUtil.java
/** * Get a reference to the root <deployment> element from the deployment.xml file. * @param deployIS//from w w w . ja v a2s.c o m * @return Returns a reference to the root <deployment> element. */ @SuppressWarnings("unchecked") public static DeploymentType getDeployment(InputStream deployIS) { try { if (m_jc == null || m_schema == null) { throw new RuntimeException("Error schema validation."); } Unmarshaller unmarshaller = m_jc.createUnmarshaller(); unmarshaller.setSchema(m_schema); JAXBElement<DeploymentType> result = (JAXBElement<DeploymentType>) unmarshaller.unmarshal(deployIS); DeploymentType deployment = result.getValue(); // move any deprecated standalone export elements to the default target ExportType export = deployment.getExport(); if (export != null && export.getTarget() != null) { if (export.getConfiguration().size() > 1) { hostLog.error( "Invalid schema, cannot use deprecated export syntax with multiple configuration tags."); return null; } //OLD syntax use target as type. if (export.getConfiguration().isEmpty()) { // this code is for RestRoundtripTest export.getConfiguration().add(new ExportConfigurationType()); } ExportConfigurationType exportConfig = export.getConfiguration().get(0); if (export.isEnabled() != null) { exportConfig.setEnabled(export.isEnabled()); } if (export.getTarget() != null) { exportConfig.setType(export.getTarget()); } if (export.getExportconnectorclass() != null) { exportConfig.setExportconnectorclass(export.getExportconnectorclass()); } //Set target to default name. exportConfig.setStream(Constants.DEFAULT_EXPORT_CONNECTOR_NAME); } populateDefaultDeployment(deployment); return deployment; } catch (JAXBException e) { // Convert some linked exceptions to more friendly errors. if (e.getLinkedException() instanceof java.io.FileNotFoundException) { hostLog.error(e.getLinkedException().getMessage()); return null; } else if (e.getLinkedException() instanceof org.xml.sax.SAXParseException) { hostLog.error( "Error schema validating deployment.xml file. " + e.getLinkedException().getMessage()); return null; } else { throw new RuntimeException(e); } } }
From source file:org.wso2.carbon.device.mgt.core.app.mgt.AppManagementConfigurationManagerTest.java
private void validateMalformedConfig(File malformedConfig) { try {/* www . j a v a 2 s . c om*/ JAXBContext ctx = JAXBContext.newInstance(AppManagementConfig.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 app management config", e); Assert.assertTrue(false); } log.error("JAXB parser occurred while unmarsharlling app management config", e); Assert.assertTrue(true); } }
From source file:org.wso2.carbon.device.mgt.core.DeviceManagementConfigTests.java
private void validateMalformedConfig(File malformedConfig) { try {// w w w . j a v a 2 s.c o m JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfig.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 device management config", e); Assert.assertTrue(false); } log.error("JAXB parser occurred while unmarsharlling device management config", e); Assert.assertTrue(true); } }
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 a va 2 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. j a v a 2 s .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(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 {/*www. j a v a2s . c o 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); } }