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    import javax.servlet.jsp.JspWriter;
023    
024    import org.apache.wiki.*;
025    
026    /**
027     *  Writes a diff link.  Body of the link becomes the link text.
028     *  <P><B>Attributes</B></P>
029     *  <UL>
030     *    <LI>page - Page name to refer to.  Default is the current page.</LI>
031     *    <LI>version - The older of these versions.  May be an integer to
032     *        signify a version number, or the text "latest" to signify the latest version.
033     *        If not specified, will default to "latest".  May also be "previous" to signify
034     *        a version prior to this particular version.</LI>
035     *    <LI>newVersion - The newer of these versions.  Can also be "latest", or "previous".  Defaults to "latest".</LI>
036     *  </UL>
037     *
038     *  If the page does not exist, this tag will fail silently, and not evaluate
039     *  its body contents.
040     *
041     *  @since 2.0
042     */
043    public class DiffLinkTag
044        extends WikiLinkTag
045    {
046        private static final long serialVersionUID = 0L;
047        
048        public static final String VER_LATEST   = "latest";
049        public static final String VER_PREVIOUS = "previous";
050        public static final String VER_CURRENT  = "current";
051    
052        private String m_version    = VER_LATEST;
053        private String m_newVersion = VER_LATEST;
054    
055        public void initTag()
056        {
057            super.initTag();
058            m_version = m_newVersion = VER_LATEST;
059        }
060    
061        public final String getVersion()
062        {
063            return m_version;
064        }
065    
066        public void setVersion( String arg )
067        {
068            m_version = arg;
069        }
070    
071        public final String getNewVersion()
072        {
073            return m_newVersion;
074        }
075    
076        public void setNewVersion( String arg )
077        {
078            m_newVersion = arg;
079        }
080    
081        public final int doWikiStartTag()
082            throws IOException
083        {
084            WikiEngine engine   = m_wikiContext.getEngine();
085            String     pageName = m_pageName;
086    
087            if( m_pageName == null )
088            {
089                if( m_wikiContext.getPage() != null )
090                {
091                    pageName = m_wikiContext.getPage().getName();
092                }
093                else
094                {
095                    return SKIP_BODY;
096                }
097            }
098    
099            JspWriter out = pageContext.getOut();
100    
101            int r1 = 0;
102            int r2 = 0;
103    
104            //
105            //  In case the page does not exist, we fail silently.
106            //
107            if(!engine.pageExists(pageName))
108            {
109                return SKIP_BODY;
110            }
111    
112            if( VER_LATEST.equals(getVersion()) )
113            {
114                WikiPage latest = engine.getPage( pageName, 
115                                                  WikiProvider.LATEST_VERSION );
116    
117                if( latest == null )
118                {
119                    // This may occur if matchEnglishPlurals is on, and we access the wrong page name
120                    return SKIP_BODY;
121                }
122                r1 = latest.getVersion();
123            }
124            else if( VER_PREVIOUS.equals(getVersion()) )
125            {
126                r1 = m_wikiContext.getPage().getVersion() - 1;
127                r1 = (r1 < 1 ) ? 1 : r1;
128            }
129            else if( VER_CURRENT.equals(getVersion()) )
130            {
131                r1 = m_wikiContext.getPage().getVersion();
132            }
133            else
134            {
135                r1 = Integer.parseInt( getVersion() );
136            }
137    
138            if( VER_LATEST.equals(getNewVersion()) )
139            {
140                WikiPage latest = engine.getPage( pageName,
141                                                  WikiProvider.LATEST_VERSION );
142    
143                r2 = latest.getVersion();
144            }
145            else if( VER_PREVIOUS.equals(getNewVersion()) )
146            {
147                r2 = m_wikiContext.getPage().getVersion() - 1;
148                r2 = (r2 < 1 ) ? 1 : r2;
149            }
150            else if( VER_CURRENT.equals(getNewVersion()) )
151            {
152                r2 = m_wikiContext.getPage().getVersion();
153            }
154            else
155            {
156                r2 = Integer.parseInt( getNewVersion() );
157            }
158    
159            String url = m_wikiContext.getURL( WikiContext.DIFF,
160                                               pageName,
161                                               "r1="+r1+"&amp;r2="+r2 );
162            switch( m_format )
163            {
164              case ANCHOR:
165                out.print("<a href=\""+url+"\">");
166    
167                break;
168    
169              case URL:
170                out.print( url );
171                break;
172            }
173    
174            return EVAL_BODY_INCLUDE;
175        }
176    }