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);
}
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);
}
}
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;
}
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;
}
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;
}
Aggregations