/*
 * WebWork, Web Application Framework
 *
 * Distributable under Apache license.
 * See terms of license at opensource.org
 */
package net.killingar.webwork.view.taglib;

import webwork.util.TextUtil;
import webwork.util.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 *  Access the value of a named property. By default (implicitly),
 *  this tag will escape its contents if it does *not* have a body.
 *  If it does have a body, the tag will not escape the contents.
 *  You can explicitly tell the tag to escape or not.
 *  Quoted text that is escaped will have its quotes stripped off.
 *
 * @author Rickard Öberg (rickard@dreambean.com)
 * @author Matt Baldree (matt@smallleap.com)
 * @version $Revision: 1.1 $
 */
public class DateTag extends webwork.view.taglib.WebWorkTagSupport
{
	// Attributes ----------------------------------------------------
	protected String valueAttr;
	protected String formatting = "yyyy-MM-dd";
	protected DateFormat formatter = new SimpleDateFormat(formatting);

	// Public --------------------------------------------------------
	public void setValue(String inName) { valueAttr = inName; }
	public void setFormat(String inFormatting) { formatting = inFormatting; formatter = new SimpleDateFormat(formatting); }

	// BodyTag implementation ----------------------------------------
	public int doStartTag() throws JspException
	{
		try
		{
			Date value = (Date)findValue(valueAttr);

			pageContext.getOut().write(formatter.format(value));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new JspTagException("Could not show value " + valueAttr + ":" + e);
		}

		return SKIP_BODY;
	}
}