001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.ken.util;
017    
018    import java.io.IOException;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.apache.log4j.Logger;
023    import org.xml.sax.EntityResolver;
024    import org.xml.sax.InputSource;
025    import org.xml.sax.SAXException;
026    
027    /**
028     * EntityResolver implementation that delegates in sequence to a list of EntityResolvers,
029     * returning the first match.
030     * @author Kuali Rice Team (rice.collab@kuali.org)
031     */
032    public class CompoundEntityResolver implements EntityResolver {
033        private static final Logger LOG = Logger.getLogger(CompoundEntityResolver.class);
034    
035        private final List<EntityResolver> resolvers;
036    
037        /**
038         * Constructs a CompoundEntityResolver.java.
039         * @param first
040         * @param second
041         */
042        public CompoundEntityResolver(EntityResolver first, EntityResolver second) {
043            this.resolvers = new ArrayList<EntityResolver>(2);
044            this.resolvers.add(first);
045            this.resolvers.add(second);
046        }
047    
048        /**
049         * Constructs a CompoundEntityResolver.java.
050         * @param resolvers
051         */
052        public CompoundEntityResolver(List<EntityResolver> resolvers) {
053            this.resolvers = resolvers;
054        }
055    
056        /**
057         * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
058         */
059        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
060            LOG.debug("resolveEntity: " + publicId + " " + systemId);
061            for (EntityResolver resolver: resolvers) {
062                LOG.debug("Invoking entity resolver: " + resolver);
063                InputSource source = resolver.resolveEntity(publicId, systemId);
064                if (source != null) {
065                    LOG.debug("source != null: " + source);
066                    return source;
067                }
068            }
069            return null;
070        }
071    }