protected void compareVersions(RenderRequest renderRequest) throws Exception {

    long fileEntryId = ParamUtil.getLong(renderRequest, "fileEntryId");

    String sourceVersion = ParamUtil.getString(renderRequest, "sourceVersion");
    String targetVersion = ParamUtil.getString(renderRequest, "targetVersion");

    FileEntry fileEntry = DLAppServiceUtil.getFileEntry(fileEntryId);

    String extension = fileEntry.getExtension();

    FileVersion sourceFileVersion = fileEntry.getFileVersion(sourceVersion);

    String sourceTitle = sourceFileVersion.getTitle();

    FileVersion targetFileVersion = fileEntry.getFileVersion(targetVersion);

    String targetTitle = targetFileVersion.getTitle();

    InputStream sourceIs = fileEntry.getContentStream(sourceVersion);
    InputStream targetIs = fileEntry.getContentStream(targetVersion);

    if (extension.equals("htm") || extension.equals("html") || extension.equals("xml")) {

      String escapedSource = HtmlUtil.escape(StringUtil.read(sourceIs));
      String escapedTarget = HtmlUtil.escape(StringUtil.read(targetIs));

      sourceIs = new UnsyncByteArrayInputStream(escapedSource.getBytes(StringPool.UTF8));
      targetIs = new UnsyncByteArrayInputStream(escapedTarget.getBytes(StringPool.UTF8));
    }

    if (DocumentConversionUtil.isEnabled()
        && DocumentConversionUtil.isConvertBeforeCompare(extension)) {

      String sourceTempFileId = DLUtil.getTempFileId(fileEntryId, sourceVersion);
      String targetTempFileId = DLUtil.getTempFileId(fileEntryId, targetVersion);

      sourceIs =
          new FileInputStream(
              DocumentConversionUtil.convert(sourceTempFileId, sourceIs, extension, "txt"));
      targetIs =
          new FileInputStream(
              DocumentConversionUtil.convert(targetTempFileId, targetIs, extension, "txt"));
    }

    List<DiffResult>[] diffResults =
        DiffUtil.diff(new InputStreamReader(sourceIs), new InputStreamReader(targetIs));

    renderRequest.setAttribute(WebKeys.SOURCE_NAME, sourceTitle + StringPool.SPACE + sourceVersion);
    renderRequest.setAttribute(WebKeys.TARGET_NAME, targetTitle + StringPool.SPACE + targetVersion);
    renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
  }
  protected String getPageDiff(long companyId, WikiPage latestPage, WikiPage page, Locale locale)
      throws SystemException {

    try {
      String templateId = "com/liferay/portlet/wiki/dependencies/rss.vm";

      String templateContent = ContentUtil.get(templateId);

      Template template =
          TemplateManagerUtil.getTemplate(
              TemplateManager.VELOCITY,
              new StringTemplateResource(templateId, templateContent),
              TemplateContextType.STANDARD);

      template.put("companyId", companyId);
      template.put("contextLine", Diff.CONTEXT_LINE);
      template.put("diffUtil", new DiffUtil());
      template.put("languageUtil", LanguageUtil.getLanguage());
      template.put("locale", locale);

      String sourceContent = WikiUtil.processContent(latestPage.getContent());
      String targetContent = WikiUtil.processContent(page.getContent());

      sourceContent = HtmlUtil.escape(sourceContent);
      targetContent = HtmlUtil.escape(targetContent);

      List<DiffResult>[] diffResults =
          DiffUtil.diff(
              new UnsyncStringReader(sourceContent), new UnsyncStringReader(targetContent));

      template.put("sourceResults", diffResults[0]);
      template.put("targetResults", diffResults[1]);

      UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

      template.processTemplate(unsyncStringWriter);

      return unsyncStringWriter.toString();
    } catch (Exception e) {
      throw new SystemException(e);
    }
  }
Exemplo n.º 3
0
  protected void compareVersions(RenderRequest renderRequest, RenderResponse renderResponse)
      throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long nodeId = ParamUtil.getLong(renderRequest, "nodeId");

    String title = ParamUtil.getString(renderRequest, "title");

    double sourceVersion = ParamUtil.getDouble(renderRequest, "sourceVersion");
    double targetVersion = ParamUtil.getDouble(renderRequest, "targetVersion");
    String type = ParamUtil.getString(renderRequest, "type", "escape");

    WikiPage sourcePage = WikiPageServiceUtil.getPage(nodeId, title, sourceVersion);
    WikiPage targetPage = WikiPageServiceUtil.getPage(nodeId, title, targetVersion);

    if (type.equals("html")) {
      WikiNode sourceNode = sourcePage.getNode();

      PortletURL viewPageURL = renderResponse.createRenderURL();

      viewPageURL.setParameter("struts_action", "/wiki/view");
      viewPageURL.setParameter("nodeName", sourceNode.getName());

      PortletURL editPageURL = renderResponse.createRenderURL();

      editPageURL.setParameter("struts_action", "/wiki/edit_page");
      editPageURL.setParameter("nodeId", String.valueOf(nodeId));
      editPageURL.setParameter("title", title);

      String attachmentURLPrefix =
          WikiUtil.getAttachmentURLPrefix(
              themeDisplay.getPathMain(), themeDisplay.getPlid(), nodeId, title);

      String htmlDiffResult =
          WikiUtil.diffHtml(sourcePage, targetPage, viewPageURL, editPageURL, attachmentURLPrefix);

      renderRequest.setAttribute(WebKeys.DIFF_HTML_RESULTS, htmlDiffResult);
    } else {
      String sourceContent = sourcePage.getContent();
      String targetContent = targetPage.getContent();

      sourceContent = WikiUtil.processContent(sourceContent);
      targetContent = WikiUtil.processContent(targetContent);

      if (type.equals("strip")) {
        sourceContent = HtmlUtil.extractText(sourceContent);
        targetContent = HtmlUtil.extractText(targetContent);
      } else {
        sourceContent = HtmlUtil.escape(sourceContent);
        targetContent = HtmlUtil.escape(targetContent);
      }

      List<DiffResult>[] diffResults =
          DiffUtil.diff(
              new UnsyncStringReader(sourceContent), new UnsyncStringReader(targetContent));

      renderRequest.setAttribute(WebKeys.DIFF_RESULTS, diffResults);
    }

    renderRequest.setAttribute(WebKeys.WIKI_NODE_ID, nodeId);
    renderRequest.setAttribute(WebKeys.TITLE, title);
    renderRequest.setAttribute(WebKeys.SOURCE_VERSION, sourceVersion);
    renderRequest.setAttribute(WebKeys.TARGET_VERSION, targetVersion);
  }