[jboss-cvs] jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/action ...

Christian Bauer christian at hibernate.org
Fri Apr 20 05:10:12 EDT 2007


  User: cbauer  
  Date: 07/04/20 05:10:12

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/action  
                        NodeHistory.java DocumentHome.java
  Log:
  Finished document history and diff feature
  
  Revision  Changes    Path
  1.4       +58 -19    jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/action/NodeHistory.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: NodeHistory.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/action/NodeHistory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- NodeHistory.java	21 Mar 2007 17:32:24 -0000	1.3
  +++ NodeHistory.java	20 Apr 2007 09:10:12 -0000	1.4
  @@ -1,15 +1,21 @@
   package org.jboss.seam.wiki.core.action;
   
   import org.jboss.seam.annotations.*;
  +import org.jboss.seam.annotations.security.Restrict;
   import org.jboss.seam.annotations.datamodel.DataModel;
   import org.jboss.seam.annotations.datamodel.DataModelSelection;
   import org.jboss.seam.ScopeType;
   import org.jboss.seam.Component;
  +import org.jboss.seam.security.Identity;
  +import org.jboss.seam.security.AuthorizationException;
  +import org.jboss.seam.contexts.Contexts;
   import org.jboss.seam.core.FacesMessages;
   import org.jboss.seam.wiki.core.dao.NodeDAO;
   import org.jboss.seam.wiki.core.model.Node;
   import org.jboss.seam.wiki.core.model.Document;
  -import org.jboss.seam.wiki.core.model.Directory;
  +import org.jboss.seam.wiki.core.engine.WikiLink;
  +import org.jboss.seam.wiki.core.engine.WikiTextParser;
  +import org.jboss.seam.wiki.core.engine.WikiTextRenderer;
   import org.jboss.seam.wiki.util.Diff;
   
   import javax.faces.application.FacesMessage;
  @@ -37,9 +43,6 @@
       @In @Out(scope = ScopeType.CONVERSATION)
       private Node currentNode;
   
  -    @Out(scope = ScopeType.CONVERSATION)
  -    private Directory currentDirectory;
  -
       private String diffResult;
   
       @Factory("historicalNodeList")
  @@ -50,14 +53,17 @@
   
       @Create
       public void create() {
  +        if (!Identity.instance().hasPermission("Node", "read", currentNode) ) {
  +            throw new AuthorizationException("You don't have permission for this operation");
  +        }
  +
           historicalNodeList = nodeDAO.findHistoricalNodes(currentNode);
  -        currentDirectory = (Directory)currentNode.getParent();
   
           if (historicalNodeList.size() == 0) {
               facesMessages.addFromResourceBundleOrDefault(
                   FacesMessage.SEVERITY_INFO,
                   "noHistory",
  -                "No stored history for this document.");
  +                "No stored history for this document, you are looking at the only existing revision.");
               NodeBrowser browser = (NodeBrowser) Component.getInstance("browser");
               browser.exitConversation(false);
           }
  @@ -65,41 +71,48 @@
   
       public void diff() {
   
  -        String revision = ((Document)currentNode).getContent();
  -        String original = ((Document)selectedHistoricalNode).getContent();
  +        // Wiki text parser needs these context variables but we don't really care because link resolving is turned off
  +        Contexts.getConversationContext().set("currentDocument", currentNode);
  +        Contexts.getConversationContext().set("currentDirectory", currentNode.getParent());
  +        String revision = renderWikiText( ((Document)currentNode).getContent() );
  +        Contexts.getConversationContext().set("currentDocument", selectedHistoricalNode);
  +        Contexts.getConversationContext().set("currentDirectory", currentNode.getParent());
  +        String original = renderWikiText( ((Document)selectedHistoricalNode).getContent() );
   
  +        // Create diff by comparing rendered HTML
           Diff diff = new Diff() {
               protected String getDeletionStartMarker() {
  -                return "XXXXXXX";
  +                return "<div class=\"diffDeleted\">";
               }
   
               protected String getDeletionEndMarker() {
  -                return "XXXXXXX";
  +                return "</div>";
               }
   
               protected String getAdditionStartMarker() {
  -                return "AAAAAAA";
  +                return "<div class=\"diffAdded\">";
               }
   
               protected String getAdditionEndMarker() {
  -                return "AAAAAAA";
  +                return "</div>";
               }
           };
   
  +        // Diff is line-based only
           String[] x = original.split("\r\n");
           String[] y = revision.split("\r\n");
  +        String[] result = diff.createDiff(x, y, "\r\n", "\r", "\n");
   
  -        String[] result = diff.renderDiff(x, y, "\r\n", "\r", "\n");
  -        
  +        // Now render the combined string array
           diffResult = Diff.renderWithDelimiter(result, "\r\n");
   
  -        System.out.println("############### RESULT OF THE DIFF: ######################");
  -
  -        System.out.println(diffResult);
  -
  -        System.out.println("#####################################");
  +        facesMessages.addFromResourceBundleOrDefault(
  +            FacesMessage.SEVERITY_INFO,
  +            "diffCreated",
  +            "Comparing current revision with historical revision " + selectedHistoricalNode.getRevision());
       }
   
  +    @Restrict("#{s:hasPermission('Node', 'edit', currentNode)}")
       public String rollback() {
           facesMessages.addFromResourceBundleOrDefault(
               FacesMessage.SEVERITY_INFO,
  @@ -111,4 +124,30 @@
       public String getDiffResult() {
           return diffResult;
       }
  +
  +    private String renderWikiText(String wikiText) {
  +        // Render the document to HTML for diff, don't resolve any wiki links (calls renderInlineLink() plain)
  +        WikiTextParser parser = new WikiTextParser(wikiText, true, false);
  +        // This renderer is really just ignoring everything and renders a few placeholders
  +        parser.setRenderer(
  +            new WikiTextRenderer() {
  +                public String renderInlineLink(WikiLink inlineLink) {
  +                    return "<span class=\"diffLink\">[" + inlineLink.getDescription() + "=>" + inlineLink.getUrl() + "]</span>";
  +                }
  +                public String renderExternalLink(WikiLink externalLink) { return null; }
  +                public String renderFileAttachmentLink(int attachmentNumber, WikiLink attachmentLink) { return null; }
  +                public String renderThumbnailImageInlineLink(WikiLink inlineLink) { return null; }
  +                public String renderMacro(String macroName) {
  +                    return "<span class=\"diffPlugin\">Plugin: " + macroName + "</span>";
  +                }
  +                public void setAttachmentLinks(List<WikiLink> attachmentLinks) {}
  +                public void setExternalLinks(List<WikiLink> externalLinks) {}
  +            }
  +        );
  +
  +        // Run the parser
  +        parser.parse(false);
  +        return parser.toString();
  +    }
  +
   }
  
  
  
  1.12      +8 -4      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/action/DocumentHome.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: DocumentHome.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/action/DocumentHome.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -b -r1.11 -r1.12
  --- DocumentHome.java	19 Apr 2007 18:36:12 -0000	1.11
  +++ DocumentHome.java	20 Apr 2007 09:10:12 -0000	1.12
  @@ -37,16 +37,20 @@
           super.create();
   
           // Rollback to historical revision?
  -        if (selectedHistoricalNode != null) getInstance().rollback(selectedHistoricalNode);
  +        if (selectedHistoricalNode != null) {
  +            getLog().debug("rolling back to revision: " + selectedHistoricalNode.getRevision());
  +            getInstance().rollback(selectedHistoricalNode);
  +        }
  +
  +        // Wiki text parser and others needs it
  +        Contexts.getConversationContext().set("currentDocument", getInstance());
  +        Contexts.getConversationContext().set("currentDirectory", getParentDirectory());
   
           // Make a copy
           historicalCopy = new Document(getInstance());
           minorRevision = (Boolean)((DocumentEditorPreferences)Component
                   .getInstance("docEditorPreferences")).getProperties().get("minorRevisionEnabled");
   
  -        // Wiki text parser needs it
  -        Contexts.getConversationContext().set("currentDocument", getInstance());
  -        Contexts.getConversationContext().set("currentDirectory", getParentDirectory());
       }
   
       /* -------------------------- Custom CUD ------------------------------ */
  
  
  



More information about the jboss-cvs-commits mailing list