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.log4j.Logger; 022import org.apache.wiki.api.core.Context; 023import org.apache.wiki.api.core.Engine; 024import org.apache.wiki.diff.DifferenceManager; 025import org.apache.wiki.pages.PageManager; 026 027import javax.servlet.jsp.JspWriter; 028import javax.servlet.jsp.PageContext; 029import java.io.IOException; 030 031/** 032 * Writes difference between two pages using a HTML table. If there is 033 * no difference, includes the body. 034 * 035 * <P><B>Attributes</B></P> 036 * <UL> 037 * <LI>page - Page name to refer to. Default is the current page. 038 * </UL> 039 * 040 * @since 2.0 041 */ 042public class InsertDiffTag extends WikiTagBase { 043 044 private static final long serialVersionUID = 0L; 045 private static final Logger log = Logger.getLogger( InsertDiffTag.class ); 046 047 /** Attribute which is used to store the old page content to the Page Context */ 048 public static final String ATTR_OLDVERSION = "olddiff"; 049 050 /** Attribute which is used to store the new page content to the Page Context */ 051 public static final String ATTR_NEWVERSION = "newdiff"; 052 053 protected String m_pageName; 054 055 /** {@inheritDoc} */ 056 @Override public void initTag() { 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( final 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 @Override 081 public final int doWikiStartTag() throws IOException { 082 final Engine engine = m_wikiContext.getEngine(); 083 final Context ctx; 084 085 if( m_pageName == null ) { 086 ctx = m_wikiContext; 087 } else { 088 ctx = m_wikiContext.clone(); 089 ctx.setPage( engine.getManager( PageManager.class ).getPage(m_pageName) ); 090 } 091 092 final Integer vernew = ( Integer )pageContext.getAttribute( ATTR_NEWVERSION, PageContext.REQUEST_SCOPE ); 093 final Integer verold = ( Integer )pageContext.getAttribute( ATTR_OLDVERSION, PageContext.REQUEST_SCOPE ); 094 095 log.debug("Request diff between version "+verold+" and "+vernew); 096 097 if( ctx.getPage() != null ) { 098 final JspWriter out = pageContext.getOut(); 099 final String diff = engine.getManager( DifferenceManager.class ).getDiff( ctx, vernew.intValue(), verold.intValue() ); 100 101 if( diff.length() == 0 ) { 102 return EVAL_BODY_INCLUDE; 103 } 104 105 out.write( diff ); 106 } 107 108 return SKIP_BODY; 109 } 110 111} 112