001/*
002 *  jDTAUS Core RI Client Container
003 *  Copyright (C) 2005 Christian Schulte
004 *  <cs@schulte.it>
005 *
006 *  This library is free software; you can redistribute it and/or
007 *  modify it under the terms of the GNU Lesser General Public
008 *  License as published by the Free Software Foundation; either
009 *  version 2.1 of the License, or any later version.
010 *
011 *  This library is distributed in the hope that it will be useful,
012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 *  Lesser General Public License for more details.
015 *
016 *  You should have received a copy of the GNU Lesser General Public
017 *  License along with this library; if not, write to the Free Software
018 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
019 *
020 */
021package org.jdtaus.core.container.ri.client;
022
023import java.io.IOException;
024import java.net.URI;
025import java.net.URISyntaxException;
026import java.net.URL;
027import java.util.Locale;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030import org.xml.sax.EntityResolver;
031import org.xml.sax.InputSource;
032import org.xml.sax.SAXException;
033
034/**
035 * {@code EntityResolver} implementation resolving any container specific
036 * system ids to classpath resources.
037 *
038 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
039 * @version $JDTAUS: BootstrapEntityResolver.java 8743 2012-10-07 03:06:20Z schulte $
040 */
041public class BootstrapEntityResolver implements EntityResolver
042{
043    //--Constants---------------------------------------------------------------
044
045    /** Mapping of schema names to the corresponding classpath location. */
046    private static final String[] SCHEMA_LOCATIONS =
047    {
048        "jdtaus-module-1.0.xsd", "org/jdtaus/core/container/xml/",
049        "jdtaus-module-1.1.xsd", "org/jdtaus/core/container/xml/",
050        "jdtaus-module-1.2.xsd", "org/jdtaus/core/model/container/module/",
051        "jdtaus-core-1.0.xsd", "org/jdtaus/core/model/",
052        "jdtaus-core-1.1.xsd", "org/jdtaus/core/model/",
053        "jdtaus-text-1.0.xsd", "org/jdtaus/core/model/text/",
054        "jdtaus-text-1.1.xsd", "org/jdtaus/core/model/text/",
055        "jdtaus-monitor-1.0.xsd", "org/jdtaus/core/model/monitor/",
056        "jdtaus-monitor-1.1.xsd", "org/jdtaus/core/model/monitor/",
057        "jdtaus-container-1.0.xsd", "org/jdtaus/core/model/container/",
058        "jdtaus-container-1.1.xsd", "org/jdtaus/core/model/container/"
059    };
060
061    //---------------------------------------------------------------Constants--
062    //--EntityResolver----------------------------------------------------------
063
064    public InputSource resolveEntity( final String publicId,
065                                      final String systemId )
066        throws SAXException, IOException
067    {
068        if ( systemId == null )
069        {
070            throw new NullPointerException( "systemId" );
071        }
072
073        InputSource schemaSource = null;
074
075        try
076        {
077            final URI systemUri = new URI( systemId );
078            String schemaName = systemUri.getPath();
079            if ( schemaName != null )
080            {
081                final int lastIndexOfSlash = schemaName.lastIndexOf( '/' );
082                if ( lastIndexOfSlash != -1 &&
083                     lastIndexOfSlash < schemaName.length() )
084                {
085                    schemaName = schemaName.substring( lastIndexOfSlash + 1 );
086                }
087
088                for ( int i = SCHEMA_LOCATIONS.length - 2; i >= 0; i -= 2 )
089                {
090                    if ( SCHEMA_LOCATIONS[i].equals( schemaName ) )
091                    {
092                        final String schemaLocation =
093                                     SCHEMA_LOCATIONS[i + 1] + schemaName;
094
095                        final URL schemaUrl = this.getClass().getClassLoader().
096                            getResource( schemaLocation );
097
098                        if ( schemaUrl == null )
099                        {
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}