use of pcgen.cdom.choiceset.ReferenceChoiceSet in project pcgen by PCGen.
the class KitLst 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);
}
if (obj instanceof NonInteractive) {
return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Non-Interactive object type: " + obj.getClass().getSimpleName(), context);
}
StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
Formula count = FormulaFactory.getFormulaFor(tok.nextToken());
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (!count.isStatic()) {
return new ParseResult.Fail("Count in " + getTokenName() + " must be a number", context);
}
if (count.resolveStatic().intValue() <= 0) {
return new ParseResult.Fail("Count in " + getTokenName() + " must be > 0", context);
}
if (!tok.hasMoreTokens()) {
return new ParseResult.Fail(getTokenName() + " must have a | separating " + "count from the list of possible values: " + value, context);
}
List<CDOMReference<Kit>> refs = new ArrayList<>();
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
CDOMReference<Kit> ref;
if (Constants.LST_ALL.equals(token)) {
ref = context.getReferenceContext().getCDOMAllReference(KIT_CLASS);
} else {
ref = context.getReferenceContext().getCDOMReference(KIT_CLASS, token);
}
refs.add(ref);
}
ReferenceChoiceSet<Kit> rcs = new ReferenceChoiceSet<>(refs);
if (!rcs.getGroupingState().isValid()) {
return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
}
ChoiceSet<Kit> cs = new ChoiceSet<>(getTokenName(), new QualifiedDecorator<>(rcs));
cs.setTitle("Kit Selection");
TransitionChoice<Kit> tc = new ConcreteTransitionChoice<>(cs, count);
context.getObjectContext().addToList(obj, ListKey.KIT_CHOICE, tc);
tc.setRequired(false);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
Aggregations