Search in sources :

Example 1 with JaxbCfgHibernateConfiguration

use of org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration in project hibernate-orm by hibernate.

the class JaxbCfgProcessor method unmarshal.

@SuppressWarnings({ "unchecked" })
private JaxbCfgHibernateConfiguration unmarshal(XMLEventReader staxEventReader, final Origin origin) {
    XMLEvent event;
    try {
        event = staxEventReader.peek();
        while (event != null && !event.isStartElement()) {
            staxEventReader.nextEvent();
            event = staxEventReader.peek();
        }
    } catch (Exception e) {
        throw new HibernateException("Error accessing stax stream", e);
    }
    if (event == null) {
        throw new HibernateException("Could not locate root element");
    }
    if (!isNamespaced(event.asStartElement())) {
        // if the elements are not namespaced, wrap the reader in a reader which will namespace them as pulled.
        log.debug("cfg.xml document did not define namespaces; wrapping in custom event reader to introduce namespace information");
        staxEventReader = new NamespaceAddingEventReader(staxEventReader, HIBERNATE_CONFIGURATION_URI);
    }
    final ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(JaxbCfgHibernateConfiguration.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setSchema(schema());
        unmarshaller.setEventHandler(handler);
        return (JaxbCfgHibernateConfiguration) unmarshaller.unmarshal(staxEventReader);
    } catch (JAXBException e) {
        throw new ConfigurationException("Unable to perform unmarshalling at line number " + handler.getLineNumber() + " and column " + handler.getColumnNumber() + " in " + origin.getType().name() + " " + origin.getName() + ". Message: " + handler.getMessage(), e);
    }
}
Also used : JaxbCfgHibernateConfiguration(org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration) HibernateException(org.hibernate.HibernateException) ConfigurationException(org.hibernate.internal.util.config.ConfigurationException) JAXBException(javax.xml.bind.JAXBException) XMLEvent(javax.xml.stream.events.XMLEvent) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) ConfigurationException(org.hibernate.internal.util.config.ConfigurationException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) HibernateException(org.hibernate.HibernateException) XsdException(org.hibernate.internal.util.xml.XsdException)

Aggregations

IOException (java.io.IOException)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLEvent (javax.xml.stream.events.XMLEvent)1 HibernateException (org.hibernate.HibernateException)1 JaxbCfgHibernateConfiguration (org.hibernate.boot.jaxb.cfg.spi.JaxbCfgHibernateConfiguration)1 ConfigurationException (org.hibernate.internal.util.config.ConfigurationException)1 XsdException (org.hibernate.internal.util.xml.XsdException)1 SAXException (org.xml.sax.SAXException)1