1 25 package org.archive.crawler.deciderules; 26 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import javax.management.AttributeNotFoundException ; 31 32 import org.archive.crawler.settings.SimpleType; 33 import org.archive.util.TextUtils; 34 35 36 37 43 public class MatchesRegExpDecideRule extends PredicatedDecideRule { 44 45 private static final long serialVersionUID = 6441410917074319295L; 46 47 private static final Logger logger = 48 Logger.getLogger(MatchesRegExpDecideRule.class.getName()); 49 50 public static final String ATTR_REGEXP = "regexp"; 51 52 56 public MatchesRegExpDecideRule(String name) { 57 super(name); 58 setDescription("MatchesRegExpDecideRule. Applies the configured " + 59 "decision to URIs matching the supplied regular expression."); 60 addElementToDefinition(new SimpleType(ATTR_REGEXP, "Java regular" + 61 "expression to match.", "")); 62 } 63 64 71 protected boolean evaluate(Object object) { 72 try { 73 String regexp = getRegexp(object); 74 String str = object.toString(); 75 boolean result = (regexp == null)? 76 false: TextUtils.matches(regexp, str); 77 if (logger.isLoggable(Level.FINE)) { 78 logger.fine("Tested '" + str + "' match with regex '" + 79 regexp + " and result was " + result); 80 } 81 return result; 82 } catch (ClassCastException e) { 83 return false; 85 } 86 } 87 88 95 protected String getRegexp(Object o) { 96 try { 97 return (String ) getAttribute(o, ATTR_REGEXP); 98 } catch (AttributeNotFoundException e) { 99 logger.severe(e.getMessage()); 100 return null; } 102 } 103 } 104 | Popular Tags |