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;
}
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);
}
}
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;
}
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);
}
}
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);
}
Aggregations