org.apache.nutch.indexer.replace

## Class FieldReplacer

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

-     public class FieldReplacer
      extends java.lang.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(java.lang.String fieldName, java.lang.String pattern, java.lang.String replacement, java.lang.Integer flags)` Field replacer with the input and output field the same.
    `FieldReplacer(java.lang.String fieldName, java.lang.String toFieldName, java.lang.String pattern, java.lang.String replacement, java.lang.Integer flags)` Create a FieldReplacer for a field.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `java.lang.String` | `checkAndReplace(java.lang.String fieldName, java.lang.String value)` Return a replacement value for a field.
    `java.lang.String` | `getFieldName()`
    `java.util.regex.Pattern` | `getPattern()`
    `java.lang.String` | `getReplacement()`
    `java.lang.String` | `getToFieldName()`
    `boolean` | `isValid()` Does this FieldReplacer have a valid fieldname and pattern?
    `java.lang.String` | `replace(java.lang.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(java.lang.String fieldName,
                               java.lang.String toFieldName,
                               java.lang.String pattern,
                               java.lang.String replacement,
                               java.lang.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(java.lang.String fieldName,
                               java.lang.String pattern,
                               java.lang.String replacement,
                               java.lang.Integer flags)

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

  - ### Method Detail

    - #### getFieldName

          public java.lang.String getFieldName()

    - #### getToFieldName

          public java.lang.String getToFieldName()

    - #### getPattern

          public java.util.regex.Pattern getPattern()

    - #### getReplacement

          public java.lang.String getReplacement()

    - #### isValid

          public boolean isValid()

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

    - #### replace

          public java.lang.String replace(java.lang.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 java.lang.String checkAndReplace(java.lang.String fieldName,
                                                  java.lang.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 © 2019 The Apache Software Foundation