Search in sources :

Example 91 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class ContainsToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, Equipment eq, String value) {
    StringTokenizer pipeTok = new StringTokenizer(value, Constants.PIPE);
    context.getObjectContext().removeList(eq, ListKey.CAPACITY);
    String weightCapacity = pipeTok.nextToken();
    boolean hadAsterisk = false;
    if (weightCapacity.charAt(0) == Constants.CHAR_ASTERISK) {
        hadAsterisk = true;
        context.getObjectContext().put(eq, ObjectKey.CONTAINER_CONSTANT_WEIGHT, Boolean.TRUE);
        weightCapacity = weightCapacity.substring(1);
    }
    int percentLoc = weightCapacity.indexOf(Constants.PERCENT);
    if (percentLoc != weightCapacity.lastIndexOf(Constants.PERCENT)) {
        return new ParseResult.Fail("Cannot have two weight reduction " + "characters (indicated by %): " + value, context);
    }
    if (percentLoc != -1) {
        if (hadAsterisk) {
            return new ParseResult.Fail("Cannot have Constant Weight (indicated by *) " + "and weight reduction (indicated by %): " + value, context);
        }
        String redString = weightCapacity.substring(0, percentLoc);
        weightCapacity = weightCapacity.substring(percentLoc + 1);
        try {
            context.getObjectContext().put(eq, IntegerKey.CONTAINER_REDUCE_WEIGHT, Integer.valueOf(redString));
        } catch (NumberFormatException ex) {
            return new ParseResult.Fail("Weight Reduction (indicated by %) must be an integer: " + value, context);
        }
    }
    BigDecimal weightCap;
    if ("UNLIM".equals(weightCapacity)) {
        weightCap = Capacity.UNLIMITED;
    } else {
        try {
            weightCap = BigDecimalHelper.trimBigDecimal(new BigDecimal(weightCapacity));
            if (BigDecimal.ZERO.compareTo(weightCap) > 0) {
                return new ParseResult.Fail("Weight Capacity must be >= 0: " + weightCapacity + "\n  Use 'UNLIM' (not -1) for unlimited Count", context);
            }
        } catch (NumberFormatException ex) {
            return new ParseResult.Fail("Weight Capacity must be 'UNLIM or a number >= 0: " + weightCapacity, context);
        }
    }
    context.getObjectContext().put(eq, ObjectKey.CONTAINER_WEIGHT_CAPACITY, weightCap);
    Capacity totalCap = null;
    boolean limited = true;
    if (!pipeTok.hasMoreTokens()) {
        limited = false;
        totalCap = Capacity.ANY;
    }
    BigDecimal limitedCapacity = BigDecimal.ZERO;
    while (pipeTok.hasMoreTokens()) {
        String typeString = pipeTok.nextToken();
        int equalLoc = typeString.indexOf(Constants.EQUALS);
        if (equalLoc != typeString.lastIndexOf(Constants.EQUALS)) {
            return new ParseResult.Fail("Two many = signs", context);
        }
        if (equalLoc == -1) {
            limited = false;
            context.getObjectContext().addToList(eq, ListKey.CAPACITY, new Capacity(typeString, Capacity.UNLIMITED));
        } else {
            String itemType = typeString.substring(0, equalLoc);
            String itemNumString = typeString.substring(equalLoc + 1);
            BigDecimal itemNumber;
            if ("UNLIM".equals(itemNumString)) {
                limited = false;
                itemNumber = Capacity.UNLIMITED;
            } else {
                try {
                    itemNumber = BigDecimalHelper.trimBigDecimal(new BigDecimal(itemNumString));
                } catch (NumberFormatException ex) {
                    return new ParseResult.Fail("Item Number for " + itemType + " must be 'UNLIM' or a number > 0: " + itemNumString, context);
                }
                if (BigDecimal.ZERO.compareTo(itemNumber) >= 0) {
                    return new ParseResult.Fail("Cannot have negative quantity of " + itemType + ": " + value, context);
                }
            }
            if (limited) {
                limitedCapacity = limitedCapacity.add(itemNumber);
            }
            context.getObjectContext().addToList(eq, ListKey.CAPACITY, new Capacity(itemType, itemNumber));
        }
    }
    if (totalCap == null) {
        BigDecimal totalCapacity = limited ? limitedCapacity : Capacity.UNLIMITED;
        totalCap = Capacity.getTotalCapacity(totalCapacity);
    }
    context.getObjectContext().put(eq, ObjectKey.TOTAL_CAPACITY, totalCap);
    return ParseResult.SUCCESS;
}
Also used : StringTokenizer(java.util.StringTokenizer) ParseResult(pcgen.rules.persistence.token.ParseResult) Capacity(pcgen.cdom.helper.Capacity) BigDecimal(java.math.BigDecimal)

Example 92 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class WtToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Equipment eq, String value) {
    try {
        BigDecimal weight = new BigDecimal(value);
        if (weight.compareTo(BigDecimal.ZERO) < 0) {
            return new ParseResult.Fail(getTokenName() + " was expecting a decimal value >= 0 : " + value, context);
        }
        context.getObjectContext().put(eq, ObjectKey.WEIGHT, weight);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException nfe) {
        return new ParseResult.Fail("Expected a Double for " + getTokenName() + ": " + value, context);
    }
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) BigDecimal(java.math.BigDecimal)

Example 93 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class ArmortypeToken method parseTokenWithSeparator.

@Override
protected ParseResult parseTokenWithSeparator(LoadContext context, EquipmentModifier mod, String value) {
    int pipeLoc = value.indexOf(Constants.PIPE);
    ChangeArmorType cat;
    if (pipeLoc == -1) {
        return new ParseResult.Fail(getTokenName() + " has no PIPE character: Must be of the form old|new", context);
    } else if (pipeLoc != value.lastIndexOf(Constants.PIPE)) {
        return new ParseResult.Fail(getTokenName() + " has too many PIPE characters: " + "Must be of the form old|new", context);
    } else {
        /*
			 * TODO Are the ArmorTypes really a subset of Encumbrence?
			 */
        String oldType = value.substring(0, pipeLoc);
        String newType = value.substring(pipeLoc + 1);
        /*
			 * TODO Need some check if the Armor Types in value are not valid...
			 */
        cat = new ChangeArmorType(oldType, newType);
    }
    context.getObjectContext().addToList(mod, ListKey.ARMORTYPE, cat);
    return ParseResult.SUCCESS;
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) ChangeArmorType(pcgen.cdom.processor.ChangeArmorType)

Example 94 with ParseResult

use of pcgen.rules.persistence.token.ParseResult in project pcgen by PCGen.

the class CostToken method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, Equipment eq, String value) {
    try {
        BigDecimal cost = new BigDecimal(value);
        // CONSIDER This apparently is not a requirement, since some items
        // in the RSRD have negative COST?
        //
        // if (cost.compareTo(BigDecimal.ZERO) < 0)
        // {
        // return new ParseResult.Fail(getTokenName()
        // + " must be a positive number: " + value, context);
        // return false;
        // }
        context.getObjectContext().put(eq, ObjectKey.COST, cost);
        return ParseResult.SUCCESS;
    } catch (NumberFormatException e) {
        return new ParseResult.Fail(getTokenName() + " expected a number: " + value, context);
    }
}
Also used : ParseResult(pcgen.rules.persistence.token.ParseResult) BigDecimal(java.math.BigDecimal)

Example 95 with ParseResult

use of pcgen.rules.persistence.token.ParseResult 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)

Aggregations

ParseResult (pcgen.rules.persistence.token.ParseResult)210 StringTokenizer (java.util.StringTokenizer)68 Test (org.junit.Test)45 CDOMReference (pcgen.cdom.base.CDOMReference)30 AbstractTokenModelTest (tokenmodel.testsupport.AbstractTokenModelTest)26 Ability (pcgen.core.Ability)21 ArrayList (java.util.ArrayList)18 Formula (pcgen.base.formula.Formula)18 ParsingSeparator (pcgen.base.text.ParsingSeparator)18 Prerequisite (pcgen.core.prereq.Prerequisite)18 PCClass (pcgen.core.PCClass)17 Ungranted (pcgen.cdom.base.Ungranted)15 PCTemplate (pcgen.core.PCTemplate)14 CNAbility (pcgen.cdom.content.CNAbility)13 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)11 ComplexParseResult (pcgen.rules.persistence.token.ComplexParseResult)11 BigDecimal (java.math.BigDecimal)10 Race (pcgen.core.Race)10 ConcretePersistentTransitionChoice (pcgen.cdom.base.ConcretePersistentTransitionChoice)9 LegalScope (pcgen.base.formula.base.LegalScope)8