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<String> DTDS = new HashSet<String>()
31      {
32          private static final long serialVersionUID = -7218559731707450072L;
33  
34          {
35              add( "logdistiller-1_0.dtd" );
36              add( "logdistiller-1_1.dtd" );
37              add( "logdistiller-1_2.dtd" );
38              add( "logdistiller-1_3.dtd" );
39              add( "logdistiller-1_4.dtd" );
40          }
41      };
42  
43      private final static int LEN = 20; // length of dtd string...
44  
45      private String currentDtd;
46  
47      public InputSource resolveEntity( String publicId, String systemId )
48      {
49          if ( systemId.length() < LEN )
50          {
51              return null;
52          }
53          String dtd = systemId.substring( systemId.length() - LEN );
54          if ( DTDS.contains( dtd ) )
55          {
56              currentDtd = dtd;
57              InputStream in = LogDistillerEntityResolver.class.getResourceAsStream( dtd );
58              if ( in == null )
59              {
60                  throw new RuntimeException( "unable to load DTD " + dtd );
61              }
62              else
63              {
64                  return new InputSource( in );
65              }
66          }
67          else
68          {
69              return null;
70          }
71      }
72  
73      public String getCurrentDtd()
74      {
75          return currentDtd;
76      }
77  }