Example usage for java.util Vector insertElementAt

List of usage examples for java.util Vector insertElementAt

Introduction

In this page you can find the example usage for java.util Vector insertElementAt.

Prototype

public synchronized void insertElementAt(E obj, int index) 

Source Link

Document

Inserts the specified object as a component in this vector at the specified index .

Usage

From source file:tvbrowser.core.search.booleansearch.BooleanSearcher.java

private static IMatcher getMatcher(Vector<Object> part, boolean caseSensitive,
        Hashtable<String, Object> matcherTable) throws ParserException {
    boolean lastWasMatch = false;
    for (int i = 0; i < part.size(); i++) {
        Object o = part.get(i);/*from w w w  . j a  v a  2  s  . c  o m*/
        if (o instanceof String) {
            String s = (String) o;
            if (!isKeyWord(s)) {
                if (lastWasMatch) {
                    Object Otemp = part.get(i - 1);
                    if (Otemp instanceof StringMatcher) {
                        StringMatcherRegEx ME = new StringMatcherRegEx(((StringMatcher) Otemp).toString(), s,
                                caseSensitive, matcherTable);
                        part.set(i - 1, ME);
                    } else {
                        StringMatcherRegEx ME = (StringMatcherRegEx) Otemp;
                        ME.addPart(s);
                    }
                    part.remove(i);
                    i--;
                    continue;
                }
                StringMatcher m = new StringMatcher(s, caseSensitive, matcherTable);
                part.set(i, m);
                lastWasMatch = true;
                continue;
            }
        }
        if (o instanceof Vector) {
            if (lastWasMatch) {
                part.insertElementAt("AND", i);
                i = 0;
                continue;
            }
        }
        lastWasMatch = false;
    }

    for (int i = 0; i < part.size(); i++) {
        Object O = part.get(i);
        if (O instanceof Vector) {
            @SuppressWarnings("unchecked")
            Vector<Object> v = (Vector<Object>) O;
            part.set(i, getMatcher(v, caseSensitive, matcherTable));
        }
    }

    boolean found = true;
    while (found) {
        found = false;
        for (int i = 0; i < part.size(); i++) {
            Object O = part.get(i);
            if ((O instanceof String) && (O.toString().equals("NOT"))) {
                if (i + 1 >= part.size()) {
                    throw new ParserException(
                            mLocalizer.msg("unexpectedEndOfInput", "Unexpected end of input"));
                }
                Object O2 = expect(part, i + 1, IMatcher.class, mLocalizer.msg("expression", "expression"));
                NotMatcher n = new NotMatcher((IMatcher) O2);
                part.remove(i);
                part.remove(i);

                /*
                 * If the previous Element is not "AND" insert an "AND"-Element
                 */
                if ((i > 0) && !(part.get(i - 1) instanceof AndMatcher)
                        && !((part.get(i - 1) instanceof String) && ((String) part.get(i - 1)).equals("AND"))) {
                    part.insertElementAt("AND", i);
                    i++;
                }
                part.insertElementAt(n, i);
                found = true;
                break;
            }
        }
    }

    found = true;
    while (found) {
        found = false;
        for (int i = 0; i < part.size(); i++) {
            Object O = part.get(i);
            if ((O instanceof String) && ((O.toString().equals("AND")) || ((O.toString().equals("&&"))))) {

                if (i <= 0) {
                    throw new ParserException(
                            mLocalizer.msg("missingExprBeforeAND", "Missing expression before 'AND'"));
                }
                if (i + 1 >= part.size()) {
                    throw new ParserException(
                            mLocalizer.msg("unexpectedEndOfInput", "Unexpected end of input"));
                }
                IMatcher O2 = (IMatcher) expect(part, i - 1, IMatcher.class,
                        mLocalizer.msg("expression", "expression"));
                IMatcher O1 = (IMatcher) expect(part, i + 1, IMatcher.class,
                        mLocalizer.msg("expression", "expression"));
                AndMatcher a = new AndMatcher(O1, O2);
                part.remove(i - 1);
                part.remove(i - 1);
                part.remove(i - 1);
                part.insertElementAt(a, i - 1);
                found = true;
                break;
            }
        }
    }

    found = true;
    while (found) {
        found = false;
        for (int i = 0; i < part.size(); i++) {
            Object O = part.get(i);
            if ((O instanceof String) && ((O.toString().equals("OR")) || (O.toString().equals("||")))) {
                if (i <= 0) {
                    throw new ParserException("Missing expression before \"OR\"");
                }
                if (i + 1 >= part.size()) {
                    throw new ParserException("Unexpected end of input");
                }
                IMatcher O2 = (IMatcher) expect(part, i - 1, IMatcher.class,
                        mLocalizer.msg("expression", "expression"));
                IMatcher O1 = (IMatcher) expect(part, i + 1, IMatcher.class,
                        mLocalizer.msg("expression", "expression"));
                OrMatcher a = new OrMatcher(O1, O2);
                part.remove(i - 1);
                part.remove(i - 1);
                part.remove(i - 1);
                part.insertElementAt(a, i - 1);
                found = true;
                break;
            }
        }
    }
    return (IMatcher) part.get(0);
}