View Javadoc

    1   /*
    2    * legalValueListConstraint
    3    *
    4    * $Id: LegalValueListConstraint.java 4661 2006-09-25 23:11:16Z paul_jack $
    5    *
    6    * Created on Mar 30, 2004
    7    *
    8    * Copyright (C) 2004 Internet Archive.
    9    *
    10   * This file is part of the Heritrix web crawler (crawler.archive.org).
    11   *
    12   * Heritrix is free software; you can redistribute it and/or modify it under the
    13   * terms of the GNU Lesser Public License as published by the Free Software
    14   * Foundation; either version 2.1 of the License, or any later version.
    15   *
    16   * Heritrix is distributed in the hope that it will be useful, but WITHOUT ANY
    17   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    18   * A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
    19   *
    20   * You should have received a copy of the GNU Lesser Public License along with
    21   * Heritrix; if not, write to the Free Software Foundation, Inc., 59 Temple
    22   * Place, Suite 330, Boston, MA 02111-1307 USA
    23   */
    24  package org.archive.crawler.settings;
    25
    26  import java.util.logging.Level;
    27
    28  /***
    29   * A constraint that checks that an attribute value matches one of the items in
    30   * the list of legal values.
    31   *
    32   * @author John Erik Halse
    33   */
    34  public class LegalValueListConstraint extends Constraint {
    35
    36      private static final long serialVersionUID = -4293290799574408033L;
    37
    38      /***
    39       * Constructs a new LegalValueListConstraint.
    40       *
    41       * @param level the severity level.
    42       * @param msg the default error message.
    43       */
    44      public LegalValueListConstraint(Level level, String msg) {
    45          super(level, msg);
    46      }
    47
    48      /***
    49       * Constructs a new LegalValueListConstraint using default severity level
    50       * ({@link Level#WARNING}).
    51       *
    52       * @param msg the default error message.
    53       */
    54      public LegalValueListConstraint(String msg) {
    55          this(Level.WARNING, msg);
    56      }
    57
    58      /***
    59       * Constructs a new LegalValueListConstraint using default error message.
    60       *
    61       * @param level
    62       */
    63      public LegalValueListConstraint(Level level) {
    64          this(level, "Value not in legal values list");
    65      }
    66
    67      /***
    68       * Constructs a new LegalValueListConstraint using default severity level
    69       * ({@link Level#WARNING}) and default error message.
    70       *
    71       */
    72      public LegalValueListConstraint() {
    73          this(Level.WARNING);
    74      }
    75
    76      /*
    77       * (non-Javadoc)
    78       *
    79       * @see org.archive.crawler.settings.Constraint#innerCheck(org.archive.crawler.settings.Type,
    80       *      java.lang.Object)
    81       */
    82      public FailedCheck innerCheck(CrawlerSettings settings, ComplexType owner,
    83              Type definition,
    84              Object value) {
    85          FailedCheck res = null;
    86
    87          // If this attribute is constrained by a list of legal values,
    88          // check that the value is in that list
    89          Object legalValues[] = definition.getLegalValues();
    90          if (legalValues != null) {
    91              boolean found = false;
    92              for (int i = 0; i < legalValues.length && !found; i++) {
    93                  if (legalValues[i].equals(value)) {
    94                      found = true;
    95                  }
    96              }
    97              if (!found) {
    98                  res = new FailedCheck(settings, owner, definition, value);
    99              }
    100         }
    101         return res;
    102     }
    103
    104 }