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

Christian Bauer christian at hibernate.org
Thu Jan 10 19:09:16 EST 2008


  User: cbauer  
  Date: 08/01/10 19:09:16

  Modified:    examples/wiki/src/main/org/jboss/seam/wiki/core/model     
                        WikiComment.java WikiDocumentDefaults.java
                        WikiDirectory.java WikiUpload.java
                        WikiDocument.java
  Log:
  Improved macro handling in documents and URL rendering
  
  Revision  Changes    Path
  1.5       +2 -2      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiComment.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiComment.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiComment.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -b -r1.4 -r1.5
  --- WikiComment.java	9 Jan 2008 15:59:23 -0000	1.4
  +++ WikiComment.java	11 Jan 2008 00:09:16 -0000	1.5
  @@ -129,11 +129,11 @@
   
   
       public String getPermURL(String suffix) {
  -        return "/" + getParentDocument().getId() + suffix + "#comment" + getId();
  +        return getParentDocument().getId() + suffix + "#comment" + getId();
       }
   
       public String getWikiURL() {
  -        return "/" + getArea().getWikiname() + "/" + getParentDocument().getWikiname() + "#comment" + getId();
  +        return getArea().getWikiname() + "/" + getParentDocument().getWikiname() + "#comment" + getId();
       }
   
       // TODO: Everything can have comments, this has the wrong name and it's crude
  
  
  
  1.2       +43 -14    jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDocumentDefaults.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiDocumentDefaults.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDocumentDefaults.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -b -r1.1 -r1.2
  --- WikiDocumentDefaults.java	19 Dec 2007 04:29:20 -0000	1.1
  +++ WikiDocumentDefaults.java	11 Jan 2008 00:09:16 -0000	1.2
  @@ -1,31 +1,60 @@
   package org.jboss.seam.wiki.core.model;
   
  +import org.jboss.seam.wiki.core.engine.WikiMacro;
  +
  +import java.util.List;
  +
   public class WikiDocumentDefaults {
   
  -    public String[] getDefaultHeaderMacros() {
  -        return new String[0];
  +    public String getName() {
  +        return "New Document";
       }
  -    public String[] getDefaultContentMacros() {
  -        return new String[0];
  +
  +    public String getContentText() {
  +        return "Edit this text...";
       }
  -    public String[] getDefaultFooterMacros() {
  +
  +    /**
  +     * @return a list of <tt>WikiMacro</tt> instances or null if <tt>getHeaderMacrosAsString()</tt> should be called.
  +     */
  +    public List<WikiMacro> getContentMacros() {
  +        return null;
  +    }
  +
  +    public String[] getContentMacrosAsString() {
           return new String[0];
       }
   
  -    public String getDefaultHeader() {
  -        return "";
  +    public String getHeaderText() {
  +        return null;
       }
  -    public String getDefaultContent() {
  -        return "Edit this text...";
  +
  +    /**
  +     * @return a list of <tt>WikiMacro</tt> instances or null if <tt>getHeaderMacrosAsString()</tt> should be called.
  +     */
  +    public List<WikiMacro> getHeaderMacros() {
  +        return null;
       }
  -    public String getDefaultFooter() {
  -        return "";
  +
  +    public String[] getHeaderMacrosAsString() {
  +        return new String[0];
       }
   
  -    public String getDefaultName() {
  -        return "New Document";
  +    public String getFooterText() {
  +        return null;
  +    }
  +
  +    /**
  +     * @return a list of <tt>WikiMacro</tt> instances or null if <tt>getHeaderMacrosAsString()</tt> should be called.
  +     */
  +    public List<WikiMacro> getFooterMacros() {
  +        return null;
  +    }
  +
  +    public String[] getFooterMacrosAsString() {
  +        return new String[0];
       }
   
  -    public void setDefaults(WikiDocument document) {}
  +    public void setOptions(WikiDocument document) {}
   
   }
  
  
  
  1.6       +4 -4      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDirectory.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiDirectory.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDirectory.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- WikiDirectory.java	9 Jan 2008 15:59:23 -0000	1.5
  +++ WikiDirectory.java	11 Jan 2008 00:09:16 -0000	1.6
  @@ -94,15 +94,15 @@
       }
   
       public String getPermURL(String suffix) {
  -        return "/" + getId() + suffix;
  +        return getId() + suffix;
       }
   
       public String getWikiURL() {
  -        if (getArea() == null) return "/"; // Wiki ROOT
  +        if (getArea() == null) return ""; // Wiki ROOT
           if (getArea().getWikiname().equals(getWikiname())) {
  -            return "/" + getArea().getWikiname();
  +            return getArea().getWikiname();
           } else {
  -            return "/" + getArea().getWikiname() + "/" + getWikiname();
  +            return getArea().getWikiname() + "/" + getWikiname();
           }
       }
   
  
  
  
  1.4       +2 -2      jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiUpload.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiUpload.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- WikiUpload.java	9 Jan 2008 15:59:23 -0000	1.3
  +++ WikiUpload.java	11 Jan 2008 00:09:16 -0000	1.4
  @@ -107,11 +107,11 @@
       }
   
       public String getPermURL(String suffix) {
  -        return "/servlets/files/download.seam?fileId=" + getId();
  +        return "servlets/files/download.seam?fileId=" + getId();
       }
   
       public String getWikiURL() {
  -        return "/servlets/files/download.seam?fileId=" + getId();
  +        return "servlets/files/download.seam?fileId=" + getId();
       }
   
       public String toString() {
  
  
  
  1.6       +152 -62   jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDocument.java
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: WikiDocument.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-seam/examples/wiki/src/main/org/jboss/seam/wiki/core/model/WikiDocument.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -b -r1.5 -r1.6
  --- WikiDocument.java	9 Jan 2008 15:59:23 -0000	1.5
  +++ WikiDocument.java	11 Jan 2008 00:09:16 -0000	1.6
  @@ -6,8 +6,7 @@
   
   import javax.persistence.*;
   import java.io.Serializable;
  -import java.util.Set;
  -import java.util.LinkedHashSet;
  +import java.util.*;
   
   @Entity
   @Table(name = "WIKI_DOCUMENT")
  @@ -36,8 +35,6 @@
       private String header;
       @Column(name = "HEADER_MACROS", nullable = true, length = 4000)
       private String headerMacrosString;
  -    @Transient
  -    private Set<WikiMacro> headerMacros = new LinkedHashSet<WikiMacro>();
   
       @Column(name = "CONTENT", nullable = false)
       @Length(min = 0, max = 32768)
  @@ -47,16 +44,12 @@
       private String content;
       @Column(name = "CONTENT_MACROS", nullable = true, length = 4000)
       private String contentMacrosString;
  -    @Transient
  -    private Set<WikiMacro> contentMacros = new LinkedHashSet<WikiMacro>();
   
       @Column(name = "FOOTER", nullable = true)
       @Length(min = 0, max = 4000)
       private String footer;
       @Column(name = "FOOTER_MACROS", nullable = true, length = 4000)
       private String footerMacrosString;
  -    @Transient
  -    private Set<WikiMacro> footerMacros = new LinkedHashSet<WikiMacro>();
   
       public WikiDocument() {
           super();
  @@ -65,7 +58,7 @@
       }
   
       public WikiDocument(WikiDocumentDefaults defaults) {
  -        super(defaults.getDefaultName());
  +        super(defaults.getName());
           setDefaults(defaults);
       }
   
  @@ -106,15 +99,6 @@
       public String getFooterMacrosString() { return footerMacrosString; }
       public void setFooterMacrosString(String footerMacrosString) { this.footerMacrosString = footerMacrosString; }
   
  -    public Set<WikiMacro> getHeaderMacros() { return headerMacros; }
  -    public void setHeaderMacros(Set<WikiMacro> headerMacros) { this.headerMacros = headerMacros; }
  -
  -    public Set<WikiMacro> getContentMacros() { return contentMacros; }
  -    public void setContentMacros(Set<WikiMacro> contentMacros) { this.contentMacros = contentMacros; }
  -
  -    public Set<WikiMacro> getFooterMacros() { return footerMacros; }
  -    public void setFooterMacros(Set<WikiMacro> footerMacros) { this.footerMacros = footerMacros; }
  -
       public void flatCopy(WikiDocument original, boolean copyLazyProperties) {
           super.flatCopy(original, copyLazyProperties);
           this.nameAsTitle = original.nameAsTitle;
  @@ -142,19 +126,6 @@
           this.content = revision.content;
       }
   
  -    public boolean macroPresent(String macroName) {
  -        for (WikiMacro headerMacro : headerMacros) {
  -            if (headerMacro.getName().equals(macroName)) return true;
  -        }
  -        for (WikiMacro contentMacro : contentMacros) {
  -            if (contentMacro.getName().equals(macroName)) return true;
  -        }
  -        for (WikiMacro footerMacro : footerMacros) {
  -            if (footerMacro.getName().equals(macroName)) return true;
  -        }
  -        return false;
  -    }
  -
       public String getFeedDescription() {
           return getContent();
       }
  @@ -164,67 +135,186 @@
       }
   
       public String getPermURL(String suffix) {
  -        return "/" + getId() + suffix;
  +        return getId() + suffix;
       }
   
       public String getWikiURL() {
  -        return "/" + getArea().getWikiname() + "/" + getWikiname();
  +        return getArea().getWikiname() + "/" + getWikiname();
       }
   
       public void setDefaults(WikiDocumentDefaults defaults) {
  -        setName(defaults.getDefaultName());
  +        setName(defaults.getName());
  +        content = defaults.getContentText() != null ? defaults.getContentText() : "";
  +        header = defaults.getHeaderText() != null ? defaults.getHeaderText() : null;
  +        footer = defaults.getFooterText() != null ? defaults.getFooterText() : null;
  +        setMacroFieldsFromDefaults(defaults);
  +        defaults.setOptions(this);
  +    }
  +
  +    /*
  +        Macro handling routines, based on the following concept:
  +
  +        - Persistent HEADER/CONTENT/FOOTER fields: This is the editable text, show to the user.
  +
  +        - Persistent HEADER_MACROS/CONTENT_MACROS/FOOTER_MACROS fields: These are strings that represent
  +          a space-separated list of all macros (names only) the user has entered. We need this separate
  +          in the database for aggregation queries, e.g. find me all documents that have "forumPosting"
  +          macro in the HEADER_MACROS field.
  +
  +        - Transient headerMacros/contentMacros/footerMacros fields: These are type-safe collections
  +          that represent the macros of this document. They are empty after you load a document from
  +          the database and need to be generated. This generation is outside of the model class, we
  +          expect that the client who loads the document will parse the persistent fields and call
  +          the setters with filled collections. Only then will macroPresent(name) return a correct
  +          result.
  +     */
  +
  +    @Transient
  +    private Collection<WikiMacro> headerMacros = new LinkedHashSet<WikiMacro>();
  +    public Collection<WikiMacro> getHeaderMacros() { return headerMacros; }
  +    public void setHeaderMacros(Collection<WikiMacro> headerMacros) {
  +        this.headerMacros = headerMacros;
  +        setHeaderMacrosString(getMacrosAsString(headerMacros));
  +        setHeader(getMacrosAsWikiText(headerMacros) + getWikiTextWithoutMacros(getHeader()) ); // Text after macros
  +    }
  +
  +    @Transient
  +    private Collection<WikiMacro> contentMacros = new LinkedHashSet<WikiMacro>();
  +    public Collection<WikiMacro> getContentMacros() { return contentMacros; }
  +    public void setContentMacros(Collection<WikiMacro> contentMacros) {
  +        this.contentMacros = contentMacros;
  +        setContentMacrosString(getMacrosAsString(contentMacros));
  +    }
  +
  +    @Transient
  +    private Collection<WikiMacro> footerMacros = new LinkedHashSet<WikiMacro>();
  +    public Collection<WikiMacro> getFooterMacros() { return footerMacros; }
  +    public void setFooterMacros(Collection<WikiMacro> footerMacros) {
  +        this.footerMacros = footerMacros;
  +        setFooterMacrosString(getMacrosAsString(footerMacros));
  +        setFooter(getWikiTextWithoutMacros(getFooter()) + getMacrosAsWikiText(footerMacros)); // Text before macros
  +    }
   
  -        setHeaderMacrosString( appendMacrosAsString(defaults.getDefaultHeaderMacros()) );
  -        setHeader( appendMacrosAsWikiTextString(defaults.getDefaultHeaderMacros()) + defaults.getDefaultHeader() );
  +    public void addHeaderMacro(WikiMacro... macro) {
  +        headerMacros.addAll(Arrays.asList(macro));
  +        setHeaderMacros(headerMacros);
  +    }
   
  -        setContentMacrosString( appendMacrosAsString(defaults.getDefaultContentMacros()) );
  -        setContent( appendMacrosAsWikiTextString(defaults.getDefaultContentMacros()) + defaults.getDefaultContent());
  +    public void addFooterMacro(WikiMacro... macro) {
  +        footerMacros.addAll(Arrays.asList(macro));
  +        setFooterMacros(footerMacros);
  +    }
   
  -        setFooterMacrosString( appendMacrosAsString(defaults.getDefaultFooterMacros()) );
  -        setFooter( appendMacrosAsWikiTextString(defaults.getDefaultFooterMacros()) + defaults.getDefaultFooter());
  +    public void removeHeaderMacro(WikiMacro... macro) {
  +        headerMacros.removeAll(Arrays.asList(macro));
  +        setHeaderMacros(headerMacros);
  +    }
   
  -        defaults.setDefaults(this);
  +    public void removeFooterMacro(WikiMacro... macro) {
  +        footerMacros.removeAll(Arrays.asList(macro));
  +        setFooterMacros(footerMacros);
       }
   
  -    public void addHeaderMacro(String... macro) {
  -        setHeaderMacrosString( getHeaderMacrosString() + appendMacrosAsString(macro));
  -        setHeader( getHeader() + appendMacrosAsWikiTextString(macro));
  +    public void removeHeaderMacros(String macroName) {
  +        removeMacrosFromCollection(headerMacros, macroName);
  +        setHeaderMacros(headerMacros);
       }
   
  -    public void addFooterMacro(String... macro) {
  -        setFooterMacrosString( getFooterMacrosString() + appendMacrosAsString(macro));
  -        setFooter( getFooter()+ appendMacrosAsWikiTextString(macro));
  +    public void removeFooterMacros(String macroName) {
  +        removeMacrosFromCollection(footerMacros, macroName);
  +        setFooterMacros(footerMacros);
       }
   
  -    // TODO: The replacement methods should tokenize the strings, not replaceAll()
  -    public void replaceHeaderMacro(String macro, String replacement) {
  -        setHeaderMacrosString(getHeaderMacrosString().replaceAll(macro, replacement));
  -        setHeader(getHeader().replaceAll(macro, replacement));
  +    public boolean macroPresent(String macroName) {
  +        for (WikiMacro headerMacro : headerMacros) {
  +            if (headerMacro.getName().equals(macroName)) return true;
  +        }
  +        for (WikiMacro contentMacro : contentMacros) {
  +            if (contentMacro.getName().equals(macroName)) return true;
  +        }
  +        for (WikiMacro footerMacro : footerMacros) {
  +            if (footerMacro.getName().equals(macroName)) return true;
  +        }
  +        return false;
       }
   
  -    public void replaceFooterMacro(String macro, String replacement) {
  -        setFooterMacrosString(getFooterMacrosString().replaceAll(macro, replacement));
  -        setFooter(getFooter().replaceAll(macro, replacement));
  +    private void removeMacrosFromCollection(Collection<WikiMacro> macros, String macroName) {
  +        Iterator<WikiMacro> it = macros.iterator();
  +        while (it.hasNext()) {
  +            WikiMacro wikiMacro = it.next();
  +            if (wikiMacro.getName().equals(macroName)) it.remove();
  +        }
       }
   
  -    private String appendMacrosAsString(String[] macros) {
  -        if (macros.length == 0) return "";
  +    private String getWikiTextWithoutMacros(String wikiText) {
  +        if (wikiText == null) return "";
  +        StringBuilder textWithoutMacro = new StringBuilder();
  +        String[] textLines = wikiText.split("\n");
  +        for (int i = 0; i < textLines.length; i++) {
  +            if (!textLines[i].startsWith("[<=")) {
  +                textWithoutMacro.append(textLines[i]);
  +                if (i < textLines.length-1) textWithoutMacro.append("\n");
  +            }
  +        }
  +        return textWithoutMacro.toString();
  +    }
  +
  +    private String getMacrosAsString(Collection<WikiMacro> macros) {
  +        if (macros.size() == 0) return "";
           StringBuilder macrosString = new StringBuilder();
  -        for (String s : macros) {
  -            macrosString.append(s).append(" ");
  +        for (WikiMacro m : macros) {
  +            macrosString.append(m.getName()).append(" ");
           }
           return macrosString.substring(0, macrosString.length() - 1);
       }
   
  -    private String appendMacrosAsWikiTextString(String[] macros) {
  -        if (macros.length == 0) return "";
  +    private String getMacrosAsWikiText(Collection<WikiMacro> macros) {
  +        if (macros.size() == 0) return "";
           StringBuilder macrosString = new StringBuilder();
  -        for (String s : macros) {
  -            macrosString.append("[<=").append(s).append("]\n");
  +        for (WikiMacro m : macros) {
  +            macrosString.append("[<=").append(m.getName());
  +            for (Map.Entry<String, String> param : m.getParams().entrySet()) {
  +                macrosString.append("[").append(param.getKey()).append("=").append(param.getValue()).append("]");
  +            }
  +            macrosString.append("]\n");
           }
           return macrosString.toString();
       }
   
  +    private void setMacroFieldsFromDefaults(WikiDocumentDefaults defaults) {
  +        if (defaults.getContentMacros() != null) {
  +            setContentMacros(defaults.getContentMacros());
  +            setContentMacrosString(getMacrosAsString(defaults.getContentMacros()));
  +            content = getMacrosAsWikiText(defaults.getContentMacros()) + "\n" + content;
  +        } else {
  +            Collection<WikiMacro> macros = new ArrayList<WikiMacro>();
  +            int i = 0;
  +            for (String m : defaults.getContentMacrosAsString()) {
  +                macros.add(new WikiMacro(i++, m));
  +            }
  +            setContentMacros(macros);
  +            setContentMacrosString(getMacrosAsString(macros));
  +            content = getMacrosAsWikiText(macros) + "\n" + content;
  +        }
  +
  +        if (defaults.getHeaderMacros() != null) {
  +            setHeaderMacros(defaults.getHeaderMacros());
  +        } else {
  +            int i = 0;
  +            for (String m : defaults.getHeaderMacrosAsString()) {
  +                addHeaderMacro(new WikiMacro(i++, m));
  +            }
  +        }
  +
  +        if (defaults.getFooterMacros() != null) {
  +            setFooterMacros(defaults.getFooterMacros());
  +        } else {
  +            int i = 0;
  +            for (String m : defaults.getFooterMacrosAsString()) {
  +                addFooterMacro(new WikiMacro(i++, m));
  +            }
  +        }
  +    }
   
       public String toString() {
           return "Document (" + getId() + "): " + getName();
  
  
  



More information about the jboss-cvs-commits mailing list