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