View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
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  package org.settings4j.config;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.InputStream;
21  
22  import org.xml.sax.EntityResolver;
23  import org.xml.sax.InputSource;
24  
25  /**
26   * An {@link EntityResolver} specifically designed to return <code>settings4j.dtd</code> which is
27   * embedded within the settings4j jar file.
28   * 
29   * @author Harald.Brabenetz
30   */
31  public class Settings4jEntityResolver implements EntityResolver {
32  
33      /** General Logger for this Class. */
34      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(Settings4jEntityResolver.class);
35  
36      /** {@inheritDoc} */
37      public InputSource resolveEntity(final String publicId, final String systemId) {
38          if (systemId.endsWith("settings4j.dtd")) {
39              final Class clazz = getClass();
40              InputStream in = clazz.getResourceAsStream("/org/settings4j/config/settings4j.dtd");
41              if (in == null) {
42                  LOG.warn("Could not find [settings4j.dtd] using [{}] class loader, parsed without DTD.",
43                      clazz.getClassLoader());
44                  in = new ByteArrayInputStream(new byte[0]);
45              }
46              return new InputSource(in);
47          }
48  
49          return null;
50      }
51  }