Skip to content

Commit

Permalink
merge master and fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stliu committed Mar 25, 2013
1 parent 72c5f8f commit d06f72a
Show file tree
Hide file tree
Showing 9 changed files with 296 additions and 413 deletions.
Expand Up @@ -52,7 +52,7 @@ public class ConfigLoader {
new ValueHolder.DeferredInitializer<JaxbConfigurationProcessor>() {
@Override
public JaxbConfigurationProcessor initialize() {
return new JaxbConfigurationProcessor( bootstrapServiceRegistry.getService( ClassLoaderService.class ) );
return new JaxbConfigurationProcessor( bootstrapServiceRegistry );
}
}
);
Expand All @@ -66,7 +66,7 @@ public JaxbHibernateConfiguration loadConfigXmlResource(String cfgXmlResourceNam
if ( stream == null ) {
throw new ConfigurationException( "Could not locate cfg.xml resource [" + cfgXmlResourceName + "]" );
}
return jaxbProcessorHolder.getValue().unmarshal( stream, new Origin( SourceType.RESOURCE, cfgXmlResourceName ) );
return (JaxbHibernateConfiguration)jaxbProcessorHolder.getValue().unmarshal( stream, new Origin( SourceType.RESOURCE, cfgXmlResourceName ) ).getRoot();
}

public Properties loadProperties(String resourceName) {
Expand Down
Expand Up @@ -23,14 +23,10 @@
*/
package org.hibernate.internal.util.xml;

import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.xml.sax.InputSource;

import org.jboss.logging.Logger;

import org.hibernate.internal.CoreMessageLogger;
Expand Down
Expand Up @@ -265,16 +265,19 @@ public Schema getSchema() {
return schema;
}
}

private static URL resolveLocalSchemaUrl(String schemaName) {
//these should move to a helper class
public static URL resolveLocalSchemaUrl(String schemaName) {
URL url = MappingReader.class.getClassLoader().getResource( schemaName );
if ( url == null ) {
throw new XmlInfrastructureException( "Unable to locate schema [" + schemaName + "] via classpath" );
}
return url;
}
public static Schema resolveLocalSchema(String schemaName){
return resolveLocalSchema( resolveLocalSchemaUrl( schemaName ) );

private static Schema resolveLocalSchema(URL schemaUrl) {
}
public static Schema resolveLocalSchema(URL schemaUrl) {

try {
InputStream schemaStream = schemaUrl.openStream();
Expand Down
@@ -0,0 +1,200 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jaxb.internal;

import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.bind.ValidationEventLocator;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;

import org.jboss.logging.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.xml.LocalXmlResourceResolver;
import org.hibernate.internal.util.xml.MappingReader;
import org.hibernate.jaxb.spi.JaxbRoot;
import org.hibernate.jaxb.spi.Origin;
import org.hibernate.jaxb.spi.hbm.JaxbHibernateMapping;
import org.hibernate.jaxb.spi.orm.JaxbEntityMappings;
import org.hibernate.metamodel.spi.source.MappingException;
import org.hibernate.service.ServiceRegistry;

/**
* @author Strong Liu <stliu@hibernate.org>
*/
abstract class AbstractJaxbProcessor {
protected static final Logger log = Logger.getLogger( AbstractJaxbProcessor.class );

// public static final String VALIDATE_XML_SETTING = "hibernate.xml.validate";

protected final ServiceRegistry serviceRegistry;
protected final boolean validateXml;

public AbstractJaxbProcessor(ServiceRegistry serviceRegistry) {
this( serviceRegistry, true );
// this(
// serviceRegistry,
// serviceRegistry.getService( ConfigurationService.class ).getSetting(
// VALIDATE_XML_SETTING,
// StandardConverters.BOOLEAN,
// true
// )
// );
}

public AbstractJaxbProcessor(ServiceRegistry serviceRegistry, boolean validateXml) {
this.serviceRegistry = serviceRegistry;
this.validateXml = validateXml;
}

public JaxbRoot unmarshal(InputStream stream, Origin origin) {
try {
XMLEventReader staxReader = staxFactory().createXMLEventReader( stream );
try {
return unmarshal( staxReader, origin );
}
finally {
try {
staxReader.close();
}
catch ( Exception ignore ) {
}
}
}
catch ( XMLStreamException e ) {
throw new MappingException( "Unable to create stax reader", e, origin );
}
}

private XMLInputFactory staxFactory;

private XMLInputFactory staxFactory() {
if ( staxFactory == null ) {
staxFactory = buildStaxFactory();
}
return staxFactory;
}

@SuppressWarnings( { "UnnecessaryLocalVariable" })
private XMLInputFactory buildStaxFactory() {
XMLInputFactory staxFactory = XMLInputFactory.newInstance();
staxFactory.setXMLResolver( LocalXmlResourceResolver.INSTANCE );
return staxFactory;
}

private static final QName ORM_VERSION_ATTRIBUTE_QNAME = new QName( "version" );

@SuppressWarnings( { "unchecked" })
private JaxbRoot 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 MappingException( "Error accessing stax stream", e, origin );
}

if ( event == null ) {
throw new MappingException( "Could not locate root element", origin );
}

staxEventReader = wrapReader( staxEventReader, event );

final Object target;
final ContextProvidingValidationEventHandler handler = new ContextProvidingValidationEventHandler();
try {
JAXBContext jaxbContext =getJaxbContext(event);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setSchema( getSchema(event, origin) );
unmarshaller.setEventHandler( handler );
target = unmarshaller.unmarshal( staxEventReader );
}
catch ( JAXBException e ) {
StringBuilder builder = new StringBuilder();
builder.append( "Unable to perform unmarshalling at line number " );
builder.append( handler.getLineNumber() );
builder.append( " and column " );
builder.append( handler.getColumnNumber() );
builder.append( ". Message: " );
builder.append( handler.getMessage() );
throw new MappingException( builder.toString(), e, origin );
}

return new JaxbRoot( target, origin );
}
protected abstract JAXBContext getJaxbContext(XMLEvent event) throws JAXBException;
protected abstract Schema getSchema(XMLEvent event, Origin origin) throws JAXBException;
protected XMLEventReader wrapReader(XMLEventReader xmlEventReader, XMLEvent event){
return xmlEventReader;
}
protected static boolean isNamespaced(StartElement startElement) {
return ! "".equals( startElement.getName().getNamespaceURI() );
}


static class ContextProvidingValidationEventHandler implements ValidationEventHandler {
private int lineNumber;
private int columnNumber;
private String message;

@Override
public boolean handleEvent(ValidationEvent validationEvent) {
ValidationEventLocator locator = validationEvent.getLocator();
lineNumber = locator.getLineNumber();
columnNumber = locator.getColumnNumber();
message = validationEvent.getMessage();
return false;
}

public int getLineNumber() {
return lineNumber;
}

public int getColumnNumber() {
return columnNumber;
}

public String getMessage() {
return message;
}
}
}

0 comments on commit d06f72a

Please sign in to comment.