use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class KnownspellsToken method unparse.
@Override
public String[] unparse(LoadContext context, PCClass pcc) {
Changes<KnownSpellIdentifier> changes = context.getObjectContext().getListChanges(pcc, ListKey.KNOWN_SPELLS);
List<String> list = new ArrayList<>();
if (changes.includesGlobalClear()) {
list.add(Constants.LST_DOT_CLEAR_ALL);
}
Collection<KnownSpellIdentifier> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
return null;
}
Collection<KnownSpellIdentifier> added = changes.getAdded();
if (added != null && !added.isEmpty()) {
TreeMapToList<CDOMReference<?>, Integer> map = new TreeMapToList<>(ReferenceUtilities.REFERENCE_SORTER);
for (KnownSpellIdentifier ksi : added) {
CDOMReference<Spell> ref = ksi.getSpellReference();
Integer i = ksi.getSpellLevel();
map.addToListFor(ref, i);
}
for (CDOMReference<?> ref : map.getKeySet()) {
for (Integer lvl : map.getListFor(ref)) {
StringBuilder sb = new StringBuilder();
boolean needComma = false;
String refString = ref.getLSTformat(false);
if (!Constants.LST_ALL.equals(refString)) {
sb.append(refString);
needComma = true;
}
if (lvl != null) {
if (needComma) {
sb.append(',');
}
sb.append("LEVEL=").append(lvl);
}
list.add(sb.toString());
}
}
}
if (list.isEmpty()) {
return null;
}
return new String[] { StringUtil.join(list, Constants.PIPE) };
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class AdddomainsToken method unparse.
@Override
public String[] unparse(LoadContext context, PCClass pcc) {
AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), pcc, PCClass.ALLOWED_DOMAINS);
Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR");
return null;
}
MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
if (mtl == null || mtl.isEmpty()) {
return null;
}
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
Set<String> set = new TreeSet<>();
Set<String> noPrereqSet = new TreeSet<>();
for (CDOMReference<Domain> domain : mtl.getKeySet()) {
for (AssociatedPrereqObject assoc : mtl.getListFor(domain)) {
StringBuilder sb = new StringBuilder(domain.getLSTformat(false));
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
if (prereqs == null || prereqs.isEmpty()) {
noPrereqSet.add(sb.toString());
continue;
}
for (Prerequisite prereq : prereqs) {
sb.append(Constants.PIPE);
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, prereq);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
sb.append(swriter.toString());
}
set.add(sb.toString());
}
}
if (!noPrereqSet.isEmpty()) {
set.add(StringUtil.join(noPrereqSet, Constants.PIPE));
}
return set.toArray(new String[set.size()]);
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class CcskillToken method parseTokenWithSeparator.
@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClass obj, String value) {
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.LOCALCCSKILL);
} 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.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
} 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.LOCALCCSKILL, 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.LOCALCCSKILL, context.getReferenceContext().getCDOMAllReference(SKILL_CLASS));
} else {
foundOther = true;
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.LOCALCCSKILL, 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 AbilityTokenTest method createSingle.
private List<CDOMReference<Ability>> createSingle(String name) {
List<CDOMReference<Ability>> refs = new ArrayList<>();
Ability obj = primaryContext.getReferenceContext().constructCDOMObject(Ability.class, name);
primaryContext.getReferenceContext().reassociateCategory(AbilityCategory.FEAT, obj);
CDOMDirectSingleRef<Ability> ar = CDOMDirectSingleRef.getRef(obj);
refs.add(ar);
if (name.indexOf('(') != -1) {
List<String> choices = new ArrayList<>();
AbilityUtilities.getUndecoratedName(name, choices);
assertEquals(1, choices.size());
ar.setChoice(choices.get(0));
}
return refs;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class ClassSkillsLevelTokenTest method testUnparseSingleRanked.
@Test
public void testUnparseSingleRanked() throws PersistenceLayerException {
List<CDOMReference<Skill>> refs = new ArrayList<>();
addSingleRef(refs, "TestWP1");
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Skill> cs = new ChoiceSet<>(getSubToken().getTokenName(), rcs);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, FormulaFactory.ONE);
primaryProf.addToListFor(ListKey.ADD, tc);
tc.setChoiceActor(new ClassSkillChoiceActor(fighter, 3));
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
expectSingle(unparsed, getSubTokenName() + '|' + "TestWP1,AUTORANK=3");
}
Aggregations