use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class LevelToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCTemplate template, String value) {
StringTokenizer tok = new StringTokenizer(value, Constants.COLON);
String levelStr = tok.nextToken();
int plusLoc = levelStr.indexOf('+');
if (plusLoc == 0) {
return new ParseResult.Fail("Malformed " + getTokenName() + " Level cannot start with +: " + value, context);
}
int lvl;
try {
/*
* Note this test of integer (even if it doesn't get used outside
* this try) is necessary for catching errors.
*/
lvl = Integer.parseInt(levelStr);
if (lvl <= 0) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Malformed " + getTokenName() + " Token (Level was <= 0): " + lvl);
cpr.addErrorMessage(" Line was: " + value);
return cpr;
}
} catch (NumberFormatException ex) {
return new ParseResult.Fail("Misunderstood Level value: " + levelStr + " in " + getTokenName(), context);
}
if (!tok.hasMoreTokens()) {
return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has one): " + value, context);
}
String typeStr = tok.nextToken();
if (!tok.hasMoreTokens()) {
return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has two): " + value, context);
}
String argument = tok.nextToken();
PCTemplate derivative = new PCTemplate();
derivative.put(ObjectKey.VISIBILITY, Visibility.HIDDEN);
derivative.put(IntegerKey.LEVEL, lvl);
context.getReferenceContext().getManufacturer(PCTemplate.class).addDerivativeObject(derivative);
context.getObjectContext().addToList(template, ListKey.LEVEL_TEMPLATES, derivative);
try {
if (context.processToken(derivative, typeStr, argument)) {
return ParseResult.SUCCESS;
}
} catch (PersistenceLayerException e) {
return new ParseResult.Fail(e.getMessage(), context);
}
return ParseResult.INTERNAL_ERROR;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class ForwardRefToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Campaign obj, String value) {
int pipeLoc = value.indexOf('|');
if (pipeLoc == -1) {
return new ParseResult.Fail(getTokenName() + " requires at least two arguments, " + "ReferenceType and Key: " + value, context);
}
if (value.lastIndexOf('|') != pipeLoc) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage(getTokenName() + " requires at only two pipe separated arguments, " + "ReferenceType and Keys: " + value);
cpr.addErrorMessage(" keys are comma separated");
return cpr;
}
String firstToken = value.substring(0, pipeLoc);
ReferenceManufacturer<? extends Loadable> rm = context.getManufacturer(firstToken);
if (rm == null) {
return new ParseResult.Fail(getTokenName() + " unable to generate manufacturer for type: " + value, context);
}
String rest = value.substring(pipeLoc + 1);
if (hasIllegalSeparator(',', rest)) {
return new ParseResult.Fail(getTokenName() + " keys are comma separated", context);
}
StringTokenizer st = new StringTokenizer(rest, Constants.COMMA);
while (st.hasMoreTokens()) {
CDOMSingleRef<? extends Loadable> ref = rm.getReference(st.nextToken());
context.getObjectContext().addToList(obj, ListKey.FORWARDREF, new Qualifier(rm.getReferenceClass(), ref));
}
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class ChooseFeatToken method parseTokenWithSeparator.
protected ParseResult parseTokenWithSeparator(LoadContext context, ReferenceManufacturer<Ability> rm, CDOMObject obj, String value) {
int pipeLoc = value.lastIndexOf('|');
String activeValue;
String title;
if (pipeLoc == -1) {
activeValue = value;
title = getDefaultTitle();
} else {
String titleString = value.substring(pipeLoc + 1);
if (titleString.startsWith("TITLE=")) {
title = titleString.substring(6);
if (title.startsWith("\"")) {
title = title.substring(1, title.length() - 1);
}
activeValue = value.substring(0, pipeLoc);
if (title == null || title.isEmpty()) {
return new ParseResult.Fail(getParentToken() + Constants.COLON + getTokenName() + " had TITLE= but no title: " + value, context);
}
} else {
activeValue = value;
title = getDefaultTitle();
}
}
PrimitiveCollection<Ability> coll = context.getChoiceSet(rm, activeValue);
if (coll == null) {
return ParseResult.INTERNAL_ERROR;
}
if (!coll.getGroupingState().isValid()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid combination of objects was used in: " + activeValue);
cpr.addErrorMessage(" Check that ALL is not combined");
cpr.addErrorMessage(" Check that a key is not joined with AND (,)");
return cpr;
}
PrimitiveChoiceSet<Ability> pcs = new CollectionToChoiceSet<>(coll);
//Tricky for compatibility...
CategorizedChooseInformation<Ability> tc = new CategorizedChooseInformation<>("ABILITY", CDOMDirectSingleRef.getRef(AbilityCategory.FEAT), pcs, Ability.class);
tc.setTitle(title);
tc.setChoiceActor(this);
context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class AbilityToken method parseTokenWithSeparator.
protected ParseResult parseTokenWithSeparator(LoadContext context, ReferenceManufacturer<Ability> rm, CDOMSingleRef<AbilityCategory> acRef, CDOMObject obj, String value) {
int pipeLoc = value.lastIndexOf('|');
String activeValue;
String title;
if (pipeLoc == -1) {
activeValue = value;
title = getDefaultTitle();
} else {
String titleString = value.substring(pipeLoc + 1);
if (titleString.startsWith("TITLE=")) {
title = titleString.substring(6);
if (title.startsWith("\"")) {
title = title.substring(1, title.length() - 1);
}
activeValue = value.substring(0, pipeLoc);
if (title == null || title.isEmpty()) {
return new ParseResult.Fail(getParentToken() + Constants.COLON + getTokenName() + " had TITLE= but no title: " + value, context);
}
} else {
activeValue = value;
title = getDefaultTitle();
}
}
PrimitiveCollection<Ability> coll = context.getChoiceSet(rm, activeValue);
if (coll == null) {
return ParseResult.INTERNAL_ERROR;
}
if (!coll.getGroupingState().isValid()) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid combination of objects was used in: " + activeValue);
cpr.addErrorMessage(" Check that ALL is not combined");
cpr.addErrorMessage(" Check that a key is not joined with AND (,)");
return cpr;
}
PrimitiveChoiceSet<Ability> pcs = new CollectionToChoiceSet<>(coll);
CategorizedChooseInformation<Ability> tc = new CategorizedChooseInformation<>(getTokenName(), acRef, pcs, Ability.class);
tc.setTitle(title);
tc.setChoiceActor(this);
context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class VisibleToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Equipment eq, 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 {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Unexpected value used in " + getTokenName() + " in Equipment");
cpr.addErrorMessage(" " + value + " is not a valid value for " + getTokenName());
cpr.addErrorMessage(" Valid values in Equipment are YES, NO, DISPLAY, EXPORT");
return cpr;
}
context.getObjectContext().put(eq, ObjectKey.VISIBILITY, vis);
return ParseResult.SUCCESS;
}
Aggregations