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