001package net.sf.logdistiller.xml;
002
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 implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017import java.io.InputStream;
018import java.util.HashSet;
019import java.util.Set;
020import org.xml.sax.EntityResolver;
021import org.xml.sax.InputSource;
022
023public class LogDistillerEntityResolver
024    implements EntityResolver
025{
026    public final static String LATEST_DTD = "logdistiller-1_4.dtd";
027
028    public final static String LATEST_DTD_URL = "http://logdistiller.sourceforge.net/dtd/" + LATEST_DTD;
029
030    private final static Set<String> DTDS = new HashSet<String>()
031    {
032        private static final long serialVersionUID = -7218559731707450072L;
033
034        {
035            add( "logdistiller-1_0.dtd" );
036            add( "logdistiller-1_1.dtd" );
037            add( "logdistiller-1_2.dtd" );
038            add( "logdistiller-1_3.dtd" );
039            add( "logdistiller-1_4.dtd" );
040        }
041    };
042
043    private final static int LEN = 20; // length of dtd string...
044
045    private String currentDtd;
046
047    public InputSource resolveEntity( String publicId, String systemId )
048    {
049        if ( systemId.length() < LEN )
050        {
051            return null;
052        }
053        String dtd = systemId.substring( systemId.length() - LEN );
054        if ( DTDS.contains( dtd ) )
055        {
056            currentDtd = dtd;
057            InputStream in = LogDistillerEntityResolver.class.getResourceAsStream( dtd );
058            if ( in == null )
059            {
060                throw new RuntimeException( "unable to load DTD " + dtd );
061            }
062            else
063            {
064                return new InputSource( in );
065            }
066        }
067        else
068        {
069            return null;
070        }
071    }
072
073    public String getCurrentDtd()
074    {
075        return currentDtd;
076    }
077}