/* ======================================================================
   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.util.html.nodevisitors;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.bodington.util.html.CSSFilter;
import org.bodington.util.html.Utils;
import org.htmlparser.Attribute;
import org.htmlparser.Tag;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.StyleTag;
import org.htmlparser.util.NodeList;
import org.htmlparser.visitors.NodeVisitor;

/**
 * Searches for CSS in the document (style attributes and content inside
 * &lt;style&gt; tags). At the moment it is very butal an if it doesn't like the
 * CSS it removes all of it.
 * @author buckett
 */
public class CSSVisitor extends NodeVisitor implements ReportingVisitor
{

    private List messages = new ArrayList();
    private List removeContent = new ArrayList();
    
    public void visitTag(Tag tag)
    {
        if (tag instanceof StyleTag)
        {
            StyleTag styleTag= (StyleTag) tag;
            CSSFilter filter = new CSSFilter(false);
            if (!filter.scan(styleTag.getStyleCode()))
            {
                messages.add(Utils.badTagContent(tag)
                    + joinMessages(filter.getMessages()));
                removeContent.add(styleTag);
            }
        }
        if (("LINK").equalsIgnoreCase(tag.getTagName()))
        {
            if (("STYLESHEET").equalsIgnoreCase(tag.getAttribute("REL")))
            {
                Attribute link = tag.getAttributeEx("HREF");
                if (link != null && !CSSFilter.isLocalUrl(link.getValue()))
                {
                    messages.add(Utils.badAttributeValue(tag, link));
                    link.setValue("");
                }
            }
        }
        Attribute style = tag.getAttributeEx("STYLE");
        if (style!= null)
        {
            CSSFilter filter = new CSSFilter(false);
            if (!filter.scan(style.getValue()))
            {
                messages.add(Utils.badAttributeValue(tag, style)
                    + joinMessages(filter.getMessages()));
                style.setValue("");
            }
        }
    }
    
    public void finishedParsing()
    {
        Iterator tags = removeContent.iterator();
        while(tags.hasNext())
        {
            ((Tag)tags.next()).setChildren(new NodeList());
        }
    }
    
    private static String joinMessages(List list)
    {
        return list.toString();
    }
    
    public List getMessages()
    {
        return Collections.unmodifiableList(messages);
    }

}
