Class PathRedactor

java.lang.Object
com.tnsai.redaction.PathRedactor
All Implemented Interfaces:
Masker, Function<String,String>, UnaryOperator<String>

public final class PathRedactor extends Object implements Masker
Hides folders in a text by replacing each with the name the caller gives it, for example a run folder with <run> and the user's home folder with ~. Every other path is left as it is.

Each folder is hidden in the form it was given, in its absolute normalized form and, when it exists, in its real form with links resolved (on macOS, /tmp/run is also /private/tmp/run). The forms of all folders are replaced longest first, so a folder inside another is replaced by its own name: with the run folder inside the home folder, a path under the run folder reads <run>/… rather than ~/….

A folder is matched as a whole path: hiding /home/ann leaves /home/anna, /data/home/ann and /home/ann.bak alone, and hides it in /home/ann/x, file:/home/ann and "/home/ann". A root is never hidden, since it would hide far more than the folder, and a relative folder is hidden only in its absolute and real forms, resolved against the working directory. A letter or digit of any script next to a form makes it a longer name, so /home/annä is left alone too.

A form is matched exactly as the platform writes it: a path written with another case, percent-encoded, with doubled separators or in another platform's form is not hidden. The redactor hides the folders it is given; it is not a filter for every secret a text may hold.

The redactor is immutable and may be used from several threads at once.

Since:
0.18.6
  • Constructor Details

    • PathRedactor

      public PathRedactor(Map<Path,String> replacements)
      Creates a redactor for the given folders.
      Parameters:
      replacements - each folder to hide and what replaces it; a null folder is skipped, and the map's order does not matter
      Throws:
      NullPointerException - if replacements is null, or a folder's replacement is null
  • Method Details

    • apply

      public String apply(String text)
      Returns the text with every folder replaced.
      Specified by:
      apply in interface Function<String,String>
      Parameters:
      text - the text, or null
      Returns:
      the redacted text, or text itself when it is null or empty