List of usage examples for javax.xml.bind UnmarshalException UnmarshalException
public UnmarshalException(String message, Throwable exception)
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);/* w w w . j a v a 2 s . com*/ } 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(); }