001: /*
002: * RegularExpressionConstraint
003: *
004: * $Id: RegularExpressionConstraint.java 3666 2005-07-06 19:23:52Z stack-sf $
005: *
006: * Created on Mar 31, 2004
007: *
008: * Copyright (C) 2004 Internet Archive.
009: *
010: * This file is part of the Heritrix web crawler (crawler.archive.org).
011: *
012: * Heritrix is free software; you can redistribute it and/or modify it under the
013: * terms of the GNU Lesser Public License as published by the Free Software
014: * Foundation; either version 2.1 of the License, or any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful, but WITHOUT ANY
017: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
018: * A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser Public License along with
021: * Heritrix; if not, write to the Free Software Foundation, Inc., 59 Temple
022: * Place, Suite 330, Boston, MA 02111-1307 USA
023: */
024: package org.archive.crawler.settings;
025:
026: import java.io.Serializable;
027: import java.util.logging.Level;
028:
029: import org.archive.util.TextUtils;
030:
031: /**
032: * A constraint that checks that a value matches a regular expression. This
033: * constraint can only be applied to textual attributes.
034: *
035: * @author John Erik Halse
036: */
037: public class RegularExpressionConstraint extends Constraint implements
038: Serializable {
039: private static final long serialVersionUID = -5916211981136071809L;
040: private final String pattern;
041:
042: /**
043: * Constructs a new RegularExpressionConstraint.
044: *
045: * @param pattern the regular expression pattern the value must match.
046: * @param level the severity level.
047: * @param msg the default error message.
048: */
049: public RegularExpressionConstraint(String pattern, Level level,
050: String msg) {
051: super (level, msg);
052: this .pattern = pattern;
053: }
054:
055: /**
056: * Constructs a new RegularExpressionConstraint using default severity level
057: * ({@link Level#WARNING}).
058: *
059: * @param pattern the regular expression pattern the value must match.
060: * @param msg the default error message.
061: */
062: public RegularExpressionConstraint(String pattern, String msg) {
063: this (pattern, Level.WARNING, msg);
064: }
065:
066: /**
067: * Constructs a new RegularExpressionConstraint using the default error
068: * message.
069: *
070: * @param pattern the regular expression pattern the value must match.
071: * @param level the severity level.
072: */
073: public RegularExpressionConstraint(String pattern, Level level) {
074: this (pattern, level, "Value did not match pattern: \""
075: + pattern + "\"");
076: }
077:
078: /**
079: * Constructs a new RegularExpressionConstraint.
080: *
081: * @param pattern the regular expression pattern the value must match.
082: */
083: public RegularExpressionConstraint(String pattern) {
084: this (pattern, Level.WARNING);
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.archive.crawler.settings.Constraint#innerCheck(org.archive.crawler.settings.Type,
091: * javax.management.Attribute)
092: */
093: public FailedCheck innerCheck(CrawlerSettings settings,
094: ComplexType owner, Type definition, Object value) {
095: if (value instanceof CharSequence) {
096: if (!TextUtils.matches(pattern, (CharSequence) value)) {
097: return new FailedCheck(settings, owner, definition,
098: value);
099:
100: }
101: } else {
102: return new FailedCheck(settings, owner, definition, value,
103: "Can't do regexp on non CharSequence.");
104: }
105: return null;
106: }
107:
108: }
|