use of pcgen.cdom.reference.CategorizedCDOMReference in project pcgen by PCGen.
the class QualifyToken method unparse.
@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.QUALIFY);
if (changes == null || changes.isEmpty()) {
return null;
}
Collection<Qualifier> quals = changes.getAdded();
HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
for (Qualifier qual : quals) {
Class<? extends Loadable> cl = qual.getQualifiedClass();
String s = StringPClassUtil.getStringFor(cl);
CDOMSingleRef<?> ref = qual.getQualifiedReference();
String key = s;
if (ref instanceof CategorizedCDOMReference) {
Category<?> cat = ((CategorizedCDOMReference<?>) ref).getCDOMCategory();
key += '=' + cat.getKeyName();
}
map.addToListFor(key, ref);
}
Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
Set<String> returnSet = new TreeSet<>();
for (String key : map.getKeySet()) {
set.clear();
set.addAll(map.getListFor(key));
StringBuilder sb = new StringBuilder();
sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.PIPE));
returnSet.add(sb.toString());
}
return returnSet.toArray(new String[returnSet.size()]);
}
use of pcgen.cdom.reference.CategorizedCDOMReference in project pcgen by PCGen.
the class ForwardRefToken method unparse.
@Override
public String[] unparse(LoadContext context, Campaign obj) {
Changes<Qualifier> changes = context.getObjectContext().getListChanges(obj, ListKey.FORWARDREF);
if (changes == null || changes.isEmpty()) {
return null;
}
Collection<Qualifier> quals = changes.getAdded();
HashMapToList<String, CDOMSingleRef<?>> map = new HashMapToList<>();
for (Qualifier qual : quals) {
Class<? extends Loadable> cl = qual.getQualifiedClass();
String s = StringPClassUtil.getStringFor(cl);
CDOMSingleRef<?> ref = qual.getQualifiedReference();
String key = s;
if (ref instanceof CategorizedCDOMReference) {
String cat = ((CategorizedCDOMReference<?>) ref).getLSTCategory();
if (Constants.FEAT_CATEGORY.equals(cat)) {
key = Constants.FEAT_CATEGORY;
} else {
key += '=' + cat;
}
}
map.addToListFor(key, ref);
}
Set<CDOMSingleRef<?>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
Set<String> returnSet = new TreeSet<>();
for (String key : map.getKeySet()) {
set.clear();
set.addAll(map.getListFor(key));
StringBuilder sb = new StringBuilder();
sb.append(key).append(Constants.PIPE).append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
returnSet.add(sb.toString());
}
return returnSet.toArray(new String[returnSet.size()]);
}
use of pcgen.cdom.reference.CategorizedCDOMReference in project pcgen by PCGen.
the class FavClassConvertPlugin method process.
@Override
public String process(TokenProcessEvent tpe) {
String value = tpe.getValue();
if (!value.startsWith(Constants.LST_CHOOSE_COLON)) {
// Don't consume, force the default processor to do the work...
return null;
}
String choices = value.substring(7);
if (isEmpty(choices) || hasIllegalSeparator('|', choices)) {
return "Empty Token";
}
boolean foundAny = false;
boolean foundOther = false;
StringTokenizer tok = new StringTokenizer(choices, Constants.PIPE);
List<CDOMReference<? extends PCClass>> refList = new ArrayList<>();
LoadContext context = tpe.getContext();
while (tok.hasMoreTokens()) {
CDOMReference<? extends PCClass> ref;
String token = tok.nextToken();
if (Constants.LST_ALL.equalsIgnoreCase(token) || Constants.LST_ANY.equalsIgnoreCase(token)) {
foundAny = true;
ref = context.getReferenceContext().getCDOMAllReference(PCCLASS_CLASS);
} else {
foundOther = true;
int dotLoc = token.indexOf('.');
if (dotLoc == -1) {
// Primitive
ref = context.getReferenceContext().getCDOMReference(PCCLASS_CLASS, token);
} else {
// SubClass
String parent = token.substring(0, dotLoc);
String subclass = token.substring(dotLoc + 1);
SubClassCategory scc = SubClassCategory.getConstant(parent);
ref = context.getReferenceContext().getCDOMReference(SUBCLASS_CLASS, scc, subclass);
}
}
refList.add(ref);
}
if (foundAny && foundOther) {
return "Non-sensical " + getTokenName() + ": Contains ANY and a specific reference: " + value;
}
String name = tpe.getPrimary().get(StringKey.CONVERT_NAME);
// TODO Need a method of guaranteeing this name is unique?
String templName = "Race " + name + " Favored Class";
tpe.append("TEMPLATE:");
tpe.append(templName);
PCTemplate templ = new PCTemplate();
context.unconditionallyProcess(templ, "FAVOREDCLASS", "%LIST");
StringBuilder chooseValue = new StringBuilder();
chooseValue.append("CLASS|");
boolean first = true;
for (CDOMReference<? extends PCClass> ref : refList) {
if (!first) {
chooseValue.append(Constants.COMMA);
}
first = false;
Class<? extends PCClass> refClass = ref.getReferenceClass();
if (SUBCLASS_CLASS.equals(refClass)) {
Category<SubClass> parent = ((CategorizedCDOMReference<SubClass>) ref).getCDOMCategory();
chooseValue.append(parent);
chooseValue.append('.');
}
chooseValue.append(ref.getLSTformat(false));
}
context.unconditionallyProcess(templ, "CHOOSE", chooseValue.toString());
templ.setName(templName);
tpe.inject(templ);
tpe.consume();
return null;
}
Aggregations