Search in sources :

Example 1 with CollectionToChoiceSet

use of pcgen.cdom.choiceset.CollectionToChoiceSet in project pcgen by PCGen.

the class ChooseFeatToken method parseTokenWithSeparator.

protected ParseResult parseTokenWithSeparator(LoadContext context, ReferenceManufacturer<Ability> rm, CDOMObject obj, String value) {
    int pipeLoc = value.lastIndexOf('|');
    String activeValue;
    String title;
    if (pipeLoc == -1) {
        activeValue = value;
        title = getDefaultTitle();
    } else {
        String titleString = value.substring(pipeLoc + 1);
        if (titleString.startsWith("TITLE=")) {
            title = titleString.substring(6);
            if (title.startsWith("\"")) {
                title = title.substring(1, title.length() - 1);
            }
            activeValue = value.substring(0, pipeLoc);
            if (title == null || title.isEmpty()) {
                return new ParseResult.Fail(getParentToken() + Constants.COLON + getTokenName() + " had TITLE= but no title: " + value, context);
            }
        } else {
            activeValue = value;
            title = getDefaultTitle();
        }
    }
    PrimitiveCollection<Ability> coll = context.getChoiceSet(rm, activeValue);
    if (coll == null) {
        return ParseResult.INTERNAL_ERROR;
    }
    if (!coll.getGroupingState().isValid()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Invalid combination of objects was used in: " + activeValue);
        cpr.addErrorMessage("  Check that ALL is not combined");
        cpr.addErrorMessage("  Check that a key is not joined with AND (,)");
        return cpr;
    }
    PrimitiveChoiceSet<Ability> pcs = new CollectionToChoiceSet<>(coll);
    //Tricky for compatibility...
    CategorizedChooseInformation<Ability> tc = new CategorizedChooseInformation<>("ABILITY", CDOMDirectSingleRef.getRef(AbilityCategory.FEAT), pcs, Ability.class);
    tc.setTitle(title);
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : Ability(pcgen.core.Ability) CategorizedChooseInformation(pcgen.cdom.base.CategorizedChooseInformation) CollectionToChoiceSet(pcgen.cdom.choiceset.CollectionToChoiceSet) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult)

Example 2 with CollectionToChoiceSet

use of pcgen.cdom.choiceset.CollectionToChoiceSet in project pcgen by PCGen.

the class AbilityToken method parseTokenWithSeparator.

protected ParseResult parseTokenWithSeparator(LoadContext context, ReferenceManufacturer<Ability> rm, CDOMSingleRef<AbilityCategory> acRef, CDOMObject obj, String value) {
    int pipeLoc = value.lastIndexOf('|');
    String activeValue;
    String title;
    if (pipeLoc == -1) {
        activeValue = value;
        title = getDefaultTitle();
    } else {
        String titleString = value.substring(pipeLoc + 1);
        if (titleString.startsWith("TITLE=")) {
            title = titleString.substring(6);
            if (title.startsWith("\"")) {
                title = title.substring(1, title.length() - 1);
            }
            activeValue = value.substring(0, pipeLoc);
            if (title == null || title.isEmpty()) {
                return new ParseResult.Fail(getParentToken() + Constants.COLON + getTokenName() + " had TITLE= but no title: " + value, context);
            }
        } else {
            activeValue = value;
            title = getDefaultTitle();
        }
    }
    PrimitiveCollection<Ability> coll = context.getChoiceSet(rm, activeValue);
    if (coll == null) {
        return ParseResult.INTERNAL_ERROR;
    }
    if (!coll.getGroupingState().isValid()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Invalid combination of objects was used in: " + activeValue);
        cpr.addErrorMessage("  Check that ALL is not combined");
        cpr.addErrorMessage("  Check that a key is not joined with AND (,)");
        return cpr;
    }
    PrimitiveChoiceSet<Ability> pcs = new CollectionToChoiceSet<>(coll);
    CategorizedChooseInformation<Ability> tc = new CategorizedChooseInformation<>(getTokenName(), acRef, pcs, Ability.class);
    tc.setTitle(title);
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : Ability(pcgen.core.Ability) CategorizedChooseInformation(pcgen.cdom.base.CategorizedChooseInformation) CollectionToChoiceSet(pcgen.cdom.choiceset.CollectionToChoiceSet) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult)

Example 3 with CollectionToChoiceSet

use of pcgen.cdom.choiceset.CollectionToChoiceSet in project pcgen by PCGen.

the class AbstractSimpleChooseToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, CDOMObject obj, String value) {
    int pipeLoc = value.lastIndexOf('|');
    String activeValue;
    String title;
    if (pipeLoc == -1) {
        activeValue = value;
        title = getDefaultTitle();
    } else {
        String titleString = value.substring(pipeLoc + 1);
        if (titleString.startsWith("TITLE=")) {
            title = titleString.substring(6);
            if (title.startsWith("\"")) {
                title = title.substring(1, title.length() - 1);
            }
            activeValue = value.substring(0, pipeLoc);
            if (title == null || title.isEmpty()) {
                return new ParseResult.Fail(getParentToken() + ':' + getTokenName() + " had TITLE= but no title: " + value, context);
            }
        } else {
            activeValue = value;
            title = getDefaultTitle();
        }
    }
    CDOMGroupRef<T> allReference = context.getReferenceContext().getCDOMAllReference(getChooseClass());
    PrimitiveCollection<T> prim;
    if (Constants.LST_ALL.equals(activeValue)) {
        prim = allReference;
    } else {
        if (hasIllegalSeparator('|', activeValue)) {
            return ParseResult.INTERNAL_ERROR;
        }
        Set<PrimitiveCollection<T>> set = new HashSet<>();
        StringTokenizer st = new StringTokenizer(activeValue, "|");
        while (st.hasMoreTokens()) {
            String tok = st.nextToken();
            PrimitiveCollection<T> ref = ChoiceSetLoadUtilities.getSimplePrimitive(context, getManufacturer(context), tok);
            if (ref == null) {
                return new ParseResult.Fail("Error: Count not get Reference for " + tok + " in " + getTokenName(), context);
            }
            if (!set.add(ref)) {
                return new ParseResult.Fail("Error, Found item: " + ref + " twice while parsing " + getTokenName(), context);
            }
        }
        if (set.isEmpty()) {
            return new ParseResult.Fail("No items in set.", context);
        }
        prim = new CompoundOrPrimitive<>(set);
    }
    if (!prim.getGroupingState().isValid()) {
        ComplexParseResult cpr = new ComplexParseResult();
        cpr.addErrorMessage("Invalid combination of objects was used in: " + activeValue);
        cpr.addErrorMessage("  Check that ALL is not combined with another item");
        return cpr;
    }
    PrimitiveChoiceSet<T> pcs = new CollectionToChoiceSet<>(prim);
    BasicChooseInformation<T> tc = new BasicChooseInformation<>(getTokenName(), pcs);
    tc.setTitle(title);
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : BasicChooseInformation(pcgen.cdom.base.BasicChooseInformation) StringTokenizer(java.util.StringTokenizer) CollectionToChoiceSet(pcgen.cdom.choiceset.CollectionToChoiceSet) PrimitiveCollection(pcgen.cdom.base.PrimitiveCollection) HashSet(java.util.HashSet)

Aggregations

CollectionToChoiceSet (pcgen.cdom.choiceset.CollectionToChoiceSet)3 CategorizedChooseInformation (pcgen.cdom.base.CategorizedChooseInformation)2 Ability (pcgen.core.Ability)2 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)2 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 BasicChooseInformation (pcgen.cdom.base.BasicChooseInformation)1 PrimitiveCollection (pcgen.cdom.base.PrimitiveCollection)1