Search in sources :

Example 96 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class CharacterAbilitiesTest method testRebuildAbilityListsMult.

/**
	 * Test method for {@link pcgen.gui2.facade.CharacterAbilities#rebuildAbilityLists()}.
	 */
@Test
public final void testRebuildAbilityListsMult() {
    PlayerCharacter pc = getCharacter();
    CharacterAbilities ca = new CharacterAbilities(pc, uiDelegate, dataset, todoManager);
    ca.rebuildAbilityLists();
    ListFacade<AbilityCategoryFacade> categories = ca.getActiveAbilityCategories();
    assertNotNull("Categories should not be null", categories);
    assertTrue("Feat should be active", categories.containsElement(AbilityCategory.FEAT));
    ListFacade<AbilityFacade> abilities = ca.getAbilities(AbilityCategory.FEAT);
    assertNotNull("Feat list should not be null", abilities);
    assertTrue("Feat list should be empty", abilities.isEmpty());
    // Add an entry - note rebuild is implicit
    Ability reading = TestHelper.makeAbility("reading", AbilityCategory.FEAT, "interest");
    reading.put(ObjectKey.MULTIPLE_ALLOWED, Boolean.TRUE);
    StringToken st = new plugin.lsttokens.choose.StringToken();
    ParseResult pr = st.parseToken(Globals.getContext(), reading, "STRING|Magazines|Books");
    assertTrue(pr.passed());
    Globals.getContext().commit();
    applyAbility(pc, AbilityCategory.FEAT, reading, "Books");
    abilities = ca.getAbilities(AbilityCategory.FEAT);
    assertFalse("Feat list should not be empty", abilities.isEmpty());
    Ability abilityFromList = (Ability) abilities.getElementAt(0);
    assertEquals("Should have found reading", reading, abilityFromList);
    assertEquals("Feat list should have one entry", 1, abilities.getSize());
    // Now add the choice
    pcgenFinalize(abilityFromList, "Magazines", pc, AbilityCategory.FEAT);
    ca.rebuildAbilityLists();
    abilities = ca.getAbilities(AbilityCategory.FEAT);
    assertEquals("Feat list should have one entry", 1, abilities.getSize());
    abilityFromList = (Ability) abilities.getElementAt(0);
    assertEquals("Should have found reading", reading, abilityFromList);
}
Also used : Ability(pcgen.core.Ability) PlayerCharacter(pcgen.core.PlayerCharacter) ParseResult(pcgen.rules.persistence.token.ParseResult) AbilityCategoryFacade(pcgen.facade.core.AbilityCategoryFacade) AbilityFacade(pcgen.facade.core.AbilityFacade) StringToken(plugin.lsttokens.choose.StringToken) Test(org.junit.Test)

Example 97 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class DefineLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    if (firstItem.startsWith("UNLOCK.")) {
        return new ParseResult.Fail("DEFINE:UNLOCK. has been deprecated, " + "please use DEFINESTAT:STAT| or DEFINESTAT:UNLOCK|", context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " varName|varFormula" + "or LOCK.<stat>|value syntax requires an argument", context);
    }
    String var = firstItem;
    if (var.isEmpty()) {
        return new ParseResult.Fail("Empty Variable Name found in " + getTokenName() + ": " + value, context);
    }
    try {
        Formula f = FormulaFactory.getFormulaFor(sep.next());
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
        if ((!f.isStatic() || f.resolveStatic().intValue() != 0) && !(var.startsWith("MAXLEVELSTAT="))) {
            Logging.deprecationPrint("DEFINE with a non zero value has been deprecated, " + "please use a DEFINE of 0 and an appropriate bonus. Tag was DEFINE:" + value + " in " + obj, context);
        }
        if (sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + ' ' + firstItem + " syntax requires only one argument: " + value, context);
        }
        if (value.startsWith("LOCK.")) {
            return new ParseResult.Fail("DEFINE:LOCK. has been deprecated, " + "please use DEFINESTAT:LOCL| or DEFINESTAT:NONSTAT|", context);
        } else {
            context.getObjectContext().put(obj, VariableKey.getConstant(var), f);
        }
        return ParseResult.SUCCESS;
    } catch (IllegalArgumentException e) {
        return new ParseResult.Fail("Illegal Formula found in " + getTokenName() + ": " + value + ' ' + e.getLocalizedMessage(), context);
    }
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) Ungranted(pcgen.cdom.base.Ungranted)

Example 98 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class DefineStatLst method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (obj instanceof Ungranted) {
        return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + obj.getClass().getSimpleName(), context);
    }
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + " may not be empty", context);
    }
    String firstItem = sep.next();
    DefineStatSubToken subToken;
    try {
        subToken = DefineStatSubToken.valueOf(firstItem);
    } catch (IllegalArgumentException e1) {
        return new ParseResult.Fail("Found unexpected sub tag " + firstItem + " in " + getTokenName() + Constants.COLON + value + ". Must be one of " + StringUtils.join(DefineStatSubToken.values(), ", ") + Constants.DOT, context);
    }
    if (!sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by a stat.", context);
    }
    String statKey = sep.next();
    CDOMSingleRef<PCStat> stat = context.getReferenceContext().getCDOMReference(PCSTAT_CLASS, statKey);
    Formula f = null;
    if (subToken == DefineStatSubToken.LOCK || subToken == DefineStatSubToken.MINVALUE || subToken == DefineStatSubToken.MAXVALUE) {
        if (!sep.hasNext()) {
            return new ParseResult.Fail(getTokenName() + Constants.COLON + subToken + "| must be followed by both a stat and a value.", context);
        }
        String formula = sep.next();
        f = FormulaFactory.getFormulaFor(formula);
        if (!f.isValid()) {
            return new ParseResult.Fail("Formula in " + getTokenName() + " was not valid: " + f.toString(), context);
        }
    }
    if (sep.hasNext()) {
        return new ParseResult.Fail(getTokenName() + Constants.COLON + value + " has too many pipe separated item.", context);
    }
    switch(subToken) {
        case LOCK:
            context.getObjectContext().addToList(obj, ListKey.STAT_LOCKS, new StatLock(stat, f));
            break;
        case UNLOCK:
            context.getObjectContext().addToList(obj, ListKey.UNLOCKED_STATS, stat);
            break;
        case NONSTAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_STATS, stat);
            break;
        case STAT:
            context.getObjectContext().addToList(obj, ListKey.NONSTAT_TO_STAT_STATS, stat);
            break;
        case MINVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MINVALUE, new StatLock(stat, f));
            break;
        case MAXVALUE:
            context.getObjectContext().addToList(obj, ListKey.STAT_MAXVALUE, new StatLock(stat, f));
            break;
    }
    return ParseResult.SUCCESS;
}
Also used : Formula(pcgen.base.formula.Formula) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult) PCStat(pcgen.core.PCStat) Ungranted(pcgen.cdom.base.Ungranted) StatLock(pcgen.cdom.helper.StatLock)

Example 99 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class InfoLst method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject cdo, String value) {
    if (value.charAt(0) == '|') {
        return new ParseResult.Fail(getTokenName() + " arguments may not start with PIPE : " + value);
    }
    if (value.charAt(value.length() - 1) == '|') {
        return new ParseResult.Fail(getTokenName() + " arguments may not end with PIPE : " + value);
    }
    int pipeLoc = value.indexOf(Constants.PIPE);
    if (pipeLoc == -1) {
        return new ParseResult.Fail(getTokenName() + " expecting '|', format is: InfoName|Info value was: " + value, context);
    }
    String key = value.substring(0, pipeLoc);
    //key length 0 caught by charAt(0) test above
    String val = value.substring(pipeLoc + 1);
    if (val.isEmpty()) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty value, " + "format is: InfoName|Info value was: " + value, context);
    }
    if (val.startsWith(Constants.PIPE)) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty value, " + "format is: InfoName|Info value was: " + value, context);
    }
    try {
        MessageFormat mf = new MessageFormat(val);
        CaseInsensitiveString cis = new CaseInsensitiveString(key);
        context.getObjectContext().put(cdo, MapKey.INFO, cis, mf);
    } catch (IllegalArgumentException e) {
        return new ParseResult.Fail(getTokenName() + " expected a valid MessageFormat, but received error: " + e.getMessage() + " when parsing: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : MessageFormat(java.text.MessageFormat) ParseResult(pcgen.rules.persistence.token.ParseResult) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 100 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class StattablehiddencolumnsToken method parseTokenWithSeparator.

@Override
public ParseResult parseTokenWithSeparator(LoadContext context, TabInfo ti, String value) {
    if (!Tab.SUMMARY.equals(ti.getTab())) {
        return new ParseResult.Fail(getTokenName() + " may only be used on the " + Tab.SUMMARY + " Tab", context);
    }
    ti.clearHiddenColumns();
    StringTokenizer st = new StringTokenizer(value, Constants.COMMA);
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        try {
            ti.hideColumn(Integer.valueOf(token));
        } catch (NumberFormatException nfe) {
            return new ParseResult.Fail(getTokenName() + " misunderstood Integer: " + token + " in " + value, context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Aggregations

ParseResult (pcgen.rules.persistence.token.ParseResult)210 StringTokenizer (java.util.StringTokenizer)68 Test (org.junit.Test)45 CDOMReference (pcgen.cdom.base.CDOMReference)30 AbstractTokenModelTest (tokenmodel.testsupport.AbstractTokenModelTest)26 Ability (pcgen.core.Ability)21 ArrayList (java.util.ArrayList)18 Formula (pcgen.base.formula.Formula)18 ParsingSeparator (pcgen.base.text.ParsingSeparator)18 Prerequisite (pcgen.core.prereq.Prerequisite)18 PCClass (pcgen.core.PCClass)17 Ungranted (pcgen.cdom.base.Ungranted)15 PCTemplate (pcgen.core.PCTemplate)14 CNAbility (pcgen.cdom.content.CNAbility)13 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)11 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)11 BigDecimal (java.math.BigDecimal)10 Race (pcgen.core.Race)10 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)9 LegalScope (pcgen.base.formula.base.LegalScope)8