use of pcgen.cdom.reference.ReferenceManufacturer in project pcgen by PCGen.
the class TemplateLst method parseToken.
@Override
public ParseResult parseToken(LoadContext context, CDOMObject cdo, String value) {
if (cdo instanceof Ungranted) {
return new ParseResult.Fail("Cannot use " + getTokenName() + " on an Ungranted object type: " + cdo.getClass().getSimpleName(), context);
}
ListKey<CDOMReference<PCTemplate>> lk;
String remaining;
boolean consolidate = false;
boolean specialLegal = false;
if (value.startsWith(Constants.LST_CHOOSE_COLON)) {
lk = ListKey.TEMPLATE_CHOOSE;
remaining = value.substring(Constants.LST_CHOOSE_COLON.length());
consolidate = true;
} else if (value.startsWith(ADDCHOICE_COLON)) {
lk = ListKey.TEMPLATE_ADDCHOICE;
remaining = value.substring(ADDCHOICE_COLON.length());
} else {
lk = ListKey.TEMPLATE;
remaining = value;
specialLegal = true;
}
if (isEmpty(remaining) || hasIllegalSeparator('|', remaining)) {
return ParseResult.INTERNAL_ERROR;
}
StringTokenizer tok = new StringTokenizer(remaining, Constants.PIPE);
List<CDOMReference<PCTemplate>> list = new ArrayList<>();
List<CDOMReference<PCTemplate>> removelist = new ArrayList<>();
while (tok.hasMoreTokens()) {
String templKey = tok.nextToken();
if (specialLegal && templKey.endsWith(".REMOVE")) {
removelist.add(context.getReferenceContext().getCDOMReference(PCTEMPLATE_CLASS, templKey.substring(0, templKey.length() - 7)));
} else if (specialLegal && templKey.equals(Constants.LST_PERCENT_LIST)) {
context.getObjectContext().addToList(cdo, ListKey.NEW_CHOOSE_ACTOR, this);
} else {
ReferenceManufacturer<PCTemplate> rm = context.getReferenceContext().getManufacturer(PCTEMPLATE_CLASS);
CDOMReference<PCTemplate> ref = TokenUtilities.getTypeOrPrimitive(rm, templKey);
if (ref == null) {
return ParseResult.INTERNAL_ERROR;
}
list.add(ref);
}
}
if (consolidate) {
CDOMCompoundOrReference<PCTemplate> ref = new CDOMCompoundOrReference<>(PCTEMPLATE_CLASS, Constants.LST_CHOOSE_COLON);
for (CDOMReference<PCTemplate> r : list) {
ref.addReference(r);
}
ref.trimToSize();
list.clear();
list.add(ref);
}
for (CDOMReference<PCTemplate> ref : list) {
context.getObjectContext().addToList(cdo, lk, ref);
}
if (!removelist.isEmpty()) {
for (CDOMReference<PCTemplate> ref : removelist) {
context.getObjectContext().addToList(cdo, ListKey.REMOVE_TEMPLATES, ref);
}
}
return ParseResult.SUCCESS;
}
Aggregations