/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package com.ecyrd.jspwiki.filters; import com.ecyrd.jspwiki.TextUtil; import com.ecyrd.jspwiki.WikiContext; import com.ecyrd.jspwiki.WikiEngine; import com.ecyrd.jspwiki.auth.UserManager; import com.ecyrd.jspwiki.auth.UserProfile; import java.text.SimpleDateFormat; import java.util.Date; /** * Implements a stamp filter to easier stamp a comment with * one's name and the current date. * Replaces
_menowwith *
''--[username] yyyy-mm-dd''. * Replaces
menow_with *
''yyyy-mm-dd [username]:''. * * todo allow customization of name and date format via user preferences * * @author Xan Gregg */ public class StampFilter extends BasicPageFilter { public String preSave( WikiContext wikiContext, String content ) { WikiEngine engine = wikiContext.getEngine(); UserManager mgr = engine.getUserManager(); UserProfile user = mgr.getUserProfile( wikiContext.getHttpRequest() ); if (user == null) return content; // bail out here, I've never seen this happen, // even for not logged-in users (whose name is their ip number) String name = user.getName(); if (name.length() == 0 || Character.isDigit(name.charAt(0))) name = "unknown"; // convert ip number else name = "[" + name + "]"; // could check for existence, but might as well encourage personal pages Date today = new Date(); SimpleDateFormat fmt = new SimpleDateFormat( "yyyy-MM-dd" ); String source = "_menow"; String replacement = "''--" + name + " " + fmt.format( today ) + "''"; content = TextUtil.replaceString( content, source, replacement ); source = "menow_"; replacement = "''" + fmt.format( today ) + " " + name + ":''"; content = TextUtil.replaceString( content, source, replacement ); return content; } }