@SuppressWarnings("JavaDoc")
  public static String escape(String toEscape) {
    if (toEscape.contains("\"") && toEscape.contains("'")) {
      boolean quoteIsLast = false;
      if (toEscape.lastIndexOf("\"") == toEscape.length() - 1) {
        quoteIsLast = true;
      }
      String[] substringsWithoutQuotes = toEscape.split("\"");

      StringBuilder quoted = new StringBuilder("concat(");
      for (int i = 0; i < substringsWithoutQuotes.length; i++) {
        quoted.append("\"").append(substringsWithoutQuotes[i]).append("\"");
        quoted
            .append(((i == substringsWithoutQuotes.length - 1) ? (quoteIsLast ? ", '\"')" : ")")
                                                           : ", '\"', "));
      }
      return quoted.toString();
    }

    // Escape string with just a quote into being single quoted: f"oo -> 'f"oo'
    if (toEscape.contains("\"")) {
      return String.format("'%s'", toEscape);
    }

    // Otherwise return the quoted string
    return String.format("\"%s\"", toEscape);
  }
@ SuppressWarnings ( "JavaDoc" ) public static String escape ( String toEscape ) { if ( toEscape . contains ( "\"" ) && toEscape . contains ( "'" ) ) { boolean quoteIsLast = false ; if ( toEscape . lastIndexOf ( "\"" ) == toEscape . length ( ) - 1 ) { quoteIsLast = true ; } String [ ] substringsWithoutQuotes = toEscape . split ( "\"" ) ; StringBuilder quoted = new StringBuilder ( "concat(" ) ; for ( int i = 0 ; i < substringsWithoutQuotes . length ; i ++ ) { quoted . append ( "\"" ) . append ( substringsWithoutQuotes [ i ] ) . append ( "\"" ) ; quoted . append ( ( ( i == substringsWithoutQuotes . length - 1 ) ? ( quoteIsLast ? ", '\"')" : ")" ) : ", '\"', " ) ) ; } return quoted . toString ( ) ; } if ( toEscape . contains ( "\"" ) ) { return String . format ( "'%s'" , toEscape ) ; } return String . format ( "\"%s\"" , toEscape ) ; }
Convert strings with both quotes and ticks into a valid xpath component

For example,

<p>
{@code foo} will be converted to {@code "foo"},
</p>
<p>
{@code f"oo} will be converted to {@code 'f"oo'},
</p>
<p>
{@code foo'"bar} will be converted to {@code concat("foo'", '"', "bar")}
</p>

@param toEscape a text to escape quotes in, e.g. {@code "f'oo"}
@return the same text with escaped quoted, e.g. {@code "\"f'oo\""}