dap4/d4core/src/main/java/dap4/core/util/Escape.java
static public String
    entityEscape(String s, String wrt)
    {
        if(wrt == null)
            wrt = ENTITYESCAPES;
        StringBuilder escaped = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int index = wrt.indexOf(c);
            if(index < 0)
                escaped.append(c);
            else switch (c) {
            case '&':
                escaped.append('&' + ENTITY_AMP + ';');
                break;
            case '<':
                escaped.append('&' + ENTITY_LT + ';');
                break;
            case '>':
                escaped.append('&' + ENTITY_GT + ';');
                break;
            case '"':
                escaped.append('&' + ENTITY_QUOT + ';');
                break;
            case '\'':
                escaped.append('&' + ENTITY_APOS + ';');
                break;
            case '\r':
            case '\t':
            case '\n':
                escaped.append(c);  // These are the only legal control chars
                break;
            case '\0':
                // What to do about nul? currrently we suppress it
                break;
            default:
                if(c >= ' ')
                    escaped.append(c);
                break;
            }
        }
        return escaped.toString();
    }
static public String
    entityEscape(String s, String wrt)
    {
        if(wrt == null)
            wrt = ENTITYESCAPES;
        StringBuilder escaped = new StringBuilder();
        for(int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int index = wrt.indexOf(c);
            if(index < 0)
                escaped.append(c);
            else switch (c) {
            case '&':
                escaped.append('&' + ENTITY_AMP + ';');
                break;
            case '<':
                escaped.append('&' + ENTITY_LT + ';');
                break;
            case '>':
                escaped.append('&' + ENTITY_GT + ';');
                break;
            case '"':
                escaped.append('&' + ENTITY_QUOT + ';');
                break;
            case '\'':
                escaped.append('&' + ENTITY_APOS + ';');
                break;
            case '\r':
            case '\t':
            case '\n':
                escaped.append(c);  // These are the only legal control chars
                break;
            case '\0':
                // What to do about nul? currrently we suppress it
                break;
            default:
                if(c >= ' ')
                    escaped.append(c);
                break;
            }
        }
        return escaped.toString();
    }
static public String entityEscape ( String s , String wrt ) { if ( wrt == null ) wrt = ENTITYESCAPES ; StringBuilder escaped = new StringBuilder ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; int index = wrt . indexOf ( c ) ; if ( index < 0 ) escaped . append ( c ) ; else switch ( c ) { case ' ' : escaped . append ( ' ' + ENTITY_AMP + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_LT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_GT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_QUOT + ' ' ) ; break ; case ' ' : escaped . append ( ' ' + ENTITY_APOS + ' ' ) ; break ; case ' ' : case ' ' : case ' ' : escaped . append ( c ) ; // These are the only legal control chars break ; case ' ' : // What to do about nul? currrently we suppress it break ; default : if ( c >= ' ' ) escaped . append ( c ) ; break ; } } return escaped . toString ( ) ; }
Escape selected characters in a string using XML entities

@param s   string to escape
@param wrt which chars to escape
@return escaped string
Escape selected characters in a string using XML entities