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