code: public static String escapeIllegalJcrChars(String name)
     {
        StringBuilder buffer = new StringBuilder(name.length() * 2);
        for (int i = 0; i < name.length(); i++)
        {
           char ch = name.charAt(i);
           if (ch == '%' || ch == '/' || ch == ':' || ch == '[' || ch == ']' || ch == '*' || ch == '\'' || ch == '"'
              || ch == '|' || (ch == '.' && name.length() < 3) || (ch == ' ' && (i == 0 || i == name.length() - 1))
              || ch == '\t' || ch == '\r' || ch == '\n')
           {
              buffer.append('%');
              buffer.append(Character.toUpperCase(Character.forDigit(ch / 16, 16)));
              buffer.append(Character.toUpperCase(Character.forDigit(ch % 16, 16)));
           }
           else
           {
              buffer.append(ch);
           }
        }
        return buffer.toString();
     }
docs: Escapes all illegal JCR name characters of a string. The encoding is loosely modeled after URI
  encoding, but only encodes the characters it absolutely needs to in order to make the resulting
  string a valid JCR name. Use {@link #unescapeIllegalJcrChars(String)} for decoding. <br> QName
  EBNF:<br>
  {@code
  <xmp> simplename ::= onecharsimplename | twocharsimplename | threeormorecharname
  onecharsimplename ::= (* Any Unicode character except: '.', '/', ':', '[', ']', '*', ''', '"',
  '|' or any whitespace character *) twocharsimplename ::= '.' onecharsimplename |
  onecharsimplename '.' | onecharsimplename onecharsimplename threeormorecharname ::= nonspace
  string nonspace string ::= char | string char char ::= nonspace | ' ' nonspace ::= (* Any
  Unicode character except: '/', ':', '[', ']', '*', ''', '"', '|' or any whitespace character *)
  </xmp>
  }
  
  @param name
  the name to escape
  @return the escaped name