Java Code Examples for java.text.StringCharacterIterator#current()

The following examples show how to use java.text.StringCharacterIterator#current() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EscapeChars.java    From freeacs with MIT License 6 votes vote down vote up
/**
 * Return <tt>aText</tt> with all <tt>'<'</tt> and <tt>'>'</tt> characters replaced by their
 * escaped equivalents.
 *
 * @param aText the a text
 * @return the string
 */
public static String toDisableTags(String aText) {
  final StringBuilder result = new StringBuilder();
  final StringCharacterIterator iterator = new StringCharacterIterator(aText);
  char character = iterator.current();
  while (character != CharacterIterator.DONE) {
    switch (character) {
      case '<':
        result.append("&lt;");
        break;
      case '>':
        result.append("&gt;");
        break;
      default:
        // the char is not a special one
        // add it to the result as is
        result.append(character);
        break;
    }
    character = iterator.next();
  }
  return result.toString();
}
 
Example 2
Source File: PropertyKeyHyperlinkDetector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private String getUnicodeString(String key) {
	StringCharacterIterator iter= new StringCharacterIterator(key);
	StringBuffer result= new StringBuffer();
	while (iter.getIndex() < iter.getEndIndex()) {
		char c= iter.current();
		if (c == '\\') {
			iter.next();
			c= iter.current();
			if (c == 'u') {
				StringBuffer unicode= new StringBuffer();
				unicode.append(iter.next());
				unicode.append(iter.next());
				unicode.append(iter.next());
				unicode.append(iter.next());
				c= (char)Integer.parseInt(unicode.toString(), 16);
			}
		}
		result.append(c);
		iter.next();
	}
	return result.toString();
}
 
Example 3
Source File: DriverLoader.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
static String escapeCharacters( String value )
{
	final StringCharacterIterator iterator = new StringCharacterIterator( value );
	char character = iterator.current( );
	final StringBuffer result = new StringBuffer( );

	while ( character != StringCharacterIterator.DONE )
	{
		if ( character == '\\' )
		{
			result.append( "\\" ); //$NON-NLS-1$
		}
		else
		{
			//the char is not a special one
			//add it to the result as is
			result.append( character );
		}
		character = iterator.next( );
	}
	return result.toString( );

}
 
Example 4
Source File: EscapeChars.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
/**
 * Return <tt>aText</tt> with all <tt>'<'</tt> and <tt>'>'</tt> characters
 * replaced by their escaped equivalents.
 */
public static String toDisableTags(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
 
Example 5
Source File: EscapeChars.java    From dsworkbench with Apache License 2.0 6 votes vote down vote up
/**
 * Escape characters for text appearing as XML data, between tags.
 *
 * <P>The following characters are replaced with corresponding character entities :
 * <table border='1' cellpadding='3' cellspacing='0'>
 * <tr><th> Character </th><th> Encoding </th></tr>
 * <tr><td> < </td><td> &lt; </td></tr>
 * <tr><td> > </td><td> &gt; </td></tr>
 * <tr><td> & </td><td> &amp; </td></tr>
 * <tr><td> " </td><td> &quot;</td></tr>
 * <tr><td> ' </td><td> &#039;</td></tr>
 * </table>
 *
 * <P>Note that JSTL's {@code <c:out>} escapes the exact same set of
 * characters as this method. <span class='highlight'>That is, {@code <c:out>}
 *  is good for escaping to produce valid XML, but not for producing safe
 *  HTML.</span>
 */
public static String forXML(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
 
Example 6
Source File: KHtmlEdit.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Escape text as HTML, escaping meta-characters.
 * @param sbuf
 *
 * @param text
 *            Raw text.
 *
 */
protected void appendHTML(final StringBuilder sbuf, final String text) {
	final StringCharacterIterator ci = new StringCharacterIterator(text);
	char ch = ci.current();

	while (ch != CharacterIterator.DONE) {
		appendHTML(sbuf, ch);
		ch = ci.next();
	}
}
 
Example 7
Source File: AssemblyLineTokenizer.java    From nand2tetris-emu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes space characters from the given string.
 */
private static String removeSpaces(String line) {
	StringBuffer nospc = new StringBuffer();
       StringCharacterIterator i = new StringCharacterIterator(line);
       for (i.first(); i.current() != CharacterIterator.DONE; i.next()) {
		if (i.current() != ' ') {
			nospc.append(i.current());
		}
	}
	return nospc.toString();
}
 
Example 8
Source File: StringUtil.java    From dkpro-jwktl with Apache License 2.0 5 votes vote down vote up
/** Doubles slashes before quotes. */
public static String escapeChars(String text){
    if (null == text) {
        System.out.println("Error in StringUtil.escapeChars(), argument is null.");
        return NULL_STRING;
    }

    if (text.equalsIgnoreCase("\\"))
        return "\\\\";

    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(text);
    char character =  iterator.current();
    while (character != StringCharacterIterator.DONE ){
      if (character == '\"') {
        result.append("\\\"");
      }
      else if (character == '\'') {
          result.append('\\');
          result.append('\'');
        //result.append("\\'");
      }
      else if (character == '\\') {
          result.append('\\');
          result.append('\\');
      }
      else {
        //the char is not a special one
        //add it to the result as is
        result.append(character);
      }
      character = iterator.next();
    }
    return result.toString();
}
 
Example 9
Source File: XMLTagsParser.java    From dkpro-jwktl with Apache License 2.0 5 votes vote down vote up
/**
  * Escapes characters for text appearing as XML data by HTML tags.
  * 
  * <P>The following characters are replaced with corresponding character entities : 
  * <table border='1' cellpadding='3' cellspacing='0' summary="Characters to be replaced with entities">
  * <tr><th> Character </th><th> Encoding </th></tr>
  * <tr><td> &lt; </td><td> &amp;lt; </td></tr>
  * <tr><td> &gt; </td><td> &amp;gt; </td></tr>
  * <tr><td> &amp; </td><td> &amp;amp; </td></tr>
  * <tr><td> " </td><td> &amp;quot;</td></tr>
  * <tr><td> ' </td><td> &amp;#039;</td></tr>
  * </table>
  * 
  * <P>Note that JSTL's {@code <c:out>} escapes the exact same set of 
  * characters as this method. <span class='highlight'>That is, {@code <c:out>}
  *  is good for escaping to produce valid XML, but not for producing safe HTML.</span>
  * see: http://www.javapractices.com/topic/TopicAction.do?Id=96
  */
public static String escapeCharFromXML(String text)
{
    if (null == text) {
        System.out.println("Error in StringUtil.escapeCharFromXML(), argument is null.");
        return NULL_STRING;
    }
    
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(text);
    char character =  iterator.current();
    
    while (character != StringCharacterIterator.DONE ){
      if (character == '&') {
          
          // skip double (error) parsing: &lt; -> &amp;lt;
          int save = iterator.getIndex();
          String ampersand_tag = isAmpersandTag(text, save);
          
          if(ampersand_tag.length() > 0) {
              result.append(ampersand_tag);
              iterator.setIndex(save + ampersand_tag.length() - 1);
          } else {
              result.append("&amp;");
          }
      } else if (' ' != character && XMLTag.existsGlyph(character)) {
        result.append(XMLTag.getHTML(character));
      }
      else {
        //the char is not a special one
        //add it to the result as is
        result.append(character);
      }
      character = iterator.next();
    }
    return result.toString();
}
 
Example 10
Source File: EscapeChars.java    From freeacs with MIT License 5 votes vote down vote up
/**
 * Escape characters for text appearing as XML data, between tags.
 *
 * <p>The following characters are replaced with corresponding character entities :
 *
 * <table border='1' cellpadding='3' cellspacing='0'>
 * <tr><th> Character </th><th> Encoding </th></tr>
 * <tr><td> < </td><td> &lt; </td></tr>
 * <tr><td> > </td><td> &gt; </td></tr>
 * <tr><td> & </td><td> &amp; </td></tr>
 * <tr><td> " </td><td> &quot;</td></tr>
 * <tr><td> ' </td><td> &#039;</td></tr>
 * </table>
 *
 * <p>Note that JSTL's {@code <c:out>} escapes the exact same set of characters as this method.
 * <span class='highlight'>That is, {@code <c:out>} is good for escaping to produce valid XML, but
 * not for producing safe HTML.</span>
 *
 * @param aText the a text
 * @return the string
 */
public static String forXML(String aText) {
  final StringBuilder result = new StringBuilder();
  final StringCharacterIterator iterator = new StringCharacterIterator(aText);
  char character = iterator.current();
  while (character != CharacterIterator.DONE) {
    switch (character) {
      case '<':
        result.append("&lt;");
        break;
      case '>':
        result.append("&gt;");
        break;
      case '\"':
        result.append("&quot;");
        break;
      case '\'':
        result.append("&#039;");
        break;
      case '&':
        result.append("&amp;");
        break;
      default:
        // the char is not a special one
        // add it to the result as is
        result.append(character);
        break;
    }
    character = iterator.next();
  }
  return result.toString();
}
 
Example 11
Source File: TextStringFormatter.java    From ha-bridge with Apache License 2.0 5 votes vote down vote up
/**
   Escapes characters for text appearing as data in the 
   <a href='http://www.json.org/'>Javascript Object Notation</a>
   (JSON) data interchange format.
   
   <P>The following commonly used control characters are escaped :
   <table border='1' cellpadding='3' cellspacing='0'>
   <tr><th> Character </th><th> Escaped As </th></tr>
   <tr><td> " </td><td> \" </td></tr>
   <tr><td> \ </td><td> \\ </td></tr>
   <tr><td> / </td><td> \/ </td></tr>
   <tr><td> back space </td><td> \b </td></tr> 
   <tr><td> form feed </td><td> \f </td></tr>
   <tr><td> line feed </td><td> \n </td></tr>
   <tr><td> carriage return </td><td> \r </td></tr>
   <tr><td> tab </td><td> \t </td></tr>
   </table>
   
   <P>See <a href='http://www.ietf.org/rfc/rfc4627.txt'>RFC 4627</a> for more information.
  */
public static String forJSON(String aText) {
	final StringBuilder result = new StringBuilder();
	StringCharacterIterator iterator = new StringCharacterIterator(aText);
	char character = iterator.current();
	while (character != StringCharacterIterator.DONE) {
		if (character == '\"') {
			result.append("\\\"");
		} else if (character == '\\') {
			result.append("\\\\");
		} else if (character == '/') {
			result.append("\\/");
		} else if (character == '\b') {
			result.append("\\b");
		} else if (character == '\f') {
			result.append("\\f");
		} else if (character == '\n') {
			result.append("\\n");
		} else if (character == '\r') {
			result.append("\\r");
		} else if (character == '\t') {
			result.append("\\t");
		} else {
			// the char is not a special one
			// add it to the result as is
			result.append(character);
		}
		character = iterator.next();
	}
	return result.toString();
}
 
Example 12
Source File: EscapeUtils.java    From development with Apache License 2.0 5 votes vote down vote up
public static String escapeJSON(String aText) {
    if (aText == null) {
        return null;
    }
    final StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != StringCharacterIterator.DONE) {
        if (character == '\"') {
            result.append("\\\"");
        } else if (character == '\\') {
            result.append("\\\\");
        } else if (character == '/') {
            result.append("\\/");
        } else if (character == '\b') {
            result.append("\\b");
        } else if (character == '\f') {
            result.append("\\f");
        } else if (character == '\n') {
            result.append("\\n");
        } else if (character == '\r') {
            result.append("\\r");
        } else if (character == '\t') {
            result.append("\\t");
        } else {
            // the char is not a special one
            // add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
 
Example 13
Source File: HackController.java    From nand2tetris-emu with GNU General Public License v2.0 5 votes vote down vote up
private static boolean compareLineWithTemplate(String out, String cmp) {
    if (out.length() != cmp.length()) {
        return false;
    }
    StringCharacterIterator outi = new StringCharacterIterator(out);
    StringCharacterIterator cmpi = new StringCharacterIterator(cmp);
    for (outi.first(), cmpi.first();
         outi.current() != CharacterIterator.DONE;
         outi.next(), cmpi.next()) {
        if (cmpi.current() != '*' && outi.current() != cmpi.current()) {
            return false;
        }
    }
    return true;
}
 
Example 14
Source File: EscapeChars.java    From pra with MIT License 5 votes vote down vote up
/**
* Escape characters for text appearing as XML data, between tags.
* 
* <P>The following characters are replaced with corresponding character entities :
* <table border='1' cellpadding='3' cellspacing='0'>
* <tr><th> Character </th><th> Encoding </th></tr>
* <tr><td> < </td><td> &lt; </td></tr>
* <tr><td> > </td><td> &gt; </td></tr>
* <tr><td> & </td><td> &amp; </td></tr>
* <tr><td> " </td><td> &quot;</td></tr>
* <tr><td> ' </td><td> &#039;</td></tr>
* </table>
* 
* <P>Note that JSTL's {@code <c:out>} escapes the exact same set of 
* characters as this method. <span class='highlight'>That is, {@code <c:out>}
*  is good for escaping to produce valid XML, but not for producing safe 
*  HTML.</span>
*/
public static String forXML(String aText){
  final StringBuilder result = new StringBuilder();
  final StringCharacterIterator iterator = new StringCharacterIterator(aText);
  char character =  iterator.current();
  while (character != CharacterIterator.DONE ){
    if (character == '<') {
      result.append("&lt;");
    }
    else if (character == '>') {
      result.append("&gt;");
    }
    else if (character == '\"') {
      result.append("&quot;");
    }
    else if (character == '\'') {
      result.append("&#039;");
    }
    else if (character == '&') {
       result.append("&amp;");
    }
    else {
      //the char is not a special one
      //add it to the result as is
      result.append(character);
    }
    character = iterator.next();
  }
  return result.toString();
}
 
Example 15
Source File: AbstractAuthorizeNetPaymentGatewayImpl.java    From yes-cart with Apache License 2.0 4 votes vote down vote up
/**
 * Sanitize strings for output. Copy from anet_java_sdk_sample_app\common\helper.jsp
 *
 * @param string string to sanitize
 * @return sanitize striung
 */
public String sanitizeString(String string) {

    StringBuilder retval = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(string);
    char character = iterator.current();

    while (character != java.text.CharacterIterator.DONE) {
        if (character == '<') {
            retval.append("&lt;");
        } else if (character == '>') {
            retval.append("&gt;");
        } else if (character == '&') {
            retval.append("&amp;");
        } else if (character == '\"') {
            retval.append("&quot;");
        } else if (character == '\t') {
            addCharEntity(9, retval);
        } else if (character == '!') {
            addCharEntity(33, retval);
        } else if (character == '#') {
            addCharEntity(35, retval);
        } else if (character == '$') {
            addCharEntity(36, retval);
        } else if (character == '%') {
            addCharEntity(37, retval);
        } else if (character == '\'') {
            addCharEntity(39, retval);
        } else if (character == '(') {
            addCharEntity(40, retval);
        } else if (character == ')') {
            addCharEntity(41, retval);
        } else if (character == '*') {
            addCharEntity(42, retval);
        } else if (character == '+') {
            addCharEntity(43, retval);
        } else if (character == ',') {
            addCharEntity(44, retval);
        } else if (character == '-') {
            addCharEntity(45, retval);
        } else if (character == '.') {
            addCharEntity(46, retval);
        } else if (character == '/') {
            addCharEntity(47, retval);
        } else if (character == ':') {
            addCharEntity(58, retval);
        } else if (character == ';') {
            addCharEntity(59, retval);
        } else if (character == '=') {
            addCharEntity(61, retval);
        } else if (character == '?') {
            addCharEntity(63, retval);
        } else if (character == '@') {
            addCharEntity(64, retval);
        } else if (character == '[') {
            addCharEntity(91, retval);
        } else if (character == '\\') {
            addCharEntity(92, retval);
        } else if (character == ']') {
            addCharEntity(93, retval);
        } else if (character == '^') {
            addCharEntity(94, retval);
        } else if (character == '_') {
            addCharEntity(95, retval);
        } else if (character == '`') {
            addCharEntity(96, retval);
        } else if (character == '{') {
            addCharEntity(123, retval);
        } else if (character == '|') {
            addCharEntity(124, retval);
        } else if (character == '}') {
            addCharEntity(125, retval);
        } else if (character == '~') {
            addCharEntity(126, retval);
        } else {
            retval.append(character);
        }
        character = iterator.next();
    }
    return retval.toString();
}
 
Example 16
Source File: Jack_Output.java    From nand2tetris-emu with GNU General Public License v2.0 4 votes vote down vote up
public static void printInt(short i) throws TerminateVMProgramThrowable {
    StringCharacterIterator iter = new StringCharacterIterator("" + i);
    for (iter.first(); iter.current() != CharacterIterator.DONE; iter.next())
        printChar((short) iter.current());
}
 
Example 17
Source File: EscapeChars.java    From dsworkbench with Apache License 2.0 4 votes vote down vote up
/**
 * Replace characters having special meaning in regular expressions
 * with their escaped equivalents, preceded by a '\' character.
 *
 * <P>The escaped characters include :
 *<ul>
 *<li>.
 *<li>\
 *<li>?, * , and +
 *<li>&
 *<li>:
 *<li>{ and }
 *<li>[ and ]
 *<li>( and )
 *<li>^ and $
 *</ul>
 */
public static String forRegex(String aRegexFragment) {
    final StringBuilder result = new StringBuilder();

    final StringCharacterIterator iterator =
            new StringCharacterIterator(aRegexFragment);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        /*
         * All literals need to have backslashes doubled.
         */
        if (character == '.') {
            result.append("\\.");
        } else if (character == '\\') {
            result.append("\\\\");
        } else if (character == '?') {
            result.append("\\?");
        } else if (character == '*') {
            result.append("\\*");
        } else if (character == '+') {
            result.append("\\+");
        } else if (character == '&') {
            result.append("\\&");
        } else if (character == ':') {
            result.append("\\:");
        } else if (character == '{') {
            result.append("\\{");
        } else if (character == '}') {
            result.append("\\}");
        } else if (character == '[') {
            result.append("\\[");
        } else if (character == ']') {
            result.append("\\]");
        } else if (character == '(') {
            result.append("\\(");
        } else if (character == ')') {
            result.append("\\)");
        } else if (character == '^') {
            result.append("\\^");
        } else if (character == '$') {
            result.append("\\$");
        } else if (character == '|') {
            result.append("\\|");
        }else {
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}
 
Example 18
Source File: StringUtil.java    From dkpro-jwktl with Apache License 2.0 4 votes vote down vote up
/** Doubles slashes before dollar sign "$" and backslash "\", skip two slashes "\\".
 * 
 * slash1 (true if prev prev is "\")                                
 *  slash2 (previous)                                               
 *   slash3 (current character)                                     <br>
 *   $ -&gt; \\$      if !slash1 &amp;&amp; !slash2 then + "\\"                <br>
 * \\  -&gt; \\       if  slash1 &amp;&amp;  slash2 then skip                  <br>
 * \\$ -&gt; \\$
 */
public static StringBuffer escapeCharDollarAndBackslash(String text){
    if (null == text) {
        System.out.println("Error in StringUtil.escapeCharDollar(), argument is null.");
        return NULL_STRINGBUFFER;   // NULL_STRING;
    }
    StringBuffer result = new StringBuffer();
    StringCharacterIterator iterator = new StringCharacterIterator(text);
    char character =  iterator.current();
    boolean slash1 = false, slash2 = false, slash3;
    while (character != StringCharacterIterator.DONE ){
        
        slash3 = character == '\\';
        boolean appended = false;
        if (    (!slash1 && !slash2)
             ||  (slash1 &&  slash2)) {
            if ('$' == character) {
                result.append("\\$");
                appended = true;
                slash1 = slash2 = false;
            } else {
                if (slash3) {
                    appended = true;    // It will be appended in the next cycle
                    slash1 = slash2 = false;
                }
            }
        } else {
            if (!slash1 && slash2) {
                if ('$' == character) {
                    result.append("\\$");
                } else {
                    result.append("\\\\");
                    if(slash3) {
                        result.append("\\\\");
                    } else {
                        result.append(character);
                    }
                }
                slash1 = slash2 = slash3 = false;
                appended = true;
            }
        }
        
        if (!appended) {
            result.append(character);
        }
      
        slash1 = slash2;
        slash2 = slash3;
        character = iterator.next();
    }
    if (slash2) {
        result.append("\\\\");
    }
    return result;
}
 
Example 19
Source File: EscapeChars.java    From freeacs with MIT License 4 votes vote down vote up
/**
 * Replace characters having special meaning in regular expressions with their escaped
 * equivalents, preceded by a '\' character.
 *
 * <p>The escaped characters include :
 *
 * <ul>
 *   <li>.
 *   <li>\
 *   <li>?, * , and +
 *   <li>&
 *   <li>:
 *   <li>{ and }
 *   <li>[ and ]
 *   <li>( and )
 *   <li>^ and $
 * </ul>
 *
 * @param aRegexFragment the a regex fragment
 * @return the string
 */
public static String forRegex(String aRegexFragment) {
  final StringBuilder result = new StringBuilder();

  final StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
  char character = iterator.current();
  while (character != CharacterIterator.DONE) {
    /* All literals need to have backslashes doubled. */
    switch (character) {
      case '.':
        result.append("\\.");
        break;
      case '\\':
        result.append("\\\\");
        break;
      case '?':
        result.append("\\?");
        break;
      case '*':
        result.append("\\*");
        break;
      case '+':
        result.append("\\+");
        break;
      case '&':
        result.append("\\&");
        break;
      case ':':
        result.append("\\:");
        break;
      case '{':
        result.append("\\{");
        break;
      case '}':
        result.append("\\}");
        break;
      case '[':
        result.append("\\[");
        break;
      case ']':
        result.append("\\]");
        break;
      case '(':
        result.append("\\(");
        break;
      case ')':
        result.append("\\)");
        break;
      case '^':
        result.append("\\^");
        break;
      case '$':
        result.append("\\$");
        break;
      default:
        // the char is not a special one
        // add it to the result as is
        result.append(character);
        break;
    }
    character = iterator.next();
  }
  return result.toString();
}
 
Example 20
Source File: EscapeChars.java    From freeacs with MIT License 4 votes vote down vote up
/**
 * Escapes characters for text appearing as data in the <a href='http://www.json.org/'>Javascript
 * Object Notation</a> (JSON) data interchange format.
 *
 * <p>The following commonly used control characters are escaped :
 *
 * <table border='1' cellpadding='3' cellspacing='0'>
 * <tr><th> Character </th><th> Escaped As </th></tr>
 * <tr><td> " </td><td> \" </td></tr>
 * <tr><td> \ </td><td> \\ </td></tr>
 * <tr><td> / </td><td> \/ </td></tr>
 * <tr><td> back space </td><td> \b </td></tr>
 * <tr><td> form feed </td><td> \f </td></tr>
 * <tr><td> line feed </td><td> \n </td></tr>
 * <tr><td> carriage return </td><td> \r </td></tr>
 * <tr><td> tab </td><td> \t </td></tr>
 * </table>
 *
 * <p>See <a href='http://www.ietf.org/rfc/rfc4627.txt'>RFC 4627</a> for more information.
 *
 * @param aText the a text
 * @return the string
 */
public static String forJSON(String aText) {
  final StringBuilder result = new StringBuilder();
  StringCharacterIterator iterator = new StringCharacterIterator(aText);
  char character = iterator.current();
  while (character != StringCharacterIterator.DONE) {
    switch (character) {
      case '\"':
        result.append("\\\"");
        break;
      case '\\':
        result.append("\\\\");
        break;
      case '/':
        result.append("\\/");
        break;
      case '\b':
        result.append("\\b");
        break;
      case '\f':
        result.append("\\f");
        break;
      case '\n':
        result.append("\\n");
        break;
      case '\r':
        result.append("\\r");
        break;
      case '\t':
        result.append("\\t");
        break;
      default:
        // the char is not a special one
        // add it to the result as is
        result.append(character);
        break;
    }
    character = iterator.next();
  }
  return result.toString();
}