001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019
020package org.apache.wiki.tags;
021
022import org.apache.wiki.util.TextUtil;
023
024import jakarta.servlet.jsp.JspTagException;
025import java.io.IOException;
026
027/**
028 *  Generates single tabbed page layout. Works together with the tabbedSection javascript.  Note that if you do not
029 *  specify an url, the body contents of the tag are loaded by the tag itself.
030 *
031 *  <P><B>Attributes</B></P>
032 *  <UL>
033 *    <LI>id - ID for this tab. (mandatory)
034 *    <LI>title - Title of this tab. (mandatory)
035 *    <LI>accesskey - Single char usable as quick accesskey (alt- or ctrl-) (optional)
036 *    <li>url - If you <i>don't</i> want to create a Javascript-enabled tag, you can use this
037 *              to make the tab look just the usual tag, but instead, it will actually link
038 *              to that page.  This can be useful in certain cases where you have something
039 *              that you want to look like a part of a tag, but for example, due to it being
040 *              very big in size, don't want to include it as a part of the page content
041 *              every time.
042 *  </UL>
043 *
044 *  @since v2.3.63
045 */
046public class TabTag extends WikiTagBase {
047
048    private static final long serialVersionUID = -8534125226484616489L;
049    private String m_accesskey;
050    private String m_tabTitle;
051    private String m_url;
052
053    /**
054     * {@inheritDoc}
055     */
056    @Override
057    public void doFinally() {
058        super.doFinally();
059
060        m_accesskey = null;
061        m_tabTitle  = null;
062        m_url       = null;
063    }
064
065    /**
066     * Sets the tab title.
067     * @param aTabTitle the tab title
068     */
069    public void setTitle( final String aTabTitle ) {
070        m_tabTitle = TextUtil.replaceEntities( aTabTitle );
071    }
072
073    /**
074     * Sets the tab access key.
075     *
076     * @param anAccesskey the access key
077     */
078    public void setAccesskey( final String anAccesskey ) {
079        m_accesskey = TextUtil.replaceEntities( anAccesskey ); //take only the first char
080    }
081
082    /**
083     * Sets the tab URL.
084     *
085     * @param url the URL
086     */
087    public void setUrl( final String url ) {
088        //  The url is written inside a single quoted attribute, and replaceEntities leaves the single quote alone.
089        m_url = url == null ? null : TextUtil.replaceEntities( url ).replace( "'", "&#39;" );
090    }
091
092    // insert <u> ..accesskey.. </u> in title
093    private boolean handleAccesskey() {
094        if( ( m_tabTitle == null ) || ( m_accesskey == null ) ) return false;
095
096        final int pos = m_tabTitle.toLowerCase().indexOf( m_accesskey.toLowerCase() );
097        if( pos > -1 ) {
098            m_tabTitle = m_tabTitle.substring( 0, pos ) + "<span class='accesskey'>"
099                    + m_tabTitle.charAt( pos ) + "</span>" + m_tabTitle.substring( pos + 1 );
100        }
101        return true;
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    @Override
108    public int doWikiStartTag() throws JspTagException {
109        final TabbedSectionTag parent = ( TabbedSectionTag ) findAncestorWithClass( this, TabbedSectionTag.class );
110
111        //  Sanity checks
112        if( getId() == null ) {
113            throw new JspTagException( "Tab Tag without \"id\" attribute" );
114        }
115        if( m_tabTitle == null ) {
116            throw new JspTagException( "Tab Tag without \"tabTitle\" attribute" );
117        }
118        if( parent == null ) {
119            throw new JspTagException( "Tab Tag without parent \"TabbedSection\" Tag" );
120        }
121        if( !parent.isStateGenerateTabBody() ) {
122            return SKIP_BODY;
123        }
124
125        final StringBuilder sb = new StringBuilder( 32 );
126        sb.append( "<div id=\"" ).append( getId() ).append( "\"" );
127        if( !parent.validateDefaultTab( getId() ) ) {
128            sb.append( " class=\"hidetab\"" );
129        }
130        sb.append( " >\n" );
131
132        try {
133            pageContext.getOut().write( sb.toString() );
134        } catch( final IOException e ) {
135            throw new JspTagException( "IO Error: " + e.getMessage() );
136        }
137
138        return EVAL_BODY_INCLUDE;
139    }
140
141    /**
142     * {@inheritDoc}
143     */
144    @Override
145    public int doEndTag() throws JspTagException {
146        final TabbedSectionTag parent = ( TabbedSectionTag ) findAncestorWithClass( this, TabbedSectionTag.class );
147        final StringBuilder sb = new StringBuilder();
148        if( parent.isStateFindDefaultTab() ) {
149            // inform the parent of each tab
150            parent.validateDefaultTab( getId() );
151        } else if( parent.isStateGenerateTabBody() ) {
152            sb.append( "</div>\n" );
153        } else if( parent.isStateGenerateTabMenu() ) {
154            sb.append( "<a" );
155            if( parent.validateDefaultTab( getId() ) ) {
156                sb.append( " class=\"activetab\"" );
157            }
158
159            sb.append( " id=\"menu-" ).append( getId() ).append( "\"" );
160
161            if( m_url != null ) {
162                sb.append( " href='" ).append( m_url ).append( "'" );
163            }
164
165            if( handleAccesskey() ) {
166                sb.append( " accesskey=\"" ).append( m_accesskey ).append( "\"" );
167            }
168
169            sb.append( " >" );
170            sb.append( m_tabTitle );
171            sb.append( "</a>" );
172        }
173
174        try {
175            pageContext.getOut().write( sb.toString() );
176        } catch( final IOException e ) {
177            throw new JspTagException( "IO Error: " + e.getMessage() );
178        }
179
180        return EVAL_PAGE;
181    }
182
183}