View Javadoc

1   package org.apache.maven.j2ee;
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   *
34   * @author  dion
35   * @version $Id: J2EEEntityResolver.java 170200 2005-05-15 06:24:19Z brett $
36   */
37  public class J2EEEntityResolver implements EntityResolver
38  {
39      /** map of ids to resource names */
40      private Map idToResource = new HashMap();
41      
42      /** log for debug output */
43      private static final Log LOG = LogFactory.getLog(J2EEEntityResolver.class);
44      
45      /** list of j2ee dtds that are being made available */
46      public static final String[] J2EE_DTDS = new String[] {
47          "-//Sun Microsystems, Inc.//DTD J2EE Application Client 1.3//EN",
48          "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN",
49          "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN",
50          "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN",
51          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN",
52          "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
53          };
54      public static final String[] J2EE_RESOURCES = new String[] {
55          "/plugin-resources/application-client_1_3.dtd",
56          "/plugin-resources/application_1_3.dtd",
57          "/plugin-resources/ejb-jar_2_0.dtd",
58          "/plugin-resources/web-app_2.2.dtd",
59          "/plugin-resources/web-app_2_3.dtd",
60          "/plugin-resources/web-jsptaglibrary_1_2.dtd"
61          };
62      
63      /** Creates a new instance of EntityResolver */
64      public J2EEEntityResolver()
65      {
66          for (int i = 0; i < J2EE_DTDS.length; i++) {
67              idToResource.put(J2EE_DTDS[i], J2EE_RESOURCES[i]);
68          }
69      }
70      
71      /** resolve the entity given by the provided Ids
72       * @param publicId the public id of the entity
73       * @param systemId the 'system location' (typically a URL) of the entity
74       * @return an {@link InputSource input source} for retrieval of the entity
75       * @throws IOException when an I/O error occurs retrieving the entity
76       * @throws SAXException if there are any problems
77       */
78      public InputSource resolveEntity(String publicId, String systemId) throws 
79          SAXException, IOException
80      {
81          LOG.debug("resolving entity with publicId='" + publicId + ", systemId='" + systemId + "'");
82          if (publicId != null)
83          {
84              String resource = (String) idToResource.get(publicId);
85              LOG.debug("resource found in map ='" + resource + "'" );
86              if (resource != null)
87              {
88                  InputStream in = getClass().getResourceAsStream(resource);
89                  LOG.debug("input stream ='" + in + "'" );
90                  if (in != null) 
91                  {
92                      return new InputSource(in);
93                  }
94              }
95          }
96          return null;
97      }
98      
99      /** Getter for publicId to resource name map.
100      * @return Value of property idToResource.
101      */
102     protected Map getIdToResource()
103     {
104         return idToResource;
105     }
106     
107     /** Setter for publicId to resource name map.
108      * @param idToResource New value of property idToResource.
109      */
110     protected void setIdToResource(Map idToResource)
111     {
112         this.idToResource = idToResource;
113     }
114     
115 }