List of usage examples for javax.xml.bind ValidationEvent getSeverity
public int getSeverity();
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);// w ww . j a v a2s.c om } 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 w w w . j a v a 2 s . 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:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java
private static boolean validationFailed(ValidationEvent event) { if (event.getSeverity() == ValidationEvent.FATAL_ERROR) { return false; }/*from w w w . j a v a 2 s. co m*/ if (event.getSeverity() == ValidationEvent.ERROR && event.getLinkedException() instanceof AccessorException && event.getLinkedException().getCause() instanceof CodedException) { return false; } return true; }
From source file:main.java.refinement_class.Useful.java
public static boolean validation(String schema_file, String xml_file) { Object obj = null;//from ww w . j a v a2s. c o m // 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;/*from w ww . j a v a 2s . c o 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 Object unmashal(String schema_file, String xml_file, Class c) { Object obj = null;/* w w w. j a va 2 s. c o 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 { // 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:com.rapid.server.RapidHttpServlet.java
public static Unmarshaller getUnmarshaller() throws JAXBException, IOException { // initialise the unmarshaller Unmarshaller unmarshaller = _jaxbContext.createUnmarshaller(); // add the encrypted xml adapter unmarshaller.setAdapter(_encryptedXmlAdapter); // add a validation listener (this makes for better error messages) unmarshaller.setEventHandler(new ValidationEventHandler() { @Override//from ww w . ja va 2s . co m public boolean handleEvent(ValidationEvent event) { // get the location ValidationEventLocator location = event.getLocator(); // log _logger.debug("JAXB validation event - " + event.getMessage() + (location == null ? "" : " at line " + location.getLineNumber() + ", column " + location.getColumnNumber() + ", node " + location.getNode())); // messages with "unrecognized type name" are very useful they're not sever themselves must almost always followed by a severe with a less meaningful message if (event.getMessage().contains("unrecognized type name") || event.getSeverity() == ValidationEvent.FATAL_ERROR) { return false; } else { return true; } } }); // return return unmarshaller; }
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 }/*from w ww . j a va 2 s. co m*/ } }; }
From source file:org.alfresco.repo.audit.model.AuditModelRegistryImpl.java
/** * Unmarshalls the Audit model from a stream. *///from w ww . j av a2s .co m private static Audit unmarshallModel(InputStream is, final String source) { final Schema schema; final JAXBContext jaxbCtx; final Unmarshaller jaxbUnmarshaller; try { SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); schema = sf.newSchema(ResourceUtils.getURL(AUDIT_SCHEMA_LOCATION)); jaxbCtx = JAXBContext.newInstance("org.alfresco.repo.audit.model._3"); jaxbUnmarshaller = jaxbCtx.createUnmarshaller(); jaxbUnmarshaller.setSchema(schema); jaxbUnmarshaller.setEventHandler(new ValidationEventHandler() { public boolean handleEvent(ValidationEvent ve) { if (ve.getSeverity() == ValidationEvent.FATAL_ERROR || ve.getSeverity() == ValidationEvent.ERROR) { ValidationEventLocator locator = ve.getLocator(); logger.error("Invalid Audit XML: \n" + " Source: " + source + "\n" + " Location: Line " + locator.getLineNumber() + " column " + locator.getColumnNumber() + "\n" + " Error: " + ve.getMessage()); } return false; } }); } catch (Throwable e) { throw new AlfrescoRuntimeException("Failed to load Alfresco Audit Schema from " + AUDIT_SCHEMA_LOCATION, e); } try { // Unmarshall with validation @SuppressWarnings("unchecked") JAXBElement<Audit> auditElement = (JAXBElement<Audit>) jaxbUnmarshaller.unmarshal(is); Audit audit = auditElement.getValue(); // Done return audit; } catch (Throwable e) { // Dig out a SAXParseException, if there is one Throwable saxError = ExceptionStackUtil.getCause(e, SAXParseException.class); if (saxError != null) { e = saxError; } throw new AuditModelException("Failed to read Audit model XML: \n" + " Source: " + source + "\n" + " Error: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { } } }
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);/* ww w . j av 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; }