Search in sources :

Example 1 with CDOMGroupRef

use of pcgen.cdom.reference.CDOMGroupRef in project pcgen by PCGen.

the class ChangeprofLst method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    Changes<ChangeProf> changes = context.getObjectContext().getListChanges(obj, ListKey.CHANGEPROF);
    Collection<ChangeProf> added = changes.getAdded();
    if (added == null || added.isEmpty()) {
        // Zero indicates no Token
        return null;
    }
    HashMapToList<CDOMGroupRef<WeaponProf>, CDOMReference<WeaponProf>> m = new HashMapToList<>();
    for (ChangeProf cp : added) {
        CDOMReference<WeaponProf> source = cp.getSource();
        CDOMGroupRef<WeaponProf> result = cp.getResult();
        m.addToListFor(result, source);
    }
    SortedSet<CDOMReference<WeaponProf>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    Set<String> returnSet = new TreeSet<>();
    for (CDOMGroupRef<WeaponProf> result : m.getKeySet()) {
        StringBuilder sb = new StringBuilder();
        boolean needComma = false;
        set.clear();
        set.addAll(m.getListFor(result));
        for (CDOMReference<WeaponProf> source : set) {
            if (needComma) {
                sb.append(Constants.COMMA);
            }
            needComma = true;
            String sourceLst = source.getLSTformat(false);
            if (sourceLst.startsWith(Constants.LST_TYPE_EQUAL)) {
                sb.append(Constants.LST_TYPE_DOT);
                sb.append(sourceLst.substring(5));
            } else {
                sb.append(sourceLst);
            }
        }
        sb.append(Constants.EQUALS).append(result.getLSTformat(false).substring(5));
        returnSet.add(sb.toString());
    }
    return new String[] { StringUtil.join(returnSet, Constants.PIPE) };
}
Also used : WeaponProf(pcgen.core.WeaponProf) HashMapToList(pcgen.base.util.HashMapToList) ChangeProf(pcgen.cdom.content.ChangeProf) CDOMGroupRef(pcgen.cdom.reference.CDOMGroupRef) TreeSet(java.util.TreeSet) CDOMReference(pcgen.cdom.base.CDOMReference)

Example 2 with CDOMGroupRef

use of pcgen.cdom.reference.CDOMGroupRef 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

CDOMGroupRef (pcgen.cdom.reference.CDOMGroupRef)2 WeaponProf (pcgen.core.WeaponProf)2 StringTokenizer (java.util.StringTokenizer)1 TreeSet (java.util.TreeSet)1 HashMapToList (pcgen.base.util.HashMapToList)1 CDOMReference (pcgen.cdom.base.CDOMReference)1 ChangeProf (pcgen.cdom.content.ChangeProf)1 ConditionalSelectionActor (pcgen.cdom.content.ConditionalSelectionActor)1 WeaponProfProvider (pcgen.cdom.helper.WeaponProfProvider)1 CDOMSingleRef (pcgen.cdom.reference.CDOMSingleRef)1 QualifiedObject (pcgen.core.QualifiedObject)1 Prerequisite (pcgen.core.prereq.Prerequisite)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1