public String quoteLiteral ( String string ) { if ( needingQuoteCharacters == null ) { needingQuoteCharacters = new UnicodeSet ( ) . addAll ( syntaxCharacters ) . addAll ( ignorableCharacters ) . addAll ( extraQuotingCharacters ) ; // .addAll(quoteCharacters) if ( usingSlash ) needingQuoteCharacters . add ( BACK_SLASH ) ; if ( usingQuote ) needingQuoteCharacters . add ( SINGLE_QUOTE ) ; } StringBuffer result = new StringBuffer ( ) ; int quotedChar = NO_QUOTE ; int cp ; for ( int i = 0 ; i < string . length ( ) ; i += UTF16 . getCharCount ( cp ) ) { cp = UTF16 . charAt ( string , i ) ; if ( escapeCharacters . contains ( cp ) ) { // we may have to fix up previous characters if ( quotedChar == IN_QUOTE ) { result . append ( SINGLE_QUOTE ) ; quotedChar = NO_QUOTE ; } appendEscaped ( result , cp ) ; continue ; } if ( needingQuoteCharacters . contains ( cp ) ) { // if we have already started a quote if ( quotedChar == IN_QUOTE ) { UTF16 . append ( result , cp ) ; if ( usingQuote && cp == SINGLE_QUOTE ) { // double it result . append ( SINGLE_QUOTE ) ; } continue ; } // otherwise not already in quote if ( usingSlash ) { result . append ( BACK_SLASH ) ; UTF16 . append ( result , cp ) ; continue ; } if ( usingQuote ) { if ( cp == SINGLE_QUOTE ) { // double it and continue result . append ( SINGLE_QUOTE ) ; result . append ( SINGLE_QUOTE ) ; continue ; } result . append ( SINGLE_QUOTE ) ; UTF16 . append ( result , cp ) ; quotedChar = IN_QUOTE ; continue ; } // we have no choice but to use \\u or \\U appendEscaped ( result , cp ) ; continue ; } // otherwise cp doesn't need quoting // we may have to fix up previous characters if ( quotedChar == IN_QUOTE ) { result . append ( SINGLE_QUOTE ) ; quotedChar = NO_QUOTE ; } UTF16 . append ( result , cp ) ; } // all done. // we may have to fix up previous characters if ( quotedChar == IN_QUOTE ) { result . append ( SINGLE_QUOTE ) ; } return result . toString ( ) ; }
Quote a literal string using the available settings . Thus syntax characters quote characters and ignorable characters will be put into quotes .