1 25 package org.archive.crawler.settings; 26 27 import java.io.Serializable ; 28 import java.text.MessageFormat ; 29 import java.util.ArrayList ; 30 import java.util.logging.Level ; 31 32 33 55 public abstract class Constraint 56 implements Comparable <Constraint>, Serializable { 57 private final Level severity; 58 private final String msg; 59 60 65 public Constraint(Level level, String msg) { 66 if (level != Level.SEVERE && level != Level.WARNING 67 && level != Level.INFO) { 68 throw new IllegalArgumentException ("Illegal level: " 69 + level.getName()); 70 } 71 this.severity = level; 72 this.msg = msg; 73 } 74 75 84 public final FailedCheck check(CrawlerSettings settings, ComplexType owner, 85 Type definition, Object value) { 86 return innerCheck(settings, owner, definition, value); 87 } 88 89 97 public abstract FailedCheck innerCheck(CrawlerSettings settings, 98 ComplexType owner, Type definition, Object value); 99 100 104 protected String getDefaultMessage() { 105 return msg; 106 } 107 108 112 public class FailedCheck { 113 private final String msg; 114 private final CrawlerSettings settings; 115 private final ComplexType owner; 116 private final Type definition; 117 private final Object value; 118 protected final ArrayList <Object > messageArguments 119 = new ArrayList <Object >(); 120 121 132 public FailedCheck(CrawlerSettings settings, ComplexType owner, 133 Type definition, Object value, String msg) { 134 this.msg = msg; 135 this.settings = settings; 136 this.owner = owner; 137 this.definition = definition; 138 this.value = value; 139 this.messageArguments.add(definition.getName()); 140 this.messageArguments.add(value); 141 this.messageArguments.add(owner.getName()); 142 } 143 144 154 public FailedCheck(CrawlerSettings settings, ComplexType owner, 155 Type definition, Object value) { 156 this(settings, owner, definition, value, getDefaultMessage()); 157 } 158 159 163 public String getMessage() { 164 return MessageFormat.format(msg, messageArguments.toArray()); 165 } 166 167 171 public Level getLevel() { 172 return severity; 173 } 174 175 179 public Type getDefinition() { 180 return definition; 181 } 182 183 187 public Object getValue() { 188 return value; 189 } 190 191 195 public ComplexType getOwner() { 196 return owner; 197 } 198 199 203 public CrawlerSettings getSettings() { 204 return settings; 205 } 206 207 212 public String toString() { 213 return getMessage(); 214 } 215 } 216 217 223 public int compareTo(Constraint o) { 224 Constraint c = (Constraint) o; 225 return c.severity.intValue() - severity.intValue(); 226 } 227 228 } 229 | Popular Tags |