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    @Deprecated
39    protected static final String TOBAGO_CONFIG_XSD_1_6 = "/org/apache/myfaces/tobago/config/tobago-config-1.6.xsd";
40    protected static final String TOBAGO_CONFIG_XSD_2_0 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.xsd";
41    protected static final String TOBAGO_CONFIG_XSD_2_0_6 = "/org/apache/myfaces/tobago/config/tobago-config-2.0.6.xsd";
42  
43    private static final Logger LOG = LoggerFactory.getLogger(TobagoConfigEntityResolver.class);
44  
45    @Override
46    public InputSource resolveEntity(final String publicId, final String systemId) throws IOException, SAXException {
47      if (LOG.isInfoEnabled()) {
48        LOG.info("Resolving publicId='" + publicId + "' and systemId='" + systemId + "'.");
49      }
50      final InputStream localStream;
51      if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config_1_0.dtd")) {
52        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0);
53      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.29.dtd")) {
54        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_29);
55      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.30.dtd")) {
56        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_30);
57      } else if (systemId.equals("http://myfaces.apache.org/tobago/tobago-config-1.0.34.dtd")) {
58        localStream = getClass().getResourceAsStream(TOBAGO_CONFIG_DTD_1_0_34);
59      } else {
60        localStream = null;
61      }
62      if (localStream != null) {
63        return new InputSource(localStream);
64      } else {
65        LOG.warn("Didn't find local resource for publicId='" + publicId + "' and systemId='" + systemId + "'. "
66            + "Trying to load with parent resolver (might be loaded over the internet).");
67        return super.resolveEntity(publicId, systemId);
68      }
69    }
70  }