Search in sources :

Example 1 with PrimitiveCollection

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

the class ChoiceSetLoadUtilities method getChoiceSet.

public static <T extends CDOMObject> PrimitiveCollection<T> getChoiceSet(LoadContext context, SelectionCreator<T> sc, String joinedOr) {
    List<PrimitiveCollection<T>> orList = new ArrayList<>();
    ParsingSeparator pipe = new ParsingSeparator(joinedOr, '|');
    pipe.addGroupingPair('[', ']');
    pipe.addGroupingPair('(', ')');
    for (; pipe.hasNext(); ) {
        String joinedAnd = pipe.next();
        if (hasIllegalSeparator(',', joinedAnd)) {
            return null;
        }
        List<PrimitiveCollection<T>> andList = new ArrayList<>();
        ParsingSeparator comma = new ParsingSeparator(joinedAnd, ',');
        comma.addGroupingPair('[', ']');
        comma.addGroupingPair('(', ')');
        for (; comma.hasNext(); ) {
            String primitive = comma.next();
            if (primitive == null || primitive.isEmpty()) {
                Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was null or empty: " + primitive);
                return null;
            }
            QualifierToken<T> qual = getQualifier(context, sc, primitive);
            if (qual == null) {
                PrimitiveCollection<T> pcf = getSimplePrimitive(context, sc, primitive);
                if (pcf == null) {
                    Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was not valid: " + primitive);
                    return null;
                } else {
                    andList.add(pcf);
                }
            } else {
                andList.add(qual);
            }
        }
        if (!andList.isEmpty()) {
            if (andList.size() == 1) {
                orList.add(andList.get(0));
            } else {
                orList.add(new CompoundAndPrimitive<>(andList));
            }
        }
    }
    if (orList.isEmpty()) {
        return null;
    } else if (orList.size() == 1) {
        return orList.get(0);
    } else {
        return new CompoundOrPrimitive<>(orList);
    }
}
Also used : ParsingSeparator(pcgen.base.text.ParsingSeparator) ArrayList(java.util.ArrayList) PrimitiveCollection(pcgen.cdom.base.PrimitiveCollection)

Example 2 with PrimitiveCollection

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

Example 3 with PrimitiveCollection

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

the class ChoiceSetLoadUtilities method getPrimitive.

public static <T extends CDOMObject> PrimitiveCollection<T> getPrimitive(LoadContext context, SelectionCreator<T> sc, String joinedOr) {
    if (joinedOr.isEmpty() || hasIllegalSeparator('|', joinedOr)) {
        return null;
    }
    List<PrimitiveCollection<T>> pcfOrList = new ArrayList<>();
    ParsingSeparator pipe = new ParsingSeparator(joinedOr, '|');
    pipe.addGroupingPair('[', ']');
    pipe.addGroupingPair('(', ')');
    for (; pipe.hasNext(); ) {
        String joinedAnd = pipe.next();
        if (joinedAnd.isEmpty() || hasIllegalSeparator(',', joinedAnd)) {
            return null;
        }
        List<PrimitiveCollection<T>> pcfAndList = new ArrayList<>();
        ParsingSeparator comma = new ParsingSeparator(joinedAnd, ',');
        comma.addGroupingPair('[', ']');
        comma.addGroupingPair('(', ')');
        for (; comma.hasNext(); ) {
            String primitive = comma.next();
            if (primitive == null || primitive.isEmpty()) {
                Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was null or empty: " + primitive);
                return null;
            }
            PrimitiveCollection<T> pcf = getSimplePrimitive(context, sc, primitive);
            if (pcf == null) {
                Logging.addParseMessage(Logging.LST_ERROR, "Choice argument was not valid: " + primitive);
                return null;
            } else {
                pcfAndList.add(pcf);
            }
        }
        if (pcfAndList.size() == 1) {
            pcfOrList.add(pcfAndList.get(0));
        } else {
            pcfOrList.add(new CompoundAndPrimitive<>(pcfAndList));
        }
    }
    if (pcfOrList.size() == 1) {
        return pcfOrList.get(0);
    } else {
        return new CompoundOrPrimitive<>(pcfOrList);
    }
}
Also used : CompoundOrPrimitive(pcgen.cdom.primitive.CompoundOrPrimitive) ParsingSeparator(pcgen.base.text.ParsingSeparator) ArrayList(java.util.ArrayList) PrimitiveCollection(pcgen.cdom.base.PrimitiveCollection)

Aggregations

PrimitiveCollection (pcgen.cdom.base.PrimitiveCollection)3 ArrayList (java.util.ArrayList)2 ParsingSeparator (pcgen.base.text.ParsingSeparator)2 HashSet (java.util.HashSet)1 StringTokenizer (java.util.StringTokenizer)1 BasicChooseInformation (pcgen.cdom.base.BasicChooseInformation)1 CollectionToChoiceSet (pcgen.cdom.choiceset.CollectionToChoiceSet)1 CompoundOrPrimitive (pcgen.cdom.primitive.CompoundOrPrimitive)1