applyMap static method

String? applyMap(
  1. String? xml,
  2. Map? map, {
  3. String? source,
  4. bool caseSensitive = true,
  5. String? prefix,
  6. bool encode = false,
})

Implementation

static String? applyMap(String? xml, Map? map, {String? source, bool caseSensitive = true, String? prefix, bool encode = false})
{
  if ((map != null) && (xml != null)) {
    map.forEach((key, value)
    {
      String oldValue = "{${S.isNullOrEmpty(prefix) ? (S.isNullOrEmpty(source) ? '' : '${source!}.') : prefix!}$key}";
      String? newValue = (value ?? '').toString();
      if ((encode) && (Xml.hasIllegalCharacters(newValue))) newValue = Xml.encodeIllegalCharacters(newValue);

      if (caseSensitive) {
        xml = xml!.replaceAll(oldValue, newValue!);
      } else {
        try
        {
          xml = xml!.replaceAll(RegExp(oldValue, caseSensitive: false), newValue!);
        }
        catch(e)
        {
          xml = xml!.replaceAll(oldValue.toLowerCase(), newValue!);
          xml = xml!.replaceAll(oldValue.toUpperCase(), newValue);
          xml = xml!.replaceAll(oldValue, newValue);
        }
      }
    });
  }
  return xml;
}