FormatTag.java :  » Development » ivatamasks » com » ivata » mask » web » tag » format » Java Open Source

Java Open Source » Development » ivatamasks 
ivatamasks » com » ivata » mask » web » tag » format » FormatTag.java
/*
 * Copyright (c) 2001 - 2005 ivata limited.
 * All rights reserved.
 * -----------------------------------------------------------------------------
 * ivata masks may be redistributed under the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * These programs are free software; you can redistribute them and/or
 * modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * These programs are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License in the file LICENSE.txt for more
 * details.
 *
 * If you would like a copy of the GNU General Public License write to
 *
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 *
 *
 * To arrange commercial support and licensing, contact ivata at
 *                  http://www.ivata.com/contact.jsp
 * -----------------------------------------------------------------------------
 * $Log: FormatTag.java,v $
 * Revision 1.4  2005/10/02 14:06:34  colinmacleod
 * Added/improved log4j logging.
 *
 * Revision 1.3  2005/09/14 13:23:58  colinmacleod
 * Added serialVersionUID.
 *
 * Revision 1.2  2005/04/09 18:04:19  colinmacleod
 * Changed copyright text to GPL v2 explicitly.
 *
 * Revision 1.1  2005/01/19 12:58:20  colinmacleod
 * Moved from ivata groupware.
 *
 * Revision 1.3  2004/03/21 21:16:09  colinmacleod
 * Shortened name to ivata op.
 *
 * Revision 1.2  2004/02/01 22:00:34  colinmacleod
 * Added full names to author tags
 *
 * Revision 1.1.1.1  2004/01/27 20:57:58  colinmacleod
 * Moved ivata openportal to SourceForge..
 *
 * Revision 1.1.1.1  2003/10/13 20:50:13  colin
 * Restructured portal into subprojects
 *
 * Revision 1.1  2003/02/25 08:16:44  colin
 * moved to jsp category
 *
 * Revision 1.4  2003/02/04 17:43:51  colin
 * copyright notice
 *
 * Revision 1.3  2002/06/21 12:03:55  colin
 * new format tag library, interface to the com.ivata.groupware.web.format.*
 * classes
 * -----------------------------------------------------------------------------
 */
package com.ivata.mask.web.tag.format;

import org.apache.log4j.Logger;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.ivata.mask.web.format.HTMLFormatter;


/**
 * <p>This class wraps the <code>HTMLFormatter</code> class, so that
 * you can call {@link com.ivata.groupware.web.format.HTMLFormatter#format
 * HTMLFormatter.format} as a JSP tag.</p>
 * <p><b>Tag attributes:</b><br/>
 * <table cellpadding='2' cellspacing='5' border='0' align='center'
 * width='85%'>
 *   <tr class='TableHeadingColor'>
 *     <th>attribute</th>
 *     <th>reqd.</th>
 *     <th>param. class</th>
 *     <th width='100%'>description</th>
 *   </tr>
 *   <tr class='TableRowColor'>
 *     <td>formatter</td>
 *     <td>false</td>
 *     <td><code>com.ivata.html.format.HTMLFormatter</code></td>
 *     <td>Sets the formatter which actually does the formatting.</td>
 *   </tr>
 * </table>
 * </p>
 *
 * @since 2002-06-20
 * @author Colin MacLeod
 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
 * @version $Revision: 1.4 $
 */
public class FormatTag extends BodyTagSupport {
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(FormatTag.class);

    /**
     * Serialization version (for <code>Serializable</code> interface).
     */
    private static final long serialVersionUID = 1L;
    /**
     * <p>This is the object which actually does all the hard work. We just wrap
     * <code>HTMLFormatter.format</code> in a JSP tag.</p>
     */
    private HTMLFormatter formatter = null;

    /**
     * <p>Here the contents of the tag are formatted using the
     * <code>HTMLFormatter</code> provided.</p>
     * <p>This method is called after the JSP engine processes the body content
     * of the tag.</p>
     *
     * @return <code>SKIP_BODY</code> since the JSP engine should not evaluate
     * the tag body again.
     * @throws JspException encapsulates any exception when calling
     * <code>out.println</code>.
     * writeTagBodyContent}.
     *
     */
    public int doAfterBody() throws JspException {
        if (logger.isDebugEnabled()) {
            logger.debug("doAfterBody() - start");
        }

        try {
            JspWriter out = getPreviousOut();
            BodyContent bodyContent = getBodyContent();
            out.print(formatter.format(bodyContent.getString()));
        } catch (IOException ex) {
            logger.error("doAfterBody()", ex);

            throw new JspException("ERROR in FormatTag: " + ex);
        }

        // for this tag, we only want to evluate the body one time

        if (logger.isDebugEnabled()) {
            logger.debug("doAfterBody() - end - return value = " + SKIP_BODY);
        }
        return SKIP_BODY;
    }

    /**
     * <p>This method is called when the JSP engine encounters the start tag,
     * after the attributes are processed.<p>
     *
     * <p>Scripting variables (if any) have their values set here.</p>
     *
     * @return <code>EVAL_BODY_BUFFERED</code> since the body is always
     * evaluated once.
     */
    public int doStartTag() {
        if (logger.isDebugEnabled()) {
            logger.debug("doStartTag() - start");
        }

        // nothing to do here, except note that we have a body...

        if (logger.isDebugEnabled()) {
            logger.debug("doStartTag() - end - return value = "
                    + EVAL_BODY_BUFFERED);
        }
        return EVAL_BODY_BUFFERED;
    }

    /**
     * <p>Set the {@link com.ivata.groupware.web.format.HTMLFormatter
     * HTMLFormatter}
     * object which actually does all the hard work. We just wrap
     * <code>HTMLFormatter.format</code> in a JSP tag.</p>
     * @param formatterParam <code>HTMLFormatter</code> used to format the tag
     * body.
     */
    public void setFormatter(final HTMLFormatter formatterParam) {
        if (logger.isDebugEnabled()) {
            logger.debug("setFormatter(HTMLFormatter formatterParam = "
                    + formatterParam + ") - start");
        }

        this.formatter = formatterParam;

        if (logger.isDebugEnabled()) {
            logger.debug("setFormatter(HTMLFormatter) - end");
        }
    }
}
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.