View Javadoc

1   package net.sf.logdistiller.xml;
2   
3   /*
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  import java.io.InputStream;
18  import java.util.HashSet;
19  import java.util.Set;
20  import org.xml.sax.EntityResolver;
21  import org.xml.sax.InputSource;
22  
23  public class LogDistillerEntityResolver
24      implements EntityResolver
25  {
26      public final static String LATEST_DTD = "logdistiller-1_4.dtd";
27  
28      public final static String LATEST_DTD_URL = "http://logdistiller.sourceforge.net/dtd/" + LATEST_DTD;
29  
30      private final static Set DTDS = new HashSet()
31      {
32          {
33              add( "logdistiller-1_0.dtd" );
34              add( "logdistiller-1_1.dtd" );
35              add( "logdistiller-1_2.dtd" );
36              add( "logdistiller-1_3.dtd" );
37              add( "logdistiller-1_4.dtd" );
38          }
39      };
40  
41      private final static int LEN = 20; // length of dtd string...
42  
43      private String currentDtd;
44  
45      public InputSource resolveEntity( String publicId, String systemId )
46      {
47          if ( systemId.length() < LEN )
48          {
49              return null;
50          }
51          String dtd = systemId.substring( systemId.length() - LEN );
52          if ( DTDS.contains( dtd ) )
53          {
54              currentDtd = dtd;
55              InputStream in = LogDistillerEntityResolver.class.getResourceAsStream( dtd );
56              if ( in == null )
57              {
58                  throw new RuntimeException( "unable to load DTD " + dtd );
59              }
60              else
61              {
62                  return new InputSource( in );
63              }
64          }
65          else
66          {
67              return null;
68          }
69      }
70  
71      public String getCurrentDtd()
72      {
73          return currentDtd;
74      }
75  }