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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author <a href="mailto:bwalding@apache.org">Ben Walding</a>
 * @version $Id$
 */
public class LinkValidatorCache {
    private LinkValidatorManager lvm;
    private final Map cache = new HashMap();

    public LinkValidatorCache(LinkValidatorManager lvm) {
        this.lvm = lvm;
    }
    public void setCachedResult(Object resourceKey, LinkValidationResult lvr) {
        cache.put(resourceKey, lvr);
    }

    /**
     * 
     * @param lvi
     * @return int Will return a status level, VALID, INVALID, UNKNOWN
     */
    public LinkValidationResult getCachedResult(LinkValidationItem lvi) {
        Iterator iter = lvm.getValidators().iterator();

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

            Object resourceKey = lv.getResourceKey(lvi);

            if (resourceKey != null) {
                return (LinkValidationResult) cache.get(resourceKey);
            }
        }
        return null;

    }

    public void load(InputStream is) {
        final LinkValidationResult LVR_VALID =
            new LinkValidationResult(LinkValidationResult.VALID, true);
        final LinkValidationResult LVR_INVALID =
            new LinkValidationResult(LinkValidationResult.INVALID, true);
        try {
            Properties p = new Properties();
            p.load(is);

            Iterator iter = p.keySet().iterator();
            while (iter.hasNext()) {
                String key = (String) iter.next();
                String value = (String) p.getProperty(key);

                if (value.equals("VALID"))
                    cache.put(key, LVR_VALID);

                if (value.equals("INVALID"))
                    cache.put(key, LVR_INVALID);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void save(OutputStream os) {

        try {
            Properties p = new Properties();
            Iterator iter = cache.keySet().iterator();
            while (iter.hasNext()) {
                String key = (String) iter.next();
                LinkValidationResult lvr =
                    (LinkValidationResult) cache.get(key);
                if (lvr.isPersistent()) {
                    if (lvr.getStatus() == LinkValidationResult.VALID) {
                        p.setProperty(key, "VALID");
                    }

                    if (lvr.getStatus() == LinkValidationResult.INVALID) {
                        p.setProperty(key, "INVALID");
                    }
                }
            }

            p.store(os, "LinkCheck Cache");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
