use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.
the class ClassSkillsLevelToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClassLevel obj, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String activeValue = sep.next();
Formula count;
if (!sep.hasNext()) {
count = FormulaFactory.ONE;
} else {
count = FormulaFactory.getFormulaFor(activeValue);
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
}
activeValue = sep.next();
}
if (sep.hasNext()) {
return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
}
ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
if (!pr.passed()) {
return pr;
}
List<CDOMReference<Skill>> refs = new ArrayList<>();
StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
CDOMGroupRef<Skill> allRef = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
Integer autoRank = null;
while (tok.hasMoreTokens()) {
String tokText = tok.nextToken();
if (Constants.LST_ALL.equals(tokText) || Constants.LST_ANY.equals(tokText)) {
refs.add(allRef);
} else {
if (Constants.LST_UNTRAINED.equals(tokText)) {
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.TRUE);
omr.returnIncludesNulls(true);
refs.add(omr);
} else if (Constants.LST_TRAINED.equals(tokText)) {
refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.FALSE));
} else if (Constants.LST_EXCLUSIVE.equals(tokText)) {
refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.TRUE));
} else if (Constants.LST_NONEXCLUSIVE.equals(tokText) || Constants.LST_CROSS_CLASS.equals(tokText)) {
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.FALSE);
omr.returnIncludesNulls(true);
refs.add(omr);
} else if (tokText.startsWith("AUTORANK=")) {
if (autoRank != null) {
return new ParseResult.Fail("Cannot have two " + "AUTORANK= items in " + getFullName() + ": " + value, context);
}
String rankString = tokText.substring(9);
try {
autoRank = Integer.decode(rankString);
if (autoRank <= 0) {
return new ParseResult.Fail("Expected AUTORANK= to be" + " greater than zero, found: " + autoRank, context);
}
} catch (NumberFormatException e) {
return new ParseResult.Fail("Expected AUTORANK= to have" + " an integer value, found: " + rankString, context);
}
} else {
CDOMReference<Skill> skref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, tokText);
if (skref == null) {
return new ParseResult.Fail(" Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
}
refs.add(skref);
}
}
}
if (refs.isEmpty()) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no skill reference: " + value, context);
}
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
if (!rcs.getGroupingState().isValid()) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
ChoiceSet<Skill> cs = new ChoiceSet<>(getTokenName(), rcs, true);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
// TODO This is a hack, to get this to work pre-CDOM
PCClass parent = (PCClass) obj.get(ObjectKey.TOKEN_PARENT);
ClassSkillChoiceActor actor = new ClassSkillChoiceActor(parent, autoRank);
tc.setChoiceActor(actor);
context.getObjectContext().addToList(obj, ListKey.ADD, tc);
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.
the class TypeLst method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject cdo, String value) {
if (value.startsWith(Constants.LST_DOT_CLEAR)) {
context.getObjectContext().removeList(cdo, ListKey.TYPE);
if (value.length() == 6) {
return ParseResult.SUCCESS;
} else if (value.charAt(6) == '.') {
value = value.substring(7);
if (isEmpty(value)) {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR. but expected to have a Type after .: " + value, context);
}
} else {
return new ParseResult.Fail(getTokenName() + "started with .CLEAR but expected next character to be .: " + value, context);
}
}
ParseResult pr = checkForIllegalSeparator('.', value);
if (!pr.passed()) {
return pr;
}
StringTokenizer aTok = new StringTokenizer(value, Constants.DOT);
boolean bRemove = false;
boolean bAdd = false;
while (aTok.hasMoreTokens()) {
final String aType = aTok.nextToken();
if ("ADD".equals(aType)) {
if (bRemove) {
return new ParseResult.Fail("Non-sensical use of .REMOVE.ADD. in " + getTokenName() + ": " + value, context);
}
bRemove = false;
bAdd = true;
} else if ("REMOVE".equals(aType)) {
if (bAdd) {
return new ParseResult.Fail("Non-sensical use of .ADD.REMOVE. in " + getTokenName() + ": " + value, context);
}
bRemove = true;
} else if ("CLEAR".equals(aType)) {
return new ParseResult.Fail("Non-sensical use of .CLEAR in " + getTokenName() + ": " + value, context);
} else if (bRemove) {
Type type = Type.getConstant(aType);
context.getObjectContext().removeFromList(cdo, ListKey.TYPE, type);
bRemove = false;
} else {
Type type = Type.getConstant(aType);
// We want to exclude any duplicates from the type list
Changes<Type> listChanges = context.getObjectContext().getListChanges(cdo, ListKey.TYPE);
if (listChanges.getAdded() == null || !listChanges.getAdded().contains(type)) {
context.getObjectContext().addToList(cdo, ListKey.TYPE, type);
}
bAdd = false;
}
}
if (bRemove) {
return new ParseResult.Fail(getTokenName() + "ended with REMOVE, so didn't have any Type to remove: " + value, context);
}
if (bAdd) {
return new ParseResult.Fail(getTokenName() + "ended with ADD, so didn't have any Type to add: " + value, context);
}
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.
the class VisibleToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Ability ability, String value) {
Visibility vis;
if (value.equals("YES")) {
vis = Visibility.DEFAULT;
} else if (value.equals("DISPLAY")) {
vis = Visibility.DISPLAY_ONLY;
} else if (value.equals("EXPORT")) {
vis = Visibility.OUTPUT_ONLY;
} else if (value.equals("NO")) {
vis = Visibility.HIDDEN;
} else {
return new ParseResult.Fail("Unable to understand " + getTokenName() + " tag: " + value, context);
}
context.getObjectContext().put(ability, ObjectKey.VISIBILITY, vis);
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.
the class ModifyLst method finishProcessing.
private <T> ParseResult finishProcessing(LoadContext context, CDOMObject obj, FormatManager<T> formatManager, String varName, String modIdentification, String modInstructions, int priorityNumber) {
ScopeInstance scopeInst = context.getActiveScope();
LegalScope scope = scopeInst.getLegalScope();
PCGenModifier<T> modifier;
try {
modifier = context.getVariableContext().getModifier(modIdentification, modInstructions, priorityNumber, scope, formatManager);
} catch (IllegalArgumentException iae) {
return new ParseResult.Fail(getTokenName() + " Modifier " + modIdentification + " had value " + modInstructions + " but it was not valid: " + iae.getMessage(), context);
}
VarModifier<T> vm = new VarModifier<>(varName, scope, modifier);
context.getObjectContext().addToList(obj, ListKey.MODIFY, vm);
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.
the class MoveLst 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 moves = new StringTokenizer(value, Constants.COMMA);
Movement cm;
if (moves.countTokens() == 1) {
cm = new Movement(1);
String mod = moves.nextToken();
ParseResult pr = validateMove(value, mod);
if (!pr.passed()) {
return pr;
}
cm.assignMovement(0, "Walk", mod);
} else {
cm = new Movement(moves.countTokens() / 2);
int x = 0;
while (moves.countTokens() > 1) {
String type = moves.nextToken();
String mod = moves.nextToken();
ParseResult pr = validateMove(value, mod);
if (!pr.passed()) {
return pr;
}
cm.assignMovement(x++, type, mod);
}
if (moves.countTokens() != 0) {
return new ParseResult.Fail("Badly formed MOVE token " + "(extra value at end of list): " + value, context);
}
}
cm.setMoveRatesFlag(0);
context.getObjectContext().addToList(obj, ListKey.MOVEMENT, cm);
return ParseResult.SUCCESS;
}
Aggregations