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  import java.lang.invoke.MethodHandles;
31  
32  public class TobagoConfigEntityResolver extends DefaultHandler {
33  
34    private static final String TOBAGO_CONFIG_DTD_1_0 = "/org/apache/myfaces/tobago/config/tobago-config_1_0.dtd";
35    private static final String TOBAGO_CONFIG_DTD_1_0_29 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.29.dtd";
36    private static final String TOBAGO_CONFIG_DTD_1_0_30 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.30.dtd";
37    private static final String TOBAGO_CONFIG_DTD_1_0_34 = "/org/apache/myfaces/tobago/config/tobago-config-1.0.34.dtd";
38    protected static final String TOBAGO_CONFIG_XSD_1_5 = "/org/apache/myfaces/tobago/config/tobago-config-1.5.xsd";
39    /**
40     * @deprecated since 2.0.0, 1.6 wasn't released
41     */
42    @Deprecated
43    protected static final String TOBAGO_CONFIG_XSD_1_6 = "/org/apache/myfaces/tobago/config/tobago-config-1.6.xsd";
44    protected static final String TOBAGO_CONFIG_XSD_2_0 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.xsd";
45    protected static final String TOBAGO_CONFIG_XSD_2_0_6 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.6.xsd";
46    protected static final String TOBAGO_CONFIG_XSD_3_0 = "/org/apache/myfaces/tobago/config/tobago-config-3.0.xsd";
47    protected static final String TOBAGO_CONFIG_XSD_4_0 = "/org/apache/myfaces/tobago/config/tobago-config-4.0.xsd";
48    protected static final String TOBAGO_CONFIG_XSD_5_0 = "/org/apache/myfaces/tobago/config/tobago-config-5.0.xsd";
49  
50    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51  
52    @Override
53    public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
54      if (LOG.isInfoEnabled()) {
55        LOG.info("Resolving publicId='" + publicId + "' and systemId='" + systemId + "'.");
56      }
57      final InputStream localStream;
58      if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config_1_0.dtd")) {
59        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0);
60      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.29.dtd")) {
61        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_29);
62      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.30.dtd")) {
63        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_30);
64      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.34.dtd")) {
65        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_34);
66      } else {
67        localStream = null;
68      }
69      if (localStream != null) {
70        return new InputSource(localStream);
71      } else {
72        LOG.warn("Didn't find local resource for publicId='" + publicId + "' and systemId='" + systemId + "'. "
73            + "Trying to load with parent resolver (might be loaded over the internet).");
74        return super.resolveEntity(publicId, systemId);
75      }
76    }
77  }