Search in sources :

Example 46 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class ClassesToken method unparse.

@Override
public String[] unparse(LoadContext context, Spell spell) {
    DoubleKeyMapToList<Prerequisite, Integer, CDOMReference<ClassSpellList>> dkmtl = new DoubleKeyMapToList<>();
    List<String> list = new ArrayList<>();
    Changes<CDOMReference<ClassSpellList>> masterChanges = context.getListContext().getMasterListChanges(getTokenName(), spell, SPELLLIST_CLASS);
    if (masterChanges.includesGlobalClear()) {
        list.add(Constants.LST_DOT_CLEAR_ALL);
    }
    if (masterChanges.hasRemovedItems()) {
        for (CDOMReference<ClassSpellList> swl : masterChanges.getRemoved()) {
            AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
            MapToList<Spell, AssociatedPrereqObject> map = changes.getRemovedAssociations();
            if (map != null && !map.isEmpty()) {
                for (Spell added : map.getKeySet()) {
                    if (!spell.getLSTformat().equals(added.getLSTformat())) {
                        context.addWriteMessage("Spell " + getTokenName() + " token cannot remove another Spell " + "(must only remove itself)");
                        return null;
                    }
                    for (AssociatedPrereqObject assoc : map.getListFor(added)) {
                        List<Prerequisite> prereqs = assoc.getPrerequisiteList();
                        if (prereqs != null && !prereqs.isEmpty()) {
                            context.addWriteMessage("Incoming Remove " + "Edge to " + spell.getKeyName() + " had a " + "Prerequisite: " + prereqs.size());
                            return null;
                        }
                        dkmtl.addToListFor(null, -1, swl);
                    }
                }
            }
        }
    }
    for (CDOMReference<ClassSpellList> swl : masterChanges.getAdded()) {
        AssociatedChanges<Spell> changes = context.getListContext().getChangesInMasterList(getTokenName(), spell, swl);
        Collection<Spell> removedItems = changes.getRemoved();
        if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
            context.addWriteMessage(getTokenName() + " does not support .CLEAR.");
            return null;
        }
        MapToList<Spell, AssociatedPrereqObject> map = changes.getAddedAssociations();
        if (map != null && !map.isEmpty()) {
            for (Spell added : map.getKeySet()) {
                if (!spell.getLSTformat().equals(added.getLSTformat())) {
                    context.addWriteMessage("Spell " + getTokenName() + " token cannot allow another Spell " + "(must only allow itself)");
                    return null;
                }
                for (AssociatedPrereqObject assoc : map.getListFor(added)) {
                    List<Prerequisite> prereqs = assoc.getPrerequisiteList();
                    Prerequisite prereq;
                    if (prereqs == null || prereqs.isEmpty()) {
                        prereq = null;
                    } else if (prereqs.size() == 1) {
                        prereq = prereqs.get(0);
                    } else {
                        context.addWriteMessage("Incoming Edge to " + spell.getKeyName() + " had more than one " + "Prerequisite: " + prereqs.size());
                        return null;
                    }
                    Integer level = assoc.getAssociation(AssociationKey.SPELL_LEVEL);
                    if (level == null) {
                        context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had no Spell Level defined");
                        return null;
                    }
                    if (level.intValue() < 0) {
                        context.addWriteMessage("Incoming Allows Edge to " + spell.getKeyName() + " had invalid Level: " + level + ". Must be >= 0.");
                        return null;
                    }
                    dkmtl.addToListFor(prereq, level, swl);
                }
            }
        }
    }
    if (dkmtl.isEmpty()) {
        if (list.isEmpty()) {
            // Legal if no CLASSES was present in the Spell
            return null;
        } else {
            return list.toArray(new String[list.size()]);
        }
    }
    PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
    SortedSet<CDOMReference<ClassSpellList>> set = new TreeSet<>(ReferenceUtilities.REFERENCE_SORTER);
    SortedSet<Integer> levelSet = new TreeSet<>();
    for (Prerequisite prereq : dkmtl.getKeySet()) {
        StringBuilder sb = new StringBuilder();
        boolean needPipe = false;
        levelSet.clear();
        levelSet.addAll(dkmtl.getSecondaryKeySet(prereq));
        for (Integer i : levelSet) {
            set.clear();
            set.addAll(dkmtl.getListFor(prereq, i));
            if (needPipe) {
                sb.append(Constants.PIPE);
            }
            sb.append(ReferenceUtilities.joinLstFormat(set, Constants.COMMA));
            sb.append('=').append(i);
            needPipe = true;
        }
        if (prereq != null) {
            sb.append('[');
            StringWriter swriter = new StringWriter();
            try {
                prereqWriter.write(swriter, prereq);
            } catch (PersistenceLayerException e) {
                context.addWriteMessage("Error writing Prerequisite: " + e);
                return null;
            }
            sb.append(swriter.toString());
            sb.append(']');
        }
        list.add(sb.toString());
    }
    return list.toArray(new String[list.size()]);
}
Also used : PrerequisiteWriter(pcgen.persistence.lst.output.prereq.PrerequisiteWriter) ArrayList(java.util.ArrayList) Spell(pcgen.core.spell.Spell) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) AssociatedPrereqObject(pcgen.cdom.base.AssociatedPrereqObject) ClassSpellList(pcgen.cdom.list.ClassSpellList) DoubleKeyMapToList(pcgen.base.util.DoubleKeyMapToList) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) CDOMReference(pcgen.cdom.base.CDOMReference) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 47 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class HdToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCTemplate template, String value) {
    StringTokenizer tok = new StringTokenizer(value, Constants.COLON);
    String hdString = tok.nextToken();
    int minhd;
    int maxhd;
    try {
        int minusLoc = hdString.indexOf('-');
        if (minusLoc == -1) {
            int plusLoc = hdString.indexOf('+');
            if (plusLoc == 0) {
                return new ParseResult.Fail("Malformed " + getTokenName() + " Cannot start with +: " + hdString, context);
            } else if (plusLoc == hdString.length() - 1) {
                minhd = Integer.parseInt(hdString.substring(0, hdString.length() - 1));
                maxhd = Integer.MAX_VALUE;
            } else {
                minhd = Integer.parseInt(hdString);
                maxhd = minhd;
            }
        } else {
            minhd = Integer.parseInt(hdString.substring(0, minusLoc));
            maxhd = Integer.parseInt(hdString.substring(minusLoc + 1));
        }
        if (maxhd < minhd) {
            return new ParseResult.Fail("Malformed " + getTokenName() + " Token (Max < Min): " + hdString, context);
        }
    } catch (NumberFormatException ex) {
        return new ParseResult.Fail("Malformed " + getTokenName() + " Token (HD syntax invalid): " + hdString, context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has one): " + value, context);
    }
    String typeStr = tok.nextToken();
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has two): " + value, context);
    }
    String argument = tok.nextToken();
    PCTemplate derivative = new PCTemplate();
    derivative.put(ObjectKey.VISIBILITY, Visibility.HIDDEN);
    derivative.put(IntegerKey.HD_MIN, minhd);
    derivative.put(IntegerKey.HD_MAX, maxhd);
    context.getReferenceContext().getManufacturer(PCTemplate.class).addDerivativeObject(derivative);
    context.getObjectContext().addToList(template, ListKey.HD_TEMPLATES, derivative);
    try {
        if (context.processToken(derivative, typeStr, argument)) {
            return ParseResult.SUCCESS;
        }
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail(e.getMessage(), context);
    }
    return ParseResult.INTERNAL_ERROR;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) PCTemplate(pcgen.core.PCTemplate)

Example 48 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class LevelToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, PCTemplate template, String value) {
    StringTokenizer tok = new StringTokenizer(value, Constants.COLON);
    String levelStr = tok.nextToken();
    int plusLoc = levelStr.indexOf('+');
    if (plusLoc == 0) {
        return new ParseResult.Fail("Malformed " + getTokenName() + " Level cannot start with +: " + value, context);
    }
    int lvl;
    try {
        /*
			 * Note this test of integer (even if it doesn't get used outside
			 * this try) is necessary for catching errors.
			 */
        lvl = Integer.parseInt(levelStr);
        if (lvl <= 0) {
            ComplexParseResult cpr = new ComplexParseResult();
            cpr.addErrorMessage("Malformed " + getTokenName() + " Token (Level was <= 0): " + lvl);
            cpr.addErrorMessage("  Line was: " + value);
            return cpr;
        }
    } catch (NumberFormatException ex) {
        return new ParseResult.Fail("Misunderstood Level value: " + levelStr + " in " + getTokenName(), context);
    }
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has one): " + value, context);
    }
    String typeStr = tok.nextToken();
    if (!tok.hasMoreTokens()) {
        return new ParseResult.Fail("Invalid " + getTokenName() + ": requires 3 colon separated elements (has two): " + value, context);
    }
    String argument = tok.nextToken();
    PCTemplate derivative = new PCTemplate();
    derivative.put(ObjectKey.VISIBILITY, Visibility.HIDDEN);
    derivative.put(IntegerKey.LEVEL, lvl);
    context.getReferenceContext().getManufacturer(PCTemplate.class).addDerivativeObject(derivative);
    context.getObjectContext().addToList(template, ListKey.LEVEL_TEMPLATES, derivative);
    try {
        if (context.processToken(derivative, typeStr, argument)) {
            return ParseResult.SUCCESS;
        }
    } catch (PersistenceLayerException e) {
        return new ParseResult.Fail(e.getMessage(), context);
    }
    return ParseResult.INTERNAL_ERROR;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) ParseResult(pcgen.rules.persistence.token.ParseResult) ComplexParseResult(pcgen.rules.persistence.token.ComplexParseResult) PCTemplate(pcgen.core.PCTemplate)

Example 49 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class AbilityTokenTest method testWithChoose.

@Test
public void testWithChoose() {
    try {
        setUpPC();
        //Need to make sure we use the character related context
        context = Globals.getContext();
        context.getReferenceContext().importObject(AbilityCategory.FEAT);
        TokenRegistration.register(ADD_TOKEN);
        TokenRegistration.register(ADD_ABILITY_TOKEN);
    } catch (PersistenceLayerException e1) {
        fail("Cannot set up PC");
    }
    Ability item = construct("ChooseAbility");
    Ability parent = construct("Parent");
    context.getReferenceContext().constructCDOMObject(Language.class, "Foo");
    context.getReferenceContext().constructCDOMObject(Language.class, "Bar");
    context.getReferenceContext().constructCDOMObject(Language.class, "Goo");
    context.getReferenceContext().constructCDOMObject(Language.class, "Wow");
    context.getReferenceContext().constructCDOMObject(Language.class, "Rev");
    AbilityCategory ff = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Fighter Feat");
    ff.setAbilityCategory(CDOMDirectSingleRef.getRef(AbilityCategory.FEAT));
    AbilityCategory oc = context.getReferenceContext().constructCDOMObject(AbilityCategory.class, "Some Other Category");
    Ability badCA = context.getReferenceContext().constructCDOMObject(Ability.class, "ChooseAbility");
    context.getReferenceContext().reassociateCategory(oc, badCA);
    try {
        assertTrue(context.processToken(item, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
        assertTrue(context.processToken(item, "MULT", "Yes"));
        assertTrue(context.processToken(badCA, "CHOOSE", "LANG|Foo|Bar|Goo|Wow|Rev"));
        assertTrue(context.processToken(badCA, "MULT", "Yes"));
        assertTrue(context.processToken(parent, "ADD", "ABILITY|FEAT|NORMAL|ChooseAbility"));
    } catch (PersistenceLayerException e) {
        e.printStackTrace();
        fail();
    }
    finishLoad(context);
    PlayerCharacter pc = new PlayerCharacter();
    Object source = UserSelection.getInstance();
    CNAbilitySelection badCACAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(oc, Nature.AUTOMATIC, badCA), "Foo");
    CNAbilitySelection fooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.AUTOMATIC, item), "Foo");
    CNAbilitySelection barCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.VIRTUAL, item), "Bar");
    CNAbilitySelection gooCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Goo");
    CNAbilitySelection wowCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Wow");
    CNAbilitySelection wowFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Wow");
    CNAbilitySelection revCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(AbilityCategory.FEAT, Nature.NORMAL, item), "Rev");
    CNAbilitySelection revFFCAS = new CNAbilitySelection(CNAbilityFactory.getCNAbility(ff, Nature.NORMAL, item), "Rev");
    assertTrue(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(badCACAS, source);
    //Should have had no effect
    assertTrue(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(fooCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertTrue(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(barCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertTrue(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(gooCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertTrue(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(wowFFCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertFalse(pca.allow(wowCAS, pc, false));
    assertTrue(pca.allow(revFFCAS, pc, false));
    pc.applyAbility(revCAS, source);
    assertFalse(pca.allow(fooCAS, pc, false));
    assertFalse(pca.allow(barCAS, pc, false));
    assertFalse(pca.allow(gooCAS, pc, false));
    assertFalse(pca.allow(wowCAS, pc, false));
    assertFalse(pca.allow(revFFCAS, pc, false));
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Ability(pcgen.core.Ability) PlayerCharacter(pcgen.core.PlayerCharacter) CNAbilitySelection(pcgen.cdom.helper.CNAbilitySelection) AbilityCategory(pcgen.core.AbilityCategory) Test(org.junit.Test)

Example 50 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException 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)

Aggregations

PersistenceLayerException (pcgen.persistence.PersistenceLayerException)180 IOException (java.io.IOException)74 Prerequisite (pcgen.core.prereq.Prerequisite)62 StringWriter (java.io.StringWriter)17 StringTokenizer (java.util.StringTokenizer)17 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)15 LoadContext (pcgen.rules.context.LoadContext)14 URI (java.net.URI)12 Campaign (pcgen.core.Campaign)11 ParseResult (pcgen.rules.persistence.token.ParseResult)11 PCClass (pcgen.core.PCClass)10 CampaignSourceEntry (pcgen.persistence.lst.CampaignSourceEntry)10 PrerequisiteWriterInterface (pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface)10 URISyntaxException (java.net.URISyntaxException)8 TreeSet (java.util.TreeSet)8 CDOMReference (pcgen.cdom.base.CDOMReference)8 PlayerCharacter (pcgen.core.PlayerCharacter)8 PrerequisiteException (pcgen.core.prereq.PrerequisiteException)8 PCClassLoader (pcgen.persistence.lst.PCClassLoader)8