diff --git a/build.gradle b/build.gradle index 1ca25ec6da..2c39bd49e3 100644 --- a/build.gradle +++ b/build.gradle @@ -123,7 +123,7 @@ subprojects { subProject -> // appropriately inject the common dependencies into each sub-project dependencies { - compile( libraries.logging ) + compile( libraries.logging ) testCompile( libraries.junit ) testRuntime( libraries.slf4j_api ) testRuntime( libraries.slf4j_log4j12 ) diff --git a/hibernate-core/hibernate-core.gradle b/hibernate-core/hibernate-core.gradle index b07d697927..f84d3585d6 100644 --- a/hibernate-core/hibernate-core.gradle +++ b/hibernate-core/hibernate-core.gradle @@ -52,7 +52,14 @@ task jaxb << { ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath) ant.jaxbTargetDir = jaxbTargetDir - ant.xjc(destdir: '${jaxbTargetDir}', package: 'org.hibernate.metamodel.source.xml') { + + ant.xjc(destdir: '${jaxbTargetDir}', package: 'org.hibernate.metamodel.source.hbm.xml.config') { + schema(dir: 'src/main/resources/org/hibernate', includes: 'hibernate-configuration-3.0.xsd') + } + ant.xjc(destdir: '${jaxbTargetDir}', package: 'org.hibernate.metamodel.source.hbm.xml.mapping') { + schema(dir: 'src/main/resources/org/hibernate', includes: 'hibernate-mapping-3.0.xsd') + } + ant.xjc(destdir: '${jaxbTargetDir}', package: 'org.hibernate.metamodel.source.annotation.xml') { schema(dir: 'src/main/resources/org/hibernate/ejb', includes: 'orm_2_0.xsd') } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java index e4d9ad74b8..526f52cfd0 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/Metadata.java @@ -29,13 +29,12 @@ import java.io.Serializable; import java.util.HashMap; import java.util.Map; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.jboss.logging.Logger; import org.hibernate.DuplicateMappingException; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.cfg.EJB3NamingStrategy; import org.hibernate.cfg.NamingStrategy; +import org.hibernate.internal.CoreMessageLogger; import org.hibernate.mapping.FetchProfile; import org.hibernate.mapping.MetadataSource; import org.hibernate.metamodel.binding.EntityBinding; @@ -44,16 +43,16 @@ import org.hibernate.metamodel.relational.Database; import org.hibernate.metamodel.source.annotations.AnnotationBinder; import org.hibernate.metamodel.source.hbm.HibernateXmlBinder; - /** - * TODO : javadoc + * Container for configuration data while building and binding the metamodel * * @author Steve Ebersole */ public class Metadata implements Serializable { - private static final Logger log = LoggerFactory.getLogger( Metadata.class ); + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, Metadata.class.getName() + ); - private static final HibernateLogger LOG = Logger.getMessageLogger(HibernateLogger.class, Metadata.class.getName()); private final AnnotationBinder annotationBinder; private final HibernateXmlBinder hibernateXmlBinder; private final ExtendsQueue extendsQueue; @@ -139,10 +138,10 @@ public class Metadata implements Serializable { if ( imports == null ) { imports = new HashMap(); } - log.trace( "Import: " + importName + " -> " + entityName ); + LOG.trace( "Import: " + importName + " -> " + entityName ); String old = imports.put( importName, entityName ); if ( old != null ) { - log.debug( "import name [{}] overrode previous [{}]", importName, old ); + LOG.debug( "import name [" + importName + "] overrode previous [{" + old + "}]" ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/MetadataSourceQueue.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/MetadataSourceQueue.java index 3c1b7b9c94..cf67136adf 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/MetadataSourceQueue.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/MetadataSourceQueue.java @@ -38,25 +38,24 @@ import java.util.Set; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.jboss.logging.Logger; -import org.hibernate.internal.CoreMessageLogger; import org.hibernate.InvalidMappingException; import org.hibernate.MappingException; import org.hibernate.cfg.MetadataSourceType; +import org.hibernate.internal.CoreMessageLogger; import org.hibernate.internal.util.collections.JoinedIterator; import org.hibernate.internal.util.xml.XmlDocument; - /** - * TODO : javadoc + * Container for xml configuration documents and annotated classes. * * @author Steve Ebersole */ public class MetadataSourceQueue implements Serializable { - private static final Logger log = LoggerFactory.getLogger( MetadataSourceQueue.class ); - private static final CoreMessageLogger LOG = Logger.getMessageLogger(CoreMessageLogger.class, MetadataSourceQueue.class.getName()); + private static final CoreMessageLogger LOG = Logger.getMessageLogger( + CoreMessageLogger.class, MetadataSourceQueue.class.getName() + ); private final Metadata metadata; private LinkedHashMap> hbmMetadataToEntityNamesMap @@ -140,7 +139,7 @@ public class MetadataSourceQueue implements Serializable { } private void processHbmXmlQueue() { - log.debug( "Processing hbm.xml files" ); + LOG.debug( "Processing hbm.xml files" ); for ( Map.Entry> entry : hbmMetadataToEntityNamesMap.entrySet() ) { // Unfortunately we have to create a Mappings instance for each iteration here processHbmXml( entry.getKey(), entry.getValue() ); @@ -163,7 +162,7 @@ public class MetadataSourceQueue implements Serializable { } private void processAnnotatedClassesQueue() { - log.debug( "Process annotated classes" ); + LOG.debug( "Process annotated classes" ); annotatedClasses.clear(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java index caba83f7fa..5ea79fbe3a 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/annotations/OrmXmlParser.java @@ -1,31 +1,21 @@ package org.hibernate.metamodel.source.annotations; import java.io.InputStream; -import java.net.URL; import java.util.HashSet; import java.util.Set; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; -import javax.xml.validation.Schema; -import javax.xml.validation.SchemaFactory; import org.jboss.jandex.Index; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; import org.hibernate.AnnotationException; -import org.hibernate.metamodel.source.xml.EntityMappings; +import org.hibernate.metamodel.source.annotation.xml.EntityMappings; +import org.hibernate.metamodel.source.util.xml.XmlHelper; /** * @author Hardy Ferentschik * @todo Need some create some XMLContext as well which can be populated w/ information which can not be expressed via annotations */ public class OrmXmlParser { - private static final Logger log = LoggerFactory.getLogger( OrmXmlParser.class ); private static final String ORM1_MAPPING_XSD = "org/hibernate/ejb/orm_1_0.xsd"; private static final String ORM2_MAPPING_XSD = "org/hibernate/ejb/orm_2_0.xsd"; @@ -42,65 +32,25 @@ public class OrmXmlParser { Set mappingStreams = new HashSet(); for ( String fileName : mappingFileNames ) { - Schema schema = getMappingSchema( ORM2_MAPPING_XSD ); - InputStream in = getInputStreamForPath( fileName ); EntityMappings entityMappings; try { - entityMappings = unmarshallXml( in, schema ); + entityMappings = XmlHelper.unmarshallXml( fileName, ORM2_MAPPING_XSD, EntityMappings.class ).getRoot(); } catch ( JAXBException orm2Exception ) { // if we cannot parse against orm_2_0.xsd we try orm_1_0.xsd for backwards compatibility try { - schema = getMappingSchema( ORM1_MAPPING_XSD ); - in = getInputStreamForPath( fileName ); - entityMappings = unmarshallXml( in, schema ); + entityMappings = XmlHelper.unmarshallXml( fileName, ORM1_MAPPING_XSD, EntityMappings.class ).getRoot(); } catch ( JAXBException orm1Exception ) { throw new AnnotationException( "Unable to parse xml configuration.", orm1Exception ); } } - // .. + entityMappings.toString(); } return null; } - - private InputStream getInputStreamForPath(String path) { - // try the context class loader first - InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream( path ); - - // try the current class loader - if ( inputStream == null ) { - inputStream = OrmXmlParser.class.getResourceAsStream( path ); - } - return inputStream; - } - - private EntityMappings unmarshallXml(InputStream in, Schema schema) throws JAXBException { - EntityMappings entityMappings; - JAXBContext jc = JAXBContext.newInstance( EntityMappings.class ); - Unmarshaller unmarshaller = jc.createUnmarshaller(); - unmarshaller.setSchema( schema ); - StreamSource stream = new StreamSource( in ); - JAXBElement root = unmarshaller.unmarshal( stream, EntityMappings.class ); - entityMappings = root.getValue(); - return entityMappings; - } - - private Schema getMappingSchema(String schemaVersion) { - ClassLoader loader = OrmXmlParser.class.getClassLoader(); - URL schemaUrl = loader.getResource( schemaVersion ); - SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); - Schema schema = null; - try { - schema = sf.newSchema( schemaUrl ); - } - catch ( SAXException e ) { - log.debug( "Unable to create schema for {}: {}", ORM2_MAPPING_XSD, e.getMessage() ); - } - return schema; - } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java index 6c0eb26540..4b94e5a5be 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/hbm/HibernateXmlBinder.java @@ -32,6 +32,8 @@ import java.util.Map; import java.util.Set; import org.dom4j.Element; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.hibernate.cfg.ExtendsQueueEntry; import org.hibernate.internal.util.StringHelper; @@ -39,6 +41,7 @@ import org.hibernate.internal.util.collections.JoinedIterator; import org.hibernate.internal.util.xml.XmlDocument; import org.hibernate.mapping.MetaAttribute; import org.hibernate.metamodel.source.Metadata; +import org.hibernate.metamodel.source.hbm.xml.mapping.HibernateMapping; /** * Binder for {@code hbm.xml} files @@ -46,6 +49,8 @@ import org.hibernate.metamodel.source.Metadata; * @author Steve Ebersole */ public class HibernateXmlBinder { + private static final Logger log = LoggerFactory.getLogger( HibernateXmlBinder.class ); + private final Metadata metadata; private final Map globalMetas; @@ -62,6 +67,11 @@ public class HibernateXmlBinder { bindRoot( metadataXml, Collections.emptySet() ); } + public void bindRoot(HibernateMapping mapping) { + // todo - process the mapping + log.debug( mapping.toString() ); + } + public void bindRoot(XmlDocument metadataXml, Set entityNames) { final HibernateMappingBinder mappingBinder = new HibernateMappingBinder( this, metadataXml ); @@ -69,7 +79,8 @@ public class HibernateXmlBinder { if ( !names.isEmpty() ) { // classes mentioned in extends not available - so put it in queue for ( String name : names ) { - metadata.getExtendsQueue().add( new ExtendsQueueEntry( name, mappingBinder.getPackageName(), metadataXml, entityNames ) ); + metadata.getExtendsQueue() + .add( new ExtendsQueueEntry( name, mappingBinder.getPackageName(), metadataXml, entityNames ) ); } return; } @@ -106,7 +117,11 @@ public class HibernateXmlBinder { // mappings might contain either the "raw" extends name (in the case of // an entity-name mapping) or a FQN (in the case of a POJO mapping). if ( getMetadata().getEntityBinding( extendsName ) == null - && getMetadata().getEntityBinding( HbmHelper.getClassName( extendsName, unqualifiedPackageName ) ) == null ) { + && getMetadata().getEntityBinding( + HbmHelper.getClassName( + extendsName, unqualifiedPackageName + ) + ) == null ) { awaitingExtends.add( extendsName ); } } @@ -160,7 +175,7 @@ public class HibernateXmlBinder { Element element = (Element) classIterator.next(); handler.handleEntity( element.attributeValue( "entity-name" ), - element.attributeValue( "name" ) + element.attributeValue( "name" ) ); recognizeEntities( element, handler ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/DomHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/DomHelper.java index 16c1d671d0..5d36a02260 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/DomHelper.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/DomHelper.java @@ -1,7 +1,7 @@ /* * Hibernate, Relational Persistence for Idiomatic Java * - * Copyright (c) 2010, Red Hat Inc. or third-party contributors as + * Copyright (c) 2011, 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. @@ -27,11 +27,14 @@ import org.dom4j.Attribute; import org.dom4j.Element; /** - * TODO : javadoc + * Helper class for working with DOM documents. * * @author Steve Ebersole */ public class DomHelper { + private DomHelper() { + } + public static String extractAttributeValue(Element element, String attributeName) { return extractAttributeValue( element, attributeName, null ); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRoot.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRoot.java new file mode 100644 index 0000000000..8fd807e438 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRoot.java @@ -0,0 +1,45 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.util.xml; + +/** + * Describes a parsed xml document. + * + * @author Hardy Ferentschik + */ +public interface JaxbRoot { + /** + * Retrieve the jaxb root + * + * @return the jaxb root object + */ + public T getRoot(); + + /** + * Retrieve the document's origin. + * + * @return The origin + */ + public Origin getOrigin(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRootImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRootImpl.java new file mode 100644 index 0000000000..6eb740184b --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/JaxbRootImpl.java @@ -0,0 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.util.xml; + +/** + * Basic implementation of {@link JaxbRoot}. + * + * @author Hardy Ferentschik + */ +public class JaxbRootImpl implements JaxbRoot { + private final T root; + private final Origin origin; + + public JaxbRootImpl(T root, Origin origin) { + this.root = root; + this.origin = origin; + } + + /** + * {@inheritDoc} + */ + public T getRoot() { + return root; + } + + /** + * {@inheritDoc} + */ + public Origin getOrigin() { + return origin; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/Origin.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/Origin.java new file mode 100644 index 0000000000..950fa3f1d4 --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/Origin.java @@ -0,0 +1,49 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.util.xml; + +import java.io.Serializable; + +/** + * Describes the origin of an xml document + * + * @author Steve Ebersole + */ +public interface Origin extends Serializable { + /** + * Retrieve the type of origin. This is not a discrete set, but might be somethign like + * {@code file} for file protocol URLs, or {@code resource} for classpath resource lookups. + * + * @return The origin type. + */ + public String getType(); + + /** + * The name of the document origin. Interpretation is relative to the type, but might be the + * resource name or file URL. + * + * @return The name. + */ + public String getName(); +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/OriginImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/OriginImpl.java new file mode 100644 index 0000000000..aa4aaa89ee --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/OriginImpl.java @@ -0,0 +1,53 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.util.xml; + +/** + * Basic implementation of {@link org.hibernate.internal.util.xml.Origin} + * + * @author Steve Ebersole + */ +public class OriginImpl implements Origin { + private final String type; + private final String name; + + public OriginImpl(String type, String name) { + this.type = type; + this.name = name; + } + + /** + * {@inheritDoc} + */ + public String getType() { + return type; + } + + /** + * {@inheritDoc} + */ + public String getName() { + return name; + } +} diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java new file mode 100644 index 0000000000..d5b2c08c0a --- /dev/null +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/source/util/xml/XmlHelper.java @@ -0,0 +1,89 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, 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.metamodel.source.util.xml; + +import java.io.InputStream; +import java.net.URL; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +/** + * @author Hardy Ferentschik + */ +public class XmlHelper { + private static final Logger log = LoggerFactory.getLogger( XmlHelper.class ); + + private XmlHelper() { + } + + public static JaxbRoot unmarshallXml(String fileName, String schemaName, Class clazz) + throws JAXBException { + Schema schema = getMappingSchema( schemaName ); + InputStream in = getInputStreamForPath( fileName ); + JAXBContext jc = JAXBContext.newInstance( clazz ); + Unmarshaller unmarshaller = jc.createUnmarshaller(); + unmarshaller.setSchema( schema ); + StreamSource stream = new StreamSource( in ); + JAXBElement elem = unmarshaller.unmarshal( stream, clazz ); + Origin origin = new OriginImpl( "", fileName ); + return new JaxbRootImpl( elem.getValue(), origin ); + } + + private static Schema getMappingSchema(String schemaVersion) { + // todo - think about class loading. does this have to go via the class loader service? + ClassLoader loader = XmlHelper.class.getClassLoader(); + URL schemaUrl = loader.getResource( schemaVersion ); + SchemaFactory sf = SchemaFactory.newInstance( javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI ); + Schema schema = null; + try { + schema = sf.newSchema( schemaUrl ); + } + catch ( SAXException e ) { + log.debug( "Unable to create schema for {}: {}", schemaVersion, e.getMessage() ); + } + return schema; + } + + private static InputStream getInputStreamForPath(String path) { + // try the context class loader first + InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream( path ); + + // try the current class loader + if ( inputStream == null ) { + inputStream = XmlHelper.class.getResourceAsStream( path ); + } + return inputStream; + } +} + + diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java index 39a33d9697..5fce773913 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/BasicHbmBindingTests.java @@ -23,6 +23,10 @@ */ package org.hibernate.metamodel.binding; +import javax.xml.bind.JAXBException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.xml.sax.InputSource; import org.hibernate.internal.util.ConfigHelper; @@ -31,6 +35,10 @@ import org.hibernate.internal.util.xml.Origin; import org.hibernate.internal.util.xml.XMLHelper; import org.hibernate.internal.util.xml.XmlDocument; import org.hibernate.metamodel.source.Metadata; +import org.hibernate.metamodel.source.hbm.xml.mapping.HibernateMapping; +import org.hibernate.metamodel.source.util.xml.XmlHelper; + +import static org.junit.Assert.fail; /** * Basic tests of {@code hbm.xml} binding code @@ -38,6 +46,7 @@ import org.hibernate.metamodel.source.Metadata; * @author Steve Ebersole */ public class BasicHbmBindingTests extends AbstractBasicBindingTests { + private static final Logger log = LoggerFactory.getLogger( BasicHbmBindingTests.class ); public EntityBinding buildSimpleEntityBinding() { Metadata metadata = new Metadata(); @@ -50,8 +59,23 @@ public class BasicHbmBindingTests extends AbstractBasicBindingTests { public EntityBinding buildSimpleVersionedEntityBinding() { Metadata metadata = new Metadata(); - XmlDocument xmlDocument = readResource( "/org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml" ); + String fileName = "/org/hibernate/metamodel/binding/SimpleVersionedEntity.hbm.xml"; + XmlDocument xmlDocument = readResource( fileName ); metadata.getHibernateXmlBinder().bindRoot( xmlDocument ); + + // todo - just temporary to show how things would look like with JAXB + fileName = "/org/hibernate/metamodel/binding/SimpleVersionedEntity.xml"; + final String HIBERNATE_MAPPING_XSD = "org/hibernate/hibernate-mapping-3.0.xsd"; + HibernateMapping mapping = null; + try { + mapping = XmlHelper.unmarshallXml( fileName, HIBERNATE_MAPPING_XSD, HibernateMapping.class ).getRoot(); + } + catch ( JAXBException e ) { + log.debug( e.getMessage() ); + fail( "Unable to load xml " + fileName ); + } + metadata.getHibernateXmlBinder().bindRoot( mapping ); + return metadata.getEntityBinding( SimpleVersionedEntity.class.getName() ); } diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleVersionedEntity.xml b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleVersionedEntity.xml new file mode 100644 index 0000000000..6e6df5d98f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/binding/SimpleVersionedEntity.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java index 3f3bdb78b8..f81a3f3a2e 100644 --- a/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java +++ b/hibernate-core/src/test/java/org/hibernate/metamodel/source/annotations/ConfiguredClassHierarchyBuilderTest.java @@ -75,7 +75,6 @@ public class ConfiguredClassHierarchyBuilderTest extends BaseUnitTestCase { assertEquals( "wrong class", DotName.createSimple( MappedSuperClass.class.getName() ), info.name() ); info = iter.next().getClassInfo(); assertEquals( "wrong class", DotName.createSimple( UnmappedSubClass.class.getName() ), info.name() ); - assertFalse( iter.hasNext() ); info = iter.next().getClassInfo(); assertEquals( "wrong class", DotName.createSimple( MappedSubClass.class.getName() ), info.name() ); assertFalse( iter.hasNext() );