    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 */
    019package org.apache.wiki.tags;
    020
    021import org.apache.commons.lang3.StringUtils;
    022import org.apache.wiki.api.exceptions.ProviderException;
    023
    024import java.io.IOException;
    025
    026/**
    027 *  Includes body, if the request context matches.  To understand more about
    028 *  RequestContexts, please look at the WikiContext class.
    029 *
    030 *  @since 2.0
    031 *  @see org.apache.wiki.WikiContext
    032 */
    033public class CheckRequestContextTag extends WikiTagBase {
    034
    035    private static final long serialVersionUID = 0L;
    036
    037    private String m_context;
    038    private String[] m_contextList = {};
    039
    040    /**
    041     *  {@inheritDoc}
    042     */
    043    @Override
    044    public void initTag() {
    045        super.initTag();
    046        m_context = null;
    047        m_contextList = new String[0];
    048    }
    049
    050    /**
    051     *  Returns the context.
    052     *
    053     *  @return Return the context.
    054     */
    055    public String getContext()
    056    {
    057        return m_context;
    058    }
    059
    060    /**
    061     *  Set the context to check for.
    062     *
    063     *  @param arg One of the RequestsContexts.
    064     */
    065    public void setContext( final String arg ) {
    066        m_context = arg;
    067        m_contextList = StringUtils.split( arg,'|' );
    068    }
    069
    070    /**
    071     *  {@inheritDoc}
    072     */
    073    @Override
    074    public final int doWikiStartTag() throws IOException, ProviderException {
    075        for ( final String checkedCtx : m_contextList ) {
    076            final String ctx = m_wikiContext.getRequestContext();
    077            if ( !checkedCtx.isEmpty() ) {
    078                if ( checkedCtx.charAt( 0 ) == '!' ) {
    079                    if ( !ctx.equalsIgnoreCase( checkedCtx.substring( 1 ) ) ) {
    080                        return EVAL_BODY_INCLUDE;
    081                    }
    082                } else if ( ctx.equalsIgnoreCase( checkedCtx ) ) {
    083                    return EVAL_BODY_INCLUDE;
    084                }
    085            }
    086        }
    087        return SKIP_BODY;
    088    }
    089
    090}