Search in sources :

Example 6 with QualifiedObject

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

the class DomainTokenTest method testUnparseSinglePre.

@Test
public void testUnparseSinglePre() throws PersistenceLayerException {
    Domain wp1 = construct(primaryContext, "TestWP1");
    CDOMDirectSingleRef<Domain> ref = CDOMDirectSingleRef.getRef(wp1);
    PreParserFactory prereqParser = PreParserFactory.getInstance();
    Prerequisite prereq = prereqParser.parse("PRERACE:1,Dwarf");
    assertNotNull(prereq);
    QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(ref, prereq);
    primaryProf.addToListFor(ListKey.DOMAIN, qo);
    String[] unparsed = getToken().unparse(primaryContext, primaryProf);
    expectSingle(unparsed, getLegalValue() + "|PRERACE:1,Dwarf");
}
Also used : PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Prerequisite(pcgen.core.prereq.Prerequisite) Test(org.junit.Test)

Example 7 with QualifiedObject

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

the class LangToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    Changes<QualifiedObject<CDOMReference<Language>>> changes = context.getObjectContext().getListChanges(obj, ListKey.AUTO_LANGUAGE);
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Collection<QualifiedObject<CDOMReference<Language>>> added = changes.getAdded();
    StringBuilder sb = new StringBuilder();
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    boolean foundAny = false;
    boolean foundOther = false;
    if (changes.includesGlobalClear()) {
        sb.append(Constants.LST_DOT_CLEAR);
    }
    if (listAdded != null && !listAdded.isEmpty()) {
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    if (sb.length() > 0) {
                        sb.append('|');
                    }
                    sb.append(cra.getLstFormat());
                    foundOther = true;
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    if (added != null) {
        boolean needPipe = sb.length() > 0;
        Prerequisite prereq = null;
        for (QualifiedObject<CDOMReference<Language>> spp : added) {
            CDOMReference<Language> lang = spp.getRawObject();
            List<Prerequisite> prereqs = spp.getPrerequisiteList();
            String ab = lang.getLSTformat(false);
            boolean isUnconditionalAll = Constants.LST_ALL.equals(ab);
            foundAny |= isUnconditionalAll;
            foundOther |= !isUnconditionalAll;
            if (needPipe) {
                sb.append('|');
            }
            needPipe = true;
            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);
                if (prereq != null && !p.equals(prereq)) {
                    context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had differing Prerequisites for " + getFullName());
                    return null;
                }
                prereq = p;
            }
            sb.append(ab);
        }
        if (prereq != null) {
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, prereq);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            sb.append('|').append(swriter.toString());
        }
    }
    if (foundAny && foundOther) {
        context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + sb);
        return null;
    }
    if (sb.length() == 0) {
        // okay
        return null;
    }
    return new String[] { sb.toString() };
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Language(pcgen.core.Language) StringWriter(java.io.StringWriter) QualifiedObject(pcgen.core.QualifiedObject) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 8 with QualifiedObject

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

the class VisionFacetTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    CDOMObject cdo1 = new PCTemplate();
    cdo1.setName("Template1");
    CDOMObject cdo2 = new Race();
    cdo2.setName("Race1");
    Vision vision1 = new Vision(VisionType.getVisionType("Normal"), FormulaFactory.getFormulaFor(30));
    Vision vision2 = new Vision(VisionType.getVisionType("Darkvision"), FormulaFactory.getFormulaFor(20));
    CDOMDirectSingleRef<Vision> ref1 = new CDOMDirectSingleRef<>(vision1);
    SimpleAssociatedObject apo1 = new SimpleAssociatedObject();
    cdo1.putToList(Vision.VISIONLIST, ref1, apo1);
    CDOMDirectSingleRef<Vision> ref2 = new CDOMDirectSingleRef<>(vision2);
    SimpleAssociatedObject apo2 = new SimpleAssociatedObject();
    cdo2.putToList(Vision.VISIONLIST, ref2, apo2);
    QualifiedObject<Vision> st1 = new QualifiedObject<>(vision1);
    QualifiedObject<Vision> st2 = new QualifiedObject<>(vision2);
    source = new CDOMObject[] { cdo1, cdo2 };
    target = new QualifiedObject[] { st1, st2 };
}
Also used : CDOMDirectSingleRef(pcgen.cdom.reference.CDOMDirectSingleRef) QualifiedObject(pcgen.core.QualifiedObject) CDOMObject(pcgen.cdom.base.CDOMObject) Vision(pcgen.core.Vision) Race(pcgen.core.Race) PCTemplate(pcgen.core.PCTemplate) SimpleAssociatedObject(pcgen.cdom.base.SimpleAssociatedObject)

Example 9 with QualifiedObject

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

the class DomainTokenTest method testUnparseSinglePre.

@Test
public void testUnparseSinglePre() throws PersistenceLayerException {
    Domain wp1 = construct(primaryContext, "TestWP1");
    CDOMDirectSingleRef<Domain> ref = CDOMDirectSingleRef.getRef(wp1);
    PreParserFactory prereqParser = PreParserFactory.getInstance();
    Prerequisite prereq = prereqParser.parse("PRERACE:1,Dwarf");
    assertNotNull(prereq);
    QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(ref, prereq);
    primaryProf.addToListFor(ListKey.DOMAIN, qo);
    String[] unparsed = getToken().unparse(primaryContext, primaryProf);
    expectSingle(unparsed, getLegalValue() + "|PRERACE:1,Dwarf");
}
Also used : PreParserFactory(pcgen.persistence.lst.prereq.PreParserFactory) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) Prerequisite(pcgen.core.prereq.Prerequisite) Test(org.junit.Test)

Example 10 with QualifiedObject

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

the class ClassLevelDomainBracketToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCClassLevel level, String value) {
    Logging.deprecationPrint(getMessage(level, value));
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    boolean first = true;
    while (pipeTok.hasMoreTokens()) {
        String tok = pipeTok.nextToken();
        if (Constants.LST_DOT_CLEAR.equals(tok)) {
            if (!first) {
                return new ParseResult.Fail("  Non-sensical " + getTokenName() + ": .CLEAR was not the first list item", context);
            }
            context.getObjectContext().removeList(level, ListKey.DOMAIN);
            continue;
        }
        // Note: May contain PRExxx
        String domainKey;
        Prerequisite prereq = null;
        int openBracketLoc = tok.indexOf('[');
        if (openBracketLoc == -1) {
            if (tok.indexOf(']') != -1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must have '[' if it contains a PREREQ tag", context);
            }
            domainKey = tok;
        } else {
            if (tok.lastIndexOf(']') != tok.length() - 1) {
                return new ParseResult.Fail("Invalid " + getTokenName() + " must end with ']' if it contains a PREREQ tag", context);
            }
            domainKey = tok.substring(0, openBracketLoc);
            String prereqString = tok.substring(openBracketLoc + 1, tok.length() - 1);
            if (prereqString.isEmpty()) {
                return new ParseResult.Fail(getTokenName() + " cannot have empty prerequisite : " + value, context);
            }
            prereq = getPrerequisite(prereqString);
            if (prereq == null) {
                return new ParseResult.Fail(getTokenName() + " had invalid prerequisite : " + prereqString, context);
            }
        }
        CDOMSingleRef<Domain> domain = context.getReferenceContext().getCDOMReference(DOMAIN_CLASS, domainKey);
        QualifiedObject<CDOMSingleRef<Domain>> qo = new QualifiedObject<>(domain);
        if (prereq != null) {
            qo.addPrerequisite(prereq);
        }
        context.getObjectContext().addToList(level, ListKey.DOMAIN, qo);
        first = false;
    }
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) QualifiedObject(pcgen.core.QualifiedObject) Domain(pcgen.core.Domain) CDOMSingleRef(pcgen.cdom.reference.CDOMSingleRef) 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