use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class TokenSupport method processSubToken.
public <T> ParseResult processSubToken(LoadContext context, T cdo, String tokenName, String key, String value) {
ComplexParseResult cpr = new ComplexParseResult();
List<? extends CDOMToken<T>> tokenList = getTokens((Class<T>) cdo.getClass(), tokenName, key);
if (tokenList != null) {
for (CDOMToken<T> token : tokenList) {
ParseResult pr = token.parseToken(context, cdo, value);
if (pr.passed()) {
return pr;
}
cpr.copyMessages(pr);
cpr.addErrorMessage("Failed in parsing subtoken: " + key + " of " + value);
}
}
/*
* CONSIDER Better option than toString, given that T != CDOMObject
*/
cpr.addErrorMessage("Illegal " + tokenName + " subtoken '" + key + "' '" + value + "' for " + cdo.toString());
return cpr;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class KnownspellsToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass pcc, String value) {
StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
boolean firstToken = true;
while (pipeTok.hasMoreTokens()) {
String totalFilter = pipeTok.nextToken();
if (Constants.LST_DOT_CLEAR_ALL.equals(totalFilter)) {
if (!firstToken) {
return new ParseResult.Fail("Non-sensical situation was " + "encountered while parsing " + getTokenName() + ": When used, .CLEARALL must be the first argument", context);
}
context.getObjectContext().removeList(pcc, ListKey.KNOWN_SPELLS);
continue;
}
ParseResult pr = checkForIllegalSeparator(',', totalFilter);
if (!pr.passed()) {
return pr;
}
StringTokenizer commaTok = new StringTokenizer(totalFilter, Constants.COMMA);
/*
* This is a rather interesting situation - this takes items that
* are ALLOWED and converts them to GRANTS. Therefore, this must be
* done as a post-manufacturing run on the Graph.
*
* As there is no guarantee when the factory is added that the list
* is complete, this resolution of known MUST be performed as a
* query against the PC, not stored in the graph as Grants edges.
*/
// must satisfy all elements in a comma delimited list
Integer levelLim = null;
CDOMReference<Spell> sp = null;
while (commaTok.hasMoreTokens()) {
String filterString = commaTok.nextToken();
if (filterString.startsWith("LEVEL=")) {
if (levelLim != null) {
return new ParseResult.Fail("Cannot have more than one Level limit in " + getTokenName() + ": " + value, context);
}
// the desired spellLevel
try {
levelLim = Integer.valueOf(filterString.substring(6));
if (levelLim < 0) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + value);
cpr.addErrorMessage(" Level must be >= 0");
return cpr;
}
} catch (NumberFormatException e) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Invalid Number in " + getTokenName() + ": " + value);
cpr.addErrorMessage(" Level must be " + "a non-negative integer");
return cpr;
}
} else {
if (sp != null) {
return new ParseResult.Fail("Cannot have more than one Type/Spell limit in " + getTokenName() + ": " + value, context);
}
sp = TokenUtilities.getTypeOrPrimitive(context, SPELL_CLASS, filterString);
if (sp == null) {
return new ParseResult.Fail(" encountered Invalid limit in " + getTokenName() + ": " + value, context);
}
}
firstToken = false;
}
if (sp == null) {
/*
* There is no need to check for an invalid construction here
* (meaning levelLim is null as well) as that was implicitly
* checked by ensuring || did not occur.
*/
sp = context.getReferenceContext().getCDOMAllReference(SPELL_CLASS);
}
KnownSpellIdentifier ksi = new KnownSpellIdentifier(sp, levelLim);
context.getObjectContext().addToList(pcc, ListKey.KNOWN_SPELLS, ksi);
}
return ParseResult.SUCCESS;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class SpellsToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, KitSpells kitSpell, String value) {
StringTokenizer aTok = new StringTokenizer(value, Constants.PIPE);
ComplexParseResult pr = new ComplexParseResult();
while (aTok.hasMoreTokens()) {
String field = aTok.nextToken();
if (field.startsWith("SPELLBOOK=")) {
if (kitSpell.getSpellBook() != null) {
return new ParseResult.Fail("Cannot reset SPELLBOOK in SPELLS: " + value, context);
}
String spellBook = field.substring(10);
if (spellBook.isEmpty()) {
return new ParseResult.Fail("Cannot set SPELLBOOK " + "to empty value in SPELLS: " + value, context);
}
kitSpell.setSpellBook(spellBook);
} else if (field.startsWith(Constants.LST_CLASS_EQUAL)) {
if (kitSpell.getCastingClass() != null) {
return new ParseResult.Fail("Cannot reset CLASS" + " in SPELLS: " + value, context);
}
String className = field.substring(6);
if (className.isEmpty()) {
return new ParseResult.Fail("Cannot set CLASS " + "to empty value in SPELLS: " + value, context);
} else if (className.equalsIgnoreCase("Default")) {
pr.addWarningMessage("Use of Default for CLASS= in KIT " + "SPELLS line is unnecessary: Ignoring");
} else {
kitSpell.setCastingClass(context.getReferenceContext().getCDOMReference(PCClass.class, className));
}
} else {
int count = 1;
int equalLoc = field.indexOf(Constants.EQUALS);
if (equalLoc != -1) {
String countStr = field.substring(equalLoc + 1);
try {
count = Integer.parseInt(countStr);
} catch (NumberFormatException e) {
return new ParseResult.Fail("Expected an Integer COUNT," + " but found: " + countStr + " in " + value, context);
}
field = field.substring(0, equalLoc);
}
if (field.isEmpty()) {
return new ParseResult.Fail("Expected an Spell in SPELLS" + " but found: " + value, context);
}
StringTokenizer subTok = new StringTokenizer(field, "[]");
String filterString = subTok.nextToken();
// must satisfy all elements in a comma delimited list
CDOMReference<Spell> sp = null;
sp = TokenUtilities.getTypeOrPrimitive(context, SPELL_CLASS, filterString);
if (sp == null) {
return new ParseResult.Fail(" encountered Invalid limit in " + getTokenName() + ": " + value, context);
}
KnownSpellIdentifier ksi = new KnownSpellIdentifier(sp, null);
ArrayList<CDOMSingleRef<Ability>> featList = new ArrayList<>();
while (subTok.hasMoreTokens()) {
String featName = subTok.nextToken();
CDOMSingleRef<Ability> feat = context.getReferenceContext().getCDOMReference(ABILITY_CLASS, AbilityCategory.FEAT, featName);
featList.add(feat);
}
kitSpell.addSpell(ksi, featList, count);
}
}
if (kitSpell.getSpellBook() == null) {
kitSpell.setSpellBook(Globals.getDefaultSpellBook());
}
return pr;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class UdamLst method parseToken.
@Override
public ParseResult parseToken(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);
}
ParseResult pr = ParseResult.SUCCESS;
if (Constants.LST_DOT_CLEAR.equals(value)) {
/*
* TODO This cross-polluting and certainly not "editor friendly",
* thus will need to be changed after 5.16
*/
if (obj instanceof PCClassLevel || obj instanceof PCClass) {
PCClass pcc;
if (obj instanceof PCClassLevel) {
pcc = (PCClass) obj.get(ObjectKey.TOKEN_PARENT);
} else {
pcc = (PCClass) obj;
}
context.getObjectContext().removeList(pcc, ListKey.UNARMED_DAMAGE);
for (PCClassLevel level : pcc.getOriginalClassLevelCollection()) {
context.getObjectContext().removeList(level, ListKey.UNARMED_DAMAGE);
}
} else {
context.getObjectContext().removeList(obj, ListKey.UNARMED_DAMAGE);
}
} else {
pr = checkForIllegalSeparator(',', value);
if (!pr.passed()) {
return pr;
}
final StringTokenizer tok = new StringTokenizer(value, Constants.COMMA);
if (context.getObjectContext().containsListFor(obj, ListKey.UNARMED_DAMAGE)) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addWarningMessage(obj.getDisplayName() + " already has " + getTokenName() + " set.");
cpr.addWarningMessage(" It will be redefined, " + "but you should be using " + getTokenName() + ":.CLEAR");
pr = cpr;
context.getObjectContext().removeList(obj, ListKey.UNARMED_DAMAGE);
}
while (tok.hasMoreTokens()) {
context.getObjectContext().addToList(obj, ListKey.UNARMED_DAMAGE, tok.nextToken());
}
}
return pr;
}
use of pcgen.rules.persistence.token.ComplexParseResult in project pcgen by PCGen.
the class ServesAsToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
if (!getLegalTypes().contains(obj.getClass())) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Cannot use SERVESAS on a " + obj.getClass());
cpr.addErrorMessage(" bad use found in " + obj.getClass().getSimpleName() + ' ' + obj.getKeyName());
return cpr;
}
return super.parseNonEmptyToken(context, obj, value);
}
Aggregations