HtmlParserCallback.java :  » Portal » Open-Portal » com » sun » portal » wireless » htmlconversion » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » wireless » htmlconversion » HtmlParserCallback.java
/*
 * Created on Feb 9, 2005
 *
 */
package com.sun.portal.wireless.htmlconversion;


import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML.Tag;
import javax.swing.text.html.HTMLEditorKit.ParserCallback;

import java.util.Enumeration;
import java.util.HashMap;


/**
 * The parser callback class, which handles events generated by the HTML parser.
 *
 * @author ashwin.mathew@sun.com
 */
public class HtmlParserCallback extends ParserCallback
{
    
    private GenericHtmlParserCallback callback;


    /**
     * 
     */
    public HtmlParserCallback(GenericHtmlParserCallback callback)
    {
        super();
        
        this.callback = callback; 
    }
    
    
    
    /* (non-Javadoc)
     * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleStartTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, int)
     */
    public void handleStartTag(Tag t, MutableAttributeSet a, int pos)
    {
        startTag(t, a, pos, false);
    }
    
    
    /* (non-Javadoc)
     * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleEndTag(javax.swing.text.html.HTML.Tag, int)
     */
    public void handleEndTag(Tag t, int pos)
    {
        callback.endTag(t.toString());
    }
    
    
    /* (non-Javadoc)
     * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleSimpleTag(javax.swing.text.html.HTML.Tag, javax.swing.text.MutableAttributeSet, int)
     */
    public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos)
    {
        startTag(t, a, pos, true);
        callback.endTag(t.toString());
    }
    
    
    /* (non-Javadoc)
     * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleText(char[], int)
     */
    public void handleText(char[] data, int pos)
    {
        callback.handleText(data, 0, data.length);
    }
    
    
    /* (non-Javadoc)
     * @see javax.swing.text.html.HTMLEditorKit.ParserCallback#handleError(java.lang.String, int)
     */
    public void handleError(String errorMsg, int pos)
    {
    }
    
    
    // Encapsulates logic for both handleStartTag() and handleSimpleTag().
    // Note that the isSimple argument is not being used at this time, but is
    // being kept as a useful placeholder, in case behaviour needs to change
    // based on the calling method.
    private void startTag(Tag t, MutableAttributeSet attributeSet, int pos, boolean isSimple)
    {
        if (callback.getState().isBypassTagProcessing())
        {
            return;
        }
        
        HashMap attributes = new HashMap();
        
        Enumeration names = attributeSet.getAttributeNames();
        while (names.hasMoreElements())
        {
            Object nameO = names.nextElement();
            String name = nameO.toString();
            String value = attributeSet.getAttribute(nameO).toString().trim();
            attributes.put(name, value);
        }
        
        callback.startTag(t.toString(), attributes);
    }
        
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.