Search in sources :

Example 1 with CategorizedCDOMReference

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()]);
}
Also used : CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference) HashMapToList(pcgen.base.util.HashMapToList) TreeSet(java.util.TreeSet) Qualifier(pcgen.cdom.reference.Qualifier)

Example 2 with CategorizedCDOMReference

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()]);
}
Also used : CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference) HashMapToList(pcgen.base.util.HashMapToList) TreeSet(java.util.TreeSet) Qualifier(pcgen.cdom.reference.Qualifier)

Example 3 with CategorizedCDOMReference

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;
}
Also used : SubClassCategory(pcgen.cdom.enumeration.SubClassCategory) SubClass(pcgen.core.SubClass) ArrayList(java.util.ArrayList) PCClass(pcgen.core.PCClass) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference) StringTokenizer(java.util.StringTokenizer) LoadContext(pcgen.rules.context.LoadContext) PCTemplate(pcgen.core.PCTemplate) CDOMReference(pcgen.cdom.base.CDOMReference) CategorizedCDOMReference(pcgen.cdom.reference.CategorizedCDOMReference)

Aggregations

CategorizedCDOMReference (pcgen.cdom.reference.CategorizedCDOMReference)3 TreeSet (java.util.TreeSet)2 HashMapToList (pcgen.base.util.HashMapToList)2 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)2 Qualifier (pcgen.cdom.reference.Qualifier)2 ArrayList (java.util.ArrayList)1 StringTokenizer (java.util.StringTokenizer)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 SubClassCategory (pcgen.cdom.enumeration.SubClassCategory)1 PCClass (pcgen.core.PCClass)1 PCTemplate (pcgen.core.PCTemplate)1 SubClass (pcgen.core.SubClass)1 LoadContext (pcgen.rules.context.LoadContext)1