View Javadoc

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