use of pcgen.cdom.base.CDOMReference 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.base.CDOMReference in project pcgen by PCGen.
the class CskillLst 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);
}
boolean first = true;
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
while (tok.hasMoreTokens()) {
String tokText = tok.nextToken();
if (Constants.LST_DOT_CLEAR.equals(tokText)) {
if (!first) {
return new ParseResult.Fail(" Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
}
context.getObjectContext().removeList(obj, ListKey.CSKILL);
} else if (tokText.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
String clearText = tokText.substring(7);
if (Constants.LST_ALL.equals(clearText)) {
context.getObjectContext().removeFromList(obj, ListKey.CSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
} else if (Constants.LST_LIST.equals(clearText)) {
context.getObjectContext().removeFromList(obj, ListKey.NEW_CHOOSE_ACTOR, this);
} else {
CDOMReference<Skill> ref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, clearText);
if (ref == null) {
return new ParseResult.Fail(" Error was encountered while parsing " + getTokenName(), context);
}
context.getObjectContext().removeFromList(obj, ListKey.CSKILL, ref);
}
} else {
/*
* Note this HAS to be done one-by-one, because the
* .clearChildNodeOfClass method above does NOT recognize the
* C/CC Skill object and therefore doesn't know how to search
* the sublists
*/
if (Constants.LST_ALL.equals(tokText)) {
foundAny = true;
context.getObjectContext().addToList(obj, ListKey.CSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
} else {
foundOther = true;
if (Constants.LST_LIST.equals(tokText)) {
context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, this);
} else {
CDOMReference<Skill> ref = getSkillReference(context, tokText);
if (ref == null) {
return new ParseResult.Fail(" Error was encountered " + "while parsing " + getTokenName(), context);
}
context.getObjectContext().addToList(obj, ListKey.CSKILL, ref);
}
}
}
first = false;
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value, context);
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class CompanionListLst method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
Changes<FollowerOption> changes = context.getObjectContext().getListChanges(obj, ListKey.COMPANIONLIST);
Collection<FollowerOption> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR");
return null;
}
Collection<FollowerOption> added = changes.getAdded();
if (added == null || added.isEmpty()) {
// Zero indicates no Token (and no global clear, so nothing to do)
return null;
}
TripleKeyMapToList<Set<Prerequisite>, CDOMReference<? extends CDOMList<?>>, Integer, CDOMReference<Race>> m = new TripleKeyMapToList<>();
for (FollowerOption fo : added) {
m.addToListFor(new HashSet<>(fo.getPrerequisiteList()), fo.getListRef(), fo.getAdjustment(), fo.getRaceRef());
}
Set<String> set = new TreeSet<>();
StringBuilder sb = new StringBuilder();
for (Set<Prerequisite> prereqs : m.getKeySet()) {
String prereqString = null;
if (prereqs != null && !prereqs.isEmpty()) {
prereqString = getPrerequisiteString(context, prereqs);
}
for (CDOMReference<? extends CDOMList<?>> cl : m.getSecondaryKeySet(prereqs)) {
for (Integer fa : m.getTertiaryKeySet(prereqs, cl)) {
sb.setLength(0);
sb.append(cl.getLSTformat(false));
sb.append(Constants.PIPE);
Set<CDOMReference<Race>> raceSet = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
raceSet.addAll(m.getListFor(prereqs, cl, fa));
sb.append(ReferenceUtilities.joinLstFormat(raceSet, Constants.COMMA, true));
if (fa != null && fa != 0) {
sb.append(Constants.PIPE);
sb.append("FOLLOWERADJUSTMENT:");
sb.append(fa);
}
if (prereqString != null) {
sb.append(Constants.PIPE);
sb.append(prereqString);
}
set.add(sb.toString());
}
}
}
return set.toArray(new String[set.size()]);
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class ClassesToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Skill skill, String value) {
StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
boolean added = false;
List<CDOMReference<ClassSkillList>> allow = new ArrayList<>();
while (pipeTok.hasMoreTokens()) {
String className = pipeTok.nextToken();
if (Constants.LST_ALL.equals(className)) {
if (added) {
return new ParseResult.Fail("Non-sensical Skill " + getTokenName() + ": Contains ALL after a specific reference: " + value, context);
}
break;
}
if (className.startsWith("!")) {
return new ParseResult.Fail("Non-sensical Skill " + getTokenName() + ": Contains ! without (or before) ALL: " + value, context);
}
allow.add(context.getReferenceContext().getCDOMReference(SKILLLIST_CLASS, className));
added = true;
}
if (pipeTok.hasMoreTokens()) {
// allow is not used (empty or an error)
FilteredReference<ClassSkillList> filtered = new FilteredReference<>(SKILLLIST_CLASS, context.getReferenceContext().getCDOMAllReference(SKILLLIST_CLASS));
while (pipeTok.hasMoreTokens()) {
String className = pipeTok.nextToken();
if (className.startsWith("!")) {
String clString = className.substring(1);
if (Constants.LST_ALL.equals(clString) || Constants.LST_ANY.equals(clString)) {
return new ParseResult.Fail("Invalid " + getTokenName() + " cannot use !ALL", context);
}
CDOMSingleRef<ClassSkillList> ref = context.getReferenceContext().getCDOMReference(SKILLLIST_CLASS, clString);
filtered.addProhibitedItem(ref);
} else {
return new ParseResult.Fail("Non-sensical Skill " + getTokenName() + ": Contains ALL and a specific reference: " + value, context);
}
}
context.getListContext().addToMasterList(getTokenName(), skill, filtered, skill);
} else if (allow.isEmpty()) {
// unqualified ALL
context.getListContext().addToMasterList(getTokenName(), skill, context.getReferenceContext().getCDOMAllReference(SKILLLIST_CLASS), skill);
} else {
// use allow
for (CDOMReference<ClassSkillList> ref : allow) {
context.getListContext().addToMasterList(getTokenName(), skill, ref, skill);
}
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class FavoredclassToken method parseFavoredClass.
public ParseResult parseFavoredClass(LoadContext context, CDOMObject cdo, String value) {
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (Constants.HIGHEST_LEVEL_CLASS.equalsIgnoreCase(token)) {
foundAny = true;
context.getObjectContext().put(cdo, ObjectKey.ANY_FAVORED_CLASS, true);
} else if (Constants.LST_PERCENT_LIST.equalsIgnoreCase(token)) {
context.getObjectContext().addToList(cdo, ListKey.NEW_CHOOSE_ACTOR, this);
} else {
CDOMReference<? extends PCClass> ref;
foundOther = true;
int dotLoc = token.indexOf('.');
if (dotLoc == -1) {
// Primitive
ref = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, token);
} else {
ParseResult pr = checkForIllegalSeparator('.', token);
if (!pr.passed()) {
return pr;
}
// SubClass
String parent = token.substring(0, dotLoc);
String subclass = token.substring(dotLoc + 1);
SubClassCategory scc = SubClassCategory.getConstant(parent);
ref = context.getReferenceContext().getCDOMReference(SUBCLASS_CLASS, scc, subclass);
}
context.getObjectContext().addToList(cdo, ListKey.FAVORED_CLASS, ref);
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getTokenName() + ": Contains " + Constants.HIGHEST_LEVEL_CLASS + " and a specific reference: " + value, context);
}
return ParseResult.SUCCESS;
}
Aggregations