Example usage for java.text StringCharacterIterator StringCharacterIterator

List of usage examples for java.text StringCharacterIterator StringCharacterIterator

Introduction

In this page you can find the example usage for java.text StringCharacterIterator StringCharacterIterator.

Prototype

public StringCharacterIterator(String text) 

Source Link

Document

Constructs an iterator with an initial index of 0.

Usage

From source file:Main.java

protected static boolean checkPrefixPart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    if (ch == '_') // Can't start with _ (bnodes labels handled separately) 
        return false;
    return checkNameTail(cIter);
}

From source file:Main.java

public static String escapeNonCustomRegex(String path) {
    /*/*from  www . j  ava  2s .com*/
     * TODO replace with a regular expression
     */
    StringBuilder sb = new StringBuilder();
    boolean inCustomRegion = false;
    CharacterIterator it = new StringCharacterIterator(path);
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {

        if (ch == CUSTOM_REGEX_START) {
            inCustomRegion = true;
        } else if (ch == CUSTOM_REGEX_END) {
            inCustomRegion = false;
        }

        if (REGEX_SPECIAL_CHARS.contains(ch) && !inCustomRegion) {
            sb.append('\\');
        }

        sb.append(ch);
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Escape characters for text appearing as XML data, between tags.
 * <P/>//from  w  w  w.java  2 s. co  m
 * The following characters are replaced with corresponding character entities :
 * '<' to '&lt';
 * '>' to '&gt';
 * '&' to '&amp;'
 * '"' to '&quot;'
 * "'" to "&#039;"
 * <P/>
 * Note that JSTL's {@code <c:out>} escapes the exact same set of characters as this method.
 */
public static String escapeCharsForXML(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();
}

From source file:com.thinkbiganalytics.feedmgr.rest.support.SystemNamingService.java

public static String generateSystemName(String name) {
    //first trim it
    String systemName = StringUtils.trimToEmpty(name);
    if (StringUtils.isBlank(systemName)) {
        return systemName;
    }//  w ww.  j  ava2 s . c  om
    systemName = systemName.replaceAll(" +", "_");
    systemName = systemName.replaceAll("[^\\w_]", "");

    int i = 0;

    StringBuilder s = new StringBuilder();
    CharacterIterator itr = new StringCharacterIterator(systemName);
    for (char c = itr.first(); c != CharacterIterator.DONE; c = itr.next()) {
        if (Character.isUpperCase(c)) {
            //if it is upper, not at the start and not at the end check to see if i am surrounded by upper then lower it.
            if (i > 0 && i != systemName.length() - 1) {
                char prevChar = systemName.charAt(i - 1);
                char nextChar = systemName.charAt(i + 1);

                if (Character.isUpperCase(prevChar) && (Character.isUpperCase(nextChar)
                        || CharUtils.isAsciiNumeric(nextChar) || '_' == nextChar || '-' == nextChar)) {
                    char lowerChar = Character.toLowerCase(systemName.charAt(i));
                    s.append(lowerChar);
                } else {
                    s.append(c);
                }
            } else if (i > 0 && i == systemName.length() - 1) {
                char prevChar = systemName.charAt(i - 1);
                if (Character.isUpperCase(prevChar) && !CharUtils.isAsciiNumeric(systemName.charAt(i))) {
                    char lowerChar = Character.toLowerCase(systemName.charAt(i));
                    s.append(lowerChar);
                } else {
                    s.append(c);
                }
            } else {
                s.append(c);
            }
        } else {
            s.append(c);
        }

        i++;
    }

    systemName = s.toString();
    systemName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, systemName);
    systemName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_UNDERSCORE, systemName);
    systemName = StringUtils.replace(systemName, "__", "_");
    // Truncate length if exceeds Hive limit
    systemName = (systemName.length() > 128 ? systemName.substring(0, 127) : systemName);
    return systemName;
}

From source file:Main.java

public static String encode(String unencodedValue) {

    CharacterIterator charIterator;
    StringBuilder encodeBuilder;//from  w  w  w . jav  a  2s  .c  om
    char currentChar;

    encodeBuilder = new StringBuilder();
    charIterator = new StringCharacterIterator(unencodedValue);
    while ((currentChar = charIterator.current()) != CharacterIterator.DONE) {
        switch (currentChar) {
        case '&':
            encodeBuilder.append("&amp;");
            break;
        case '<':
            encodeBuilder.append("&lt;");
            break;
        case '>':
            encodeBuilder.append("&gt;");
            break;
        default:
            encodeBuilder.append(currentChar);
        }
        charIterator.next();
    }

    return encodeBuilder.toString();
}

From source file:Utils.java

/**
 * Determine whether the supplied string represents a well-formed fully-qualified Java classname. This utility method enforces
 * no conventions (e.g., packages are all lowercase) nor checks whether the class is available on the classpath.
 * //from ww w .  j  ava 2 s . com
 * @param classname
 * @return true if the string is a fully-qualified class name
 */
public static boolean isFullyQualifiedClassname(String classname) {
    if (classname == null)
        return false;
    String[] parts = classname.split("[\\.]");
    if (parts.length == 0)
        return false;
    for (String part : parts) {
        CharacterIterator iter = new StringCharacterIterator(part);
        // Check first character (there should at least be one character for each part) ...
        char c = iter.first();
        if (c == CharacterIterator.DONE)
            return false;
        if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c))
            return false;
        c = iter.next();
        // Check the remaining characters, if there are any ...
        while (c != CharacterIterator.DONE) {
            if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c))
                return false;
            c = iter.next();
        }
    }
    return true;
}

From source file:XmlValueEncoder.java

/**
 * {@inheritDoc}/*from   w  ww  . ja  va2  s  . c  om*/
 *
 * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String)
 */
public String encode(String text) {
    if (text == null)
        return null;
    StringBuilder sb = new StringBuilder();
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '&':
            sb.append("&amp;");
            break;
        case '"':
            sb.append("&quot;");
            break;
        case '<':
            sb.append("&lt;");
            break;
        case '>':
            sb.append("&gt;");
            break;
        case '\'':
            sb.append("&#039;");
            break;
        default:
            sb.append(c);
        }
    }
    return sb.toString();
}

From source file:Util.java

/**
* Returns a string that contains all characters of the given string in
* reverse order./* ww  w .ja  v a 2  s . com*/
*
* @param str
*
* @return
*/
public String reverse(String str) {
    if (str == null) {
        return null;
    }

    char[] newStr = new char[str.length()];
    StringCharacterIterator iterator = new StringCharacterIterator(str);
    int i = 0;

    for (char ch = iterator.last(); ch != CharacterIterator.DONE; ch = iterator.previous()) {
        newStr[i] = ch;
        i++;
    }

    return new String(newStr);
}

From source file:Main.java

public static String escapeStringForXML(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 {/*from  w  w w .  j a va2  s .  c om*/
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:nz.co.fortytwo.signalk.util.MjsonCamelDataFormat.java

public Object unmarshal(Exchange exchange, InputStream stream) throws Exception {
    return Json.read(new StringCharacterIterator(IOUtils.toString(stream)));
}