use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PreLevelParser method parse.
/**
* Parse the pre req list
*
* @param kind The kind of the prerequisite (less the "PRE" prefix)
* @param formula The body of the prerequisite.
* @param invertResult Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite
* of any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
StringTokenizer tok = new StringTokenizer(formula, ",");
Prerequisite maxPrereq = new Prerequisite();
Prerequisite minPrereq = new Prerequisite();
boolean hasMin = false;
boolean hasMax = false;
while (tok.hasMoreTokens()) {
String value = tok.nextToken();
String[] vals = value.split("=");
if (vals.length != 2) {
throw new PersistenceLayerException("PRELEVEL must be either 'MIN=x', 'MAX=y' or " + "'MIN=x,MAX=y' where 'x' and 'y' are integers. '" + formula + "' is not valid. ");
}
String token = vals[0];
String hdVal = vals[1];
try {
Integer.parseInt(hdVal);
} catch (NumberFormatException nfe) {
throw new PersistenceLayerException("PRELEVEL must be either 'MIN=x', 'MAX=y' or " + "'MIN=x,MAX=y' where 'x' and 'y' are integers. '" + formula + "' is not valid: " + hdVal + " is not an integer");
}
if (token.equals("MIN")) {
minPrereq.setKind("level");
minPrereq.setOperator(PrerequisiteOperator.GTEQ);
minPrereq.setOperand(hdVal);
hasMin = true;
}
if (token.equals("MAX")) {
maxPrereq.setKind("level");
maxPrereq.setOperator(PrerequisiteOperator.LTEQ);
maxPrereq.setOperand(hdVal);
hasMax = true;
}
}
if (hasMin && hasMax) {
// PREMULT
prereq.setKind(null);
prereq.setOperand("2");
prereq.addPrerequisite(minPrereq);
prereq.addPrerequisite(maxPrereq);
} else if (hasMin) {
prereq = minPrereq;
} else if (hasMax) {
prereq = maxPrereq;
}
if (invertResult) {
prereq.setOperator(prereq.getOperator().invert());
}
return prereq;
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PrePCLevelParser method parse.
/**
* Parse the pre req list
*
* @param kind The kind of the prerequisite (less the "PRE" prefix)
* @param formula The body of the prerequisite.
* @param invertResult Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite
* of any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
if (formula.contains("MIN") || formula.contains("MAX")) {
StringTokenizer tok = new StringTokenizer(formula, ",");
Prerequisite maxPrereq = new Prerequisite();
Prerequisite minPrereq = new Prerequisite();
boolean hasMin = false;
boolean hasMax = false;
while (tok.hasMoreTokens()) {
String value = tok.nextToken();
String[] vals = value.split("=");
if (vals.length != 2) {
throw new PersistenceLayerException("PREPCLEVEL must be either 'MIN=x', 'MAX=y' or " + "'MIN=x,MAX=y' where 'x' and 'y' are integers. '" + formula + "' is not valid. ");
}
String token = vals[0];
String hdVal = vals[1];
try {
Integer.parseInt(hdVal);
} catch (NumberFormatException nfe) {
throw new PersistenceLayerException("PREPCLEVEL must be either 'MIN=x', 'MAX=y' or " + "'MIN=x,MAX=y' where 'x' and 'y' are integers. '" + formula + "' is not valid: " + hdVal + " is not an integer");
}
if (token.equals("MIN")) {
minPrereq.setKind("pclevel");
minPrereq.setOperator(PrerequisiteOperator.GTEQ);
minPrereq.setOperand(hdVal);
hasMin = true;
}
if (token.equals("MAX")) {
maxPrereq.setKind("pclevel");
maxPrereq.setOperator(PrerequisiteOperator.LTEQ);
maxPrereq.setOperand(hdVal);
hasMax = true;
}
}
if (hasMin && hasMax) {
// PREMULT
prereq.setKind(null);
prereq.setOperand("2");
prereq.addPrerequisite(minPrereq);
prereq.addPrerequisite(maxPrereq);
} else if (hasMin) {
prereq = minPrereq;
} else if (hasMax) {
prereq = maxPrereq;
}
}
if (invertResult) {
prereq.setOperator(prereq.getOperator().invert());
}
return prereq;
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PreFactParser 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. 2,TYPE=ItemCreation
checkFactKey(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);
checkFactKey(elements[i]);
subreq.setKey(elements[i]);
subreq.setOperand("1");
prereq.addPrerequisite(subreq);
}
}
setLocation(prereq, filetype);
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PreFactSetParser method parse.
/**
* Parse the pre req list
*
* @param kind
* The kind of the prerequisite (less the "PRE" prefix)
* @param formula
* The body of the prerequisite.
* @param invertResult
* Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite of
* any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
if (prereq.isCountMultiples()) {
throw new PersistenceLayerException("PREFACTSET does not support CHECKMULT");
}
prereq.setOverrideQualify(overrideQualify);
return prereq;
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PreSpellCastParser method parse.
/**
* Parse the pre req list
*
* @param kind The kind of the prerequisite (less the "PRE" prefix)
* @param formula The body of the prerequisite.
* @param invertResult Whether the prerequisite should invert the result.
* @param overrideQualify
* if set true, this prerequisite will be enforced in spite
* of any "QUALIFY" tag that may be present.
* @return PreReq
* @throws PersistenceLayerException
*/
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
// PREMULT
prereq.setKind(null);
final StringTokenizer inputTokenizer = new StringTokenizer(formula, ",");
while (inputTokenizer.hasMoreTokens()) {
String token = inputTokenizer.nextToken();
if (token.startsWith("MEMORIZE")) {
// Deal with MEMORIZE=Y tokens
Prerequisite subprereq = new Prerequisite();
prereq.addPrerequisite(subprereq);
subprereq.setKind("spellcast.memorize");
subprereq.setOperator(PrerequisiteOperator.GTEQ);
String[] elements = token.split("=");
if (elements[1].startsWith("N")) {
subprereq.setKey("N");
} else {
subprereq.setKey("Y");
}
} else if (token.startsWith("TYPE")) {
// Deal with TYPE=Arcane tokens
Prerequisite subprereq = new Prerequisite();
prereq.addPrerequisite(subprereq);
subprereq.setKind("spellcast.type");
subprereq.setOperator(PrerequisiteOperator.GTEQ);
subprereq.setKey(token.substring(5));
} else {
throw new PersistenceLayerException("Each argument in PRESPELLCAST " + " must start wth either MEMORIZE= or TYPE= : " + formula);
}
}
if ((prereq.getPrerequisiteCount() == 1) && prereq.getOperator().equals(PrerequisiteOperator.GTEQ) && prereq.getOperand().equals("1")) {
prereq = prereq.getPrerequisites().get(0);
}
if (invertResult) {
prereq.setOperator(prereq.getOperator().invert());
}
return prereq;
}
Aggregations