001/**
002 * Copyright 2005-2017 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 */
016package org.kuali.rice.ken.util;
017
018import java.io.IOException;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.apache.log4j.Logger;
023import org.xml.sax.EntityResolver;
024import org.xml.sax.InputSource;
025import 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 */
032public 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}