File Source: CommentValidationManager.java
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.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.ResourceBundle;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.roller.weblogger.config.WebloggerConfig;
28 import org.apache.roller.weblogger.pojos.WeblogEntryComment;
29 import org.apache.roller.weblogger.ui.rendering.servlets.CommentServlet;
30 import org.apache.roller.weblogger.util.RollerMessages;
31 import org.apache.roller.weblogger.util.Utilities;
32
33 /**
34 * Responsible for loading validators and using them to validate comments.
35 */
36 public class CommentValidationManager {
/*
P/P * Method: org.apache.roller.weblogger.ui.rendering.plugins.comments.CommentValidationManager__static_init
*
* Postconditions:
* init'ed(log)
*/
37 private static Log log = LogFactory.getLog(CommentValidationManager.class);
38 private ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
39 private List validators = new ArrayList();
40
/*
P/P * Method: void org.apache.roller.weblogger.ui.rendering.plugins.comments.CommentValidationManager()
*
* Preconditions:
* log != null
*
* Presumptions:
* java.lang.Class:forName(...)@49 != null
*
* Postconditions:
* init'ed(this.bundle)
* this.validators == &new ArrayList(CommentValidationManager#1)
* new ArrayList(CommentValidationManager#1) num objects == 1
*/
41 public CommentValidationManager() {
42
43 // instantiate the validators that are configured
44 try {
45 String vals = WebloggerConfig.getProperty("comment.validator.classnames");
46 String[] valsarray = Utilities.stringToStringArray(vals, ",");
47 for(int i=0; i < valsarray.length; i++) {
48 try {
49 Class valClass = Class.forName(valsarray[i]);
50 CommentValidator val = (CommentValidator)valClass.newInstance();
51 validators.add(val);
52 log.info("Configured CommentValidator: " + val.getName() + " / " + valClass.getName());
53 } catch (ClassNotFoundException cnfe) {
54 log.warn("Error finding comment validator: " + valsarray[i]);
55 } catch (InstantiationException ie) {
56 log.warn("Error insantiating comment validator: " + valsarray[i]);
57 } catch (IllegalAccessException iae) {
58 log.warn("Error accessing comment validator: " + valsarray[i]);
59 }
60 }
61
62 } catch (Exception e) {
63 log.error("Error instantiating comment validators");
64 }
65 log.info("Configured " + validators.size() + " CommentValidators");
66 }
67
68 /**
69 * Add validator to those managed by this manager (testing purposes).
70 */
71 public void addCommentValidator(CommentValidator val) {
/*
P/P * Method: void addCommentValidator(CommentValidator)
*
* Preconditions:
* this.validators != null
*/
72 validators.add(val);
73 }
74
75 /**
76 * Return total number of validators (for teasting purposes).
77 */
78 public int getValidatorCount() {
/*
P/P * Method: int getValidatorCount()
*
* Preconditions:
* this.validators != null
*
* Postconditions:
* init'ed(return_value)
*/
79 return validators.size();
80 }
81
82 /**
83 * @param comment Comment to be validated
84 * @param messages Messages object to which errors will be added
85 * @return Number indicating confidence that comment is valid (100 meaning 100%)
86 */
87 public int validateComment(WeblogEntryComment comment, RollerMessages messages) {
/*
P/P * Method: int validateComment(WeblogEntryComment, RollerMessages)
*
* Preconditions:
* this.validators != null
* (soft) log != null
*
* Presumptions:
* java.util.Iterator:next(...)@91 != null
* java.util.List:size(...)@95 != 0
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@90: {0}, {1}
* java.util.List:size(...)@89: {-231..0}, {1..232-1}
*/
88 int total = 0;
89 if (validators.size() > 0) {
90 for (Iterator it = validators.iterator(); it.hasNext();) {
91 CommentValidator val = (CommentValidator) it.next();
92 log.debug("Invoking comment validator "+val.getName());
93 total += val.validate(comment, messages);
94 }
95 total = total / validators.size();
96 } else {
97 // When no validators: consider all comments valid
98 total = 100;
99 }
100 return total;
101 }
102
103 }
SofCheck Inspector Build Version : 2.18479
| CommentValidationManager.java |
2009-Jan-02 14:24:58 |
| CommentValidationManager.class |
2009-Sep-04 03:12:45 |