View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.xml.sax.InputSource;
25  import org.xml.sax.SAXException;
26  import org.xml.sax.helpers.DefaultHandler;
27  
28  import java.io.IOException;
29  import java.io.InputStream;
30  
31  public class TobagoConfigEntityResolver extends DefaultHandler {
32  
33    private static final String TOBAGO_CONFIG_DTD_1_0 = "/org/apache/myfaces/tobago/config/tobago-config_1_0.dtd";
34    private static final String TOBAGO_CONFIG_DTD_1_0_29 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.29.dtd";
35    private static final String TOBAGO_CONFIG_DTD_1_0_30 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.30.dtd";
36    private static final String TOBAGO_CONFIG_DTD_1_0_34 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.34.dtd";
37    protected static final String TOBAGO_CONFIG_XSD_1_5 = "/org/apache/myfaces/tobago/config/tobago-config-1.5.xsd";
38    /**
39     * @deprecated since 2.0.0, 1.6 wasn't released
40     */
41    @Deprecated
42    protected static final String TOBAGO_CONFIG_XSD_1_6 = "/org/apache/myfaces/tobago/config/tobago-config-1.6.xsd";
43    protected static final String TOBAGO_CONFIG_XSD_2_0 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.xsd";
44    protected static final String TOBAGO_CONFIG_XSD_2_0_6 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.6.xsd";
45    protected static final String TOBAGO_CONFIG_XSD_3_0 = "/org/apache/myfaces/tobago/config/tobago-config-3.0.xsd";
46    protected static final String TOBAGO_CONFIG_XSD_4_0 = "/org/apache/myfaces/tobago/config/tobago-config-4.0.xsd";
47  
48    private static final Logger LOG = LoggerFactory.getLogger(TobagoConfigEntityResolver.class);
49  
50    @Override
51    public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
52      if (LOG.isInfoEnabled()) {
53        LOG.info("Resolving publicId='" + publicId + "' and systemId='" + systemId + "'.");
54      }
55      final InputStream localStream;
56      if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config_1_0.dtd")) {
57        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0);
58      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.29.dtd")) {
59        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_29);
60      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.30.dtd")) {
61        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_30);
62      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.34.dtd")) {
63        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_34);
64      } else {
65        localStream = null;
66      }
67      if (localStream != null) {
68        return new InputSource(localStream);
69      } else {
70        LOG.warn("Didn't find local resource for publicId='" + publicId + "' and systemId='" + systemId + "'. "
71            + "Trying to load with parent resolver (might be loaded over the internet).");
72        return super.resolveEntity(publicId, systemId);
73      }
74    }
75  }