Search in sources :

Example 46 with ParseResult

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

the class ValuesToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, KitTable kitTable, String value) {
    ParsingSeparator sep = new ParsingSeparator(value, '|');
    sep.addGroupingPair('[', ']');
    sep.addGroupingPair('(', ')');
    while (sep.hasNext()) {
        String thing = sep.next();
        if (thing.isEmpty()) {
            return new ParseResult.Fail(getTokenName() + " arguments has invalid pipe separator: " + value, context);
        }
        KitGear optionInfo = new KitGear();
        for (String s : thing.split("[\\[\\]]")) {
            if (s.isEmpty()) {
                continue;
            }
            int colonLoc = s.indexOf(':');
            if (colonLoc == -1) {
                return new ParseResult.Fail("Expected colon in Value item: " + s + " within: " + value, context);
            }
            String key = s.substring(0, colonLoc);
            String thingValue = s.substring(colonLoc + 1);
            try {
                boolean passed = context.processToken(optionInfo, key, thingValue);
                if (!passed) {
                    return new ParseResult.Fail("Failure in token: " + key, context);
                }
            } catch (PersistenceLayerException e) {
                return new ParseResult.Fail("Failure in token: " + key + " " + e.getMessage(), context);
            }
        }
        if (!sep.hasNext()) {
            return new ParseResult.Fail("Odd token count in Value: " + value, context);
        }
        String range = sep.next();
        if (!processRange(kitTable, optionInfo, range)) {
            return new ParseResult.Fail("Invalid Range in Value: " + range + " within " + value, context);
        }
    }
    return ParseResult.SUCCESS;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) KitGear(pcgen.core.kit.KitGear) ParsingSeparator(pcgen.base.text.ParsingSeparator) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 47 with ParseResult

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

the class LoadmultToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, LoadInfo info, String value) {
    try {
        BigDecimal mult = new BigDecimal(value);
        if (mult.compareTo(BigDecimal.ZERO) <= 0) {
            return new ParseResult.Fail(getTokenName() + " requires a positive load multiplier, found : " + value, context);
        }
        info.setLoadScoreMultiplier(mult);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        return new ParseResult.Fail("Misunderstood Double in Tag: " + value, context);
    }
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) BigDecimal(java.math.BigDecimal)

Example 48 with ParseResult

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

the class AbilityToken method parseToken.

@Override
public ParseResult parseToken(LoadContext context, KitLevelAbility kitAbility, String value) {
    if (!value.startsWith("PROMPT:")) {
        return new ParseResult.Fail("Expected " + getTokenName() + " to start with PROMPT: " + value, context);
    }
    StringTokenizer st = new StringTokenizer(value, Constants.PIPE);
    String first = st.nextToken();
    PersistentTransitionChoice<?> ptc;
    try {
        ptc = Compatibility.processOldAdd(context, first);
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail(e.getMessage(), context);
    }
    if (ptc == null) {
        return new ParseResult.Fail("Error was in " + getTokenName() + ' ' + value, context);
    }
    kitAbility.setAdd(ptc);
    while (st.hasMoreTokens()) {
        String choiceString = st.nextToken();
        if (!choiceString.startsWith("CHOICE:")) {
            return new ParseResult.Fail("Expected " + getTokenName() + " choice string to start with CHOICE: " + value, context);
        }
        String choice = choiceString.substring(7);
        if (first.equals("FEAT") && !choice.startsWith("CATEGORY=")) {
            /*
				 * In the case of FEAT, need to provide the context (since
				 * persistence assumes this CATEGORY= exists)
				 */
            choice = "CATEGORY=FEAT|" + choice;
        }
        /*
			 * TODO This is load order dependent, this really should be storing
			 * references into kitAbility, not a String - thpr Dec 8 2012
			 */
        if (ptc.decodeChoice(context, choice) == null) {
            return new ParseResult.Fail(choiceString + " is not a valid selection for ADD:" + first, context);
        }
        kitAbility.addChoice(choice);
    }
    return ParseResult.SUCCESS;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult)

Example 49 with ParseResult

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

the class CcskillToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass obj, String value) {
    boolean first = true;
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    while (tok.hasMoreTokens()) {
        String tokText = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tokText)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(obj, ListKey.LOCALCCSKILL);
        } else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
            String clearText = tokText.substring(7);
            if (Constants.LST_ALL.equals(clearText)) {
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                CDOMReference<Skill> ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, clearText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().removeFromList(obj, ListKey.LOCALCCSKILL, ref);
            }
        } else {
            /*
				 * Note this HAS to be done one-by-one, because the
				 * .clearChildNodeOfClass method above does NOT recognize the
				 * C/CC Skill object and therefore doesn't know how to search
				 * the sublists
				 */
            if (Constants.LST_ALL.equals(tokText)) {
                foundAny = true;
                context.getObjectContext().addToList(obj, ListKey.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
            } else {
                foundOther = true;
                CDOMReference<Skill> ref = getSkillReference(context, tokText);
                if (ref == null) {
                    return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
                }
                context.getObjectContext().addToList(obj, ListKey.LOCALCCSKILL, ref);
            }
        }
        first = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 50 with ParseResult

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

the class Gui2InfoFactoryTest method testGetChoices.

/**
	 * Test the getChoices method with text choices. 
	 */
public void testGetChoices() {
    PlayerCharacter pc = getCharacter();
    Gui2InfoFactory ca = new Gui2InfoFactory(pc);
    Ability choiceAbility = TestHelper.makeAbility("Skill Focus", AbilityCategory.FEAT, "General");
    choiceAbility.put(ObjectKey.MULTIPLE_ALLOWED, Boolean.TRUE);
    StringToken st = new plugin.lsttokens.choose.StringToken();
    ParseResult pr = st.parseToken(Globals.getContext(), choiceAbility, "SKILL|Perception|Acrobatics");
    assertTrue(pr.passed());
    Globals.getContext().commit();
    pcgenFinalize(choiceAbility, "Perception", pc, AbilityCategory.FEAT);
    assertEquals("Incorrect single choice", "Perception", ca.getChoices(choiceAbility));
    pcgenFinalize(choiceAbility, "Acrobatics", pc, AbilityCategory.FEAT);
    assertEquals("Incorrect multiple choice", "Acrobatics, Perception", ca.getChoices(choiceAbility));
}
Also used : Ability(pcgen.core.Ability) PlayerCharacter(pcgen.core.PlayerCharacter) ParseResult(pcgen.rules.persistence.token.ParseResult) StringToken(plugin.lsttokens.choose.StringToken)

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