use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.
the class DomainsTokenTest method testRoundRobinUnparseDirect.
@Test
public void testRoundRobinUnparseDirect() throws PersistenceLayerException {
construct(primaryContext, "TestWP1");
construct(primaryContext, "TestWP2");
AssociatedPrereqObject apo = new SimpleAssociatedObject();
apo.setAssociation(AssociationKey.TOKEN, "DOMAINS");
primaryProf.putToList(Deity.DOMAINLIST, CDOMDirectSingleRef.getRef(primaryContext.getReferenceContext().silentlyGetConstructedCDOMObject(getTargetClass(), "TestWP1")), apo);
primaryProf.putToList(Deity.DOMAINLIST, CDOMDirectSingleRef.getRef(primaryContext.getReferenceContext().silentlyGetConstructedCDOMObject(getTargetClass(), "TestWP2")), apo);
String[] unparsed = getToken().unparse(primaryContext, primaryProf);
assertNotNull(unparsed);
assertEquals(1, unparsed.length);
assertEquals("TestWP1,TestWP2", unparsed[0]);
}
use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.
the class SpelllevelLst method subParse.
private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
int equalLoc = tokString.indexOf(Constants.EQUALS);
if (equalLoc == -1) {
Logging.errorPrint("Expected an = in SPELLLEVEL " + "definition: " + tokString);
return false;
}
String casterString = tokString.substring(0, equalLoc);
String spellLevel = tokString.substring(equalLoc + 1);
Integer splLevel;
try {
splLevel = Integer.decode(spellLevel);
} catch (NumberFormatException nfe) {
Logging.errorPrint("Expected a number for SPELLLEVEL, found: " + spellLevel);
return false;
}
if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
return false;
}
StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
while (clTok.hasMoreTokens()) {
String classString = clTok.nextToken();
CDOMReference<CL> ref;
if (classString.startsWith("SPELLCASTER.")) {
/*
* This is actually a TYPE
*/
ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
} else {
ref = context.getReferenceContext().getCDOMReference(tagType, classString);
}
slList.add(ref);
}
if (hasIllegalSeparator(',', spellString)) {
return false;
}
StringTokenizer spTok = new StringTokenizer(spellString, ",");
while (spTok.hasMoreTokens()) {
String spellName = spTok.nextToken();
CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
tpr.addAllPrerequisites(prereqs);
}
}
return true;
}
use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.
the class SpellknownLst method subParse.
/**
* Parse the tag contents after the SPELLKNOWN:CLASS| section.
*
* @param context the context under which the tag is being parsed.
* @param obj the obj The object owning the tag.
* @param tagType the type of object the tag creates
* @param tokString the tok string The string defining the caster type/class and spell level.
* @param spellString the spell string The string containing the spell name(s)
* @param prereqs the prereqs The prerequisites to be applied.
*
* @return true, if successful
*/
private <CL extends Loadable & CDOMList<Spell>> boolean subParse(LoadContext context, CDOMObject obj, Class<CL> tagType, String tokString, String spellString, List<Prerequisite> prereqs) {
int equalLoc = tokString.indexOf(Constants.EQUALS);
if (equalLoc == -1) {
Logging.errorPrint("Expected an = in SPELLKNOWN " + "definition: " + tokString);
return false;
}
String casterString = tokString.substring(0, equalLoc);
String spellLevel = tokString.substring(equalLoc + 1);
Integer splLevel;
try {
splLevel = Integer.decode(spellLevel);
} catch (NumberFormatException nfe) {
Logging.errorPrint("Expected a number for SPELLKNOWN, found: " + spellLevel);
return false;
}
if (isEmpty(casterString) || hasIllegalSeparator(',', casterString)) {
return false;
}
StringTokenizer clTok = new StringTokenizer(casterString, Constants.COMMA);
List<CDOMReference<? extends CDOMList<Spell>>> slList = new ArrayList<>();
while (clTok.hasMoreTokens()) {
String classString = clTok.nextToken();
CDOMReference<CL> ref;
if (classString.startsWith("SPELLCASTER.")) {
/*
* This is actually a TYPE
*/
ref = context.getReferenceContext().getCDOMTypeReference(tagType, classString.substring(12));
} else {
ref = context.getReferenceContext().getCDOMReference(tagType, classString);
}
slList.add(ref);
}
if (hasIllegalSeparator(',', spellString)) {
return false;
}
StringTokenizer spTok = new StringTokenizer(spellString, ",");
while (spTok.hasMoreTokens()) {
String spellName = spTok.nextToken();
CDOMReference<Spell> sp = context.getReferenceContext().getCDOMReference(Spell.class, spellName);
for (CDOMReference<? extends CDOMList<Spell>> sl : slList) {
AssociatedPrereqObject tpr = context.getListContext().addToList(getTokenName(), obj, sl, sp);
tpr.setAssociation(AssociationKey.SPELL_LEVEL, splLevel);
tpr.setAssociation(AssociationKey.KNOWN, Boolean.TRUE);
tpr.addAllPrerequisites(prereqs);
}
}
return true;
}
use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.
the class VisionLst 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 aTok = new StringTokenizer(value, Constants.PIPE);
String visionString = aTok.nextToken();
if (looksLikeAPrerequisite(visionString)) {
return new ParseResult.Fail("Cannot have only PRExxx subtoken in " + getTokenName() + ": " + value, context);
}
ArrayList<AssociatedPrereqObject> edgeList = new ArrayList<>();
boolean foundClear = false;
while (true) {
if (Constants.LST_DOT_CLEAR.equals(visionString)) {
context.getListContext().removeAllFromList(getTokenName(), obj, Vision.VISIONLIST);
foundClear = true;
} else if (visionString.startsWith(Constants.LST_DOT_CLEAR_DOT)) {
try {
Vision vis = Vision.getVision(visionString.substring(7));
context.getListContext().removeFromList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vis));
} catch (IllegalArgumentException e) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Bad Syntax for Cleared Vision in " + getTokenName());
cpr.addErrorMessage(e.getMessage());
return cpr;
}
foundClear = true;
} else if (looksLikeAPrerequisite(visionString)) {
break;
} else {
try {
Vision vision = Vision.getVision(visionString);
AssociatedPrereqObject edge = context.getListContext().addToList(getTokenName(), obj, Vision.VISIONLIST, new CDOMDirectSingleRef<>(vision));
edgeList.add(edge);
} catch (IllegalArgumentException e) {
ComplexParseResult cpr = new ComplexParseResult();
cpr.addErrorMessage("Bad Syntax for Vision in " + getTokenName());
cpr.addErrorMessage(e.getMessage());
return cpr;
}
}
if (!aTok.hasMoreTokens()) {
return ParseResult.SUCCESS;
}
visionString = aTok.nextToken();
}
if (foundClear) {
return new ParseResult.Fail("Cannot use PREREQs when using .CLEAR or .CLEAR. in " + getTokenName(), context);
}
while (true) {
Prerequisite prereq = getPrerequisite(visionString);
if (prereq == null) {
return new ParseResult.Fail(" (Did you put vision after the " + "PRExxx tags in " + getTokenName() + ":?)", context);
}
for (AssociatedPrereqObject edge : edgeList) {
edge.addPrerequisite(prereq);
}
if (!aTok.hasMoreTokens()) {
break;
}
visionString = aTok.nextToken();
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.AssociatedPrereqObject in project pcgen by PCGen.
the class DomainsToken method unparse.
@Override
public String[] unparse(LoadContext context, Spell spell) {
DoubleKeyMapToList<Prerequisite, Integer, CDOMReference<DomainSpellList>> dkmtl = new DoubleKeyMapToList<>();
List<String> list = new ArrayList<>();
Changes<CDOMReference<DomainSpellList>> masterChanges = context.getListContext().getMasterListChanges(getTokenName(), spell, SPELLLIST_CLASS);
if (masterChanges.includesGlobalClear()) {
list.add(Constants.LST_DOT_CLEAR_ALL);
}
if (masterChanges.hasRemovedItems()) {
for (CDOMReference<DomainSpellList> swl : masterChanges.getRemoved()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
MapToList<Spell, AssociatedPrereqObject> map = changes.getRemovedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot remove another Spell " + "(must only remove itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
if (prereqs != null && !prereqs.isEmpty()) {
context.addWriteMessage("Incoming Remove " + "Edge to " + spell.getKeyName() + " had a " + "Prerequisite: " + prereqs.size());
return null;
}
dkmtl.addToListFor(null, -1, swl);
}
}
}
}
}
for (CDOMReference<DomainSpellList> swl : masterChanges.getAdded()) {
AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
Collection<Spell> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
return null;
}
MapToList<Spell, AssociatedPrereqObject> map = changes.getAddedAssociations();
if (map != null && !map.isEmpty()) {
for (Spell added : map.getKeySet()) {
if (!spell.getLSTformat().equals(added.getLSTformat())) {
context.addWriteMessage("Spell " + getTokenName() + " token cannot allow another Spell " + "(must only allow itself)");
return null;
}
for (AssociatedPrereqObject assoc : map.getListFor(added)) {
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
Prerequisite prereq;
if (prereqs == null || prereqs.isEmpty()) {
prereq = null;
} else if (prereqs.size() == 1) {
prereq = prereqs.get(0);
} else {
context.addWriteMessage("Incoming Edge to " + spell.getKeyName() + " had more than one " + "Prerequisite: " + prereqs.size());
return null;
}
Integer level = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
if (level == null) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had no Spell Level defined");
return null;
}
if (level.intValue() < 0) {
context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had invalid Level: " + level + ". Must be >= 0.");
return null;
}
dkmtl.addToListFor(prereq, level, swl);
}
}
}
}
if (dkmtl.isEmpty()) {
if (list.isEmpty()) {
// Legal if no DOMAINS was present in the Spell
return null;
} else {
return list.toArray(new String[list.size()]);
}
}
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
SortedSet<CDOMReference<DomainSpellList>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
SortedSet<Integer> levelSet = new TreeSet<>();
for (Prerequisite prereq : dkmtl.getKeySet()) {
StringBuilder sb = new StringBuilder();
boolean needPipe = false;
levelSet.clear();
levelSet.addAll(dkmtl.getSecondaryKeySet(prereq));
for (Integer i : levelSet) {
set.clear();
set.addAll(dkmtl.getListFor(prereq, i));
if (needPipe) {
sb.append(Constants.PIPE);
}
sb.append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
sb.append('=').append(i);
needPipe = true;
}
if (prereq != null) {
sb.append('[');
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, prereq);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
sb.append(swriter.toString());
sb.append(']');
}
list.add(sb.toString());
}
return list.toArray(new String[list.size()]);
}
Aggregations