001    /*
002     * Copyright 2006 Stephen J. McConnell.
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.station;
020    
021    import dpml.lang.DOMInput;
022    
023    import java.io.InputStream;
024    import java.io.InputStreamReader;
025    import java.net.URL;
026    
027    import net.dpml.lang.NamespaceError;
028    
029    import org.w3c.dom.ls.LSInput;
030    import org.w3c.dom.ls.LSResourceResolver;
031    
032    /**
033     * LSInput resolver service for the <tt>dpml:station</tt> namespace.
034     *
035     * @author <a href="http://www.dpml.net">Digital Product Management Laboratory</a>
036     * @version 2.0.1
037     */
038    public final class PlanNamespaceResolver implements LSResourceResolver
039    {
040       /**
041        * Return the schema definition for a <tt>dpml:station</tt> namespace.
042        *
043        * @param type the node type
044        * @param namespace the node namespace
045        * @param publicId the public id
046        * @param systemId the system id
047        * @param base the base value
048        * @return the LS input instance or null if not recognized
049        */
050        public LSInput resolveResource( 
051          String type, String namespace, String publicId, String systemId, String base )
052        {
053            URL url = getNamespaceURL( namespace );
054            if( null == url )
055            {
056                return null;
057            }
058            try
059            {
060                DOMInput input = new DOMInput();
061                input.setPublicId( publicId );
062                input.setSystemId( systemId );
063                input.setBaseURI( base );
064                InputStream stream = url.openStream();
065                InputStreamReader reader = new InputStreamReader( stream );
066                input.setCharacterStream( reader );
067                return input;
068            }
069            catch( Exception e )
070            {
071                final String error = 
072                  "Internal error while resolving the resource for the namespace ["
073                  + namespace 
074                  + "].";
075                  
076                throw new NamespaceError( error, e );
077            }
078        }
079        
080        private URL getNamespaceURL( String namespace )
081        {
082            ClassLoader classloader = getClass().getClassLoader();
083            if( PlanContentHandler.NAMESPACE.equals( namespace ) )
084            {
085                return classloader.getResource( "dpml/metro/dpml-metro-plan.xsd" );
086            }
087            else
088            {
089                return null;
090            }
091        }
092    }