    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.wiki.api.core.Attachment;
    022import org.apache.wiki.api.core.Page;
    023
    024import java.io.IOException;
    025
    026/**
    027 *  Includes the body, if the current page is of proper type.
    028 *
    029 *  <B>Attributes</B>
    030 *  <UL>
    031 *   <LI>type - either "page", "attachment" or "weblogentry"
    032 *  </UL>
    033 *
    034 *  @since 2.0
    035 */
    036public class PageTypeTag extends WikiTagBase {
    037
    038    private static final long serialVersionUID = 0L;
    039
    040    private String m_type;
    041
    042    @Override
    043    public void initTag() {
    044        super.initTag();
    045        m_type = null;
    046    }
    047
    048    public void setType( final String arg )
    049    {
    050        m_type = arg.toLowerCase();
    051    }
    052
    053    @Override
    054    public final int doWikiStartTag() throws IOException {
    055        final Page page = m_wikiContext.getPage();
    056        if( page != null ) {
    057            if( m_type.equals( "attachment" ) && page instanceof Attachment ) {
    058                return EVAL_BODY_INCLUDE;
    059            }
    060            if( m_type.equals( "page" ) && !( page instanceof Attachment ) ) {
    061                return EVAL_BODY_INCLUDE;
    062            }
    063            if( m_type.equals( "weblogentry" ) && !( page instanceof Attachment ) && page.getName().contains( "_blogentry_" ) ) {
    064                return EVAL_BODY_INCLUDE;
    065            }
    066        }
    067
    068        return SKIP_BODY;
    069    }
    070}