Package java.text

Examples of java.text.CharacterIterator


     */
    public int preceding(int offset) {
        // if we start by updating the current iteration position to the
        // position specified by the caller, we can just use previous()
        // to carry out this operation
        CharacterIterator text = getText();
  checkOffset(offset, text);
  text.setIndex(offset);
        return previous();
    }
View Full Code Here


     * or after "offset".
     * @param offset the offset to check.
     * @return True if "offset" is a boundary position.
     */
    public boolean isBoundary(int offset) {
        CharacterIterator text = getText();
        checkOffset(offset, text);
        if (offset == text.getBeginIndex()) {
            return true;
        }

        // to check whether this is a boundary, we can use following() on the
        // position before the specified one and return true if the position we
View Full Code Here

     * of the text or the state machine transitions to state 0.  We update our return
     * value every time the state machine passes through a possible end state.
     */
    protected int handleNext() {
        // if we're already at the end of the text, return DONE.
        CharacterIterator text = getText();
        if (text.getIndex() == text.getEndIndex()) {
            return BreakIterator.DONE;
        }

        // no matter what, we always advance at least one character forward
        int result = getNextIndex();
        int lookaheadResult = 0;

        // begin in state 1
        int state = START_STATE;
        int category;
        int c = getCurrent();

        // loop until we reach the end of the text or transition to state 0
        while (c != CharacterIterator.DONE && state != STOP_STATE) {

            // look up the current character's character category (which tells us
            // which column in the state table to look at)
            category = lookupCategory(c);

            // if the character isn't an ignore character, look up a state
            // transition in the state table
            if (category != IGNORE) {
                state = lookupState(state, category);
            }

            // if the state we've just transitioned to is a lookahead state,
            // (but not also an end state), save its position.  If it's
            // both a lookahead state and an end state, update the break position
            // to the last saved lookup-state position
            if (lookaheadStates[state]) {
                if (endStates[state]) {
                    result = lookaheadResult;
                }
                else {
                    lookaheadResult = getNextIndex();
                }
            }

            // otherwise, if the state we've just transitioned to is an accepting
            // state, update the break position to be the current iteration position
            else {
                if (endStates[state]) {
                    result = getNextIndex();
                }
            }

            c = getNext();
        }

        // if we've run off the end of the text, and the very last character took us into
        // a lookahead state, advance the break position to the lookahead position
        // (the theory here is that if there are no characters at all after the lookahead
        // position, that always matches the lookahead criteria)
        if (c == CharacterIterator.DONE && lookaheadResult == text.getEndIndex()) {
            result = lookaheadResult;
        }

        text.setIndex(result);
        return result;
    }
View Full Code Here

     * The various calling methods then iterate forward from this safe position to
     * the appropriate position to return(For more information, see the description
     * of buildBackwardsStateTable() in RuleBasedBreakIterator.Builder.)
     */
    protected int handlePrevious() {
        CharacterIterator text = getText();
        int state = START_STATE;
        int category = 0;
        int lastCategory = 0;
        int c = getCurrent();

        // loop until we reach the beginning of the text or transition to state 0
        while (c != CharacterIterator.DONE && state != STOP_STATE) {

            // save the last character's category and look up the current
            // character's category
            lastCategory = category;
            category = lookupCategory(c);

            // if the current character isn't an ignore character, look up a
            // state transition in the backwards state table
            if (category != IGNORE) {
                state = lookupBackwardState(state, category);
            }

            // then advance one character backwards
            c = getPrevious();
        }

        // if we didn't march off the beginning of the text, we're either one or two
        // positions away from the real break position.  (One because of the call to
        // previous() at the end of the loop above, and another because the character
        // that takes us into the stop state will always be the character BEFORE
        // the break position.)
        if (c != CharacterIterator.DONE) {
            if (lastCategory != IGNORE) {
                getNext();
                getNext();
            }
            else {
                getNext();
            }
        }
        return text.getIndex();
    }
View Full Code Here

            }
            catch(CloneNotSupportedException e) {
                throw new Error("Clone not supported: " + e);
            }

            CharacterIterator copyOfBase = (CharacterIterator) base.clone();
            copy.base = copyOfBase;
            return copy;
        }
View Full Code Here

     * @param str The expression string
     * @return The expression parts
     */
    public static String[] getExpressionsFromString(String str)
    {
        CharacterIterator ci = new StringCharacterIterator(str);
        int braces = 0;
        String text = "";
        ArrayList exprList = new ArrayList();
        while( ci.getIndex() != ci.getEndIndex() )
        {
            char c = ci.current();
            if( c == ',' && braces == 0)
            {
                exprList.add(text);
                text = "";
            }
            else if( c == '(' )
            {
                braces++;
                text += c;
            }
            else if( c == ')' )
            {
                braces--;
                text += c;
            }
            else
            {
                text += c;
            }
            ci.next();
        }
        exprList.add(text);
        return (String[])exprList.toArray(new String[exprList.size()]);
    }
View Full Code Here

      if (getPixelWidth(candidate) <= width) {
        // check for line breaks within the provided text
        // unfortunately, the BreakIterators are too dumb to tell *why* they consider the
        // location a break, so the check needs to be implemented here
        final CharacterIterator cit = iter.getText();
        if (isHardLineBreak(cit)) {
          lines.add(candidate);
          previous = iter.current();
          best = null;
        } else {
          best = candidate;
        }
      } else {
        if (best == null) {
          // could not break the line - the word's simply too long. Use more force to
          // to fit it to the width
          best = splitAggressively(candidate, width);
          // splitAggressively returns an iterator with its own indexing,
          // so instead of using it directly we need to adjust the old one
          previous += best.getEndIndex() - best.getBeginIndex();
        } else {
          previous = best.getEndIndex();
          // Trim the trailing white space
          char endChar = best.last();

          int endIndex = previous;
          while (Character.isWhitespace(endChar) && endChar != CharacterIterator.DONE) {
            endIndex = best.getIndex();
            endChar = best.previous();
          }

          best = text.getIterator(null, best.getBeginIndex(), endIndex);
        }

        lines.add(best);

        // a special check for a hard line break just after the word
        // that got moved to the next line
        final CharacterIterator cit = iter.getText();
        if (isHardLineBreak(cit)) {
          lines.add(text.getIterator(null, previous, iter.current()));
          previous = iter.current();
        }
       
View Full Code Here

     * @param str The expression string
     * @return The expression parts
     */
    public static String[] getExpressionsFromString(String str)
    {
        CharacterIterator ci = new StringCharacterIterator(str);
        int braces = 0;
        String text = "";
        ArrayList exprList = new ArrayList();
        while (ci.getIndex() != ci.getEndIndex())
        {
            char c = ci.current();
            if (c == ',' && braces == 0)
            {
                exprList.add(text);
                text = "";
            }
            else if (c == '(')
            {
                braces++;
                text += c;
            }
            else if (c == ')')
            {
                braces--;
                text += c;
            }
            else
            {
                text += c;
            }
            ci.next();
        }
        exprList.add(text);
        return (String[])exprList.toArray(new String[exprList.size()]);
    }
View Full Code Here

    private Parameter[] parse(String pattern) {

        List<Parameter> parameterList = new ArrayList<Parameter>();
        StringBuilder buf = new StringBuilder();

        CharacterIterator sr = new StringCharacterIterator(pattern);

        for (int c = sr.first(); c != CharacterIterator.DONE; c = sr.next()) {
            if (c == '%') {
                int c1 = sr.next();
                if (c1 != '%') {
                    if (buf.length() > 0) {
                        Parameter text = new PlainTextParameter(buf.toString());
                        parameterList.add(text);
                        buf.setLength(0);
View Full Code Here

         * subst = pat '^' repl '^' EOL .
         * regex = str pat str repl str EOL .
         * </pre>
         */

        final CharacterIterator ci = new StringCharacterIterator(commandLine.toString());

        String event;
        char c = ci.current();
        if (c == '!') {
            c = ci.next();
            if (c == '!') {
                event = this.commands.getLast();
                ci.next();
            } else if ((c >= '0' && c <= '9') || c == '-') {
                event = getCommand(ci);
            } else if (c == '?') {
                event = findContains(ci);
                ci.next();
            } else {
                ci.previous();
                event = findStartsWith(ci);
            }
        } else if (c == '^') {
            event = subst(ci, c, false, this.commands.getLast());
        } else {
            throw new IllegalArgumentException(commandLine + ": Unsupported event");
        }

        if (ci.current() == ':') {
            c = ci.next();
            boolean global = (c == 'a' || c == 'g');
            if (global) {
                c = ci.next();
            }
            if (c == 's') {
                event = subst(ci, ci.next(), global, event);
            }
        }

        return event;
    }
View Full Code Here

TOP

Related Classes of java.text.CharacterIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.