View Javadoc

1   /*
2    *  jDTAUS Core RI Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.client;
22  
23  import java.io.IOException;
24  import java.net.URI;
25  import java.net.URISyntaxException;
26  import java.net.URL;
27  import java.util.Locale;
28  import java.util.logging.Level;
29  import java.util.logging.Logger;
30  import org.xml.sax.EntityResolver;
31  import org.xml.sax.InputSource;
32  import org.xml.sax.SAXException;
33  
34  /**
35   * {@code EntityResolver} implementation resolving any container specific
36   * system ids to classpath resources.
37   *
38   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
39   * @version $JDTAUS: BootstrapEntityResolver.java 8743 2012-10-07 03:06:20Z schulte $
40   */
41  public class BootstrapEntityResolver implements EntityResolver
42  {
43      //--Constants---------------------------------------------------------------
44  
45      /** Mapping of schema names to the corresponding classpath location. */
46      private static final String[] SCHEMA_LOCATIONS =
47      {
48          "jdtaus-module-1.0.xsd", "org/jdtaus/core/container/xml/",
49          "jdtaus-module-1.1.xsd", "org/jdtaus/core/container/xml/",
50          "jdtaus-module-1.2.xsd", "org/jdtaus/core/model/container/module/",
51          "jdtaus-core-1.0.xsd", "org/jdtaus/core/model/",
52          "jdtaus-core-1.1.xsd", "org/jdtaus/core/model/",
53          "jdtaus-text-1.0.xsd", "org/jdtaus/core/model/text/",
54          "jdtaus-text-1.1.xsd", "org/jdtaus/core/model/text/",
55          "jdtaus-monitor-1.0.xsd", "org/jdtaus/core/model/monitor/",
56          "jdtaus-monitor-1.1.xsd", "org/jdtaus/core/model/monitor/",
57          "jdtaus-container-1.0.xsd", "org/jdtaus/core/model/container/",
58          "jdtaus-container-1.1.xsd", "org/jdtaus/core/model/container/"
59      };
60  
61      //---------------------------------------------------------------Constants--
62      //--EntityResolver----------------------------------------------------------
63  
64      public InputSource resolveEntity( final String publicId,
65                                        final String systemId )
66          throws SAXException, IOException
67      {
68          if ( systemId == null )
69          {
70              throw new NullPointerException( "systemId" );
71          }
72  
73          InputSource schemaSource = null;
74  
75          try
76          {
77              final URI systemUri = new URI( systemId );
78              String schemaName = systemUri.getPath();
79              if ( schemaName != null )
80              {
81                  final int lastIndexOfSlash = schemaName.lastIndexOf( '/' );
82                  if ( lastIndexOfSlash != -1 &&
83                       lastIndexOfSlash < schemaName.length() )
84                  {
85                      schemaName = schemaName.substring( lastIndexOfSlash + 1 );
86                  }
87  
88                  for ( int i = SCHEMA_LOCATIONS.length - 2; i >= 0; i -= 2 )
89                  {
90                      if ( SCHEMA_LOCATIONS[i].equals( schemaName ) )
91                      {
92                          final String schemaLocation =
93                                       SCHEMA_LOCATIONS[i + 1] + schemaName;
94  
95                          final URL schemaUrl = this.getClass().getClassLoader().
96                              getResource( schemaLocation );
97  
98                          if ( schemaUrl == null )
99                          {
100                             Logger.getLogger( this.getClass().getName() ).log(
101                                 Level.WARNING,
102                                 BootstrapEntityResolverBundle.getInstance().
103                                 getResourceNotAvailableMessage(
104                                 Locale.getDefault(), schemaLocation ) );
105 
106                             continue;
107                         }
108 
109                         schemaSource = new InputSource();
110                         schemaSource.setPublicId( publicId );
111                         schemaSource.setSystemId( schemaUrl.toExternalForm() );
112 
113                         Logger.getLogger( this.getClass().getName() ).log(
114                             Level.FINE,
115                             BootstrapEntityResolverBundle.getInstance().
116                             getResolvedSystemIdUriMessage(
117                             Locale.getDefault(), systemUri.toASCIIString(),
118                             schemaSource.getSystemId() ) );
119 
120                         break;
121                     }
122 
123                 }
124             }
125         }
126         catch ( final URISyntaxException e )
127         {
128             Logger.getLogger( this.getClass().getName() ).log(
129                 Level.WARNING,
130                 BootstrapEntityResolverBundle.getInstance().
131                 getUnsupportedSystemIdUriMessage( Locale.getDefault(), systemId,
132                                                   e.getMessage() ) );
133 
134             schemaSource = null;
135         }
136 
137         return schemaSource;
138     }
139 
140     //----------------------------------------------------------EntityResolver--
141     //--BootstrapEntityResolver-------------------------------------------------
142 
143     /** Creates a new {@code BootstrapEntityResolver} instance. */
144     public BootstrapEntityResolver()
145     {
146         super();
147     }
148 
149     //-------------------------------------------------BootstrapEntityResolver--
150 }