Java Code Examples for java.text.StringCharacterIterator

The following examples show how to use java.text.StringCharacterIterator. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: xiaoV   Source File: JsonValidatorUtil.java    License: 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 2
Source Project: xiaoV   Source File: JsonValidatorUtil.java    License: 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 3
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 4
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 5
Source Project: pegasus   Source File: XMLOutput.java    License: 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 6
Source Project: freeacs   Source File: EscapeChars.java    License: 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 7
Source Project: aion-germany   Source File: Locator.java    License: 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 8
Source Project: alipay-sdk-java-all   Source File: JSONWriter.java    License: 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 9
Source Project: alipay-sdk-java-all   Source File: JSONValidator.java    License: 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 10
Source Project: alipay-sdk-java-all   Source File: JSONValidator.java    License: 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 11
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 12
Source Project: pra   Source File: EscapeChars.java    License: 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 13
Source Project: j2objc   Source File: BreakIteratorTest.java    License: 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 14
Source Project: wechat-pay-sdk   Source File: JSONWriter.java    License: 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 Project: wechat-pay-sdk   Source File: JSONValidator.java    License: 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 16
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 17
/**
 * 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 18
Source Project: alipay-sdk   Source File: JSONValidator.java    License: 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 19
Source Project: j2objc   Source File: SearchTest.java    License: 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 20
Source Project: pay   Source File: JSONValidator.java    License: 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 21
Source Project: j2objc   Source File: SearchTest.java    License: 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 22
Source Project: birt   Source File: DriverLoader.java    License: 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 23
Source Project: jdk8u_jdk   Source File: BreakIteratorTest.java    License: 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 24
Source Project: ToolsFinal   Source File: JsonValidator.java    License: 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 25
Source Project: dsworkbench   Source File: EscapeChars.java    License: 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 26
Source Project: dsworkbench   Source File: EscapeChars.java    License: 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 27
Source Project: Bats   Source File: Text.java    License: 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;
}
 
Example 28
Source Project: charliebot   Source File: InputNormalizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
public static String patternFitIgnoreCase(String s) {
    s = Toolkit.removeMarkup(s);
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    StringBuffer stringbuffer = new StringBuffer(s.length());
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
        if (!Character.isLetterOrDigit(c))
            stringbuffer.append(' ');
        else
            stringbuffer.append(c);

    return Toolkit.filterWhitespace(stringbuffer.toString());
}
 
Example 29
Source Project: activiti6-boot2   Source File: BpmnXMLUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static List<String> parseDelimitedList(String s) {
  List<String> result = new ArrayList<String>();
  if (StringUtils.isNotEmpty(s)) {

    StringCharacterIterator iterator = new StringCharacterIterator(s);
    char c = iterator.first();

    StringBuilder strb = new StringBuilder();
    boolean insideExpression = false;

    while (c != StringCharacterIterator.DONE) {
      if (c == '{' || c == '$') {
        insideExpression = true;
      } else if (c == '}') {
        insideExpression = false;
      } else if (c == ',' && !insideExpression) {
        result.add(strb.toString().trim());
        strb.delete(0, strb.length());
      }

      if (c != ',' || (insideExpression)) {
        strb.append(c);
      }

      c = iterator.next();
    }

    if (strb.length() > 0) {
      result.add(strb.toString().trim());
    }

  }
  return result;
}
 
Example 30
Source Project: pra   Source File: EscapeChars.java    License: 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();
}