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:Base64.java

public static byte[] decodeASCII85(String s) {
    s = s.trim();/*from  w w  w . j a v a 2  s.co  m*/
    if (s.startsWith("<~") && s.endsWith("~>")) {
        s = s.substring(2, s.length() - 2).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;
    int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE && ch != '~'; ch = it.next()) {
        if (ch == 'z' && j == 0) {
            out.write(0);
            out.write(0);
            out.write(0);
            out.write(0);
        } else if (ch == 'y' && j == 0) {
            out.write(' ');
            out.write(' ');
            out.write(' ');
            out.write(' ');
        } else if (ch == 'x' && j == 0) {
            out.write(-1);
            out.write(-1);
            out.write(-1);
            out.write(-1);
        } else if (ch >= '!' && ch <= 'u') {
            i = i * 85L + (long) (ch - '!');
            j++;
            if (j >= 5) {
                out.write((int) (i >> 24L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 8L));
                out.write((int) i);
                i = 0;
                j = 0;
            }
        }
    }
    switch (j) {
    case 4:
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 8L));
        break;
    case 3:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        break;
    case 2:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        break;
    }
    return out.toByteArray();
}

From source file:XmlNameEncoder.java

/**
 * {@inheritDoc}//from  w w w  .j av a2s  .c o  m
 * 
 * @see org.jboss.dna.common.text.TextEncoder#encode(java.lang.String)
 */
public String encode(String text) {
    if (text == null)
        return null;
    if (text.length() == 0)
        return text;
    StringBuilder sb = new StringBuilder();
    String hex = null;
    CharacterIterator iter = new StringCharacterIterator(text);
    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c == '_') {
            // Read the next character (if there is one) ...
            char next = iter.next();
            if (next == CharacterIterator.DONE) {
                sb.append(c);
                break;
            }
            // If the next character is not 'x', then these are just regular characters ...
            if (next != 'x') {
                sb.append(c).append(next);
                continue;
            }
            // The next character is 'x', so write out the '_' character in encoded form ...
            sb.append("_x005f_");
            // And then write out the next character ...
            sb.append(next);
        } else if (XML_NAME_ALLOWED_CHARACTERS.get(c)) {
            // Legal characters for an XML Name ...
            sb.append(c);
        } else {
            // All other characters must be escaped with '_xHHHH_' where 'HHHH' is the hex string for the code point
            hex = Integer.toHexString(c);
            // The hex string excludes the leading '0's, so check the character values so we know how many to prepend
            if (c >= '\u0000' && c <= '\u000f') {
                sb.append("_x000").append(hex);
            } else if (c >= '\u0010' && c <= '\u00ff') {
                sb.append("_x00").append(hex);
            } else if (c >= '\u0100' && c <= '\u0fff') {
                sb.append("_x0").append(hex);
            } else {
                sb.append("_x").append(hex);
            }
            sb.append('_');
        }
    }
    return sb.toString();
}

From source file:org.activiti.designer.kickstart.eclipse.sync.SyncUtil.java

protected static String backlashReplace(String myStr) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(myStr);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '\\') {
            result.append("/");
        } else {//from  w  w  w  .  j  a  va  2 s .c  o m
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:org.hyperic.hq.ui.action.resource.common.monitor.visibility.IndicatorChartsAction.java

/**
 * Find characters having special meaning <em>inside</em> HTML tags and
 * URLs.//from  w w  w.j ava 2 s . c  o m
 * 
 * <P>
 * The special characters are :
 * <ul>
 * <li><
 * <li>>
 * <li>"
 * <li>'
 * <li>\
 * <li>&
 * <li>|
 * <li>?
 * </ul>
 * 
 * <P>
 */
private int indexOfSpecialChars(String aTagFragment) {
    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    int i = 0;
    for (char character = iterator.current(); character != StringCharacterIterator.DONE; character = iterator
            .next(), i++) {
        switch (character) {
        case '<':
        case '>':
        case '\"':
        case '\'':
        case '\\':
        case '&':
        case '|':
        case '?':
            return i;
        default:
            break;
        }
    }
    return -1;
}

From source file:uk.ac.ebi.intact.dataexchange.psimi.xml.converter.util.PsiConverterUtils.java

public static String forXML(String aText) {
    if (aText == null) {
        return null;
    }/* w ww .j  a v a2s .c  om*/

    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 if (!XMLChar.isInvalid(character)) {
            //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:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Convert the plain text string into something that
 * is a valid HTML/XML text string (i.e. escaping angle brackets,
 * etc.)./*from  w  w  w .  j  av a 2 s  .c  o m*/
 * I owe a lot to the apache project for their source code
 * was an assistance. It is annoying that they don't have
 * this as a public method somewhere, though.
 * @param str the string to escape 
 * @return the string, with some characters converted to XML character entity references
 */
public static String webify(String str) {
    StringBuffer buff = new StringBuffer();
    StringCharacterIterator iter = new StringCharacterIterator(str);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '<':
            buff.append("&lt;");
            break;

        case '>':
            buff.append("&gt;");
            break;

        case '"':
            buff.append("&quot;");
            break;

        case '\'':
            buff.append("&apos;");
            break;

        case '&':
            buff.append("&amp;");
            break;

        default:
            if ((c >= ' ' && c < 0xF7) || c == '\n' || c == '\r' || c == '\t') {
                buff.append(c);
            } else {
                buff.append("&#x").append(Integer.toHexString(c)).append(';');
            }
        }
    }
    return buff.toString();
}

From source file:Base64.java

public static byte[] decodeKreative85(String s) {
    CharacterIterator it = new StringCharacterIterator(s.trim());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;//from  w  w w .j a va  2  s  .co m
    int j = 0;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        int v = k85d(ch);
        if (v >= 0) {
            i = i * 85L + v;
            j++;
            if (j >= 5) {
                out.write((int) (i >> 24L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 8L));
                out.write((int) i);
                i = 0;
                j = 0;
            }
        }
    }
    switch (j) {
    case 4:
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 8L));
        break;
    case 3:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        out.write((int) (i >> 16L));
        break;
    case 2:
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        i = i * 85L + 84L;
        out.write((int) (i >> 24L));
        break;
    }
    return out.toByteArray();
}

From source file:Base64.java

public static byte[] decodeLegacy85(String s) {
    int targetLength = -1;
    s = s.trim();/*  w w w .  j a v  a 2  s. c o  m*/
    if (s.length() >= 7 && s.charAt(0) == '<' && s.charAt(6) == '>') {
        targetLength = k85d(s.charAt(1)) + k85d(s.charAt(2)) * 85 + k85d(s.charAt(3)) * 7225
                + k85d(s.charAt(4)) * 614125 + k85d(s.charAt(5)) * 52200625;
        s = s.substring(7).trim();
    }
    CharacterIterator it = new StringCharacterIterator(s);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    long i = 0;
    int j = 0;
    long k = 1;
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        int v = k85d(ch);
        if (v >= 0) {
            i += v * k;
            j++;
            k *= 85;
            if (j >= 5) {
                out.write((int) i);
                out.write((int) (i >> 8L));
                out.write((int) (i >> 16L));
                out.write((int) (i >> 24L));
                i = 0;
                j = 0;
                k = 1;
            }
        }
    }
    if (j > 0) {
        out.write((int) i);
        out.write((int) (i >> 8L));
        out.write((int) (i >> 16L));
        out.write((int) (i >> 24L));
    }
    if (targetLength >= 0) {
        byte[] b = out.toByteArray();
        byte[] bt = new byte[targetLength];
        for (int x = 0; x < targetLength && x < b.length; x++) {
            bt[x] = b[x];
        }
        return bt;
    } else {
        return out.toByteArray();
    }
}

From source file:org.getobjects.eoaccess.EOAdaptor.java

public boolean escapeIntoBuffer(final StringBuilder _sb, final String _value, final char _quoteChar) {
    if (_value == null)
        return false;

    StringCharacterIterator localParser = new StringCharacterIterator(_value);

    for (char c = localParser.current(); c != CharacterIterator.DONE; c = localParser.next()) {
        if (c == _quoteChar) {
            // TBD: buggy? Just quotes single-quotes?
            _sb.append('\'');
            _sb.append('\'');
        } else if (c == '\\') { // escape backslash with double-backslash (why?)
            _sb.append('\\');
            _sb.append('\\');
        } else//from   w w w .j a va  2  s.c o  m
            _sb.append(c);
    }

    return true;
}

From source file:edu.sdsc.nbcr.opal.OpalClient.java

/**
 *  This function is used to escape illegal character in html
 *//*from  ww w. jav  a 2  s .  com*/
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();
}