Class KeyValueParser

java.lang.Object
org.codice.ddf.catalog.plugin.metacard.util.KeyValueParser

public class KeyValueParser extends Object
Helper class used for parsing key-value pairs in the format of 'key = value'. Uses a regex under the hood. In plain English, the rule states that any String is a valid key-value pair if, for key K and for value V, the representation is K=V where the alphabet for K and V is any symbol except =.

Note that neither K or V can be empty. Any whitespace leading or tailing the key and value after parsing is removed. Whitespace within the key or value is preserved, but a key or value cannot consist entirely of whitespace.

  • Constructor Details

    • KeyValueParser

      public KeyValueParser()
      Setup a default instance of KeyValueParser.
    • KeyValueParser

      public KeyValueParser(boolean failFast)
      Sets up an instance of KeyValueParser that throws an IllegalArgumentException on the first failure during batch operations.
      Parameters:
      failFast - Parsing failures throw exceptions if true; otherwise they are silently ignored.
  • Method Details

    • validatePair

      public boolean validatePair(String pair)
      Given a string, returns true if it is a valid key-value pair. This method is useful for external policy validation.
      Parameters:
      pair - The string suspected to be of the form "key=value"
      Returns:
      True if the input is a valid key-value pair. False otherwise.
    • parsePairsToMap

      public Map<String,String> parsePairsToMap(List<String> pairList)
      Transforms a List of strings representing key-value pairs into a Map with only the valid mappings.
      Parameters:
      pairList - The list of strings of the form "key=value".
      Returns:
      A map of key -> value for only the validated key-value strings in the given list.