/* ======================================================================
   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.LinkedList;
import java.util.List;

import org.htmlparser.Text;
import org.htmlparser.lexer.Page;
import org.htmlparser.tags.CompositeTag;
import org.htmlparser.visitors.NodeVisitor;

/**
 * This visitor is used to encode &lt; and &gt; that apear in text nodes
 * into the HTML versions. This is because if a tag begins with a number,
 * eg &lt;3hr&gt; it is considered a text node (XML Spec).
 * @author buckett
 */
public class EscapeHtmlVisitor extends NodeVisitor implements ReportingVisitor
{
    
    private List messages = new LinkedList();

    public void visitStringNode(Text string)
    {  
        if (string.getParent() instanceof CompositeTag)
        {
            CompositeTag compTag = (CompositeTag) string.getParent();
            if(("STYLE").equalsIgnoreCase(compTag.getTagName()))
                return;
        }
        String text = string.getText();
        StringBuffer output = null;
        int copyFrom = 0;
        Page page = string.getPage();
        for (int i = 0; i < text.length(); i++)
        {
            switch (text.charAt(i))
            {
                case '<':
                    if (output == null)
                        output = new StringBuffer();
                    output.append(text.substring(copyFrom, i));
                    output.append("&lt;");
                    copyFrom = i+1;
                    messages.add("Unescaped '<' at line: "+
                        (page.row(string.getStartPosition()+i)+1)+ " column: "+
                        (page.column(string.getStartPosition()+i)+1)
                        );
                    break;
                case '>':
                    if (output == null)
                        output = new StringBuffer();
                    output.append(text.substring(copyFrom, i));
                    output.append("&gt;");
                    copyFrom = i+1;
                    messages.add("Unescaped '>' at line: "+
                        (page.row(string.getStartPosition()+i)+1)+ " column: "+
                        (page.column(string.getStartPosition()+i)+1)
                        );
                    break;
                case '\0':
                    if (output == null)
                        output = new StringBuffer();
                    output.append(text.substring(copyFrom, i));
                    copyFrom = i+1;
                    messages.add("NULL character is not allowed anywhere.");
                    break;
            }
        }
        if (output != null)
        {
            if (copyFrom != text.length())
                output.append(text.substring(copyFrom));
                string.setText(output.toString());
        }
    }

    public List getMessages()
    {
        return messages;
    }
    
    
}
