1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.action;
24  
25  import com.liferay.portal.kernel.util.HtmlUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.wiki.NoSuchPageException;
31  import com.liferay.portlet.wiki.model.WikiPage;
32  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
33  import com.liferay.portlet.wiki.util.WikiUtil;
34  import com.liferay.util.diff.DiffResult;
35  import com.liferay.util.diff.DiffUtil;
36  import com.liferay.util.servlet.SessionErrors;
37  
38  import java.io.StringReader;
39  
40  import java.util.List;
41  
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Bruno Farache
54   *
55   */
56  public class CompareVersionsAction extends PortletAction {
57  
58      public ActionForward render(
59              ActionMapping mapping, ActionForm form, PortletConfig config,
60              RenderRequest req, RenderResponse res)
61          throws Exception {
62  
63          try {
64              ActionUtil.getNode(req);
65              ActionUtil.getPage(req);
66  
67              compareVersions(req);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchPageException) {
71  
72                  SessionErrors.add(req, e.getClass().getName());
73  
74                  return mapping.findForward("portlet.wiki.error");
75              }
76              else {
77                  throw e;
78              }
79          }
80  
81          return mapping.findForward("portlet.wiki.compare_versions");
82      }
83  
84      protected void compareVersions(RenderRequest req) throws Exception {
85          long nodeId = ParamUtil.getLong(req, "nodeId");
86  
87          String title = ParamUtil.getString(req, "title");
88  
89          double sourceVersion = ParamUtil.getDouble(req, "sourceVersion");
90          double targetVersion = ParamUtil.getDouble(req, "targetVersion");
91          String type = ParamUtil.getString(req, "type", "escape");
92  
93          WikiPage sourcePage = WikiPageServiceUtil.getPage(
94              nodeId, title, sourceVersion);
95          WikiPage targetPage = WikiPageServiceUtil.getPage(
96              nodeId, title, targetVersion);
97  
98          String sourceContent = sourcePage.getContent();
99          String targetContent = targetPage.getContent();
100 
101         sourceContent = WikiUtil.processContent(sourceContent);
102         targetContent = WikiUtil.processContent(targetContent);
103 
104         if (type.equals("escape")) {
105             sourceContent = HtmlUtil.escape(sourceContent);
106             targetContent = HtmlUtil.escape(targetContent);
107         }
108         else if (type.equals("strip")) {
109             sourceContent = HtmlUtil.extractText(sourceContent);
110             targetContent = HtmlUtil.extractText(targetContent);
111         }
112 
113         List<DiffResult>[] diffResults = DiffUtil.diff(
114             new StringReader(sourceContent), new StringReader(targetContent));
115 
116         req.setAttribute(
117             WebKeys.SOURCE_NAME, title + StringPool.SPACE + sourceVersion);
118         req.setAttribute(
119             WebKeys.TARGET_NAME, title + StringPool.SPACE + targetVersion);
120         req.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
121     }
122 
123 }