diff --git a/hibernate/src/main/org/apache/maven/hibernate/HibernateEntityResolver.java b/hibernate/src/main/org/apache/maven/hibernate/HibernateEntityResolver.java new file mode 100644 index 00000000..00732c2f --- /dev/null +++ b/hibernate/src/main/org/apache/maven/hibernate/HibernateEntityResolver.java @@ -0,0 +1,106 @@ +package org.apache.maven.hibernate; + +/* ==================================================================== + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +import java.io.IOException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * A class to resolve external entity definitions for j2ee artifacts. + * Based on the J2EEEntityResolver from the J2EE plugin + * + * @author eric pugh + * @version $Id: HibernateEntityResolver.java,v 1.1 2004/07/23 11:25:25 epugh Exp $ + */ +public class HibernateEntityResolver implements EntityResolver +{ + /** map of ids to resource names */ + private Map idToResource = new HashMap(); + + /** log for debug output */ + private static final Log LOG = LogFactory.getLog(HibernateEntityResolver.class); + + /** list of j2ee dtds that are being made available */ + public static final String[] HIBERNATE_DTDS = new String[] { + "-//Hibernate/Hibernate Mapping DTD 2.0//EN" + }; + public static final String[] HIBERNATE_RESOURCES = new String[] { + "/plugin-resources/hibernate-mapping-2.0.dtd" + }; + + /** Creates a new instance of EntityResolver */ + public HibernateEntityResolver() + { + for (int i = 0; i < HIBERNATE_DTDS.length; i++) { + idToResource.put(HIBERNATE_DTDS[i], HIBERNATE_RESOURCES[i]); + } + } + + /** resolve the entity given by the provided Ids + * @param publicId the public id of the entity + * @param systemId the 'system location' (typically a URL) of the entity + * @return an {@link InputSource input source} for retrieval of the entity + * @throws IOException when an I/O error occurs retrieving the entity + * @throws SAXException if there are any problems + */ + public InputSource resolveEntity(String publicId, String systemId) throws + SAXException, IOException + { + LOG.debug("resolving entity with publicId='" + publicId + ", systemId='" + systemId + "'"); + if (publicId != null) + { + String resource = (String) idToResource.get(publicId); + LOG.debug("resource found in map ='" + resource + "'" ); + if (resource != null) + { + InputStream in = getClass().getResourceAsStream(resource); + LOG.debug("input stream ='" + in + "'" ); + if (in != null) + { + return new InputSource(in); + } + } + } + return null; + } + + /** Getter for publicId to resource name map. + * @return Value of property idToResource. + */ + protected Map getIdToResource() + { + return idToResource; + } + + /** Setter for publicId to resource name map. + * @param idToResource New value of property idToResource. + */ + protected void setIdToResource(Map idToResource) + { + this.idToResource = idToResource; + } + +} diff --git a/hibernate/src/main/org/apache/maven/hibernate/beans/MappingsAggregatorBean.java b/hibernate/src/main/org/apache/maven/hibernate/beans/MappingsAggregatorBean.java index 1cf78caf..8109d589 100644 --- a/hibernate/src/main/org/apache/maven/hibernate/beans/MappingsAggregatorBean.java +++ b/hibernate/src/main/org/apache/maven/hibernate/beans/MappingsAggregatorBean.java @@ -23,6 +23,7 @@ import java.util.Iterator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.maven.hibernate.HibernateEntityResolver; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; @@ -33,7 +34,7 @@ import org.dom4j.io.XMLWriter; /** * * @author Alex Shneyderman - * @since $Id: MappingsAggregatorBean.java,v 1.1 2004/07/02 07:32:53 epugh Exp $ + * @since $Id: MappingsAggregatorBean.java,v 1.2 2004/07/23 11:25:24 epugh Exp $ */ public class MappingsAggregatorBean extends CommonOperationsBean { @@ -56,11 +57,16 @@ public class MappingsAggregatorBean extends CommonOperationsBean File f = new File(getAggregateOutputFile()); OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileWriter(f), format); + writer.setEntityResolver(new HibernateEntityResolver()); + //writer.setResolveEntityRefs(false); Document finalDoc = DocumentHelper.createDocument(); Element rootHM = null; for(int i = 0; i < files.length; i++) { - SAXReader reader = new SAXReader(); + SAXReader reader = new SAXReader(false); + reader.setEntityResolver(new HibernateEntityResolver()); + //reader.setIncludeExternalDTDDeclarations(false); + //reader.setIncludeExternalDTDDeclarations(false); Document current = reader.read(files[i]); String currentVersion = getVersion(current); if(version == null) diff --git a/hibernate/src/plugin-resources/hibernate-mapping-2.0.dtd b/hibernate/src/plugin-resources/hibernate-mapping-2.0.dtd new file mode 100644 index 00000000..d6d398a6 --- /dev/null +++ b/hibernate/src/plugin-resources/hibernate-mapping-2.0.dtd @@ -0,0 +1,536 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate/src/test/org/apache/maven/hibernate/HibernateEntityResolverTest.java b/hibernate/src/test/org/apache/maven/hibernate/HibernateEntityResolverTest.java new file mode 100644 index 00000000..7e7e4033 --- /dev/null +++ b/hibernate/src/test/org/apache/maven/hibernate/HibernateEntityResolverTest.java @@ -0,0 +1,80 @@ +package org.apache.maven.hibernate; + +/* ==================================================================== + * Copyright 2001-2004 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +import junit.framework.TestCase; + +/** + * Test cases for the {@link HibernateEntityResolver}. Based heavily + * on J2EEEntityResolver. + * + * @author Eric Pugh + * @version $Id: HibernateEntityResolverTest.java,v 1.1 2004/07/23 11:25:24 epugh Exp $ + */ +public class HibernateEntityResolverTest extends TestCase +{ + + /** instance for unit testing */ + private HibernateEntityResolver instance; + /** + * Creates a new instance of J2EEEntityResolverTest + * @param testName the name of the test + */ + public HibernateEntityResolverTest(String testName) + { + super(testName); + } + + /** + * Initialize per test data + * @throws Exception when there is an unexpected problem + */ + public void setUp() throws Exception + { + instance = new HibernateEntityResolver(); + } + + /** + * Test that all public ids are available and being found + */ + public void testAllAvailable() + { + assertNotNull("id to resource map not available", instance.getIdToResource()); + int numDTDs = HibernateEntityResolver.HIBERNATE_DTDS.length; + assertEquals("wrong number of entries", numDTDs, instance.getIdToResource().size()); + for (int i = 0; i < numDTDs; i++) + { + assertNotNull("Can't find resource for publicId", + instance.getIdToResource().get(HibernateEntityResolver.HIBERNATE_DTDS[i])); + } + } + + /** + * Test that the resolver is getting resources locally + */ + public void testResolvingLocally() throws Exception + { + int numDTDs = HibernateEntityResolver.HIBERNATE_DTDS.length; + for (int i = 0; i < numDTDs; i++) + { + String publicId = HibernateEntityResolver.HIBERNATE_DTDS[i]; + assertNotNull("Can't find resource for publicId " + publicId, + instance.resolveEntity(publicId, null)); + } + } +} \ No newline at end of file