List of usage examples for javax.xml.bind Unmarshaller setSchema
public void setSchema(javax.xml.validation.Schema schema);
From source file:eu.scape_project.tool.toolwrapper.data.components_spec.utils.Utils.java
/** * Method that creates a {@link Components} instance from the provided * component spec file, validating it against component spec XML Schema * /*from www . jav a 2 s .com*/ * @param componentSpecFilePath * path to the component spec file */ public static Components createComponents(String componentSpecFilePath) { Components component = null; File schemaFile = null; try { JAXBContext context = JAXBContext.newInstance(Components.class); Unmarshaller unmarshaller = context.createUnmarshaller(); // copy XML Schema from resources to a temporary location schemaFile = File.createTempFile("schema", null); FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES), schemaFile); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile); // validate provided toolspec against XML Schema unmarshaller.setSchema(schema); // unmarshal it final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath)); try { component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue(); } finally { stream.close(); } } catch (JAXBException e) { log.error("The component spec provided doesn't validate against its schema!", e); } catch (SAXException e) { log.error("The XML Schema is not valid!", e); } catch (IOException e) { log.error("An error occured while copying the XML Schema from the resources to a temporary location!", e); } catch (Exception e) { log.error("An error occured!", e); } finally { if (schemaFile != null) { schemaFile.deleteOnExit(); } } return component; }
From source file:main.java.refinement_class.Useful.java
public static boolean validation(String schema_file, String xml_file) { Object obj = null;/*from w ww .j ava 2s. c om*/ // create a JAXBContext capable of handling classes generated into // JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class ); JAXBContext jc; try { jc = JAXBContext.newInstance(); // create an Unmarshaller Unmarshaller u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { javax.xml.validation.Schema schema = sf.newSchema(new File(schema_file)); u.setSchema((javax.xml.validation.Schema) schema); u.setEventHandler(new ValidationEventHandler() { // allow unmarshalling to continue even if there are errors public boolean handleEvent(ValidationEvent ve) { // ignore warnings if (ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); } return true; } }); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); LOG.error(se.toString()); } } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); LOG.error(e.toString()); } return true; }
From source file:main.java.refinement_class.Useful.java
public static Object unmashal2(String schema_file, String xml_file, Class c) { Object obj = null;/*w ww . jav a 2s . c om*/ try { // create a JAXBContext capable of handling classes generated into // JAXBContext jc = JAXBContext.newInstance(ObjectFactory.class ); JAXBContext jc = JAXBContext.newInstance(c); // create an Unmarshaller Unmarshaller u = jc.createUnmarshaller(); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { javax.xml.validation.Schema schema = sf.newSchema(new File(schema_file)); u.setSchema((javax.xml.validation.Schema) schema); u.setEventHandler(new ValidationEventHandler() { // allow unmarshalling to continue even if there are errors public boolean handleEvent(ValidationEvent ve) { // ignore warnings if (ve.getSeverity() != ValidationEvent.WARNING) { ValidationEventLocator vel = ve.getLocator(); System.out.println("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); } return true; } }); } catch (org.xml.sax.SAXException se) { System.out.println("Unable to validate due to following error."); se.printStackTrace(); LOG.error(se.toString()); } obj = u.unmarshal(new File(xml_file)); // even though document was determined to be invalid unmarshalling, // marshal out result. // System.out.println(""); // System.out.println("Still able to marshal invalid document"); // javax.xml.bind.Marshaller m = jc.createMarshaller(); // m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); // m.marshal(poe, System.out); } catch (UnmarshalException ue) { // The JAXB specification does not mandate how the JAXB provider // must behave when attempting to unmarshal invalid XML data. // those cases, the JAXB provider is allowed to terminate the // call to unmarshal with an UnmarshalException. System.out.println("Caught UnmarshalException"); } catch (JAXBException je) { je.printStackTrace(); LOG.error(je.toString()); } return obj; }
From source file:Main.java
/** * Unmarshal XML data from XML file path using XSD string and return the resulting JAXB content tree * /*from ww w .j a va 2 s .co m*/ * @param dummyCtxObject * Dummy contect object for creating related JAXB context * @param strXMLFilePath * XML file path * @param strXSD * XSD * @return resulting JAXB content tree * @throws Exception * in error case */ public static Object doUnmarshallingFromXMLFile(Object dummyCtxObject, String strXMLFilePath, String strXSD) throws Exception { if (dummyCtxObject == null) { throw new RuntimeException("No dummy context object (null)!"); } if (strXMLFilePath == null) { throw new RuntimeException("No XML file path (null)!"); } if (strXSD == null) { throw new RuntimeException("No XSD (null)!"); } Object unmarshalledObject = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName()); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); // unmarshaller.setValidating(true); /* javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema( new java.io.File(m_strXSDFilePath)); */ StringReader reader = null; FileInputStream fis = null; try { reader = new StringReader(strXSD); javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI) .newSchema(new StreamSource(reader)); unmarshaller.setSchema(schema); fis = new FileInputStream(strXMLFilePath); unmarshalledObject = unmarshaller.unmarshal(fis); } finally { if (fis != null) { fis.close(); fis = null; } if (reader != null) { reader.close(); reader = null; } } // } catch (JAXBException e) { // //m_logger.error(e); // throw new OrderException(e); } catch (Exception e) { // Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } return unmarshalledObject; }
From source file:gov.nih.nci.ncicb.tcga.dcc.common.jaxb.JAXBUtil.java
private static Object validate(final Unmarshaller unmarshaller, final ValidationEventHandler validationEventHandler, final Reader reader, final boolean includeMetaDataSchema) throws JAXBException, SAXException, IOException { Object jaxbObject = null;/*from w w w . j a v a 2 s . com*/ try { // Set the schema for the unmarshaller final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); final Schema schema = schemaFactory .newSchema(getSchemaSources(includeMetaDataSchema).toArray(new Source[] {})); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(validationEventHandler); // Unmarshal and validate jaxbObject = unmarshaller.unmarshal(reader); } catch (UnmarshalException ue) { // Swallow the exception. The ValidationEventHandler attached to the unmarshaller will // contain the validation events logger.info(ue); } return jaxbObject; }
From source file:com.springsource.hq.plugin.tcserver.serverconfig.ProfileMarshaller.java
public Profile unmarshal(Source source) throws JAXBException, IOException { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); unmarshaller.setSchema(profileSchema); return (Profile) unmarshaller.unmarshal(source); }
From source file:com.geewhiz.pacify.managers.EntityManager.java
private PMarker unmarshal(File file) throws JAXBException { Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); jaxbUnmarshaller.setSchema(schema); return (PMarker) jaxbUnmarshaller.unmarshal(file); }
From source file:org.openwms.core.configuration.file.ApplicationPreferenceTest.java
/** * Just test to validate the given XML file against the schema declaration. If the XML file is not compliant with the schema, the test * will fail./*from w w w . j av a2s . c om*/ * * @throws Exception any error */ @Test public void testReadPreferences() throws Exception { SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = schemaFactory.newSchema(ResourceUtils.getFile("classpath:preferences.xsd")); // Schema schema = schemaFactory.newSchema(new // URL("http://www.openwms.org/schema/preferences.xsd")); JAXBContext ctx = JAXBContext.newInstance("org.openwms.core.configuration.file"); Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new ValidationEventHandler() { @Override public boolean handleEvent(ValidationEvent event) { RuntimeException ex = new RuntimeException(event.getMessage(), event.getLinkedException()); LOGGER.error(ex.getMessage()); throw ex; } }); Preferences prefs = Preferences.class.cast(unmarshaller .unmarshal(ResourceUtils.getFile("classpath:org/openwms/core/configuration/file/preferences.xml"))); for (AbstractPreference pref : prefs.getApplications()) { LOGGER.info(pref.toString()); } for (AbstractPreference pref : prefs.getModules()) { LOGGER.info(pref.toString()); } for (AbstractPreference pref : prefs.getUsers()) { LOGGER.info(pref.toString()); } }
From source file:org.ff4j.console.conf.XmlConfigurationParser.java
/** * Mthode permettant de crer le Unmarshaller pour le parsing du XML. * @param modelPackage// ww w.j a v a2 s .c om * nom du package qui contient les beans utilises pour le parsing du fichier. * @param schemafile * le fichier XSD pour la validation du XML. * @return Unmarshaller * @throws JAXBException * @throws SAXException */ public Unmarshaller getUnmarshaller(String modelPackage, File schemafile) throws JAXBException, SAXException { JAXBContext jc = JAXBContext.newInstance(modelPackage); SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(schemafile); Unmarshaller u = jc.createUnmarshaller(); u.setSchema(schema); u.setEventHandler(new XmlConfigurationErrorHandler()); return u; }
From source file:org.apache.taverna.scufl2.translator.t2flow.defaultactivities.AbstractActivityParser.java
public <ConfigType> ConfigType unmarshallElement(T2FlowParser t2FlowParser, Element element, Class<ConfigType> configType) throws ReaderException { Unmarshaller unmarshaller2 = t2FlowParser.getUnmarshaller(); unmarshaller2.setSchema(null); try {//from w w w .j av a2s . co m JAXBElement<ConfigType> configElemElem = unmarshaller2.unmarshal(element, configType); return configElemElem.getValue(); } catch (JAXBException | ClassCastException e) { throw new ReaderException("Can't parse element " + element, e); } }