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 java.io.IOException;
022
023import javax.servlet.jsp.JspWriter;
024import javax.servlet.jsp.PageContext;
025
026import org.apache.log4j.Logger;
027import org.apache.wiki.WikiContext;
028import org.apache.wiki.WikiEngine;
029
030/**
031 *  Writes difference between two pages using a HTML table.  If there is
032 *  no difference, includes the body.
033 *
034 *  <P><B>Attributes</B></P>
035 *  <UL>
036 *    <LI>page - Page name to refer to.  Default is the current page.
037 *  </UL>
038 *
039 *  @since 2.0
040 */
041public class InsertDiffTag extends WikiTagBase {
042
043    private static final long serialVersionUID = 0L;
044    private static final Logger log = Logger.getLogger( InsertDiffTag.class );
045    
046    /** Attribute which is used to store the old page content to the Page Context */
047    public static final String ATTR_OLDVERSION = "olddiff";
048
049    /** Attribute which is used to store the new page content to the Page Context */
050    public static final String ATTR_NEWVERSION = "newdiff";
051
052    protected String m_pageName;
053
054    /** {@inheritDoc} */
055    public void initTag()
056    {
057        super.initTag();
058        m_pageName = null;
059    }
060
061    /**
062     *  Sets the page name.
063     *  @param page Page to get diff from.
064     */
065    public void setPage( String page )
066    {
067        m_pageName = page;
068    }
069
070    /**
071     *  Gets the page name.
072     * @return The page name.
073     */
074    public String getPage()
075    {
076        return m_pageName;
077    }
078
079    /** {@inheritDoc} */
080    public final int doWikiStartTag()
081        throws IOException
082    {
083        WikiEngine engine = m_wikiContext.getEngine();
084        WikiContext ctx;
085        
086        if( m_pageName == null )
087        {
088            ctx = m_wikiContext;
089        }
090        else
091        {
092            ctx = (WikiContext)m_wikiContext.clone();
093            ctx.setPage( engine.getPage(m_pageName) );
094        }
095
096        Integer vernew = (Integer) pageContext.getAttribute( ATTR_NEWVERSION,
097                                                             PageContext.REQUEST_SCOPE );
098        Integer verold = (Integer) pageContext.getAttribute( ATTR_OLDVERSION,
099                                                             PageContext.REQUEST_SCOPE );
100
101        log.debug("Request diff between version "+verold+" and "+vernew);
102
103        if( ctx.getPage() != null )
104        {
105            JspWriter out = pageContext.getOut();
106
107            String diff = engine.getDiff( ctx, 
108                                          vernew.intValue(), 
109                                          verold.intValue() );
110
111            if( diff.length() == 0 )
112            {
113                return EVAL_BODY_INCLUDE;
114            }
115
116            out.write( diff );
117        }
118
119        return SKIP_BODY;
120    }
121}
122