1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.DiffResult;
24  import com.liferay.portal.kernel.util.DiffUtil;
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  
35  import java.io.StringReader;
36  
37  import java.util.List;
38  
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="CompareVersionsAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Bruno Farache
51   *
52   */
53  public class CompareVersionsAction extends PortletAction {
54  
55      public ActionForward render(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              RenderRequest renderRequest, RenderResponse renderResponse)
58          throws Exception {
59  
60          try {
61              ActionUtil.getNode(renderRequest);
62              ActionUtil.getPage(renderRequest);
63  
64              compareVersions(renderRequest);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchPageException) {
68  
69                  SessionErrors.add(renderRequest, e.getClass().getName());
70  
71                  return mapping.findForward("portlet.wiki.error");
72              }
73              else {
74                  throw e;
75              }
76          }
77  
78          return mapping.findForward("portlet.wiki.compare_versions");
79      }
80  
81      protected void compareVersions(RenderRequest renderRequest)
82          throws Exception {
83  
84          long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
85  
86          String title = ParamUtil.getString(renderRequest, "title");
87  
88          double sourceVersion = ParamUtil.getDouble(
89              renderRequest, "sourceVersion");
90          double targetVersion = ParamUtil.getDouble(
91              renderRequest, "targetVersion");
92          String type = ParamUtil.getString(renderRequest, "type", "escape");
93  
94          WikiPage sourcePage = WikiPageServiceUtil.getPage(
95              nodeId, title, sourceVersion);
96          WikiPage targetPage = WikiPageServiceUtil.getPage(
97              nodeId, title, targetVersion);
98  
99          String sourceContent = sourcePage.getContent();
100         String targetContent = targetPage.getContent();
101 
102         sourceContent = WikiUtil.processContent(sourceContent);
103         targetContent = WikiUtil.processContent(targetContent);
104 
105         if (type.equals("escape")) {
106             sourceContent = HtmlUtil.escape(sourceContent);
107             targetContent = HtmlUtil.escape(targetContent);
108         }
109         else if (type.equals("strip")) {
110             sourceContent = HtmlUtil.extractText(sourceContent);
111             targetContent = HtmlUtil.extractText(targetContent);
112         }
113 
114         List<DiffResult>[] diffResults = DiffUtil.diff(
115             new StringReader(sourceContent), new StringReader(targetContent));
116 
117         renderRequest.setAttribute(
118             WebKeys.SOURCE_NAME, title + StringPool.SPACE + sourceVersion);
119         renderRequest.setAttribute(
120             WebKeys.TARGET_NAME, title + StringPool.SPACE + targetVersion);
121         renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
122     }
123 
124 }