List of usage examples for javax.xml.bind ValidationEvent WARNING
int WARNING
To view the source code for javax.xml.bind ValidationEvent WARNING.
Click Source Link
From source file:Main.java
@SuppressWarnings("rawtypes") public static void marshal(Object object, Writer w) throws JAXBException { Class clazz = object.getClass(); JAXBContext context = s_contexts.get(clazz); if (context == null) { context = JAXBContext.newInstance(clazz); s_contexts.put(clazz, context);/*from ww w . j a v a 2 s. c o m*/ } ValidationEventCollector valEventHndlr = new ValidationEventCollector(); Marshaller marshaller = context.createMarshaller(); marshaller.setSchema(null); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setEventHandler(valEventHndlr); try { marshaller.marshal(object, w); } catch (Exception e) { if (e instanceof JAXBException) { throw (JAXBException) e; } else { throw new MarshalException(e.getMessage(), e); } } if (valEventHndlr.hasEvents()) { for (ValidationEvent valEvent : valEventHndlr.getEvents()) { if (valEvent.getSeverity() != ValidationEvent.WARNING) { // throw a new Marshall Exception if there is a parsing error throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException()); } } } }
From source file:Main.java
public static <T> T unmarshal(Reader r, Class<T> clazz) throws JAXBException, XMLStreamException, FactoryConfigurationError { JAXBContext context = s_contexts.get(clazz); if (context == null) { context = JAXBContext.newInstance(clazz); s_contexts.put(clazz, context);//from www. j a va 2s . c o m } ValidationEventCollector valEventHndlr = new ValidationEventCollector(); XMLStreamReader xmlsr = XMLInputFactory.newFactory().createXMLStreamReader(r); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setSchema(null); unmarshaller.setEventHandler(valEventHndlr); JAXBElement<T> elem = null; try { elem = unmarshaller.unmarshal(xmlsr, clazz); } catch (Exception e) { if (e instanceof JAXBException) { throw (JAXBException) e; } else { throw new UnmarshalException(e.getMessage(), e); } } if (valEventHndlr.hasEvents()) { for (ValidationEvent valEvent : valEventHndlr.getEvents()) { if (valEvent.getSeverity() != ValidationEvent.WARNING) { // throw a new Unmarshall Exception if there is a parsing error String msg = MessageFormat.format("Line {0}, Col: {1}: {2}", valEvent.getLocator().getLineNumber(), valEvent.getLocator().getColumnNumber(), valEvent.getLinkedException().getMessage()); throw new UnmarshalException(msg, valEvent.getLinkedException()); } } } return elem.getValue(); }
From source file:main.java.refinement_class.Useful.java
public static Object unmashal(String schema_file, String xml_file, Class c) { Object obj = null;// w ww.j av a 2 s . 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 { // LOG.info("\n\n XX -->> Schema file: " + schema_file); // LOG.info("\n\n XX -->> xml_file file: " + xml_file); //++ javax.xml.validation.Schema schema; if (schema_file.contains("/tmp/")) { schema = sf.newSchema(new File(schema_file)); } else { URL urlSchema = getUrl(H2mserviceImpl.class, schema_file); // LOG.info("\n\n XX -->> urlSchema: " + urlSchema.getPath()); schema = sf.newSchema(urlSchema); } //-- //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("===>[1]ERROR Unmashaling \n\n" + se.toString()); LOG.error(Useful.getStackTrace(se)); } catch (Exception e) { LOG.error("===>[2]ERROR Unmashaling \n\n" + e.toString()); LOG.error(Useful.getStackTrace(e)); } if (xml_file.contains("/tmp/")) { obj = u.unmarshal(new File(xml_file)); } else { URL url_xml_file = getUrl(H2mserviceImpl.class, xml_file); LOG.info("\n\n XX -->> url_xml_file: " + url_xml_file.getPath()); obj = u.unmarshal(url_xml_file); } //-- //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"); LOG.error("===>[3]ERROR Unmashaling \n\n" + ue.toString()); } catch (JAXBException je) { je.printStackTrace(); LOG.error("===>[4]ERROR Unmashaling \n\n" + je.toString()); LOG.error(Useful.getStackTrace(je)); } catch (Exception e) { LOG.error("===>[5]ERROR Unmashaling \n\n" + e.toString()); LOG.error(Useful.getStackTrace(e)); } if (obj == null) { LOG.error("===>[6]ERROR Unmashaling Object NULL"); } return obj; }
From source file:ee.ria.xroad.common.message.SoapParserImpl.java
@SuppressWarnings("unchecked") static <T> T unmarshalHeader(Class<?> clazz, SOAPHeader soapHeader, boolean checkRequiredFields) throws Exception { Unmarshaller unmarshaller = JaxbUtils.createUnmarshaller(clazz); if (checkRequiredFields) { unmarshaller.setListener(new RequiredHeaderFieldsChecker(clazz)); }/*from w ww . jav a 2s . c o m*/ unmarshaller.setEventHandler(event -> { switch (event.getSeverity()) { case ValidationEvent.WARNING: return true; case ValidationEvent.ERROR: Throwable t = event.getLinkedException(); return !(t instanceof AccessorException && t.getCause() instanceof CodedException); case ValidationEvent.FATAL_ERROR: return false; default: return true; } }); JAXBElement<T> jaxbElement = (JAXBElement<T>) unmarshaller.unmarshal(soapHeader, clazz); return jaxbElement.getValue(); }
From source file:org.cleverbus.core.common.asynch.TraceHeaderProcessor.java
private ValidationEventHandler getValidationEventHandler() { return new ValidationEventHandler() { public boolean handleEvent(ValidationEvent event) { if (event.getSeverity() == ValidationEvent.WARNING) { Log.warn("Ignored {}", event, event.getLinkedException()); return true; // handled - ignore as WARNING does not prevent unmarshalling } else { return false; // not handled - ERROR and FATAL_ERROR prevent successful unmarshalling }// w w w. ja va 2 s . co m } }; }
From source file:main.java.refinement_class.Useful.java
public static Object unmashal2(String schema_file, String xml_file, Class c) { Object obj = null;//from ww w . j a v a 2s.co m 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.refinement_class.Useful.java
public static boolean validation(String schema_file, String xml_file) { Object obj = null;/* www . j av a2s . 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:org.cloudgraph.config.CloudGraphConfigValidationEventHandler.java
public boolean handleEvent(ValidationEvent ve) { boolean result = this.cumulative; this.errorCount++; ValidationEventLocator vel = ve.getLocator(); String message = "Line:Col:Offset[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + ":" + String.valueOf(vel.getOffset()) + "] - " + ve.getMessage(); switch (ve.getSeverity()) { case ValidationEvent.WARNING: log.warn(message);//from w w w.ja v a 2s . c o m break; case ValidationEvent.ERROR: case ValidationEvent.FATAL_ERROR: log.fatal(message); throw new CloudGraphConfigurationException(message); default: log.error(message); } return result; }
From source file:org.cloudgraph.store.mapping.StoreMappingValidationEventHandler.java
public boolean handleEvent(ValidationEvent ve) { boolean result = this.cumulative; this.errorCount++; ValidationEventLocator vel = ve.getLocator(); String message = "Line:Col:Offset[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + ":" + String.valueOf(vel.getOffset()) + "] - " + ve.getMessage(); switch (ve.getSeverity()) { case ValidationEvent.WARNING: log.warn(message);/*from w ww . j av a 2 s. c o m*/ break; case ValidationEvent.ERROR: case ValidationEvent.FATAL_ERROR: log.fatal(message); throw new StoreMappingException(message); default: log.error(message); } return result; }
From source file:org.eclipse.smila.connectivity.framework.schema.internal.JaxbPluginContext.java
/** * Creates the validating unmarshaller./* w w w . jav a 2 s . c om*/ * * @return the unmarshaller * * @throws JAXBException * the JAXB exception * @throws SchemaNotFoundException * the index order schema not found exception */ public Unmarshaller createValidatingUnmarshaller() throws JAXBException, SchemaNotFoundException { initilize(); assertNotNull(_context); final Unmarshaller unmarshaller = _context.createUnmarshaller(); final SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI); try { sf.setResourceResolver(new XSDContextURIResolver(sf.getResourceResolver())); final javax.xml.validation.Schema schema = sf .newSchema(Platform.getBundle(_id).getEntry(_plugIn.getSchemaLocation())); unmarshaller.setSchema(schema); unmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(final ValidationEvent ve) { if (ve.getSeverity() != ValidationEvent.WARNING) { final ValidationEventLocator vel = ve.getLocator(); _log.error("Line:Col[" + vel.getLineNumber() + ":" + vel.getColumnNumber() + "]:" + ve.getMessage()); return false; } return true; } }); } catch (final org.xml.sax.SAXException se) { throw new SchemaRuntimeException("Unable to validate due to following error.", se); } return unmarshaller; }