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

The following examples show how to use java.text.StringCharacterIterator#next() . 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: 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 2
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 3
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 4
Source File: Toolkit.java    From charliebot with GNU General Public License v2.0 6 votes vote down vote up
public static ArrayList wordSplit(String s) {
    ArrayList arraylist = new ArrayList();
    int i = s.length();
    if (i == 0) {
        arraylist.add("");
        return arraylist;
    }
    int j = 0;
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
        if (c == ' ') {
            int k = stringcharacteriterator.getIndex();
            arraylist.add(s.substring(j, k));
            j = k + 1;
        }

    if (j < s.length())
        arraylist.add(s.substring(j));
    return arraylist;
}
 
Example 5
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 6
Source File: TrieTest.java    From staash with Apache License 2.0 5 votes vote down vote up
public boolean addWord(String word) {
    word = word.toUpperCase();
    
    StringCharacterIterator iter = new StringCharacterIterator(word);
    TrieNode current = root;
    for (Character ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next()) {
        current = current.getOrCreateChild(ch);
    }
    current.setIsWord(true);
    return true;
}
 
Example 7
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 8
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 9
Source File: TrieTest.java    From staash with Apache License 2.0 5 votes vote down vote up
public boolean containsWord(String word) {
    word = word.toUpperCase();
    StringCharacterIterator iter = new StringCharacterIterator(word);
    TrieNode current = root;
    for (Character ch = iter.first(); ch != CharacterIterator.DONE; ch = iter.next()) {
        current = current.getChild(ch);
        if (current == null)
            return false;
    }
    return current.isWord();
}
 
Example 10
Source File: CmmnXmlUtil.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
public static List<String> parseDelimitedList(String s) {
    List<String> result = new ArrayList<>();
    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 11
Source File: RestfulMockSortingUtils.java    From smockin with Apache License 2.0 5 votes vote down vote up
private String leftPad(String stringToPad, String padder, Integer size) {

        final StringBuilder sb = new StringBuilder(size.intValue());
        final StringCharacterIterator sci = new StringCharacterIterator(padder);

        while (sb.length() < (size.intValue() - stringToPad.length())) {
            for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) {
                if (sb.length() < (size.intValue() - stringToPad.length())) {
                    sb.insert(sb.length(), String.valueOf(ch));
                }
            }
        }

        return sb.append(stringToPad).toString();
    }
 
Example 12
Source File: XlsUtils.java    From data-prep with Apache License 2.0 5 votes vote down vote up
/**
 * return the column number from a cell reference (AA242)
 *
 * @param lastCell
 * @return
 */
public static int getColumnNumberFromCellRef(String lastCell) {

    StringBuilder letters = new StringBuilder();
    // get all letters to remove row number
    StringCharacterIterator iter = new StringCharacterIterator(lastCell);
    for (char c = iter.first(); c != StringCharacterIterator.DONE; c = iter.next()) {
        if (!NumberUtils.isNumber(String.valueOf(c))) {
            letters.append(c);
        }
    }
    // use poi api to calculate column number from an excell column format
    return CellReference.convertColStringToIndex(letters.toString());

}
 
Example 13
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 14
Source File: PatternArbiter.java    From charliebot with GNU General Public License v2.0 4 votes vote down vote up
public static void checkAIMLPattern(String s, boolean flag)
        throws NotAnAIMLPatternException {
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    boolean flag1 = true;
    int j = 1;
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
        int i;
        if (!Character.isLetterOrDigit(c)) {
            i = 32;
            if (Character.isWhitespace(c)) {
                i |= 0x10;
                if (c == ' ')
                    i |= 8;
                else
                    throw new NotAnAIMLPatternException("The only allowed whitespace is a space ( ).", s);
            }
            if (c == '*' || c == '_') {
                i |= 2;
                if (j != 1 && (j == 4 || (j & 2) == 2))
                    throw new NotAnAIMLPatternException("A wildcard cannot be preceded by a wildcard, a letter or a digit.", s);
            }
            if (c == '<') {
                int k = stringcharacteriterator.getIndex();
                if (s.regionMatches(false, k, "<bot name=\"", 0, 11)) {
                    stringcharacteriterator.setIndex(k + 11);
                    for (c = stringcharacteriterator.next(); c != '\uFFFF' && c != '"' && (Character.isLetterOrDigit(c) || c == ' ' || c == '_'); c = stringcharacteriterator.next())
                        ;
                    k = stringcharacteriterator.getIndex();
                    if (!s.regionMatches(false, k, "\"/>", 0, 3))
                        throw new NotAnAIMLPatternException("Invalid or malformed <bot/> element.", s);
                    stringcharacteriterator.setIndex(k + 3);
                } else {
                    throw new NotAnAIMLPatternException("Invalid or malformed inner element.", s);
                }
            }
        } else {
            i = 4;
            if (!flag && Character.toUpperCase(c) != c)
                throw new NotAnAIMLPatternException("Characters with case mappings must be uppercase.", s);
            if (j != 1 && (j & 2) == 2)
                throw new NotAnAIMLPatternException("A letter or digit may not be preceded by a wildcard.", s);
        }
        j = i;
    }

}
 
Example 15
Source File: ExpectEmulation.java    From expect4j with Apache License 2.0 4 votes vote down vote up
public static String escape(final String value) {
    String raw = value;
    boolean isString = false;

    if( value.indexOf('"') == 0 && value.lastIndexOf('"') == value.length() - 1) {
        isString = true;
        raw = value.substring(1, value.length() - 1);
    }

    final StringBuffer result = new StringBuffer();

    StringCharacterIterator iterator = new StringCharacterIterator(raw);
    char character = iterator.current();
    while (character != StringCharacterIterator.DONE ){
        /*
         * All literals need to have backslashes doubled.
         * &;`'"|*?~<>^()[]{}$\
         */
        switch( character ) {
            case '&':
            case ';':
            case '\'':
            case '"':
            case '|':
            case '*':
            case '?':
            case '~':
            case '<':
            case '>':
            case '^':
            case '(':
            case ')':
            case '[':
            case ']':
            case '{':
            case '}':
            case '$':
            case '\\': result.append("\\");
            default: result.append(character);
        }
        character = iterator.next();
    }
    String clean = result.toString();
    if( isString )
        clean = '"' + clean + '"';

    return clean;
}
 
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: TextToUnicodeConverter.java    From hortonmachine with GNU General Public License v3.0 4 votes vote down vote up
/**
    * This method allows to convert an Autocad text in an Unicode text
    * 
    * @param s Autocad text
    * @return String Unicode text
    */
public static String convertText(String s) {
       StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
       StringBuffer stringbuffer = new StringBuffer();
       int ai[] = new int[s.length()];
       int i = 0;
       int j = 0;
       for(char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next())
           if(c == '%')
           {
               c = stringcharacteriterator.next();
               if(c != '%')
               {
                   stringbuffer.append('%');
                   c = stringcharacteriterator.previous();
               } else
               {
                   c = stringcharacteriterator.next();
                   switch(c)
                   {
                   case 37: // '%'
                       stringbuffer.append('%');
                       break;

                   case 80: // 'P'
                   case 112: // 'p'
                       stringbuffer.append('\361');
                       break;

                   case 67: // 'C'
                   case 99: // 'c'
                       stringbuffer.append('\355');
                       break;

                   case 68: // 'D'
                   case 100: // 'd'
                       stringbuffer.append('\u00b0');
                       break;

                   case 85: // 'U'
                   case 117: // 'u'
                       ai[stringbuffer.length()] ^= 1;
                       i++;
                       break;

                   case 79: // 'O'
                   case 111: // 'o'
                       ai[stringbuffer.length()] ^= 2;
                       j++;
                       break;

                   default:
                       if(c >= '0' && c <= '9')
                       {
                           int k = 3;
                           char c1 = (char)(c - 48);
                           for(c = stringcharacteriterator.next(); c >= '0' && c <= '9' && --k > 0; c = stringcharacteriterator.next())
                               c1 = (char)(10 * c1 + (c - 48));

                           stringbuffer.append(c1);
                       }
                       c = stringcharacteriterator.previous();
                       break;
                   }
               }
           } else
           if(c == '^')
           {
               c = stringcharacteriterator.next();
               if(c == ' ')
                   stringbuffer.append('^');
           } else
           {
               stringbuffer.append(c);
           }
       s = Unicode.char2DOS437(stringbuffer, 2, '?');

	String ss = s;
	return ss;
}
 
Example 18
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 19
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();
}
 
Example 20
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();
}