Search in sources :

Example 1 with Changes

use of pcgen.rules.context.Changes in project pcgen by PCGen.

the class TypeLst method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject cdo, String value) {
    if (value.startsWith(Constants.LST_DOT_CLEAR)) {
        context.getObjectContext().removeList(cdo, ListKey.TYPE);
        if (value.length() == 6) {
            return ParseResult.SUCCESS;
        } else if (value.charAt(6) == '.') {
            value = value.substring(7);
            if (isEmpty(value)) {
                return new ParseResult.Fail(getTokenName() + "started with .CLEAR. but expected to have a Type after .: " + value, context);
            }
        } else {
            return new ParseResult.Fail(getTokenName() + "started with .CLEAR but expected next character to be .: " + value, context);
        }
    }
    ParseResult pr = checkForIllegalSeparator('.', value);
    if (!pr.passed()) {
        return pr;
    }
    StringTokenizer aTok = new StringTokenizer(value, Constants.DOT);
    boolean bRemove = false;
    boolean bAdd = false;
    while (aTok.hasMoreTokens()) {
        final String aType = aTok.nextToken();
        if ("ADD".equals(aType)) {
            if (bRemove) {
                return new ParseResult.Fail("Non-sensical use of .REMOVE.ADD. in " + getTokenName() + ": " + value, context);
            }
            bRemove = false;
            bAdd = true;
        } else if ("REMOVE".equals(aType)) {
            if (bAdd) {
                return new ParseResult.Fail("Non-sensical use of .ADD.REMOVE. in " + getTokenName() + ": " + value, context);
            }
            bRemove = true;
        } else if ("CLEAR".equals(aType)) {
            return new ParseResult.Fail("Non-sensical use of .CLEAR in " + getTokenName() + ": " + value, context);
        } else if (bRemove) {
            Type type = Type.getConstant(aType);
            context.getObjectContext().removeFromList(cdo, ListKey.TYPE, type);
            bRemove = false;
        } else {
            Type type = Type.getConstant(aType);
            // We want to exclude any duplicates from the type list
            Changes<Type> listChanges = context.getObjectContext().getListChanges(cdo, ListKey.TYPE);
            if (listChanges.getAdded() == null || !listChanges.getAdded().contains(type)) {
                context.getObjectContext().addToList(cdo, ListKey.TYPE, type);
            }
            bAdd = false;
        }
    }
    if (bRemove) {
        return new ParseResult.Fail(getTokenName() + "ended with REMOVE, so didn't have any Type to remove: " + value, context);
    }
    if (bAdd) {
        return new ParseResult.Fail(getTokenName() + "ended with ADD, so didn't have any Type to add: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : Changes(pcgen.rules.context.Changes) StringTokenizer(java.util.StringTokenizer) Type(pcgen.cdom.enumeration.Type) ParseResult(pcgen.rules.persistence.token.ParseResult)

Aggregations

StringTokenizer (java.util.StringTokenizer)1 Type (pcgen.cdom.enumeration.Type)1 Changes (pcgen.rules.context.Changes)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1