package org.seasar.jsf.processor;

import java.util.Iterator;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

import org.seasar.framework.util.StringUtil;
import org.seasar.jsf.JsfConfig;
import org.seasar.jsf.JsfContext;
import org.seasar.jsf.TagPool;
import org.seasar.jsf.taglib.ElementTag;
import org.xml.sax.Attributes;

/**
 * @author higa
 *  
 */
public class ElementProcessor extends TagProcessorImpl {

	private String tagName;

	public ElementProcessor() {
		setTagClass(ElementTag.class);
	}

	public void setup(String namespaceURI, String localName, String qName,
			Attributes attributes, JsfConfig jsfConfig) {

		tagName = qName;
		super.setup(namespaceURI, localName, qName, attributes, jsfConfig);
	}

	public void setProperties(Tag tag, JsfContext pagesContext) {
		ElementTag elementTag = (ElementTag) tag;
		elementTag.setTagName(tagName);
		for (Iterator i = getPropertyKeys(); i.hasNext();) {
			String propertyName = (String) i.next();
			Object value = getProperty(propertyName);
			String s = (String) value;
			if (StringUtil.isEmpty(s) || isCustomProperty(propertyName)) {
				continue;
			}
			if (value != null) {
				elementTag.addAttribute(propertyName, value.toString());
			}
		}
	}

	public void process(JsfContext pagesContext, Tag parentTag)
			throws JspException {

		TagPool tagPool = pagesContext.getTagPool();
		Tag tag = tagPool.request(getTagClass());
		try {
			process(pagesContext, tag, parentTag);
		} finally {
			tag.release();
			tagPool.release(tag);
		}

	}
}