comment.java

### File Source: comment.java

             /*
        P/P   *  Method: net.sourceforge.pebble.domain.Comment__static_init
              */
         1  /*
         2   * Copyright (c) 2003-2006, Simon Brown
         3   * All rights reserved.
         4   *
         5   * Redistribution and use in source and binary forms, with or without
         6   * modification, are permitted provided that the following conditions are met:
         7   *
         8   *   - Redistributions of source code must retain the above copyright
         9   *     notice, this list of conditions and the following disclaimer.
        10   *
        11   *   - Redistributions in binary form must reproduce the above copyright
        12   *     notice, this list of conditions and the following disclaimer in
        13   *     the documentation and/or other materials provided with the
        14   *     distribution.
        15   *
        16   *   - Neither the name of Pebble nor the names of its contributors may
        17   *     be used to endorse or promote products derived from this software
        18   *     without specific prior written permission.
        19   *
        20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
        24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        30   * POSSIBILITY OF SUCH DAMAGE.
        31   */
        32  package net.sourceforge.pebble.domain;
        33
        34  import net.sourceforge.pebble.api.event.comment.CommentEvent;
        35  import net.sourceforge.pebble.util.StringUtils;
        36
        37  import java.util.ArrayList;
        38  import java.util.Date;
        39  import java.util.List;
        40
        41  /**
        42   * Represents a blog comment.
        43   *
        44   * @author    Simon Brown
        45   */
        46  public class Comment extends Response {
        47
        48    /** the body of the comment */
        49    private String body;
        50
        51    /** the name of the author */
        52    private String author;
        53
        54    /** the author's e-mail address */
        55    private String email;
        56
        57    /** the author's website */
        58    private String website;
        59
        60    /** the parent comment, if applicable */
        61    private Comment parent;
        62
        63    /** the collection of nested comments */
        64    private List comments = new ArrayList();
        65
        66    /** a flag to indicate whether the user was authenticated when the comment was left */
        67    private boolean authenticated = false;
        68
               /*
        P/P     *  Method: void net.sourceforge.pebble.domain.Comment()
                *
                *  Postconditions:
                *    this.authenticated == 0
                *    this.spamScore == 0
                *    this.comments == &new ArrayList(Comment#1)
                *    this.events == &new ArrayList(Content#1)
                *    init'ed(this.eventsEnabled)
                *    this.propertyChangeEvents == &new ArrayList(Content#3)
                *    this.propertyChangeSupport == &new PropertyChangeSupport(Content#2)
                *    new ArrayList(Comment#1) num objects == 1
                *    new ArrayList(Content#1) num objects == 1
                *    new ArrayList(Content#3) num objects == 1
                *    ...
                */
        69    public Comment() {
        70    }
        71
        72    /**
        73     * Creates a new comment with the specified properties.
        74     *
        75     * @param title        the comment title
        76     * @param body        the comment body
        77     * @param author      the name of the author
        78     * @param website     the author's website
        79     * @param ipAddress the IP address of the author
        80     * @param date        the date that this comment was left
        81     * @param state       the state of the comment
        82     * @param blogEntry   the owning blog entry
        83     */
        84    Comment(String title, String body, String author, String email, String website, String ipAddress, Date date, State state, BlogEntry blogEntry) {
                 /*
        P/P       *  Method: void net.sourceforge.pebble.domain.Comment(String, String, String, String, String, String, Date, State, BlogEntry)
                  *
                  *  Preconditions:
                  *    blogEntry != null
                  *
                  *  Postconditions:
                  *    init'ed(this.authenticated)
                  *    this.author == One-of{&"Anonymous", author}
                  *    this.author != null
                  *    this.blogEntry == blogEntry
                  *    this.blogEntry != null
                  *    this.body == One-of{null, body}
                  *    init'ed(this.body)
                  *    this.comments == &new ArrayList(Comment#1)
                  *    init'ed(this.date)
                  *    init'ed(this.email)
                  *    ...
                  */
        85      super(title, ipAddress, date, state, blogEntry);
        86
        87      setBody(body);
        88      setAuthor(author);
        89      setEmail(email);
        90      setWebsite(website);
        91    }
        92
        93    /**
        94     * Gets the body of this comment.
        95     *
        96     * @return    the body of this comment as a String
        97     */
        98    public String getBody() {
                 /*
        P/P       *  Method: String getBody()
                  *
                  *  Preconditions:
                  *    init'ed(this.body)
                  *
                  *  Postconditions:
                  *    return_value == this.body
                  *    init'ed(return_value)
                  */
        99      return body;
       100    }
       101
       102    /**
       103     * Gets the content of this response.
       104     *
       105     * @return a String
       106     */
       107    public String getContent() {
                 /*
        P/P       *  Method: String getContent()
                  *
                  *  Preconditions:
                  *    init'ed(this.body)
                  *
                  *  Postconditions:
                  *    return_value == this.body
                  *    init'ed(return_value)
                  */
       108      return getBody();
       109    }
       110
       111    /**
       112     * Gets the body of this comment, truncated and without HTML tags.
       113     *
       114     * @return    the body of this comment as a String
       115     */
       116    public String getTruncatedBody() {
                 /*
        P/P       *  Method: String getTruncatedBody()
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.body)
                  *    (soft) init'ed(this.excerpt)
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       117      return this.getTruncatedContent();
       118    }
       119
       120    /**
       121     * Sets the title of this comment.
       122     *
       123     * @param   title    the title of this comment as a String
       124     */
       125    public void setTitle(String title) {
                 /*
        P/P       *  Method: void setTitle(String)
                  *
                  *  Preconditions:
                  *    (soft) init'ed(this.blogEntry)
                  *
                  *  Postconditions:
                  *    this.title != null
                  *
                  *  Test Vectors:
                  *    this.blogEntry: Addr_Set{null}, Inverse{null}
                  *    title: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@126: {1..232-1}, {0}
                  */
       126      if (title == null || title.length() == 0) {
       127        if (blogEntry != null) {
       128          this.title = "Re: " + blogEntry.getTitle();
       129        } else {
       130          this.title = "Comment";
       131        }
       132      } else {
       133        this.title = title;
       134      }
       135    }
       136
       137    /**
       138     * Sets the body of this comment.
       139     *
       140     * @param   body    the body of this comment as a String
       141     */
       142    public void setBody(String body) {
                 /*
        P/P       *  Method: void setBody(String)
                  *
                  *  Postconditions:
                  *    this.body == One-of{null, body}
                  *    init'ed(this.body)
                  *
                  *  Test Vectors:
                  *    body: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@143: {1..232-1}, {0}
                  */
       143      if (body == null || body.length() == 0) {
       144        this.body = null;
       145      } else {
       146        this.body = body;
       147      }
       148    }
       149
       150    /**
       151     * Gets the name of the author.
       152     *
       153     * @return    the name of the author as a String
       154     */
       155    public String getAuthor() {
                 /*
        P/P       *  Method: String getAuthor()
                  *
                  *  Preconditions:
                  *    init'ed(this.author)
                  *
                  *  Postconditions:
                  *    return_value == this.author
                  *    init'ed(return_value)
                  */
       156      return author;
       157    }
       158
       159    /**
       160     * Gets the name of the source of this response.
       161     *
       162     * @return a String
       163     */
       164    public String getSourceName() {
                 /*
        P/P       *  Method: String getSourceName()
                  *
                  *  Preconditions:
                  *    init'ed(this.author)
                  *
                  *  Postconditions:
                  *    return_value == this.author
                  *    init'ed(return_value)
                  */
       165      return getAuthor();
       166    }
       167
       168    /**
       169     * Sets the author of this blog comment. If an author isn't specified,
       170     * the author is set to be "Anonymous".
       171     *
       172     * @param author    the name of the author
       173     */
       174    public void setAuthor(String author) {
                 /*
        P/P       *  Method: void setAuthor(String)
                  *
                  *  Postconditions:
                  *    this.author == One-of{&"Anonymous", author}
                  *    this.author != null
                  *
                  *  Test Vectors:
                  *    author: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@175: {1..232-1}, {0}
                  */
       175      if (author == null || author.length() == 0) {
       176        this.author = "Anonymous";
       177      } else {
       178        this.author = author;
       179      }
       180    }
       181
       182    /**
       183     * Gets the author's e-mail address.
       184     *
       185     * @return    the author's e-mail address as a String
       186     */
       187    public String getEmail() {
                 /*
        P/P       *  Method: String getEmail()
                  *
                  *  Preconditions:
                  *    init'ed(this.email)
                  *
                  *  Postconditions:
                  *    return_value == this.email
                  *    init'ed(return_value)
                  */
       188      return email;
       189    }
       190
       191    /**
       192     * Sets the author's e-mail address.
       193     *
       194     * @param email   the e-mail address
       195     */
       196    public void setEmail(String email) {
                 /*
        P/P       *  Method: void setEmail(String)
                  *
                  *  Postconditions:
                  *    init'ed(this.email)
                  *
                  *  Test Vectors:
                  *    email: Addr_Set{null}, Inverse{null}
                  *    java.lang.String:length(...)@197: {1..232-1}, {0}
                  */
       197      if (email == null || email.length() == 0) {
       198        this.email = null;
       199      } else {
       200        this.email = StringUtils.transformHTML(email);
       201      }
       202    }
       203
       204    /**
       205     * Gets the author's website.
       206     *
       207     * @return    the author's website as a String
       208     */
       209    public String getWebsite() {
                 /*
        P/P       *  Method: String getWebsite()
                  *
                  *  Preconditions:
                  *    init'ed(this.website)
                  *
                  *  Postconditions:
                  *    return_value == this.website
                  *    init'ed(return_value)
                  */
       210      return website;
       211    }
       212
       213    /**
       214     * Gets the link to the source of this response.
       215     *
       216     * @return a String
       217     */
       218    public String getSourceLink() {
                 /*
        P/P       *  Method: String getSourceLink()
                  *
                  *  Preconditions:
                  *    init'ed(this.website)
                  *
                  *  Postconditions:
                  *    return_value == this.website
                  *    init'ed(return_value)
                  */
       219      return getWebsite();
       220    }
       221
       222    /**
       223     * Sets the author's website.
       224     *
       225     * @param website   the website url
       226     */
       227    public void setWebsite(String website) {
                 /*
        P/P       *  Method: void setWebsite(String)
                  *
                  *  Postconditions:
                  *    init'ed(this.website)
                  *
                  *  Test Vectors:
                  *    java.lang.String:length(...)@229: {1..232-1}, {0}
                  *    java.lang.String:startsWith(...)@232: {1}, {0}
                  *    java.lang.String:startsWith(...)@233: {1}, {0}
                  *    java.lang.String:startsWith(...)@234: {1}, {0}
                  *    java.lang.String:startsWith(...)@235: {1}, {0}
                  *    net.sourceforge.pebble.util.StringUtils:filterHTML(...)@228: Addr_Set{null}, Inverse{null}
                  */
       228      website = StringUtils.filterHTML(website);
       229      if (website == null || website.length() == 0) {
       230        this.website = null;
       231      } else if (
       232          !website.startsWith("http://") &&
       233          !website.startsWith("https://") &&
       234          !website.startsWith("ftp://") &&
       235          !website.startsWith("mailto:")) {
       236        this.website = "http://" + website;
       237      } else {
       238        this.website = website;
       239      }
       240    }
       241
       242    /**
       243     * Gets the permalink for this comment.
       244     *
       245     * @return  a URL as a String
       246     */
       247    public String getPermalink() {
                 /*
        P/P       *  Method: String getPermalink()
                  *
                  *  Preconditions:
                  *    init'ed(this.blogEntry)
                  *    (soft) init'ed(this.blogEntry.permalink)
                  *    (soft) this.blogEntry.blog != null
                  *    (soft) this.date != null
                  *
                  *  Postconditions:
                  *    return_value != null
                  *    init'ed(this.blogEntry.permalink)
                  *
                  *  Preconditions:
                  *    (soft) net/sourceforge/pebble/domain/BlogManager.instance != null
                  *    (soft) init'ed(net/sourceforge/pebble/domain/BlogManager.instance.multiBlog)
                  *    (soft) init'ed(this.blogEntry.blog.id)
                  *    (soft) this.blogEntry.blog.permalinkProvider != null
                  *
                  *  Test Vectors:
                  *    this.blogEntry: Addr_Set{null}, Inverse{null}
                  */
       248      if (blogEntry != null) {
       249        return blogEntry.getLocalPermalink() + "#comment" + getId();
       250      } else {
       251        return "";
       252      }
       253    }
       254
       255    /**
       256     * Gets the owning comment, if this comment is nested.
       257     *
       258     * @return    a Comment instance, or null if this comment isn't nested
       259     */
       260    public Comment getParent() {
                 /*
        P/P       *  Method: Comment getParent()
                  *
                  *  Preconditions:
                  *    init'ed(this.parent)
                  *
                  *  Postconditions:
                  *    return_value == this.parent
                  *    init'ed(return_value)
                  */
       261      return this.parent;
       262    }
       263
       264    /**
       265     * Sets the owning comment.
       266     *
       267     * @param parent    the owning Comment instance
       268     */
       269    public void setParent(Comment parent) {
                 /*
        P/P       *  Method: void setParent(Comment)
                  *
                  *  Postconditions:
                  *    this.parent == parent
                  *    init'ed(this.parent)
                  */
       270      this.parent = parent;
       271    }
       272
       273    /**
       274     * Gets the number of parents that this comment has.
       275     *
       276     * @return  the number of parents as an int
       277     */
       278    public int getNumberOfParents() {
                 /*
        P/P       *  Method: int getNumberOfParents()
                  *
                  *  Preconditions:
                  *    init'ed(this.parent)
                  *    (soft) init'ed(this...parent)
                  *
                  *  Postconditions:
                  *    return_value >= 0
                  */
       279      int count = 0;
       280      Comment c = getParent();
       281      while (c != null) {
       282        count++;
       283        c = c.getParent();
       284      }
       285
       286      return count;
       287    }
       288
       289    /**
       290     * Adds a child comment.
       291     *
       292     * @param comment   the Comment to add
       293     */
       294    void addComment(Comment comment) {
                 /*
        P/P       *  Method: void addComment(Comment)
                  *
                  *  Preconditions:
                  *    (soft) this.comments != null
                  *
                  *  Postconditions:
                  *    comment.parent == One-of{old comment.parent, this}
                  *
                  *  Test Vectors:
                  *    comment: Addr_Set{null}, Inverse{null}
                  *    java.util.List:contains(...)@295: {1}, {0}
                  */
       295      if (comment != null && !comments.contains(comment)) {
       296        comments.add(comment);
       297        comment.setParent(this);
       298      }
       299    }
       300
       301    /**
       302     * Removes a child comment.
       303     *
       304     * @param comment   the Comment to be removed
       305     */
       306    void removeComment(Comment comment) {
                 /*
        P/P       *  Method: void removeComment(Comment)
                  *
                  *  Preconditions:
                  *    (soft) comment.blogEntry != null
                  *    (soft) comment.comments != null
                  *    (soft) init'ed(comment.eventsEnabled)
                  *    (soft) this.blogEntry != null
                  *    (soft) this.comments != null
                  *    (soft) init'ed(this.eventsEnabled)
                  *
                  *  Presumptions:
                  *    child.blogEntry@309 != null
                  *    child.comments@309 != null
                  *
                  *  Postconditions:
                  *    comment.parent == One-of{old comment.parent, null}
                  *
                  *  Test Vectors:
                  *    comment: Addr_Set{null}, Inverse{null}
                  *    java.util.Iterator:hasNext(...)@309: {1}, {0}
                  *    java.util.List:contains(...)@307: {0}, {1}
                  */
       307      if (comment != null && comments.contains(comment)) {
       308        // remove all children
       309        for (Comment child : getComments()) {
       310          comment.removeComment(child);
       311        }
       312
       313        // then remove the comment itself
       314        comments.remove(comment);
       315        comment.setParent(null);
       316
       317        if (areEventsEnabled()) {
       318          getBlogEntry().addEvent(new CommentEvent(comment, CommentEvent.COMMENT_REMOVED));
       319        }
       320      }
       321    }
       322
       323    /**
       324     * Gets a list of comments, in the order that they were left.
       325     *
       326     * @return  a List of Comment instances
       327     */
       328    public List<Comment> getComments() {
                 /*
        P/P       *  Method: List getComments()
                  *
                  *  Preconditions:
                  *    init'ed(this.comments)
                  *
                  *  Postconditions:
                  *    return_value == &new ArrayList(getComments#1)
                  *    new ArrayList(getComments#1) num objects == 1
                  */
       329      return new ArrayList<Comment>(comments);
       330    }
       331
       332    /**
       333     * Creates and returns a copy of this object.
       334     *
       335     * @return a clone of this instance.
       336     * @see Cloneable
       337     */
       338    public Object clone() {
                 /*
        P/P       *  Method: Object clone()
                  *
                  *  Preconditions:
                  *    init'ed(this.authenticated)
                  *    init'ed(this.author)
                  *    this.blogEntry != null
                  *    init'ed(this.body)
                  *    init'ed(this.date)
                  *    init'ed(this.email)
                  *    init'ed(this.ipAddress)
                  *    init'ed(this.parent)
                  *    init'ed(this.state)
                  *    init'ed(this.title)
                  *    ...
                  *
                  *  Postconditions:
                  *    return_value == &new Comment(clone#1)
                  *    new ArrayList(Comment#1) num objects == 1
                  *    new ArrayList(Content#1) num objects == 1
                  *    new ArrayList(Content#3) num objects == 1
                  *    new Comment(clone#1) num objects == 1
                  *    new PropertyChangeSupport(Content#2) num objects == 1
                  *    return_value.authenticated == this.authenticated
                  *    init'ed(return_value.authenticated)
                  *    return_value.author == One-of{&"Anonymous", this.author}
                  *    return_value.author != null
                  *    ...
                  */
       339      Comment comment = new Comment(title, body, author, email, website, ipAddress, date, getState(), blogEntry);
       340      comment.setParent(parent);
       341      comment.setAuthenticated(authenticated);
       342      return comment;
       343    }
       344
       345    public boolean equals(Object o) {
                 /*
        P/P       *  Method: bool equals(Object)
                  *
                  *  Preconditions:
                  *    (soft) o.blogEntry != null
                  *    (soft) o.date != null
                  *    (soft) this.blogEntry != null
                  *    (soft) this.date != null
                  *
                  *  Presumptions:
                  *    net.sourceforge.pebble.domain.BlogEntry:getId(...)@355 != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  *
                  *  Test Vectors:
                  *    this == o: {0}, {1}
                  *    java.lang.String:equals(...)@355: {0}, {1}
                  */
       346      if (this == o) {
       347        return true;
       348      }
       349
       350      if (!(o instanceof Comment)) {
       351        return false;
       352      }
       353
       354      Comment comment = (Comment)o;
       355      return (getId() == comment.getId() && blogEntry.getId().equals(comment.getBlogEntry().getId()));
       356    }
       357
       358    public int hashCode() {
                 /*
        P/P       *  Method: int hashCode()
                  *
                  *  Preconditions:
                  *    this.date != null
                  *
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
       359      return ("" + getId()).hashCode();
       360    }
       361
       362    /**
       363     * Sets the state of this comment.
       364     */
       365    void setState(State s) {
                 /*
        P/P       *  Method: void setState(State)
                  *
                  *  Preconditions:
                  *    init'ed(this.state)
                  *    init'ed(this.eventsEnabled)
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#1).name != null
                  *    (soft) net.sourceforge.pebble.domain.State__static_init.new State(State__static_init#2).name != null
                  *    (soft) s != null
                  *    (soft) init'ed(s.name)
                  *    (soft) this.blogEntry != null
                  *
                  *  Postconditions:
                  *    this.state == s
                  *    (soft) this.state != null
                  *
                  *  Test Vectors:
                  *    this.eventsEnabled: {0}, {1}
                  */
       366      State previousState = getState();
       367      super.setState(s);
       368
       369      if (areEventsEnabled()) {
       370        if (isApproved() && previousState != State.APPROVED) {
       371          getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_APPROVED));
       372        } else if (isRejected() && previousState != State.REJECTED) {
       373          getBlogEntry().addEvent(new CommentEvent(this, CommentEvent.COMMENT_REJECTED));
       374        }
       375      }
       376    }
       377
       378    public boolean isAuthenticated() {
                 /*
        P/P       *  Method: bool isAuthenticated()
                  *
                  *  Preconditions:
                  *    init'ed(this.authenticated)
                  *
                  *  Postconditions:
                  *    return_value == this.authenticated
                  *    init'ed(return_value)
                  */
       379      return authenticated;
       380    }
       381
       382    public void setAuthenticated(boolean authenticated) {
                 /*
        P/P       *  Method: void setAuthenticated(bool)
                  *
                  *  Postconditions:
                  *    this.authenticated == authenticated
                  *    init'ed(this.authenticated)
                  */
       383      this.authenticated = authenticated;
       384    }
       385
       386  }

SofCheck Inspector Build Version : 2.22510

comment.java | 2010-Jun-25 19:40:32
comment.class | 2010-Jul-19 20:23:40