java.text.StringCharacterIterator Java Examples

The following examples show how to use java.text.StringCharacterIterator. 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 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 #2
Source File: JsonValidatorUtil.java    From xiaoV with GNU General Public License v3.0 6 votes vote down vote up
private boolean literal(String text) {
	CharacterIterator ci = new StringCharacterIterator(text);
	char t = ci.first();
	if (c != t)
		return false;

	int start = col;
	boolean ret = true;
	for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
		if (t != nextCharacter()) {
			ret = false;
			break;
		}
	}
	nextCharacter();
	if (!ret)
		error("literal " + text, start);
	return ret;
}
 
Example #3
Source File: JsonValidatorUtil.java    From xiaoV with GNU General Public License v3.0 6 votes vote down vote up
private boolean valid(String input) {
	if ("".equals(input))
		return true;

	boolean ret = true;
	it = new StringCharacterIterator(input);
	c = it.first();
	col = 1;
	if (!value()) {
		ret = error("value", 1);
	} else {
		skipWhiteSpace();
		if (c != CharacterIterator.DONE) {
			ret = error("end", col);
		}
	}

	return ret;
}
 
Example #4
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 #5
Source File: BreakIteratorTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    boolean gotException = false;
    boolean dummy;

    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            dummy = iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #6
Source File: JSONWriter.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
private void string(Object obj) {
    add('"');
    CharacterIterator it = new StringCharacterIterator(obj.toString());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"') { add("\\\""); } else if (c == '\\') { add("\\\\"); } else if (c == '/') { add("\\/"); } else if (c == '\b') {
            add("\\b");
        } else if (c
                == '\f') { add("\\f"); } else if (c == '\n') {
            add("\\n");
        } else if (c
                == '\r') { add("\\r"); } else if (c
                == '\t') { add("\\t"); } else if (Character
                .isISOControl(c)) {
            unicode(c);
        } else {
            add(c);
        }
    }
    add('"');
}
 
Example #7
Source File: BreakIteratorTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    boolean gotException = false;
    boolean dummy;

    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            dummy = iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #8
Source File: XMLOutput.java    From pegasus with Apache License 2.0 6 votes vote down vote up
/**
 * Escapes certain characters inappropriate for textual output.
 *
 * <p>Since this method does not hurt, and may be useful in other regards, it will be retained
 * for now.
 *
 * @param original is a string that needs to be quoted
 * @return a string that is "safe" to print.
 */
public static String escape(String original) {
    if (original == null) return null;
    StringBuilder result = new StringBuilder(2 * original.length());
    StringCharacterIterator i = new StringCharacterIterator(original);
    for (char ch = i.first(); ch != StringCharacterIterator.DONE; ch = i.next()) {
        if (ch == '\r') {
            result.append("\\r");
        } else if (ch == '\n') {
            result.append("\\n");
        } else if (ch == '\t') {
            result.append("\\t");
        } else {
            // DO NOT escape apostrophe. If apostrophe escaping is required,
            // do it beforehand.
            if (ch == '\"' || ch == '\\') result.append('\\');
            result.append(ch);
        }
    }

    return result.toString();
}
 
Example #9
Source File: Locator.java    From aion-germany with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Decodes an Uri with % characters.
 * 
 * @param uri
 *            String with the uri possibly containing % characters.
 * @return The decoded Uri
 */
private static String decodeUri(String uri) {
	if (uri.indexOf('%') == -1) {
		return uri;
	}
	StringBuffer sb = new StringBuffer();
	CharacterIterator iter = new StringCharacterIterator(uri);
	for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
		if (c == '%') {
			char c1 = iter.next();
			if (c1 != CharacterIterator.DONE) {
				int i1 = Character.digit(c1, 16);
				char c2 = iter.next();
				if (c2 != CharacterIterator.DONE) {
					int i2 = Character.digit(c2, 16);
					sb.append((char) ((i1 << 4) + i2));
				}
			}
		} else {
			sb.append(c);
		}
	}
	String path = sb.toString();
	return path;
}
 
Example #10
Source File: JSONValidator.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) { return true; }

    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }

    return ret;
}
 
Example #11
Source File: JSONValidator.java    From alipay-sdk-java-all with Apache License 2.0 6 votes vote down vote up
private boolean literal(String text) {
    CharacterIterator ci = new StringCharacterIterator(text);
    char t = ci.first();
    if (c != t) { return false; }

    int start = col;
    boolean ret = true;
    for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
        if (t != nextCharacter()) {
            ret = false;
            break;
        }
    }
    nextCharacter();

    if (!ret) { error("literal " + text, start); }
    return ret;
}
 
Example #12
Source File: BreakIteratorTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    boolean gotException = false;
    boolean dummy;

    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            dummy = iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #13
Source File: EscapeChars.java    From pra 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.
*/
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 #14
Source File: JSONWriter.java    From wechat-pay-sdk with MIT License 6 votes vote down vote up
private void string(Object obj) {
    add('"');
    CharacterIterator it = new StringCharacterIterator(obj.toString());
    for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) {
        if (c == '"') add("\\\"");
        else if (c == '\\') add("\\\\");
        else if (c == '/') add("\\/");
        else if (c == '\b') add("\\b");
        else if (c == '\f') add("\\f");
        else if (c == '\n') add("\\n");
        else if (c == '\r') add("\\r");
        else if (c == '\t') add("\\t");
        else if (Character.isISOControl(c)) {
            unicode(c);
        } else {
            add(c);
        }
    }
    add('"');
}
 
Example #15
Source File: BlobDescriptorList.java    From aard2-android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Notifies the attached observers that the underlying data has been changed
 * and any View reflecting the data set should refresh itself.
 */
public void notifyDataSetChanged() {
    this.filteredList.clear();
    if (filter == null || filter.length() == 0) {
        this.filteredList.addAll(this.list);
    }
    else {
        for (BlobDescriptor bd : this.list) {
            StringSearch stringSearch = new StringSearch(
                    filter, new StringCharacterIterator(bd.key), filterCollator);
            int matchPos = stringSearch.first();
            if (matchPos != StringSearch.DONE) {
                this.filteredList.add(bd);
            }
        }
    }
    sortOrderChanged();
}
 
Example #16
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 #17
Source File: JsonValidator.java    From ToolsFinal with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) { return true; }

    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = false;
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = false;
        }
    }

    return ret;
}
 
Example #18
Source File: BreakIteratorTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    boolean gotException = false;
    boolean dummy;

    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            dummy = iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #19
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 #20
Source File: SearchTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestDiactricMatch() {
    String pattern = "pattern";
    String text = "text";
    StringSearch strsrch = null;
    try {
        strsrch = new StringSearch(pattern, text);
    } catch (Exception e) {
        errln("Error opening string search ");
        return;
    }

    for (int count = 0; count < DIACTRICMATCH.length; count++) {
        strsrch.setCollator(getCollator(DIACTRICMATCH[count].collator));
        strsrch.getCollator().setStrength(DIACTRICMATCH[count].strength);
        strsrch.setBreakIterator(getBreakIterator(DIACTRICMATCH[count].breaker));
        strsrch.reset();
        text = DIACTRICMATCH[count].text;
        pattern = DIACTRICMATCH[count].pattern;
        strsrch.setTarget(new StringCharacterIterator(text));
        strsrch.setPattern(pattern);
        if (!assertEqualWithStringSearch(strsrch, DIACTRICMATCH[count])) {
            errln("Error at test number " + count);
        }
    }
}
 
Example #21
Source File: JSONValidator.java    From pay with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) return true;
    
    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }
    
    return ret;
}
 
Example #22
Source File: SearchTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestReset() {
    StringCharacterIterator text = new StringCharacterIterator("fish fish");
    String pattern = "s";

    StringSearch  strsrch = new StringSearch(pattern, text, m_en_us_, null);
    strsrch.setOverlapping(true);
    strsrch.setCanonical(true);
    strsrch.setIndex(9);
    strsrch.reset();
    if (strsrch.isCanonical() || strsrch.isOverlapping() ||
        strsrch.getIndex() != 0 || strsrch.getMatchLength() != 0 ||
        strsrch.getMatchStart() != SearchIterator.DONE) {
            errln("Error resetting string search");
    }

    strsrch.previous();
    if (strsrch.getMatchStart() != 7 || strsrch.getMatchLength() != 1) {
        errln("Error resetting string search\n");
    }
}
 
Example #23
Source File: JSONValidator.java    From alipay-sdk with Apache License 2.0 6 votes vote down vote up
private boolean valid(String input) {
    if ("".equals(input)) return true;
    
    boolean ret = true;
    it = new StringCharacterIterator(input);
    c = it.first();
    col = 1;
    if (!value()) {
        ret = error("value", 1);
    } else {
        skipWhiteSpace();
        if (c != CharacterIterator.DONE) {
            ret = error("end", col);
        }
    }
    
    return ret;
}
 
Example #24
Source File: BreakIteratorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    boolean gotException = false;
    boolean dummy;

    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            dummy = iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #25
Source File: JSONValidator.java    From wechat-pay-sdk with MIT License 6 votes vote down vote up
private boolean literal(String text) {
    CharacterIterator ci = new StringCharacterIterator(text);
    char t = ci.first();
    if (c != t) return false;

    int start = col;
    boolean ret = true;
    for (t = ci.next(); t != CharacterIterator.DONE; t = ci.next()) {
        if (t != nextCharacter()) {
            ret = false;
            break;
        }
    }
    nextCharacter();

    if (!ret) error("literal " + text, start);
    return ret;
}
 
Example #26
Source File: BreakIteratorTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestBug4153072() {
    BreakIterator iter = BreakIterator.getWordInstance();
    String str = "...Hello, World!...";
    int begin = 3;
    int end = str.length() - 3;
    // not used boolean gotException = false;


    iter.setText(new StringCharacterIterator(str, begin, end, begin));
    for (int index = -1; index < begin + 1; ++index) {
        try {
            iter.isBoundary(index);
            if (index < begin)
                errln("Didn't get exception with offset = " + index +
                                " and begin index = " + begin);
        }
        catch (IllegalArgumentException e) {
            if (index >= begin)
                errln("Got exception with offset = " + index +
                                " and begin index = " + begin);
        }
    }
}
 
Example #27
Source File: StringUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String reverse(final String value) {
  if (value != null) {
    final CharacterIterator it = new StringCharacterIterator(value);
    final StringBuilder buffer = new StringBuilder();

    for (char c = it.last(); c != CharacterIterator.DONE; c = it.previous()) {
      buffer.append(c);
    }

    return buffer.toString();
  }

  return value;
}
 
Example #28
Source File: Breadcrumb.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * replaces backslash with forward slash
 * @param str
 * @return
 */
private static String replaceBacklash(String str){
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(str);
    char character =  iterator.current();
    while (character != CharacterIterator.DONE ){	     
      if (character == '\\') {
         result.append("/");
      }else {
        result.append(character);
      }	      
      character = iterator.next();
    }
    return result.toString();
}
 
Example #29
Source File: StringUtils.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static String reverse(final String value) {
  if (value != null) {
    final CharacterIterator it = new StringCharacterIterator(value);
    final StringBuilder buffer = new StringBuilder();

    for (char c = it.last(); c != CharacterIterator.DONE; c = it.previous()) {
      buffer.append(c);
    }

    return buffer.toString();
  }

  return value;
}
 
Example #30
Source File: Text.java    From DataVec with Apache License 2.0 5 votes vote down vote up
/**
 * For the given string, returns the number of UTF-8 bytes
 * required to encode the string.
 * @param string text to encode
 * @return number of UTF-8 bytes required to encode
 */
public static int utf8Length(String string) {
    CharacterIterator iter = new StringCharacterIterator(string);
    char ch = iter.first();
    int size = 0;
    while (ch != CharacterIterator.DONE) {
        if ((ch >= 0xD800) && (ch < 0xDC00)) {
            // surrogate pair?
            char trail = iter.next();
            if ((trail > 0xDBFF) && (trail < 0xE000)) {
                // valid pair
                size += 4;
            } else {
                // invalid pair
                size += 3;
                iter.previous(); // rewind one
            }
        } else if (ch < 0x80) {
            size++;
        } else if (ch < 0x800) {
            size += 2;
        } else {
            // ch < 0x10000, that is, the largest char value
            size += 3;
        }
        ch = iter.next();
    }
    return size;
}