use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class LangToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
Changes<QualifiedObject<CDOMReference<Language>>> changes = context.getObjectContext().getListChanges(obj, ListKey.AUTO_LANGUAGE);
Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
Collection<QualifiedObject<CDOMReference<Language>>> added = changes.getAdded();
StringBuilder sb = new StringBuilder();
Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
boolean foundAny = false;
boolean foundOther = false;
if (changes.includesGlobalClear()) {
sb.append(Constants.LST_DOT_CLEAR);
}
if (listAdded != null && !listAdded.isEmpty()) {
for (ChooseSelectionActor<?> cra : listAdded) {
if (cra.getSource().equals(getTokenName())) {
try {
if (sb.length() > 0) {
sb.append('|');
}
sb.append(cra.getLstFormat());
foundOther = true;
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
}
}
}
if (added != null) {
boolean needPipe = sb.length() > 0;
Prerequisite prereq = null;
for (QualifiedObject<CDOMReference<Language>> spp : added) {
CDOMReference<Language> lang = spp.getRawObject();
List<Prerequisite> prereqs = spp.getPrerequisiteList();
String ab = lang.getLSTformat(false);
boolean isUnconditionalAll = Constants.LST_ALL.equals(ab);
foundAny |= isUnconditionalAll;
foundOther |= !isUnconditionalAll;
if (needPipe) {
sb.append('|');
}
needPipe = true;
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);
if (prereq != null && !p.equals(prereq)) {
context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had differing Prerequisites for " + getFullName());
return null;
}
prereq = p;
}
sb.append(ab);
}
if (prereq != null) {
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, prereq);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
sb.append('|').append(swriter.toString());
}
}
if (foundAny && foundOther) {
context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + sb);
return null;
}
if (sb.length() == 0) {
// okay
return null;
}
return new String[] { sb.toString() };
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class ShieldProfToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
String shieldProf;
// 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.
shieldProf = value;
StringTokenizer tok = new StringTokenizer(shieldProf, 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;
shieldProf = value.substring(0, preStart);
isPre = true;
}
}
} else {
return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
}
ParseResult pr = checkForIllegalSeparator('|', shieldProf);
if (!pr.passed()) {
return pr;
}
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(shieldProf, Constants.PIPE);
List<CDOMReference<ShieldProf>> shieldProfs = new ArrayList<>();
List<CDOMReference<Equipment>> equipTypes = new ArrayList<>();
while (tok.hasMoreTokens()) {
String aProf = tok.nextToken();
if (Constants.LST_PERCENT_LIST.equals(aProf)) {
foundOther = true;
ChooseSelectionActor<ShieldProf> cra;
if (prereq == null) {
cra = this;
} else {
ConditionalSelectionActor<ShieldProf> cca = new ConditionalSelectionActor<>(this);
cca.addPrerequisite(prereq);
cra = cca;
}
context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
} else if (Constants.LST_ALL.equalsIgnoreCase(aProf)) {
foundAny = true;
shieldProfs.add(context.getReferenceContext().getCDOMAllReference(SHIELDPROF_CLASS));
} else if (aProf.startsWith("SHIELDTYPE.") || aProf.startsWith("SHIELDTYPE=")) {
foundOther = true;
CDOMReference<Equipment> ref = TokenUtilities.getTypeReference(context, EQUIPMENT_CLASS, "SHIELD." + aProf.substring(11));
if (ref == null) {
return ParseResult.INTERNAL_ERROR;
}
equipTypes.add(ref);
} else {
foundOther = true;
shieldProfs.add(context.getReferenceContext().getCDOMReference(SHIELDPROF_CLASS, aProf));
}
}
if (foundAny && foundOther) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
if (!shieldProfs.isEmpty() || !equipTypes.isEmpty()) {
ShieldProfProvider pp = new ShieldProfProvider(shieldProfs, equipTypes);
if (prereq != null) {
pp.addPrerequisite(prereq);
}
context.getObjectContext().addToList(obj, ListKey.AUTO_SHIELDPROF, pp);
}
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class ClassSkillsToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, PCClass obj, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String activeValue = sep.next();
Formula count;
if (!sep.hasNext()) {
count = FormulaFactory.ONE;
} else {
count = FormulaFactory.getFormulaFor(activeValue);
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
}
activeValue = sep.next();
}
if (sep.hasNext()) {
return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
}
ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
if (!pr.passed()) {
return pr;
}
List<CDOMReference<Skill>> refs = new ArrayList<>();
StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
CDOMGroupRef<Skill> allRef = context.getReferenceContext().getCDOMAllReference(SKILL_CLASS);
Integer autoRank = null;
while (tok.hasMoreTokens()) {
String tokText = tok.nextToken();
if (Constants.LST_ALL.equals(tokText) || Constants.LST_ANY.equals(tokText)) {
refs.add(allRef);
} else {
if (Constants.LST_UNTRAINED.equals(tokText)) {
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.TRUE);
omr.returnIncludesNulls(true);
refs.add(omr);
} else if (Constants.LST_TRAINED.equals(tokText)) {
refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.USE_UNTRAINED, Boolean.FALSE));
} else if (Constants.LST_EXCLUSIVE.equals(tokText)) {
refs.add(new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.TRUE));
} else if (Constants.LST_NONEXCLUSIVE.equals(tokText) || Constants.LST_CROSS_CLASS.equals(tokText)) {
ObjectMatchingReference<Skill, Boolean> omr = new ObjectMatchingReference<>(tokText, SKILL_CLASS, allRef, ObjectKey.EXCLUSIVE, Boolean.FALSE);
omr.returnIncludesNulls(true);
refs.add(omr);
} else if (tokText.startsWith("AUTORANK=")) {
if (autoRank != null) {
return new ParseResult.Fail("Cannot have two " + "AUTORANK= items in " + getFullName() + ": " + value, context);
}
String rankString = tokText.substring(9);
try {
autoRank = Integer.decode(rankString);
if (autoRank <= 0) {
return new ParseResult.Fail("Expected AUTORANK= to be" + " greater than zero, found: " + autoRank, context);
}
} catch (NumberFormatException e) {
return new ParseResult.Fail("Expected AUTORANK= to have" + " an integer value, found: " + rankString, context);
}
} else {
CDOMReference<Skill> skref = TokenUtilities.getTypeOrPrimitive(context, SKILL_CLASS, tokText);
if (skref == null) {
return new ParseResult.Fail(" Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
}
refs.add(skref);
}
}
}
if (refs.isEmpty()) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains no skill reference: " + value, context);
}
ReferenceChoiceSet<Skill> rcs = new ReferenceChoiceSet<>(refs);
if (!rcs.getGroupingState().isValid()) {
return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
}
ChoiceSet<Skill> cs = new ChoiceSet<>(getTokenName(), rcs, true);
PersistentTransitionChoice<Skill> tc = new ConcretePersistentTransitionChoice<>(cs, count);
ClassSkillChoiceActor actor = new ClassSkillChoiceActor(obj, autoRank);
tc.setChoiceActor(actor);
context.getObjectContext().addToList(obj, ListKey.ADD, tc);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class EquipToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String activeValue = sep.next();
Formula count;
if (!sep.hasNext()) {
count = FormulaFactory.ONE;
} else {
count = FormulaFactory.getFormulaFor(activeValue);
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
}
activeValue = sep.next();
}
if (sep.hasNext()) {
return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
}
ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
if (!pr.passed()) {
return pr;
}
List<CDOMReference<Equipment>> refs = new ArrayList<>();
StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
while (tok.hasMoreTokens()) {
String tokText = tok.nextToken();
CDOMReference<Equipment> lang = TokenUtilities.getTypeOrPrimitive(context, EQUIPMENT_CLASS, tokText);
if (lang == null) {
return new ParseResult.Fail(" Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText, context);
}
refs.add(lang);
}
ReferenceChoiceSet<Equipment> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<Equipment> cs = new ChoiceSet<>(getTokenName(), new QualifiedDecorator<>(rcs));
cs.setTitle("Equipment Choice");
PersistentTransitionChoice<Equipment> tc = new ConcretePersistentTransitionChoice<>(cs, count);
context.getObjectContext().addToList(obj, ListKey.ADD, tc);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
use of pcgen.cdom.base.CDOMReference in project pcgen by PCGen.
the class TemplateToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
String activeValue = sep.next();
Formula count;
if (!sep.hasNext()) {
count = FormulaFactory.ONE;
} else {
count = FormulaFactory.getFormulaFor(activeValue);
if (!count.isValid()) {
return new ParseResult.Fail("Count in " + getTokenName() + " was not valid: " + count.toString(), context);
}
if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) {
return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context);
}
activeValue = sep.next();
}
if (sep.hasNext()) {
return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value, context);
}
ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue);
if (!pr.passed()) {
return pr;
}
List<CDOMReference<PCTemplate>> refs = new ArrayList<>();
StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA);
while (tok.hasMoreTokens()) {
refs.add(context.getReferenceContext().getCDOMReference(PCTEMPLATE_CLASS, tok.nextToken()));
}
ReferenceChoiceSet<PCTemplate> rcs = new ReferenceChoiceSet<>(refs);
ChoiceSet<PCTemplate> cs = new ChoiceSet<>("TEMPLATE", rcs);
PersistentTransitionChoice<PCTemplate> tc = new ConcretePersistentTransitionChoice<>(cs, count);
context.getObjectContext().addToList(obj, ListKey.ADD, tc);
tc.setChoiceActor(this);
return ParseResult.SUCCESS;
}
Aggregations