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    package org.apache.wiki.tags;
020    
021    import java.io.IOException;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.wiki.api.exceptions.ProviderException;
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     */
033    public class CheckRequestContextTag
034        extends WikiTagBase
035    {
036        private static final long serialVersionUID = 0L;
037        
038        private String m_context;
039        private String[] m_contextList = {};
040    
041        /**
042         *  {@inheritDoc}
043         */
044        @Override
045        public void initTag()
046        {
047            super.initTag();
048            m_context = null;
049            m_contextList = new String[0];
050        }
051        
052        /**
053         *  Returns the context.
054         *  
055         *  @return Return the context.
056         */
057        public String getContext()
058        {
059            return m_context;
060        }
061    
062        /**
063         *  Set the context to check for.
064         *  
065         *  @param arg One of the RequestsContexts.
066         */
067        public void setContext( String arg )
068        {
069            m_context = arg;
070            
071            m_contextList = StringUtils.split(arg,'|');
072        }
073    
074        /**
075         *  {@inheritDoc}
076         */
077        @Override
078        public final int doWikiStartTag()
079            throws IOException,
080                   ProviderException
081        {
082            for(int i = 0; i < m_contextList.length; i++ )
083            {
084                String ctx = m_wikiContext.getRequestContext();
085                
086                String checkedCtx = m_contextList[i];
087    
088                if( checkedCtx.length() > 0 )
089                {
090                    if( checkedCtx.charAt(0) == '!' )
091                    {
092                        if( !ctx.equalsIgnoreCase(checkedCtx.substring(1) ) )
093                        {
094                            return EVAL_BODY_INCLUDE;
095                        }
096                    }
097                    else if( ctx.equalsIgnoreCase(m_contextList[i]) )
098                    {
099                        return EVAL_BODY_INCLUDE;
100                    }
101                }
102            }
103    
104            return SKIP_BODY;
105        }
106    }