org.apache.nutch.indexer.replace

## Class FieldReplacer

- java.lang.Object
- - org.apache.nutch.indexer.replace.FieldReplacer

-     public class FieldReplacer
      extends Object

  POJO to store a filename, its match pattern and its replacement string. A checkAndReplace method is provided where you can simultaneously check if the field matches this replacer and if the pattern matches your field value.
  Author:
    Peter Ciuffetti

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `FieldReplacer(String fieldName, String pattern, String replacement, Integer flags)` Field replacer with the input and output field the same.
    `FieldReplacer(String fieldName, String toFieldName, String pattern, String replacement, Integer flags)` Create a FieldReplacer for a field.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `checkAndReplace(String fieldName, String value)` Return a replacement value for a field.
    `String` | `getFieldName()`
    `Pattern` | `getPattern()`
    `String` | `getReplacement()`
    `String` | `getToFieldName()`
    `boolean` | `isValid()` Does this FieldReplacer have a valid fieldname and pattern?
    `String` | `replace(String value)` Return the replacement value for a field value.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### FieldReplacer

          public FieldReplacer(String fieldName,
                               String toFieldName,
                               String pattern,
                               String replacement,
                               Integer flags)

      Create a FieldReplacer for a field. Any pattern exceptions are caught within this constructor and the object is marked inValid. The error will be logged. This prevents this caller from attempting invalid replacements.
      Parameters:
        `fieldName` - the name of the source field to operate on. Required.
        `toFieldName` - the name of the target field. Required.
        `pattern` - the pattern the field must match. Required.
        `replacement` - the replacement string
        `flags` - the Pattern flags value, or null if no flags are needed

    - #### FieldReplacer

          public FieldReplacer(String fieldName,
                               String pattern,
                               String replacement,
                               Integer flags)

      Field replacer with the input and output field the same.
      Parameters:
        `fieldName` -
        `pattern` -
        `replacement` -
        `flags` -

  - ### Method Detail

    - #### getFieldName

          public String getFieldName()

    - #### getToFieldName

          public String getToFieldName()

    - #### getPattern

          public Pattern getPattern()

    - #### getReplacement

          public String getReplacement()

    - #### isValid

          public boolean isValid()

      Does this FieldReplacer have a valid fieldname and pattern?
      Returns:

    - #### replace

          public String replace(String value)

      Return the replacement value for a field value. This does not check for a matching field; the caller must decide if this FieldReplacer should operate on this value by checking getFieldName(). The method returns the value with the replacement. If the value returned is not different then eiher the pattern didn't match or the replacement was a no-op.
      Parameters:
        `value` -
      Returns:

    - #### checkAndReplace

          public String checkAndReplace(String fieldName,
                                        String value)

      Return a replacement value for a field. This is designed to fail fast and trigger a replacement only when necessary. If this method returns null, either the field does not match or the value does not match the pattern (or possibly the pattern is invalid). So only if the method returns a non-null value will you need to replace the value for the field.
      Parameters:
        `fieldName` - the name of the field you are checking
        `value` - the value of the field you are checking
      Returns:
        a replacement value. If null, either the field does not match or the value does not match.

Copyright © 2021 The Apache Software Foundation