View Javadoc
1   /*
2    * #%L
3    * settings4j
4    * ===============================================================
5    * Copyright (C) 2008 - 2015 Brabenetz Harald, Austria
6    * ===============================================================
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.settings4j.config;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.InputStream;
24  
25  import org.xml.sax.EntityResolver;
26  import org.xml.sax.InputSource;
27  
28  /**
29   * An {@link EntityResolver} specifically designed to return <code>settings4j.dtd</code> which is
30   * embedded within the settings4j jar file.
31   *
32   * @author Harald.Brabenetz
33   */
34  public class Settings4jEntityResolver implements EntityResolver {
35  
36      /** General Logger for this Class. */
37      private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(Settings4jEntityResolver.class);
38  
39      @Override
40      public InputSource resolveEntity(final String publicId, final String systemId) {
41          if (systemId.endsWith("settings4j.dtd")) {
42              final Class<?> clazz = getClass();
43              InputStream in = clazz.getResourceAsStream("/org/settings4j/config/settings4j.dtd");
44              if (in == null) {
45                  LOG.warn("Could not find [settings4j.dtd] using [{}] class loader, parsed without DTD.",
46                      clazz.getClassLoader());
47                  in = new ByteArrayInputStream(new byte[0]);
48              }
49              return new InputSource(in);
50          }
51  
52          return null;
53      }
54  }