File Source: ExcessLinksCommentValidator.java

         /* 
    P/P   *  Method: org.apache.roller.weblogger.ui.rendering.plugins.comments.ExcessLinksCommentValidator__static_init
          */
     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   *  contributor license agreements.  The ASF licenses this file to You
     4   * under the Apache License, Version 2.0 (the "License"); you may not
     5   * use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.  For additional information regarding
    15   * copyright in this work, please see the NOTICE file in the top level
    16   * directory of this distribution.
    17   */
    18  
    19  package org.apache.roller.weblogger.ui.rendering.plugins.comments;
    20  
    21  import java.util.ResourceBundle;
    22  import java.util.regex.Matcher;
    23  import java.util.regex.Pattern;
    24  import org.apache.roller.weblogger.config.WebloggerConfig;
    25  import org.apache.roller.weblogger.pojos.WeblogEntryComment;
    26  import org.apache.roller.weblogger.util.RollerMessages;
    27  
    28  /**
    29   * Validates comment only if it has fewer links than comment.validator.excessSize.threshold
    30   */
    31  public class ExcessLinksCommentValidator implements CommentValidator {
    32      private ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");  
    33      private Pattern linkPattern = Pattern.compile("<a\\s*href\\s*=");    
    34      private int threshold;
    35          
             /* 
    P/P       *  Method: void org.apache.roller.weblogger.ui.rendering.plugins.comments.ExcessLinksCommentValidator()
              * 
              *  Postconditions:
              *    init'ed(this.bundle)
              *    init'ed(this.linkPattern)
              *    init'ed(this.threshold)
              */
    36      public ExcessLinksCommentValidator() {
    37          threshold = WebloggerConfig.getIntProperty("comment.validator.excessLinks.threshold");
    38      }
    39          
    40      public String getName() {
                 /* 
    P/P           *  Method: String getName()
                  * 
                  *  Preconditions:
                  *    this.bundle != null
                  * 
                  *  Postconditions:
                  *    init'ed(return_value)
                  */
    41          return bundle.getString("comment.validator.excessLinksName");
    42      }
    43  
    44      public int validate(WeblogEntryComment comment, RollerMessages messages) {
                 /* 
    P/P           *  Method: int validate(WeblogEntryComment, RollerMessages)
                  * 
                  *  Preconditions:
                  *    comment != null
                  *    this.linkPattern != null
                  *    (soft) messages != null
                  *    (soft) messages.mErrors != null
                  *    (soft) this.threshold <= 232-3
                  * 
                  *  Presumptions:
                  *    java.util.regex.Pattern:matcher(...)@45 != null
                  * 
                  *  Postconditions:
                  *    return_value in {0, 100}
                  * 
                  *  Test Vectors:
                  *    java.util.regex.Matcher:find(...)@47: {0}, {1}
                  */
    45          Matcher m = linkPattern.matcher(comment.getContent());
    46          int count = 0;
    47          while (m.find()) {
    48              if (count++ > threshold) {
    49                  messages.addError("comment.validator.excessLinksMessage", Integer.toString(threshold));
    50                  return 0;
    51              }
    52          }
    53          return 100;
    54      }
    55      
    56  }








SofCheck Inspector Build Version : 2.18479
ExcessLinksCommentValidator.java 2009-Jan-02 14:24:48
ExcessLinksCommentValidator.class 2009-Sep-04 03:12:45