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 *  Writes a link to a parent of a Wiki page.
028 *
029 *  <P><B>Attributes</B></P>
030 *  <UL>
031 *    <LI>page - Page name to refer to.  Default is the current page.
032 *    <LI>format - either "anchor" or "url" to output either an <A>... or just the HREF part of one.
033 *  </UL>
034 *
035 *  @since 2.0
036 */
037public class LinkToParentTag extends LinkToTag {
038
039    private static final long serialVersionUID = 0L;
040    
041    @Override
042    public int doWikiStartTag() throws IOException {
043        final Page p = m_wikiContext.getPage();
044
045        //  We just simply set the page to be our parent page and call the superclass.
046        if( p instanceof Attachment ) {
047            setPage( ((Attachment)p).getParentName() );
048        } else {
049            final String name = p.getName();
050            final int entrystart = name.indexOf( "_blogentry_" );
051            if( entrystart != -1 ) {
052                setPage( name.substring( 0, entrystart ) );
053            }
054
055            final int commentstart = name.indexOf( "_comments_" );
056            if( commentstart != -1 ) {
057                setPage( name.substring( 0, commentstart ) );
058            }
059        }
060
061        return super.doWikiStartTag();
062    }
063
064}