package org.apache.maven.linkcheck.validation;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * ====================================================================
 */
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
 * @version $Id: LinkValidatorManager.java,v 1.5 2004/03/02 15:09:17 evenisse Exp $
 */

public class LinkValidatorManager {
    /**
     * Log for debug output
     */
    private static Log LOG = LogFactory.getLog(LinkValidatorManager.class);

    private List validators = new ArrayList();
    private LinkValidatorCache cache = new LinkValidatorCache(this);
    private String exclude;

    public LinkValidatorManager() {

    }

    public void addLinkValidator(LinkValidator lv) {
        validators.add(lv);
    }

    public List getValidators() {
        return validators;
    }

    public LinkValidationResult validateLink(LinkValidationItem lvi)
        throws Exception {
        {
            LinkValidationResult status = cache.getCachedResult(lvi);
            if (status != null) {
                return status;
            }
        }

        if (exclude != null && lvi.getLink().startsWith(exclude)) {
            return new LinkValidationResult(LinkValidationResult.VALID, false);
        }

        Iterator iter = validators.iterator();
        while (iter.hasNext()) {
            LinkValidator lv = (LinkValidator) iter.next();

            Object resourceKey = lv.getResourceKey(lvi);

            if (resourceKey != null) {

                LinkValidationResult lvr = lv.validateLink(lvi);

                if (lvr.getStatus() == LinkValidationResult.NOTMINE) {
                    continue;
                }

                cache.setCachedResult(resourceKey, lvr);
                return lvr;

            }

        }

        LOG.info("Unable to validate link : " + lvi.getLink());
        return new LinkValidationResult(LinkValidationResult.UNKNOWN, false);
    }

    public void loadCache(String cacheFilename) {
        try {
            File f = new File(cacheFilename);
            if (f.exists()) {
                this.cache.load(new FileInputStream(cacheFilename));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void saveCache(String cacheFilename) {
        try {
            File cacheFile = new File(cacheFilename);
            File dir = cacheFile.getParentFile();
            if (dir != null) {
                dir.mkdirs();
            }
            this.cache.save(new FileOutputStream(cacheFilename));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Returns the exclude.
     * @return String
     */
    public String getExclude() {
        return exclude;
    }

    /**
     * Sets the exclude.
     * @param exclude The exclude to set
     */
    public void setExclude(String exclude) {
        this.exclude = exclude;
    }

}
