Internal method to escape special attribute name characters, to handle things like name spaces.
@param str The string to escape the characters in.
431604
private String escapeStringSpecialCharacters(String str)
    {
        if (logger.isLoggable(Level.FINER)) logger.exiting(className, "escapeStringSpecialCharacters(String)");

        if (str != null)
        {
            StringBuffer strBuf = new StringBuffer("");

            for (int i = 0; i < str.length(); i++)
            {
                char strChar = str.charAt(i);

                switch (strChar)
                {
                    case '"':
                        {
                            strBuf.append("\\\"");
                            break;
                        }
                    case '\t':
                        {
                            strBuf.append("\\t");
                            break;
                        }
                    case '\b':
                        {
                            strBuf.append("\\b");
                            break;
                        }
                    case '\\':
                        {
                            strBuf.append("\\\\");
                            break;
                        }
                    case '\f':
                        {
                            strBuf.append("\\f");
                            break;
                        }
                    case '\r':
                        {
                            strBuf.append("\\r");
                            break;
                        }
                    case '/':
                        {
                            strBuf.append("\\/");
                            break;
                        }
                    default:
                        {
                            if ((strChar >= 32) && (strChar <= 126) )
                            {
                                strBuf.append(strChar);
                            }
                            else if (strChar > 126)
                            {
                              if ((escapeNonAsciiChars)) 
                              {
                                  strBuf.append("\\u");
                                  StringBuffer sb = new StringBuffer(Integer.toHexString(strChar));
                                  while (sb.length() < 4)
                                  {
                                      sb.insert(0,'0');
                                  }
                                  strBuf.append(sb.toString());
                              }
                              else {
                                strBuf.append(strChar);
                              }
                            }
                            else 
                            {
                                strBuf.append("\\u");
                                StringBuffer sb = new StringBuffer(Integer.toHexString(strChar));
                                while (sb.length() < 4)
                                {
                                    sb.insert(0,'0');
                                }
                                strBuf.append(sb.toString());
                            }

                            break;
                        }
                }
            }
            str = strBuf.toString();
        }
        if (logger.isLoggable(Level.FINER)) logger.exiting(className, "escapeStringSpecialCharacters(String)");
        return str;
    }
81816
private String encodeString(String str) {
        m_buffer.setLength(0);
        char ch;
        int inx = 0;
        while (inx < str.length()) {
            ch = str.charAt(inx++);
            switch (ch) {
            case '"':  m_buffer.append("\\\""); break;
            case '\\': m_buffer.append("\\\\"); break;
            case '\b':  m_buffer.append("\\b"); break;
            case '\f':  m_buffer.append("\\f"); break;
            case '\n':  m_buffer.append("\\n"); break;
            case '\r':  m_buffer.append("\\r"); break;
            case '\t':  m_buffer.append("\\t"); break;
            default:
                // All other chars are allowed as-is, despite JSON spec.
                m_buffer.append(ch);
            }
        }
        return m_buffer.toString();
    }
586470
@Override
    public void writeAttribute(String localName, String value)
        throws IOException, XMLStreamException
    {
        writeAscii(BYTE_SPACE);
        writeName(localName);
        writeAscii(BYTE_EQ, BYTE_QUOT);

        int len = value.length();
        if (len > 0) {
            if (mAttrValueWriter != null) { // custom escaping?
                mAttrValueWriter.write(value, 0, len);
            } else { // nope, default
                writeAttrValue(value);
            }
        }
        writeAscii(BYTE_QUOT);
    }
715906
private StringBuilder appendAttributeValue(final String value, final StringBuilder buf) {
      if(value.contains("\"")) {
         buf.append("\'").append(escapeAttribute(value)).append("\'");
      } else if(value.contains(" ") || value.contains("\'") || value.contains("=")) {
         buf.append("\"").append(escapeAttribute(value)).append("\"");
      } else {
         buf.append(escapeAttribute(value));
      }
      return buf;
   }
320381
public static String encode(String str)
    {
        String encoded;
        try {
            encoded = URLEncoder.encode(str, "UTF-8").replace(".", "%2E").replace("*", "%2A");
        } catch (UnsupportedEncodingException e) {
            // Should never happen

            encoded = str;
        }

        return encoded;
    }
967984
public static String escapeAttributeName(String s) {
        s = cleanWS(s);
        s = replaceAll(s, BackSlashPtn, EscBackSlash);
        s = replaceAll(s, EqualPtn, EscEqual);
        return s;
    }
1304341
public void text(String str)
    {
        if (fieldNames.isEmpty() || fieldValues.isEmpty())
            return;
        
        String val = (String)fieldValues.pop();
        val += str;
        fieldValues.push(val);
    }
1201382
protected void writeAttribute( String name, String value)
    {
    print( " ");
    print( name);
    print( "=\"");
    // StringEscapeUtils escapes symbols ', < >, &, ", and some control characters
    // NumericEntityEscaper translates additional control characters \n, \t, ...
    print( NumericEntityEscaper.below(0x20).translate(StringEscapeUtils.escapeXml11(value)));
    print( "\"");
    }
1249185
protected String add_escapes(String str) {
      StringBuffer retval = new StringBuffer();
      char ch;
      for (int i = 0; i < str.length(); i++) {
        switch (str.charAt(i))
        {
           case 0 :
              continue;
           case '\b':
              retval.append("\\b");
              continue;
           case '\t':
              retval.append("\\t");
              continue;
           case '\n':
              retval.append("\\n");
              continue;
           case '\f':
              retval.append("\\f");
              continue;
           case '\r':
              retval.append("\\r");
              continue;
           case '\"':
              retval.append("\\\"");
              continue;
           case '\'':
              retval.append("\\\'");
              continue;
           case '\\':
              retval.append("\\\\");
              continue;
           default:
              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
                 String s = "0000" + Integer.toString(ch, 16);
                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
              } else {
                 retval.append(ch);
              }
              continue;
        }
      }
      return retval.toString();
   }
1370782
@SuppressWarnings("unchecked")
    private String encodeAttribute(String s, Charset charset) throws ErrorDataEncoderException {
        if (s == null) {
            return "";
        }
        try {
            String encoded = URLEncoder.encode(s, charset.name());
            if (encoderMode == EncoderMode.RFC3986) {
                for (Map.Entry<Pattern, String> entry : percentEncodings) {
                    String replacement = entry.getValue();
                    encoded = entry.getKey().matcher(encoded).replaceAll(replacement);
                }
            }
            return encoded;
        } catch (UnsupportedEncodingException e) {
            throw new ErrorDataEncoderException(charset.name(), e);
        }
    }
683002
@Override
  public final String xmlEscape(final char pChar) throws Exception {
    if (pChar == '<') {
      return "&lt;";
    } else if (pChar == '>') {
      return "&gt;";
    } else if (pChar == '"') {
      return "&quot;";
    } else if (pChar == '\'') {
      return "&apos;";
    } else if (pChar == '&') {
      return "&amp;";
    }
    return String.valueOf(pChar);
  }
652764
public static void escape(String str, StringBuilder rval) {
        for (int i = 0; i < str.length(); i++) {
            final char hi = str.charAt(i);
            if (hi <= 0x8 || hi == 0xB || hi == 0xC || (hi >= 0xE && hi <= 0x1F)
                    || (hi >= 0x7F && hi <= 0xA0) || // 0xA0 is end of
                    // non-printable latin-1
                    // supplement
                    // characters
                    ((hi >= 0x24F // 0x24F is the end of latin extensions
                            && !Character.isHighSurrogate(hi))
                    // TODO: there's probably a lot of other characters that
                    // shouldn't be escaped that
                    // fall outside these ranges, this is one example from the
                    // json-ld tests
                    )) {
                rval.append(String.format("\\u%04x", (int) hi));
            } else if (Character.isHighSurrogate(hi)) {
                final char lo = str.charAt(++i);
                final int c = (hi << 10) + lo + (0x10000 - (0xD800 << 10) - 0xDC00);
                rval.append(String.format("\\U%08x", c));
            } else {
                switch (hi) {
                case '\b':
                    rval.append("\\b");
                    break;
                case '\n':
                    rval.append("\\n");
                    break;
                case '\t':
                    rval.append("\\t");
                    break;
                case '\f':
                    rval.append("\\f");
                    break;
                case '\r':
                    rval.append("\\r");
                    break;
                // case '\'':
                // rval += "\\'";
                // break;
                case '\"':
                    rval.append("\\\"");
                    // rval += "\\u0022";
                    break;
                case '\\':
                    rval.append("\\\\");
                    break;
                default:
                    // just put the char as is
                    rval.append(hi);
                    break;
                }
            }
        }
        // return rval;
    }
1366210
public static String escape(String str) {
        int i, max;
        StringBuilder result;
        char c;

        max = str.length();
        for (i = 0; i < max; i++) {
            if (str.charAt(i) < 32) {
                break;
            }
        }
        if (i == max) {
            return str;
        }
        result = new StringBuilder(max + 10);
        for (i = 0; i < max; i++) {
            c = str.charAt(i);
            switch (c) {
                case '\n':
                    result.append("\\n");
                    break;
                case '\r':
                    result.append("\\r");
                    break;
                case '\t':
                    result.append("\\t");
                    break;
                case '\\':
                    result.append("\\\\");
                    break;
                default:
                    if (c < 32) {
                        result.append("\\u").append(Strings.padLeft(Integer.toHexString(c), 4, '0'));
                    } else {
                        result.append(c);
                    }
            }
        }
        return result.toString();
    }
286698
public static String escapeCsvStr(String str) {
        if (str != null && !str.equals("")) {
            boolean needQuote = false;
            if (str.contains("\"")) {
                str = str.replaceAll("\"", "\"\"");
                needQuote = true;
            }
            if (!needQuote && str.contains(",")) {
                needQuote = true;
            }
            if (needQuote) {
                str = "\"" + str + "\"";
            }
            return str;
        } else {
            return "";
        }
    }
1236368
private void renderAttribute(InternalStringBuilder buf, String name, String value)
    {
        assert (name != null);
        if (value == null)
            return;

        buf.append(" ");
        buf.append(name);
        buf.append("=\"");
        buf.append(value);
        buf.append("\"");
    }
913079
public static String unescape(String str, char escape)
    {
        if (str == null) {
            return "";
        }
        StringBuffer buf = new StringBuffer();
        char[] array = str.toCharArray();
        boolean escaped = false;
        for (int i = 0; i < array.length; i++) {
            char ch = array[i];
            if (escaped) {
                buf.append(ch);
                escaped = false;
            } else {
                escaped = (ch == escape);
                if (!escaped) {
                    buf.append(ch);
                }
            }
        }
        return buf.toString();
    }
524743
public final String escapeAttribute(String in) {
        in = escape(in);
        in = in.replaceAll(
                "\"",
                "&quot;"
            );

        return in;
    }