001: /**
002: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the latest version of the GNU Lesser General
006: * Public License as published by the Free Software Foundation;
007: *
008: * This program is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
011: * GNU Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public License
014: * along with this program (LICENSE.txt); if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
016: */package org.jamwiki.servlets;
017:
018: import java.util.Collection;
019: import java.util.Enumeration;
020: import javax.servlet.http.HttpServletRequest;
021: import javax.servlet.http.HttpServletResponse;
022: import org.apache.commons.lang.StringUtils;
023: import org.jamwiki.WikiMessage;
024: import org.jamwiki.utils.DiffUtil;
025: import org.jamwiki.utils.WikiLogger;
026: import org.jamwiki.utils.WikiUtil;
027: import org.springframework.web.servlet.ModelAndView;
028:
029: /**
030: * Used to display a diff between two versions of a topic.
031: */
032: public class DiffServlet extends JAMWikiServlet {
033:
034: private static final WikiLogger logger = WikiLogger
035: .getLogger(DiffServlet.class.getName());
036: protected static final String JSP_DIFF = "diff.jsp";
037:
038: /**
039: *
040: */
041: protected ModelAndView handleJAMWikiRequest(
042: HttpServletRequest request, HttpServletResponse response,
043: ModelAndView next, WikiPageInfo pageInfo) throws Exception {
044: this .diff(request, next, pageInfo);
045: return next;
046: }
047:
048: /**
049: *
050: */
051: protected void diff(HttpServletRequest request, ModelAndView next,
052: WikiPageInfo pageInfo) throws Exception {
053: String topicName = WikiUtil.getTopicFromRequest(request);
054: String diffType = request.getParameter("type");
055: if (diffType != null && diffType.equals("arbitrary")) {
056: // FIXME - used with history.jsp, this is ugly
057: int firstVersion = -1;
058: int secondVersion = -1;
059: Enumeration e = request.getParameterNames();
060: while (e.hasMoreElements()) {
061: String name = (String) e.nextElement();
062: if (name.startsWith("diff:")) {
063: int version = Integer.parseInt(name.substring(name
064: .indexOf(':') + 1));
065: if (firstVersion >= 0) {
066: secondVersion = version;
067: } else {
068: firstVersion = version;
069: }
070: }
071: }
072: if (firstVersion == -1 || secondVersion == -1) {
073: next.addObject("badinput", "true");
074: } else {
075: Collection diffs = DiffUtil.diffTopicVersions(
076: topicName, Math
077: .max(firstVersion, secondVersion), Math
078: .min(firstVersion, secondVersion));
079: next.addObject("diffs", diffs);
080: }
081: } else {
082: int topicVersionId1 = 0;
083: if (!StringUtils.isBlank(request.getParameter("version1"))) {
084: topicVersionId1 = new Integer(request
085: .getParameter("version1")).intValue();
086: }
087: int topicVersionId2 = 0;
088: if (!StringUtils.isBlank(request.getParameter("version2"))) {
089: topicVersionId2 = new Integer(request
090: .getParameter("version2")).intValue();
091: }
092: Collection diffs = DiffUtil.diffTopicVersions(topicName,
093: topicVersionId1, topicVersionId2);
094: next.addObject("diffs", diffs);
095: }
096: pageInfo.setPageTitle(new WikiMessage("diff.title", topicName));
097: pageInfo.setTopicName(topicName);
098: pageInfo.setContentJsp(JSP_DIFF);
099: }
100: }
|