View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Protique Ltd
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.codehaus.activemq.spring;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.springframework.core.io.ClassPathResource;
23  import org.springframework.core.io.Resource;
24  import org.xml.sax.EntityResolver;
25  import org.xml.sax.InputSource;
26  
27  import java.io.IOException;
28  
29  /***
30   * EntityResolver implementation for the ActiveMQ DTD,
31   * to load the DTD from the ActiveMQ classpath JAR file.
32   * <p/>
33   * <p>Fetches "activemq.dtd" from the classpath resource
34   * "/org/codehaus/activemq/activemq.dtd",
35   * no matter if specified as some local URL or as
36   * "http://activemq.codehaus.org/dtd/activemq.dtd".
37   *
38   * @version $Revision: 1.1 $
39   */
40  public class ActiveMQDtdResolver implements EntityResolver {
41  
42      private static final String DTD_NAME = "activemq.dtd";
43  
44      private static final String SEARCH_PACKAGE = "/org/codehaus/activemq/";
45  
46      protected final Log logger = LogFactory.getLog(getClass());
47  
48      public InputSource resolveEntity(String publicId, String systemId) throws IOException {
49          logger.debug("Trying to resolve XML entity with public ID [" + publicId +
50                  "] and system ID [" + systemId + "]");
51          if (systemId != null && systemId.indexOf(DTD_NAME) > systemId.lastIndexOf("/")) {
52  
53              String dtdFile = systemId.substring(systemId.indexOf(DTD_NAME));
54              logger.debug("Trying to locate [" + dtdFile + "] under [" + SEARCH_PACKAGE + "]");
55              try {
56                  String name = SEARCH_PACKAGE + dtdFile;
57                  Resource resource = new ClassPathResource(name, getClass());
58                  InputSource source = new InputSource(resource.getInputStream());
59                  source.setPublicId(publicId);
60                  source.setSystemId(systemId);
61                  logger.debug("Found beans DTD [" + systemId + "] in classpath");
62                  return source;
63              }
64              catch (IOException ex) {
65                  logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
66              }
67          }
68          // use the default behaviour -> download from website or wherever
69          return null;
70      }
71  
72  }