use of pcgen.cdom.reference.ObjectMatchingReference in project pcgen by PCGen.
the class CompanionListLst method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(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);
}
StringTokenizer tok = new StringTokenizer(value, LstUtils.PIPE);
String companionType = tok.nextToken();
if (!tok.hasMoreTokens()) {
return new ParseResult.Fail(getTokenName() + " requires more than just a Type: " + value, context);
}
String list = tok.nextToken();
ParseResult pr = checkForIllegalSeparator(',', list);
if (!pr.passed()) {
return pr;
}
StringTokenizer subTok = new StringTokenizer(list, LstUtils.COMMA);
Set<CDOMReference<Race>> races = new HashSet<>();
boolean foundAny = false;
while (subTok.hasMoreTokens()) {
String tokString = subTok.nextToken();
if (Constants.LST_ANY.equalsIgnoreCase(tokString)) {
foundAny = true;
races.add(context.getReferenceContext().getCDOMAllReference(Race.class));
} else if (tokString.startsWith("RACETYPE=")) {
String raceType = tokString.substring(9);
if (raceType.isEmpty()) {
return new ParseResult.Fail(getTokenName() + " Error: RaceType was not specified.", context);
}
races.add(new ObjectMatchingReference<>(tokString, Race.class, context.getReferenceContext().getCDOMAllReference(Race.class), ObjectKey.RACETYPE, RaceType.getConstant(raceType)));
} else if (tokString.startsWith("RACESUBTYPE=")) {
String raceSubType = tokString.substring(12);
if (raceSubType.isEmpty()) {
return new ParseResult.Fail(getTokenName() + " Error: RaceSubType was not specified.", context);
}
races.add(new ListMatchingReference<>(tokString, Race.class, context.getReferenceContext().getCDOMAllReference(Race.class), ListKey.RACESUBTYPE, RaceSubType.getConstant(raceSubType)));
} else if (looksLikeAPrerequisite(tokString)) {
return new ParseResult.Fail(getTokenName() + " Error: " + tokString + " found where companion race expected.", context);
} else {
races.add(context.getReferenceContext().getCDOMReference(Race.class, tokString));
}
}
if (foundAny && races.size() > 1) {
return new ParseResult.Fail("Non-sensical Race List includes Any and specific races: " + value, context);
}
if (!tok.hasMoreTokens()) {
// No other args, so we're done
finish(context, obj, companionType, races, null, null);
return ParseResult.SUCCESS;
}
// The remainder of the elements are optional.
Integer followerAdjustment = null;
String optArg = tok.nextToken();
while (true) {
if (optArg.startsWith(FOLLOWERADJUSTMENT)) {
if (followerAdjustment != null) {
return new ParseResult.Fail(getTokenName() + " Error: Multiple " + FOLLOWERADJUSTMENT + " tags specified.", context);
}
int faStringLength = FOLLOWERADJUSTMENT.length();
if (optArg.length() <= faStringLength + 1) {
return new ParseResult.Fail("Empty FOLLOWERADJUSTMENT value in " + getTokenName() + " is prohibited", context);
}
String adj = optArg.substring(faStringLength + 1);
try {
followerAdjustment = Integer.valueOf(adj);
} catch (NumberFormatException nfe) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Expecting a number for FOLLOWERADJUSTMENT: " + adj);
cpr.addErrorMessage(" was parsing Token " + getTokenName());
return cpr;
}
} else if (looksLikeAPrerequisite(optArg)) {
break;
} else {
return new ParseResult.Fail(getTokenName() + ": Unknown argument (was expecting FOLLOWERADJUSTMENT: or PRExxx): " + optArg, context);
}
if (!tok.hasMoreTokens()) {
// No prereqs, so we're done
finish(context, obj, companionType, races, followerAdjustment, null);
return ParseResult.SUCCESS;
}
optArg = tok.nextToken();
}
List<Prerequisite> prereqs = new ArrayList<>();
while (true) {
Prerequisite prereq = getPrerequisite(optArg);
if (prereq == null) {
return new ParseResult.Fail(" (Did you put items after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
}
prereqs.add(prereq);
if (!tok.hasMoreTokens()) {
break;
}
optArg = tok.nextToken();
}
finish(context, obj, companionType, races, followerAdjustment, prereqs);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.reference.ObjectMatchingReference in project pcgen by PCGen.
the class ClassSkillsLevelTokenTest method testUnparseTrained.
@Test
public void testUnparseTrained() throws PersistenceLayerException {
List<CDOMReference<Skill>> refs = new ArrayList<>();
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>("TRAINED", Skill.class, getAllRef(), ObjectKey.USE_UNTRAINED, Boolean.FALSE);
omr.returnIncludesNulls(true);
refs.add(omr);
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
primaryProf.addToListFor(ListKey.ADD, tc);
tc.setChoiceActor(new ClassSkillChoiceActor(fighter, null));
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getSubTokenName() + '|' + "TRAINED");
}
use of pcgen.cdom.reference.ObjectMatchingReference in project pcgen by PCGen.
the class ClassSkillsLevelTokenTest method testUnparseNonExclusive.
@Test
public void testUnparseNonExclusive() throws PersistenceLayerException {
List<CDOMReference<Skill>> refs = new ArrayList<>();
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>("NONEXCLUSIVE", Skill.class, getAllRef(), ObjectKey.EXCLUSIVE, Boolean.FALSE);
omr.returnIncludesNulls(true);
refs.add(omr);
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
primaryProf.addToListFor(ListKey.ADD, tc);
tc.setChoiceActor(new ClassSkillChoiceActor(fighter, null));
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getSubTokenName() + '|' + "NONEXCLUSIVE");
}
use of pcgen.cdom.reference.ObjectMatchingReference in project pcgen by PCGen.
the class ClassSkillsTokenTest method testUnparseExclusive.
@Test
public void testUnparseExclusive() throws PersistenceLayerException {
List<CDOMReference<Skill>> refs = new ArrayList<>();
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>("EXCLUSIVE", Skill.class, getAllRef(), ObjectKey.EXCLUSIVE, Boolean.TRUE);
omr.returnIncludesNulls(true);
refs.add(omr);
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
primaryProf.addToListFor(ListKey.ADD, tc);
tc.setChoiceActor(new ClassSkillChoiceActor(fighter, null));
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getSubTokenName() + '|' + "EXCLUSIVE");
}
use of pcgen.cdom.reference.ObjectMatchingReference in project pcgen by PCGen.
the class ClassSkillsTokenTest method testUnparseNonExclusive.
@Test
public void testUnparseNonExclusive() throws PersistenceLayerException {
List<CDOMReference<Skill>> refs = new ArrayList<>();
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>("NONEXCLUSIVE", Skill.class, getAllRef(), ObjectKey.EXCLUSIVE, Boolean.FALSE);
omr.returnIncludesNulls(true);
refs.add(omr);
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
primaryProf.addToListFor(ListKey.ADD, tc);
tc.setChoiceActor(new ClassSkillChoiceActor(fighter, null));
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getSubTokenName() + '|' + "NONEXCLUSIVE");
}
Aggregations