View Javadoc

1   package org.apache.maven.hibernate;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.xml.sax.EntityResolver;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * A class to resolve external entity definitions for j2ee artifacts.  
33   * Based on the J2EEEntityResolver from the J2EE plugin
34   *
35   * @author  eric pugh
36   * @version $Id: HibernateEntityResolver.java 170200 2005-05-15 06:24:19Z brett $
37   */
38  public class HibernateEntityResolver implements EntityResolver
39  {
40      /** map of ids to resource names */
41      private Map idToResource = new HashMap();
42  
43      /** log for debug output */
44      private static final Log LOG = LogFactory.getLog(HibernateEntityResolver.class);
45  
46      /** list of j2ee dtds that are being made available */
47      public static final String[] HIBERNATE_DTDS = new String[] {
48          "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
49          };
50      public static final String[] HIBERNATE_RESOURCES = new String[] {
51          "/plugin-resources/hibernate-mapping-2.0.dtd"
52          };
53  
54      /** Creates a new instance of EntityResolver */
55      public HibernateEntityResolver()
56      {
57          for (int i = 0; i < HIBERNATE_DTDS.length; i++) {
58              idToResource.put(HIBERNATE_DTDS[i], HIBERNATE_RESOURCES[i]);
59          }
60      }
61  
62      /** resolve the entity given by the provided Ids
63       * @param publicId the public id of the entity
64       * @param systemId the 'system location' (typically a URL) of the entity
65       * @return an {@link InputSource input source} for retrieval of the entity
66       * @throws IOException when an I/O error occurs retrieving the entity
67       * @throws SAXException if there are any problems
68       */
69      public InputSource resolveEntity(String publicId, String systemId) throws
70          SAXException, IOException
71      {
72          LOG.debug("resolving entity with publicId='" + publicId + ", systemId='" + systemId + "'");
73          if (publicId != null)
74          {
75              String resource = (String) idToResource.get(publicId);
76              LOG.debug("resource found in map ='" + resource + "'" );
77              if (resource != null)
78              {
79                  InputStream in = getClass().getResourceAsStream(resource);
80                  LOG.debug("input stream ='" + in + "'" );
81                  if (in != null)
82                  {
83                      return new InputSource(in);
84                  }
85              }
86          }
87          return null;
88      }
89  
90      /** Getter for publicId to resource name map.
91       * @return Value of property idToResource.
92       */
93      protected Map getIdToResource()
94      {
95          return idToResource;
96      }
97  
98      /** Setter for publicId to resource name map.
99       * @param idToResource New value of property idToResource.
100      */
101     protected void setIdToResource(Map idToResource)
102     {
103         this.idToResource = idToResource;
104     }
105 
106 }