View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.ken.util;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.log4j.Logger;
23  import org.xml.sax.EntityResolver;
24  import org.xml.sax.InputSource;
25  import org.xml.sax.SAXException;
26  
27  /**
28   * EntityResolver implementation that delegates in sequence to a list of EntityResolvers,
29   * returning the first match.
30   * @author Kuali Rice Team (rice.collab@kuali.org)
31   */
32  public class CompoundEntityResolver implements EntityResolver {
33      private static final Logger LOG = Logger.getLogger(CompoundEntityResolver.class);
34  
35      private final List<EntityResolver> resolvers;
36  
37      /**
38       * Constructs a CompoundEntityResolver.java.
39       * @param first
40       * @param second
41       */
42      public CompoundEntityResolver(EntityResolver first, EntityResolver second) {
43          this.resolvers = new ArrayList<EntityResolver>(2);
44          this.resolvers.add(first);
45          this.resolvers.add(second);
46      }
47  
48      /**
49       * Constructs a CompoundEntityResolver.java.
50       * @param resolvers
51       */
52      public CompoundEntityResolver(List<EntityResolver> resolvers) {
53          this.resolvers = resolvers;
54      }
55  
56      /**
57       * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
58       */
59      public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
60          LOG.debug("resolveEntity: " + publicId + " " + systemId);
61          for (EntityResolver resolver: resolvers) {
62              LOG.debug("Invoking entity resolver: " + resolver);
63              InputSource source = resolver.resolveEntity(publicId, systemId);
64              if (source != null) {
65                  LOG.debug("source != null: " + source);
66                  return source;
67              }
68          }
69          return null;
70      }
71  }