Search in sources :

Example 26 with ChooseSelectionActor

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

the class LangToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    String lang = value;
    ParseResult pr = checkSeparatorsAndNonEmpty('|', lang);
    if (!pr.passed()) {
        return pr;
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(lang, Constants.PIPE);
    boolean isPre = false;
    // Do not initialize, null is significant!
    Prerequisite prereq = null;
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (PreParserFactory.isPreReqString(token)) {
            if (isPre) {
                String errorText = "Invalid " + getTokenName() + ": " + value + "  PRExxx must be at the END of the Token";
                Logging.errorPrint(errorText);
                return new ParseResult.Fail(errorText, context);
            }
            prereq = getPrerequisite(token);
            if (prereq == null) {
                return new ParseResult.Fail("Error generating Prerequisite " + prereq + " in " + getFullName(), context);
            }
            int preStart = value.indexOf(token) - 1;
            lang = value.substring(0, preStart);
            isPre = true;
        }
    }
    boolean firstToken = true;
    tok = new StringTokenizer(lang, Constants.PIPE);
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(token)) {
            if (!firstToken) {
                return new ParseResult.Fail("Non-sensical situation was " + "encountered while parsing " + getTokenName() + ": When used, .CLEAR must be the first argument", context);
            }
            context.getObjectContext().removeList(obj, ListKey.AUTO_LANGUAGE);
        } else if (Constants.LST_PERCENT_LIST.equals(token)) {
            ChooseSelectionActor<Language> cra;
            if (prereq == null) {
                cra = this;
            } else {
                ConditionalSelectionActor<Language> cca = new ConditionalSelectionActor<>(this);
                cca.addPrerequisite(prereq);
                cra = cca;
            }
            foundOther = true;
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
        } else if (Constants.LST_ALL.equals(token)) {
            foundAny = true;
            context.getObjectContext().addToList(obj, ListKey.AUTO_LANGUAGE, new QualifiedObject<>(context.getReferenceContext().getCDOMAllReference(LANGUAGE_CLASS), prereq));
        } else {
            foundOther = true;
            CDOMReference<Language> ref = TokenUtilities.getTypeOrPrimitive(context, LANGUAGE_CLASS, token);
            if (ref == null) {
                return new ParseResult.Fail("  Error was encountered while parsing " + getTokenName(), context);
            }
            context.getObjectContext().addToList(obj, ListKey.AUTO_LANGUAGE, new QualifiedObject<>(ref, prereq));
        }
        firstToken = false;
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) StringTokenizer(java.util.StringTokenizer) ConditionalSelectionActor(pcgen.cdom.content.ConditionalSelectionActor) Language(pcgen.core.Language) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 27 with ChooseSelectionActor

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

the class ShieldProfToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<ShieldProfProvider> changes = context.getObjectContext().getListChanges(obj, ListKey.AUTO_SHIELDPROF);
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Collection<ShieldProfProvider> added = changes.getAdded();
    Set<String> set = new TreeSet<>();
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    boolean foundAny = false;
    boolean foundOther = false;
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    set.add(cra.getLstFormat());
                    foundOther = true;
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    if (added != null) {
        for (ShieldProfProvider spp : added) {
            StringBuilder sb = new StringBuilder();
            sb.append(spp.getLstFormat());
            if (spp.hasPrerequisites()) {
                sb.append('|');
                sb.append(getPrerequisiteString(context, spp.getPrerequisiteList()));
            }
            String ab = sb.toString();
            boolean isUnconditionalAll = Constants.LST_ALL.equals(ab);
            foundAny |= isUnconditionalAll;
            foundOther |= !isUnconditionalAll;
            set.add(ab);
        }
    }
    if (foundAny && foundOther) {
        context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + set);
        return null;
    }
    if (set.isEmpty()) {
        //okay
        return null;
    }
    return set.toArray(new String[set.size()]);
}
Also used : ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) TreeSet(java.util.TreeSet) ShieldProfProvider(pcgen.cdom.helper.ShieldProfProvider)

Aggregations

ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)27 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)7 Prerequisite (pcgen.core.prereq.Prerequisite)6 ArrayList (java.util.ArrayList)5 CDOMReference (pcgen.cdom.base.CDOMReference)5 TreeSet (java.util.TreeSet)3 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)3 StringWriter (java.io.StringWriter)2 List (java.util.List)2 StringTokenizer (java.util.StringTokenizer)2 ListKey (pcgen.cdom.enumeration.ListKey)2 Nature (pcgen.cdom.enumeration.Nature)2 AbilitySelector (pcgen.cdom.helper.AbilitySelector)2 AbilityTargetSelector (pcgen.cdom.helper.AbilityTargetSelector)2 AbilityList (pcgen.cdom.list.AbilityList)2 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)2 Ability (pcgen.core.Ability)2 AbilityCategory (pcgen.core.AbilityCategory)2 Language (pcgen.core.Language)2 QualifiedObject (pcgen.core.QualifiedObject)2