Search in sources :

Example 71 with PersistenceLayerException

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

the class PreLegsWriter method write.

/**
	 * @see pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface#write(java.io.Writer, pcgen.core.prereq.Prerequisite)
	 */
@Override
public void write(Writer writer, Prerequisite prereq) throws PersistenceLayerException {
    try {
        writer.write("PRELEGS" + (prereq.isOverrideQualify() ? "Q:" : ""));
        writer.write(prereq.getOperator().toString().toUpperCase());
        writer.write(':');
        writer.write(prereq.getOperand());
    } catch (IOException e) {
        throw new PersistenceLayerException(e.getMessage());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) IOException(java.io.IOException)

Example 72 with PersistenceLayerException

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

the class AddClassSkillsTest method testGetChoicesListWithClassSkill.

/**
	 * Test method for 'pcgen.core.levelability.LevelAbilityClassSkills.getChoicesList(String, PlayerCharacter)'
	 */
@Test
public void testGetChoicesListWithClassSkill() {
    CampaignSourceEntry source;
    try {
        source = new CampaignSourceEntry(new Campaign(), new URI("file:/" + getClass().getName() + ".java"));
    } catch (URISyntaxException e) {
        throw new UnreachableError(e);
    }
    String classPCCText = "CLASS:Cleric	HD:8		TYPE:Base.PC	ABB:Clr\n" + "CLASS:Cleric	STARTSKILLPTS:2	CSKILL:KEY_Knowledge (Dungeoneering)";
    PCClass po;
    try {
        po = parsePCClassText(classPCCText, source);
    } catch (PersistenceLayerException e) {
        throw new UnreachableError(e);
    }
    getCharacter().incrementClassLevel(1, po, false);
    PCTemplate pct = new PCTemplate();
    Skill bluff = Globals.getContext().getReferenceContext().silentlyGetConstructedCDOMObject(Skill.class, "KEY_Bluff");
    pct.addToListFor(ListKey.CSKILL, CDOMDirectSingleRef.getRef(bluff));
    getCharacter().addTemplate(pct);
    Globals.getContext().unconditionallyProcess(po, "ADD", "CLASSSKILLS|2|KEY_Bluff,KEY_Listen,KEY_Knowledge (Arcana)");
    assertTrue(Globals.getContext().getReferenceContext().resolveReferences(null));
    List<PersistentTransitionChoice<?>> choiceList = po.getListFor(ListKey.ADD);
    assertEquals(1, choiceList.size());
    TransitionChoice<?> choice = choiceList.get(0);
    Collection<?> choiceSet = choice.getChoices().getSet(getCharacter());
    assertEquals(3, choiceSet.size());
    Set<Object> limitedSet = new HashSet<>();
    ClassSkillChoiceActor csca = new ClassSkillChoiceActor(po, 0);
    for (Object sc : choiceSet) {
        if (csca.allow((Skill) sc, getCharacter(), true)) {
            limitedSet.add(sc);
        }
    }
    assertEquals(2, limitedSet.size());
    assertEquals(2, choice.getCount().resolve(getCharacter(), ""));
    List<String> choiceStrings = new ArrayList<>();
    for (Object o : limitedSet) {
        choiceStrings.add(o.toString());
    }
    assertTrue(choiceStrings.contains("Listen"));
    assertTrue(choiceStrings.contains("Knowledge (Arcana)"));
}
Also used : PersistentTransitionChoice(pcgen.cdom.base.PersistentTransitionChoice) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) UnreachableError(pcgen.base.lang.UnreachableError) PCClass(pcgen.core.PCClass) URI(java.net.URI) CampaignSourceEntry(pcgen.persistence.lst.CampaignSourceEntry) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Skill(pcgen.core.Skill) Campaign(pcgen.core.Campaign) ClassSkillChoiceActor(pcgen.cdom.helper.ClassSkillChoiceActor) PCTemplate(pcgen.core.PCTemplate) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 73 with PersistenceLayerException

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

the class PreMultParserTest method testUnbalancedBracket.

@Test
public void testUnbalancedBracket() {
    PreMultParser parser = new PreMultParser();
    try {
        Prerequisite prereq = parser.parse("mult", "1,[PREPROFWITHARMOR:1,TYPE.Medium],[PREFEAT:1,Armor Proficiency (Medium)", false, false);
        fail("Expected unbalanced bracket to be detected.");
    } catch (PersistenceLayerException e) {
        assertEquals("Unbalanced [] in PREMULT '[PREPROFWITHARMOR:1,TYPE.Medium],[PREFEAT:1,Armor Proficiency (Medium)'.", e.getMessage());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Prerequisite(pcgen.core.prereq.Prerequisite) Test(org.junit.Test)

Example 74 with PersistenceLayerException

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

the class PreFactSetParser method parsePrereqListType.

@Override
protected void parsePrereqListType(Prerequisite prereq, String kind, String formula) throws PersistenceLayerException {
    // Sanity checking
    ParseResult parseResult = checkForIllegalSeparator(kind, ',', formula);
    if (!parseResult.passed()) {
        throw new PersistenceLayerException(parseResult.toString());
    }
    if (formula.contains("[") || formula.contains("]")) {
        throw new PersistenceLayerException("Prerequisite " + kind + " can not contain []: " + formula);
    }
    if (formula.contains("|")) {
        throw new PersistenceLayerException("Prerequisite " + kind + " can not contain |: " + formula);
    }
    String[] elements = formula.split(",");
    try {
        Integer.parseInt(elements[0]);
        if (elements.length == 1) {
            throw new PersistenceLayerException("Prerequisite " + kind + " can not have only a count: " + formula);
        }
    } catch (NumberFormatException nfe) {
        throw new PersistenceLayerException('\'' + elements[0] + "' is not a valid integer");
    }
    String filetype = elements[1];
    String[] fileElements = filetype.split("\\.");
    if (!OutputDB.isLegal(fileElements[0])) {
        throw new PersistenceLayerException('\'' + elements[1] + "' is not a valid location to check for a FACT");
    }
    for (int i = 2; i < elements.length; i++) {
        if (elements[i].indexOf('=') == -1) {
            throw new PersistenceLayerException("PREFACT require a target value, e.g. Key=Value, found: " + elements[i]);
        }
    }
    prereq.setOperand(elements[0]);
    if (elements.length == 3) {
        // We only have a number of prereqs to pass, and a single prereq so we do not want a
        // wrapper prereq around a list of 1 element.
        // i.e. 1,DEITY,PANTHEONS=Greek
        checkFactSetKey(elements[2]);
        prereq.setKey(elements[2]);
    } else {
        // Token now contains all of the possible matches,
        // min contains the target number (if there is one)
        // number contains the number of 'tokens' that be at least 'min'
        prereq.setOperator(PrerequisiteOperator.GTEQ);
        // we have more than one option, so use a group
        prereq.setKind(null);
        for (int i = 2; i < elements.length; i++) {
            Prerequisite subreq = new Prerequisite();
            subreq.setKind(kind.toLowerCase());
            subreq.setCountMultiples(true);
            // The element is either of the form "TYPE=foo" or "DEX=9"
            // if it is the later, we need to extract the '9'
            subreq.setOperator(PrerequisiteOperator.GTEQ);
            subreq.setCategoryName(filetype);
            checkFactSetKey(elements[i]);
            subreq.setKey(elements[i]);
            subreq.setOperand("1");
            prereq.addPrerequisite(subreq);
        }
    }
    setLocation(prereq, filetype);
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) ParseResult(pcgen.rules.persistence.token.ParseResult) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 75 with PersistenceLayerException

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

the class PreHDParser method processOldSyntax.

/**
	 * @param formula
	 * @param prereq
	 * @throws PersistenceLayerException
	 */
private static void processOldSyntax(String formula, Prerequisite prereq) throws PersistenceLayerException {
    int plusLoc = formula.indexOf('+');
    if (plusLoc == -1) {
        int minusLoc = formula.indexOf('-');
        if (minusLoc == -1) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' is not valid: It does not have either a + or a -");
        }
        if (minusLoc != formula.lastIndexOf('-')) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' is not valid: It has more than one -");
        }
        String hd1String = formula.substring(0, minusLoc);
        String hd2String = formula.substring(minusLoc + 1);
        try {
            Integer.parseInt(hd1String);
        } catch (NumberFormatException nfe) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' is not valid: " + hd1String + " is not an integer");
        }
        try {
            Integer.parseInt(hd2String);
        } catch (NumberFormatException nfe) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' is not valid: " + hd2String + " is not an integer");
        }
        Prerequisite minPrereq = new Prerequisite();
        minPrereq.setKind("hd");
        minPrereq.setOperator(PrerequisiteOperator.GTEQ);
        minPrereq.setOperand(hd1String);
        Prerequisite maxPrereq = new Prerequisite();
        maxPrereq.setKind("hd");
        maxPrereq.setOperator(PrerequisiteOperator.LTEQ);
        maxPrereq.setOperand(hd2String);
        // PREMULT
        prereq.setKind(null);
        prereq.setOperand("2");
        prereq.addPrerequisite(minPrereq);
        prereq.addPrerequisite(maxPrereq);
    } else {
        if (plusLoc != formula.length() - 1) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' has a + but does not end with it: It is not valid");
        }
        String hdString = formula.substring(0, plusLoc);
        try {
            int min = Integer.parseInt(hdString);
            prereq.setOperand(Integer.toString(min));
            prereq.setOperator(PrerequisiteOperator.GTEQ);
        } catch (NumberFormatException nfe) {
            throw new PersistenceLayerException("PREHD must be either 'x+' or 'x-y' where 'x' and 'y' are integers. '" + formula + "' is not valid: " + hdString + " is not an integer");
        }
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) 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