Search in sources :

Example 1 with BasicChooseInformation

use of pcgen.cdom.base.BasicChooseInformation in project pcgen by PCGen.

the class StringToken method parseToken.

@Override
public ParseResult parseToken(LoadContext context, CDOMObject obj, String value) {
    if (value == null || value.isEmpty()) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " must have arguments", context);
    }
    if (value.indexOf(',') != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not contain , : " + value, context);
    }
    if (value.indexOf('[') != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not contain [] : " + value, context);
    }
    if (value.charAt(0) == '|') {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not start with | : " + value, context);
    }
    if (value.charAt(value.length() - 1) == '|') {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments may not end with | : " + value, context);
    }
    if (value.indexOf("||") != -1) {
        return new ParseResult.Fail("CHOOSE:" + getTokenName() + " arguments uses double separator || : " + value, context);
    }
    StringTokenizer tok = new StringTokenizer(value, Constants.PIPE);
    Set<String> set = new HashSet<>();
    while (tok.hasMoreTokens()) {
        String tokString = tok.nextToken();
        set.add(tokString);
    }
    SimpleChoiceSet<String> scs = new SimpleChoiceSet<>(set, Constants.PIPE);
    BasicChooseInformation<String> tc = new BasicChooseInformation<>(getTokenName(), scs);
    tc.setTitle("Choose an Item");
    tc.setChoiceActor(this);
    context.getObjectContext().put(obj, ObjectKey.CHOOSE_INFO, tc);
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) BasicChooseInformation(pcgen.cdom.base.BasicChooseInformation) SimpleChoiceSet(pcgen.cdom.choiceset.SimpleChoiceSet) HashSet(java.util.HashSet)

Example 2 with BasicChooseInformation

use of pcgen.cdom.base.BasicChooseInformation 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

HashSet (java.util.HashSet)2 StringTokenizer (java.util.StringTokenizer)2 BasicChooseInformation (pcgen.cdom.base.BasicChooseInformation)2 PrimitiveCollection (pcgen.cdom.base.PrimitiveCollection)1 CollectionToChoiceSet (pcgen.cdom.choiceset.CollectionToChoiceSet)1 SimpleChoiceSet (pcgen.cdom.choiceset.SimpleChoiceSet)1