/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   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.

====================================================================== */

package org.bodington.server;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.bodington.util.ReaderCharSequence;
import org.bodington.util.Scanner;
import org.bodington.util.ScannerResults;
import org.bodington.util.html.CSSFilter;
import org.bodington.util.html.HtmlFilterFactory;

/**
 * A scanner that will scan CSS files for bad things.
 * @author buckett
 */
public class CSSScanner implements Scanner
{

    public ScannerResults scan(File source)
    {  
        CssScannerResults results = new CssScannerResults();
        if (HtmlFilterFactory.isFiltered())
        {
            try
            {
                ReaderCharSequence buffer = new ReaderCharSequence(
                    new FileReader(source));
                CSSFilter filter = new CSSFilter(true);

                results.ok = filter.scan(buffer);
                results.messages = filter.getMessages();
            }
            catch (IOException ioe)
            {
                results.ok = false;
                results.messages = Arrays
                    .asList(new String[]{"Problem reading file."});
            }
        }
        else
        {
            results.ok = true;
        }
        
        return results;
        
    }
    
    public static boolean shouldScan(String filename)
    {
        return filename.matches("(?i).*\\.css$");
    }

    private class CssScannerResults implements ScannerResults
    {
        
        public CssScannerResults(){}

        private boolean ok = false;
        private List messages;
        
        public boolean isOk()
        {
            return ok;
        }

        public List getMessages()
        {
            return messages;
        }
        
    }

}
