001    package 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    
017    import java.io.InputStream;
018    import java.util.HashSet;
019    import java.util.Set;
020    import org.xml.sax.EntityResolver;
021    import org.xml.sax.InputSource;
022    
023    public 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 DTDS = new HashSet()
031        {
032            {
033                add( "logdistiller-1_0.dtd" );
034                add( "logdistiller-1_1.dtd" );
035                add( "logdistiller-1_2.dtd" );
036                add( "logdistiller-1_3.dtd" );
037                add( "logdistiller-1_4.dtd" );
038            }
039        };
040    
041        private final static int LEN = 20; // length of dtd string...
042    
043        private String currentDtd;
044    
045        public InputSource resolveEntity( String publicId, String systemId )
046        {
047            if ( systemId.length() < LEN )
048            {
049                return null;
050            }
051            String dtd = systemId.substring( systemId.length() - LEN );
052            if ( DTDS.contains( dtd ) )
053            {
054                currentDtd = dtd;
055                InputStream in = LogDistillerEntityResolver.class.getResourceAsStream( dtd );
056                if ( in == null )
057                {
058                    throw new RuntimeException( "unable to load DTD " + dtd );
059                }
060                else
061                {
062                    return new InputSource( in );
063                }
064            }
065            else
066            {
067                return null;
068            }
069        }
070    
071        public String getCurrentDtd()
072        {
073            return currentDtd;
074        }
075    }