1   /**
2    * Copyright (c) 2000-2009 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.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.DiffResult;
27  import com.liferay.portal.kernel.util.DiffUtil;
28  import com.liferay.portal.kernel.util.HtmlUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.kernel.util.StringPool;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.wiki.NoSuchPageException;
34  import com.liferay.portlet.wiki.model.WikiPage;
35  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
36  import com.liferay.portlet.wiki.util.WikiUtil;
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 portletConfig,
60              RenderRequest renderRequest, RenderResponse renderResponse)
61          throws Exception {
62  
63          try {
64              ActionUtil.getNode(renderRequest);
65              ActionUtil.getPage(renderRequest);
66  
67              compareVersions(renderRequest);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchPageException) {
71  
72                  SessionErrors.add(renderRequest, 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 renderRequest)
85          throws Exception {
86  
87          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
88  
89          String title = ParamUtil.getString(renderRequest, "title");
90  
91          double sourceVersion = ParamUtil.getDouble(
92              renderRequest, "sourceVersion");
93          double targetVersion = ParamUtil.getDouble(
94              renderRequest, "targetVersion");
95          String type = ParamUtil.getString(renderRequest, "type", "escape");
96  
97          WikiPage sourcePage = WikiPageServiceUtil.getPage(
98              nodeId, title, sourceVersion);
99          WikiPage targetPage = WikiPageServiceUtil.getPage(
100             nodeId, title, targetVersion);
101 
102         String sourceContent = sourcePage.getContent();
103         String targetContent = targetPage.getContent();
104 
105         sourceContent = WikiUtil.processContent(sourceContent);
106         targetContent = WikiUtil.processContent(targetContent);
107 
108         if (type.equals("escape")) {
109             sourceContent = HtmlUtil.escape(sourceContent);
110             targetContent = HtmlUtil.escape(targetContent);
111         }
112         else if (type.equals("strip")) {
113             sourceContent = HtmlUtil.extractText(sourceContent);
114             targetContent = HtmlUtil.extractText(targetContent);
115         }
116 
117         List<DiffResult>[] diffResults = DiffUtil.diff(
118             new StringReader(sourceContent), new StringReader(targetContent));
119 
120         renderRequest.setAttribute(
121             WebKeys.SOURCE_NAME, title + StringPool.SPACE + sourceVersion);
122         renderRequest.setAttribute(
123             WebKeys.TARGET_NAME, title + StringPool.SPACE + targetVersion);
124         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
125     }
126 
127 }