docs: private static CharSequence escapeForXml(final CharSequence input, final XmlEscapeMode xmlEscapeMode) {
          if (input == null) {
              return null;
          }
          final int len = input.length();
          final StringBuilder out = new StringBuilder((int) (len * 1.3));
          CharSequence toAppend;
          char ch;
          int last = 0;
          int i = 0;
          while (i < len) {
              toAppend = null;
              ch = input.charAt(i);
              switch (xmlEscapeMode) {
              case safe:
                  switch (ch) {
                  case '<':
                      toAppend = LT_ENCODE;
                      break;
                  case '>':
                      toAppend = GT_ENCODE;
                      break;
                  case '&':
                      toAppend = AMP_ENCODE;
                      break;
                  case '"':
                      toAppend = QUOTE_ENCODE;
                      break;
                  case '\'':
                      toAppend = APOS_ENCODE;
                      break;
                  default:
                      break;
                  }
                  break;
              case forAttribute:
                  // No need to escape '>' for attributes.
                  switch (ch) {
                  case '<':
                      toAppend = LT_ENCODE;
                      break;
                  case '&':
                      toAppend = AMP_ENCODE;
                      break;
                  case '"':
                      toAppend = QUOTE_ENCODE;
                      break;
                  case '\'':
                      toAppend = APOS_ENCODE;
                      break;
                  default:
                      break;
                  }
                  break;
              case forAttributeApos:
                  // No need to escape '>' and '"' for attributes using '\'' as quote.
                  switch (ch) {
                  case '<':
                      toAppend = LT_ENCODE;
                      break;
                  case '&':
                      toAppend = AMP_ENCODE;
                      break;
                  case '\'':
                      toAppend = APOS_ENCODE;
                      break;
                  default:
                      break;
                  }
                  break;
              case forText:
                  // No need to escape '"', '\'', and '>' for text.
                  switch (ch) {
                  case '<':
                      toAppend = LT_ENCODE;
                      break;
                  case '&':
                      toAppend = AMP_ENCODE;
                      break;
                  default:
                      break;
                  }
                  break;
              }
              if (toAppend != null) {
                  if (i > last) {
                      out.append(input, last, i);
                  }
                  out.append(toAppend);
                  last = ++i;
              } else {
                  i++;
              }
          }
          if (last == 0) {
              return input;
          }
          if (i > last) {
              out.append(input, last, i);
          }
          return out;
      }