use of pcgen.core.QualifiedObject in project pcgen by PCGen.
the class EquipToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
List<String> list = new ArrayList<>();
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
Changes<QualifiedObject<CDOMReference<Equipment>>> changes = context.getObjectContext().getListChanges(obj, ListKey.EQUIPMENT);
Collection<QualifiedObject<CDOMReference<Equipment>>> added = changes.getAdded();
HashMapToList<List<Prerequisite>, CDOMReference<Equipment>> m = new HashMapToList<>();
if (added != null) {
for (QualifiedObject<CDOMReference<Equipment>> qo : added) {
m.addToListFor(qo.getPrerequisiteList(), qo.getRawObject());
}
}
Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
if (listAdded != null && !listAdded.isEmpty()) {
for (ChooseSelectionActor<?> cra : listAdded) {
if (cra.getSource().equals(getTokenName())) {
try {
list.add(cra.getLstFormat());
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
}
}
}
for (List<Prerequisite> prereqs : m.getKeySet()) {
List<CDOMReference<Equipment>> eq = m.getListFor(prereqs);
WeightedCollection<CDOMReference<Equipment>> refs = new WeightedCollection<>(ReferenceUtilities.REFERENCE_SORTER);
refs.addAll(eq);
String ab = ReferenceUtilities.joinLstFormat(refs, Constants.PIPE);
if (prereqs != null && !prereqs.isEmpty()) {
if (prereqs.size() > 1) {
context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
return null;
}
Prerequisite p = prereqs.get(0);
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, p);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
ab = ab + '|' + swriter.toString();
}
list.add(ab);
}
if (list.isEmpty()) {
// Empty indicates no Token
return null;
}
return list.toArray(new String[list.size()]);
}
use of pcgen.core.QualifiedObject in project pcgen by PCGen.
the class WeaponProfToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
String weaponProfs;
// Do not initialize, null is significant!
Prerequisite prereq = null;
boolean isPre = false;
if (value.indexOf("[") == -1) {
// Supported version of PRExxx using |. Needs to be at the front of the
// Parsing code because many objects expect the pre to have been determined
// Ahead of time. Until deprecated code is removed, it will have to stay
// like this.
weaponProfs = value;
StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (PreParserFactory.isPreReqString(token)) {
if (isPre) {
String errorText = "Invalid " + getTokenName() + ": " + value + " PRExxx must be at the END of the Token";
Logging.errorPrint(errorText);
return new ParseResult.Fail(errorText, context);
}
prereq = getPrerequisite(token);
if (prereq == null) {
return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
}
int preStart = value.indexOf(token) - 1;
weaponProfs = value.substring(0, preStart);
isPre = true;
ParseResult fail = checkForLoopPrereqs(prereq, context);
if (fail != null) {
return fail;
}
}
}
} else {
return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
}
ParseResult pr = checkForIllegalSeparator('|', weaponProfs);
if (!pr.passed()) {
return pr;
}
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
WeaponProfProvider wpp = new WeaponProfProvider();
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (Constants.LST_PERCENT_LIST.equals(token)) {
foundOther = true;
ChooseSelectionActor<WeaponProf> cra;
if (prereq == null) {
cra = this;
} else {
ConditionalSelectionActor<WeaponProf> cca = new ConditionalSelectionActor<>(this);
cca.addPrerequisite(prereq);
cra = cca;
}
context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
} else if ("DEITYWEAPONS".equals(token)) {
foundOther = true;
context.getObjectContext().put(obj, ObjectKey.HAS_DEITY_WEAPONPROF, new QualifiedObject<>(Boolean.TRUE, prereq));
} else {
if (Constants.LST_ALL.equalsIgnoreCase(token)) {
foundAny = true;
CDOMGroupRef<WeaponProf> allRef = context.getReferenceContext().getCDOMAllReference(WEAPONPROF_CLASS);
wpp.addWeaponProfAll(allRef);
} else {
foundOther = true;
if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
CDOMGroupRef<WeaponProf> rr = TokenUtilities.getTypeReference(context, WEAPONPROF_CLASS, token.substring(5));
if (rr == null) {
return ParseResult.INTERNAL_ERROR;
}
wpp.addWeaponProfType(rr);
} else {
CDOMSingleRef<WeaponProf> ref = context.getReferenceContext().getCDOMReference(WEAPONPROF_CLASS, token);
wpp.addWeaponProf(ref);
}
}
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
if (!wpp.isEmpty()) {
if (prereq != null) {
wpp.addPrerequisite(prereq);
}
context.getObjectContext().addToList(obj, ListKey.WEAPONPROF, wpp);
}
return ParseResult.SUCCESS;
}
Aggregations