HeaderTag.java :  » J2EE » Jaffa » org » jaffa » presentation » portlet » widgets » taglib » Java Open Source

Java Open Source » J2EE » Jaffa 
Jaffa » org » jaffa » presentation » portlet » widgets » taglib » HeaderTag.java
/*
 * ====================================================================
 * JAFFA - Java Application Framework For All
 *
 * Copyright (C) 2002 JAFFA Development Group
 *
 *     This library is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU Lesser General Public
 *     License as published by the Free Software Foundation; either
 *     version 2.1 of the License, or (at your option) any later version.
 *
 *     This library is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *     Lesser General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public
 *     License along with this library; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Redistribution and use of this software and associated documentation ("Software"),
 * with or without modification, are permitted provided that the following conditions are met:
 * 1.  Redistributions of source code must retain copyright statements and notices.
 *         Redistributions must also contain a copy of this document.
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 3.  The name "JAFFA" must not be used to endorse or promote products derived from
 *   this Software without prior written permission. For written permission,
 *   please contact mail to: jaffagroup@yahoo.com.
 * 4.  Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
 *   appear in their names without prior written permission.
 * 5.  Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 */

package org.jaffa.presentation.portlet.widgets.taglib;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.ServletRequest;
import java.io.PrintWriter;
import java.io.IOException;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import org.jaffa.presentation.portlet.widgets.taglib.exceptions.JspWriteRuntimeException;
import org.jaffa.util.URLHelper;
import javax.servlet.jsp.tagext.TryCatchFinally;

/** Tag Handler for the Header tag.*/
public class HeaderTag extends TagSupport implements TryCatchFinally {

    private static Logger log = Logger.getLogger(HeaderTag.class);

    /** property declaration for tag attribute: errorBoxInSameWindow.
     */
    private boolean m_errorBoxInSameWindow;

    /** property declaration for tag attribute: noCache.
     */
    private boolean m_noCache = true;

    /** Default constructor.
     */
    public HeaderTag() {
        super();
        initTag();
    }

    private void initTag() {
        m_errorBoxInSameWindow = false;
        m_noCache = true;
    }

    ////////////////////////////////////////////////////////////////
    ///                                                          ///
    ///   User methods.                                          ///
    ///                                                          ///
    ///   Modify these methods to customize your tag handler.    ///
    ///                                                          ///
    ////////////////////////////////////////////////////////////////

    /** Returns the html for the BASE tag.
     * @return the html for the BASE tag.
     */
    private String getBaseTag() {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        StringBuffer buf = new StringBuffer("<base href=\"");
        buf.append(URLHelper.getBase((HttpServletRequest) pageContext.getRequest()));
        buf.append("\"/>\n" );
        return buf.toString();
    }


    /** Returns the html for disabling caching.
     * @return the html for disabling caching.
     */
    private String getNoCacheTag() {
        if (getNoCache()) {
            StringBuffer buf = new StringBuffer();
            buf.append("<META http-equiv=\"Pragma\" content=\"no-cache\"/>\n");
            buf.append("<META http-equiv=\"Cache-Control\" content=\"no-cache\"/>\n");
            buf.append("<META http-equiv=\"Expires\" content=\"0\"/>\n");
            return buf.toString();
        } else {
            return "";
        }
    }

    //
    // methods called from doStartTag()
    //
    /**
     * This generates the HTML for the tag.
     */
    public void otherDoStartTagOperations()  {

        //
        // TODO: code that performs other operations in doStartTag
        //       should be placed here.
        //       It will be called after initializing variables,
        //       finding the parent, setting IDREFs, etc, and
        //       before calling theBodyShouldBeEvaluated().
        //

        JspWriter writer = pageContext.getOut();
        StringBuffer buf = new StringBuffer();

        buf.append(getNoCacheTag());
        buf.append(getBaseTag());
        buf.append( "<SCRIPT type=\"text/javascript\" src=\"jaffa/js/panels/header.js\"/></SCRIPT>\n" );
        buf.append( "<SCRIPT type=\"text/javascript\" src=\"jaffa/js/panels/errorpopup.js\"/></SCRIPT>\n" );
        if ( getErrorBoxInSameWindow() ) {
            buf.append("<script> var windowType = \"false\" </script>");
            pageContext.setAttribute(TagHelper.ATTRIBUTE_ERROR_BOX_IN_SAME_WINDOW, Boolean.TRUE, pageContext.REQUEST_SCOPE);
        } else {
            buf.append("<script> var windowType = \"true\" </script>");
            pageContext.setAttribute(TagHelper.ATTRIBUTE_ERROR_BOX_IN_SAME_WINDOW, Boolean.FALSE, pageContext.REQUEST_SCOPE);
        }

        try {
            writer.println(buf.toString());
        } catch (IOException e) {
            String str = "Exception in writing the HeaderTag";
            log.error(str, e);
            throw new JspWriteRuntimeException(str, e);
        }
    }

    /** Returns a true.
     * @return a true.
     */
    public boolean theBodyShouldBeEvaluated()  {

        //
        // TODO: code that determines whether the body should be
        //       evaluated should be placed here.
        //       Called from the doStartTag() method.
        //
        return true;

    }


    //
    // methods called from doEndTag()
    //
    /** Does nothing.
     */
    public void otherDoEndTagOperations()  {

        //
        // TODO: code that performs other operations in doEndTag
        //       should be placed here.
        //       It will be called after initializing variables,
        //       finding the parent, setting IDREFs, etc, and
        //       before calling shouldEvaluateRestOfPageAfterEndTag().
        //


    }

    /** Returns a true.
     * @return a true.
     */
    public boolean shouldEvaluateRestOfPageAfterEndTag()  {

        //
        // TODO: code that determines whether the rest of the page
        //       should be evaluated after the tag is processed
        //       should be placed here.
        //       Called from the doEndTag() method.
        //
        return true;

    }


    //
    // methods called from doAfterBody()
    //
    /** Returns a false.
     * @return a false.
     */
    public boolean theBodyShouldBeEvaluatedAgain()  {

        //
        // TODO: code that determines whether the tag body should be
        //       evaluated again after processing the tag
        //       should be placed here.
        //       You can use this method to create iterating tags.
        //       Called from the doAfterBody() method.
        //
        return false;

    }


    ////////////////////////////////////////////////////////////////
    ///                                                          ///
    ///   Tag Handler interface methods.                         ///
    ///                                                          ///
    ///   Do not modify these methods; instead, modify the       ///
    ///   methods that they call.                                ///
    ///                                                          ///
    ////////////////////////////////////////////////////////////////


    /** .//GEN-BEGIN:doStartTag
     *
     * This method is called when the JSP engine encounters the start tag,
     * after the attributes are processed.
     * Scripting variables (if any) have their values set here.
     * @return EVAL_BODY_TAG if the JSP engine should evaluate the tag body, otherwise return SKIP_BODY.
     * This method is automatically generated. Do not modify this method.
     * Instead, modify the methods that this method calls.
     *
     */
    public int doStartTag() throws JspException, JspException {
        otherDoStartTagOperations();

        if (theBodyShouldBeEvaluated()) {
            return EVAL_BODY_INCLUDE;
        } else {
            return SKIP_BODY;
        }
    }//GEN-END:doStartTag

    /** .//GEN-BEGIN:doEndTag
     *
     *
     * This method is called after the JSP engine finished processing the tag.
     * @return EVAL_PAGE if the JSP engine should continue evaluating the JSP page, otherwise return SKIP_PAGE.
     * This method is automatically generated. Do not modify this method.
     * Instead, modify the methods that this method calls.
     *
     */
    public int doEndTag() throws JspException, JspException {
        otherDoEndTagOperations();

        if (shouldEvaluateRestOfPageAfterEndTag()) {
            return EVAL_PAGE;
        } else {
            return SKIP_PAGE;
        }
    }//GEN-END:doEndTag

    /** Getter for the attribute errorBoxInSameWindow.
     * @return Value of attribute errorBoxInSameWindow.
     */
    public boolean getErrorBoxInSameWindow() {
        return m_errorBoxInSameWindow;
    }

    /** Setter for the attribute errorBoxInSameWindow.
     * @param value New value of attribute errorBoxInSameWindow.
     */
    public void setErrorBoxInSameWindow(boolean value) {
        m_errorBoxInSameWindow = value;
    }

    /** Getter for the attribute noCache.
     * @return Value of attribute noCache.
     */
    public boolean getNoCache() {
        return m_noCache;
    }

    /** Setter for the attribute noCache.
     * @param value New value of attribute noCache.
     */
    public void setNoCache(boolean value) {
        m_noCache = value;
    }

    /** Invoked if a Throwable occurs while evaluating the BODY inside a tag or in any of the following methods: Tag.doStartTag(), Tag.doEndTag(), IterationTag.doAfterBody() and BodyTag.doInitBody().
     * This method is not invoked if the Throwable occurs during one of the setter methods.
     * This method will merely re-throw the input Throwable.
     * @param t The throwable exception navigating through this tag
     * @throws Throwable The throwable exception navigating through this tag.
     */
    public void doCatch(Throwable t) throws Throwable {
        throw t;
    }

    /** Invoked in all cases after doEndTag() for any class implementing Tag, IterationTag or BodyTag. This method is invoked even if an exception has occurred in the BODY of the tag, or in any of the following methods: Tag.doStartTag(), Tag.doEndTag(), IterationTag.doAfterBody() and BodyTag.doInitBody().
     * This method is not invoked if the Throwable occurs during one of the setter methods.
     * This will reset the internal fields, so that the Tag can be re-used.
     */
    public void doFinally() {
        initTag();
    }

}
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.