Search in sources :

Example 16 with QualifiedObject

use of pcgen.core.QualifiedObject in project pcgen by PCGen.

the class EquipToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<QualifiedObject<CDOMReference<Equipment>>> changes = context.getObjectContext().getListChanges(obj, ListKey.EQUIPMENT);
    Collection<QualifiedObject<CDOMReference<Equipment>>> added = changes.getAdded();
    HashMapToList<List<Prerequisite>, CDOMReference<Equipment>> m = new HashMapToList<>();
    if (added != null) {
        for (QualifiedObject<CDOMReference<Equipment>> qo : added) {
            m.addToListFor(qo.getPrerequisiteList(), qo.getRawObject());
        }
    }
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    list.add(cra.getLstFormat());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    for (List<Prerequisite> prereqs : m.getKeySet()) {
        List<CDOMReference<Equipment>> eq = m.getListFor(prereqs);
        WeightedCollection<CDOMReference<Equipment>> refs = new WeightedCollection<>(ReferenceUtilities.REFERENCE_SORTER);
        refs.addAll(eq);
        String ab = ReferenceUtilities.joinLstFormat(refs, Constants.PIPE);
        if (prereqs != null && !prereqs.isEmpty()) {
            if (prereqs.size() > 1) {
                context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                return null;
            }
            Prerequisite p = prereqs.get(0);
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, p);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            ab = ab + '|' + swriter.toString();
        }
        list.add(ab);
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) HashMapToList(pcgen.base.util.HashMapToList) Equipment(pcgen.core.Equipment) StringWriter(java.io.StringWriter) QualifiedObject(pcgen.core.QualifiedObject) WeightedCollection(pcgen.base.util.WeightedCollection) ArrayList(java.util.ArrayList) HashMapToList(pcgen.base.util.HashMapToList) List(java.util.List) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 17 with QualifiedObject

use of pcgen.core.QualifiedObject in project pcgen by PCGen.

the class WeaponProfToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) {
    String weaponProfs;
    // Do not initialize, null is significant!
    Prerequisite prereq = null;
    boolean isPre = false;
    if (value.indexOf("[") == -1) {
        // Supported version of PRExxx using |.  Needs to be at the front of the
        // Parsing code because many objects expect the pre to have been determined
        // Ahead of time.  Until deprecated code is removed, it will have to stay
        // like this.
        weaponProfs = value;
        StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
        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;
                weaponProfs = value.substring(0, preStart);
                isPre = true;
                ParseResult fail = checkForLoopPrereqs(prereq, context);
                if (fail != null) {
                    return fail;
                }
            }
        }
    } else {
        return new ParseResult.Fail("Use of [] for Prerequisites has been removed. " + "Please use | based standard", context);
    }
    ParseResult pr = checkForIllegalSeparator('|', weaponProfs);
    if (!pr.passed()) {
        return pr;
    }
    boolean foundAny = false;
    boolean foundOther = false;
    StringTokenizer tok = new StringTokenizer(weaponProfs, Constants.PIPE);
    WeaponProfProvider wpp = new WeaponProfProvider();
    while (tok.hasMoreTokens()) {
        String token = tok.nextToken();
        if (Constants.LST_PERCENT_LIST.equals(token)) {
            foundOther = true;
            ChooseSelectionActor<WeaponProf> cra;
            if (prereq == null) {
                cra = this;
            } else {
                ConditionalSelectionActor<WeaponProf> cca = new ConditionalSelectionActor<>(this);
                cca.addPrerequisite(prereq);
                cra = cca;
            }
            context.getObjectContext().addToList(obj, ListKey.NEW_CHOOSE_ACTOR, cra);
        } else if ("DEITYWEAPONS".equals(token)) {
            foundOther = true;
            context.getObjectContext().put(obj, ObjectKey.HAS_DEITY_WEAPONPROF, new QualifiedObject<>(Boolean.TRUE, prereq));
        } else {
            if (Constants.LST_ALL.equalsIgnoreCase(token)) {
                foundAny = true;
                CDOMGroupRef<WeaponProf> allRef = context.getReferenceContext().getCDOMAllReference(WEAPONPROF_CLASS);
                wpp.addWeaponProfAll(allRef);
            } else {
                foundOther = true;
                if (token.startsWith(Constants.LST_TYPE_DOT) || token.startsWith(Constants.LST_TYPE_EQUAL)) {
                    CDOMGroupRef<WeaponProf> rr = TokenUtilities.getTypeReference(context, WEAPONPROF_CLASS, token.substring(5));
                    if (rr == null) {
                        return ParseResult.INTERNAL_ERROR;
                    }
                    wpp.addWeaponProfType(rr);
                } else {
                    CDOMSingleRef<WeaponProf> ref = context.getReferenceContext().getCDOMReference(WEAPONPROF_CLASS, token);
                    wpp.addWeaponProf(ref);
                }
            }
        }
    }
    if (foundAny && foundOther) {
        return new ParseResult.Fail("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context);
    }
    if (!wpp.isEmpty()) {
        if (prereq != null) {
            wpp.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(obj, ListKey.WEAPONPROF, wpp);
    }
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) WeaponProf(pcgen.core.WeaponProf) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) StringTokenizer(java.util.StringTokenizer) ConditionalSelectionActor(pcgen.cdom.content.ConditionalSelectionActor) QualifiedObject(pcgen.core.QualifiedObject) CDOMGroupRef(pcgen.cdom.reference.CDOMGroupRef) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

QualifiedObject (pcgen.core.QualifiedObject)17 Prerequisite (pcgen.core.prereq.Prerequisite)10 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)9 StringTokenizer (java.util.StringTokenizer)6 Domain (pcgen.core.Domain)6 CDOMObject (pcgen.cdom.base.CDOMObject)5 ArrayList (java.util.ArrayList)3 CDOMReference (pcgen.cdom.base.CDOMReference)3 Vision (pcgen.core.Vision)3 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)3 PreParserFactory (pcgen.persistence.lst.prereq.PreParserFactory)3 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 Test (org.junit.Test)2 Formula (pcgen.base.formula.Formula)2 AssociatedPrereqObject (pcgen.cdom.base.AssociatedPrereqObject)2 ChooseSelectionActor (pcgen.cdom.base.ChooseSelectionActor)2