use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PreParserFactory method parse.
public Prerequisite parse(String prereqStr) throws PersistenceLayerException {
if ((prereqStr == null) || (prereqStr.length() <= 0)) {
throw new PersistenceLayerException("Null or empty PRE string");
}
int index = prereqStr.indexOf(':');
if (index < 0) {
throw new PersistenceLayerException("'" + prereqStr + "'" + " is a badly formatted prereq.");
}
String kind = prereqStr.substring(0, index);
String formula = prereqStr.substring(index + 1);
boolean overrideQualify = false;
if (formula.startsWith("Q:")) {
formula = formula.substring(2);
overrideQualify = true;
}
boolean invertResult = false;
if (kind.startsWith("!")) {
invertResult = true;
kind = kind.substring(1);
}
kind = kind.substring(3);
PrerequisiteParserInterface parser = getParser(kind);
if (parser == null) {
throw new PersistenceLayerException("Can not determine which parser to use for " + "'" + prereqStr + "'");
}
try {
Prerequisite prereq = parser.parse(kind, formula, invertResult, overrideQualify);
// is a PREMULT with only 1 element.
while (prereq.getKind() == null && prereq.getPrerequisiteCount() == 1 && prereq.getOperator().equals(PrerequisiteOperator.GTEQ) && prereq.getOperand().equals("1")) {
Prerequisite sub = prereq.getPrerequisites().get(0);
sub.setOriginalCheckmult(prereq.isOriginalCheckMult());
prereq = sub;
}
return prereq;
} catch (Throwable t) {
throw new PersistenceLayerException("Can not parse '" + prereqStr + "': " + t.getMessage());
}
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class VariableLoader method loadLstFile.
/**
* This method loads a single LST formatted file.
*
* @param sourceEntry
* CampaignSourceEntry containing the absolute file path or the
* URL from which to read LST formatted data.
*/
protected void loadLstFile(LoadContext context, CampaignSourceEntry sourceEntry) {
setChanged();
URI uri = sourceEntry.getURI();
notifyObservers(uri);
StringBuilder dataBuffer;
try {
dataBuffer = LstFileLoader.readFromURI(uri);
} catch (PersistenceLayerException ple) {
String message = LanguageBundle.getFormattedString(//$NON-NLS-1$
"Errors.LstFileLoader.LoadError", uri, ple.getMessage());
Logging.errorPrint(message);
setChanged();
return;
}
String aString = dataBuffer.toString();
if (context != null) {
context.setSourceURI(uri);
}
String[] fileLines = aString.split(LstFileLoader.LINE_SEPARATOR_REGEXP);
for (int i = 0; i < fileLines.length; i++) {
String line = fileLines[i];
if ((line.isEmpty()) || (line.charAt(0) == LstFileLoader.LINE_COMMENT_CHAR)) {
continue;
}
if (line.trim().isEmpty()) {
// Ignore the line
} else {
try {
parseLine(context, line, sourceEntry);
} catch (PersistenceLayerException ple) {
String message = LanguageBundle.getFormattedString(//$NON-NLS-1$
"Errors.LstFileLoader.ParseError", uri, i + 1, ple.getMessage());
Logging.errorPrint(message);
setChanged();
if (Logging.isDebugMode()) {
//$NON-NLS-1$
Logging.debugPrint("Parse error:", ple);
}
} catch (Throwable t) {
String message = LanguageBundle.getFormattedString(//$NON-NLS-1$
"Errors.LstFileLoader.ParseError", uri, i + 1, t.getMessage());
Logging.errorPrint(message, t);
setChanged();
Logging.errorPrint(LanguageBundle.getString("Errors.LstFileLoader.Ignoring: " + t.getMessage()));
if (Logging.isDebugMode()) {
Logging.errorPrint(LanguageBundle.getString("Errors.LstFileLoader.Ignoring"), t);
t.printStackTrace();
}
}
}
}
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class WieldCategoryLoader method parseLine.
/**
* Parse the WIELDCATEGORY line
*
* @param gameMode
* @param lstLine
* @throws PersistenceLayerException
*/
public void parseLine(GameMode gameMode, String lstLine, URI source) throws PersistenceLayerException {
LoadContext context = gameMode.getModeContext();
StringTokenizer colToken = new StringTokenizer(lstLine, SystemLoader.TAB_DELIM);
WieldCategory cat = null;
String preKey = null;
CDOMSingleRef<WieldCategory> preVal = null;
while (colToken.hasMoreTokens()) {
final String colString = colToken.nextToken().trim();
final int idxColon = colString.indexOf(':');
String key = "";
try {
key = colString.substring(0, idxColon);
} catch (StringIndexOutOfBoundsException e) {
// TODO Deal with Exception
}
if (key.equals("WIELDCATEGORY")) {
final String value = colString.substring(idxColon + 1).trim();
cat = context.getReferenceContext().silentlyGetConstructedCDOMObject(WieldCategory.class, value);
if (cat == null) {
cat = new WieldCategory();
cat.setName(value.intern());
gameMode.addWieldCategory(cat);
}
} else if (colString.startsWith("PREVAR")) {
//TODO ensure preKey is null
// a PREVARxx formula used to switch
// weapon categories based on size
preKey = colString;
} else if (key.equals("SWITCH")) {
//TODO ensure preVal is null
// If matches PRE, switch category to this
preVal = context.getReferenceContext().getCDOMReference(WieldCategory.class, colString.substring(7));
} else {
final String value = colString.substring(idxColon + 1).trim();
if (context.processToken(cat, key, value)) {
context.commit();
} else {
context.rollback();
Logging.replayParsedMessages();
}
Logging.clearParseMessages();
}
}
//TODO Error checking if preVal w/o preKey, vice versa, etc.
if ((cat != null) && (preVal != null) && (preKey != null)) {
try {
QualifiedObject<CDOMSingleRef<WieldCategory>> qo = new QualifiedObject<>(preVal);
qo.addPrerequisite(prereqParser.parse(preKey));
cat.addCategorySwitch(qo);
} catch (PersistenceLayerException ple) {
Logging.errorPrint("Error parsing Prerequisite in " + source + ": " + preKey + "\n " + ple.getMessage());
}
}
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class AbstractPrerequisiteListParser method parsePrereqListType.
/*
* Parses a PRE type, some examples below:
*
* CLASS:1,Spellcaster=3
* <prereq kind="class" key="Spellcaster" min="3" />
*
* SKILL:1,Heal=5
* <prereq kind="skill" key="Heal" min="5" />
*
* FEAT:1,TYPE=Necromantic
* <prereq kind="feat" key="TYPE=Necromantic" />
*
* SKILL:2,Knowledge (Anthropology),Knowledge (Biology),Knowledge
* (Chemistry)=5
* <prereq min="2">
* <prereq kind="skill" key="Knowledge (Anthropology)" min="5" />
* <prereq kind="skill" key="Knowledge (Biology)" min="5" />
* <prereq kind="skill" key="Knowledge (Chemistry)" min="5" />
* </prereq>
*
* FEAT:2,CHECKMULT,Spell Focus
* <prereq kind="feat" count-multiples="true" key="feat.spell_focus" />
*
* FEAT:2,CHECKMULT,Spell Focus,[Spell Focus(Enchantment)]
* <prereq min="2">
* <prereq kind="feat" key="feat.spell_focus" count-multiples="true" min="2"/>
* <prereq kind="feat" key="feat.spell_focus_enchantment" logical="not" />
* </prereq>
*
* STAT:1,DEX=9,STR=13
* <prereq operator="gteq" op1="1">
* <prereq kind="stat" key="dex" operator="gteq" op1="9" />
* <prereq kind="stat" key="str" operator="gteq" op1="13" />
* </prereq>
*/
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 (!allowsNegate() && (formula.indexOf("[") >= 0 || formula.indexOf("]") >= 0)) {
throw new PersistenceLayerException("Prerequisite " + kind + " can not contain []: " + formula);
}
if (formula.indexOf("|") >= 0) {
throw new PersistenceLayerException("Prerequisite " + kind + " can not contain |: " + formula);
}
String[] elements = formula.split(",");
int numRequired;
try {
numRequired = 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");
}
// Examine the last element to see if it is of the form "foo=n"
int elementsLength = elements.length;
for (int i = elementsLength - 1; i >= 0; --i) {
if ("CHECKMULT".equalsIgnoreCase(elements[i])) {
prereq.setCountMultiples(true);
--elementsLength;
}
}
// number contains the number of 'tokens' that be at least 'min'
if (elementsLength > 2) {
// we have more than one option, so use a group
prereq.setOperator(PrerequisiteOperator.GTEQ);
prereq.setOperand(Integer.toString(numRequired));
prereq.setKind(null);
boolean hasKeyValue = false;
boolean hasKeyOnly = false;
int min = -99;
for (int i = 1; i < elements.length; i++) {
String thisElement = elements[i];
if ("CHECKMULT".equals(thisElement)) {
continue;
}
boolean warnIgnored = isNoWarnElement(thisElement);
Prerequisite subreq = new Prerequisite();
subreq.setKind(kind.toLowerCase());
subreq.setCountMultiples(true);
if (thisElement.indexOf('=') >= 0) {
// 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);
String[] tokens = thisElement.split("=");
try {
int valueIndx = tokens.length - 1;
min = Integer.parseInt(tokens[valueIndx]);
subreq.setOperand(Integer.toString(min));
String requirementKey = getRequirementKey(tokens);
subreq.setKey(requirementKey);
// now back fill all of the previous prereqs with this minimum
for (Prerequisite p : new ArrayList<>(prereq.getPrerequisites())) {
if (p.getOperand().equals("-99")) {
p.setOperand(Integer.toString(min));
// If this requirement has already been added, we don't want to repeat it.
if (p.getKey().equals(requirementKey)) {
prereq.removePrerequisite(p);
}
}
}
if (!warnIgnored) {
hasKeyValue = true;
}
} catch (NumberFormatException nfe) {
subreq.setKey(thisElement);
if (!warnIgnored) {
hasKeyOnly = true;
}
}
} else {
if (requiresValue()) {
throw new PersistenceLayerException("Prerequisites of kind " + kind + " require a target value, e.g. Key=Value");
}
String assumed = getAssumedValue();
if (assumed == null) {
subreq.setOperand(Integer.toString(min));
} else {
Logging.deprecationPrint("Old syntax detected: " + "Prerequisites of kind " + kind + " now require a target value, " + "e.g. Key=Value. Assuming Value=" + assumed);
subreq.setOperand(assumed);
}
if (!warnIgnored) {
hasKeyOnly = true;
}
subreq.setKey(thisElement);
subreq.setOperator(PrerequisiteOperator.GTEQ);
}
subreq.setOperand(Integer.toString(min));
prereq.addPrerequisite(subreq);
}
for (Prerequisite element : prereq.getPrerequisites()) {
if (element.getOperand().equals("-99")) {
element.setOperand("1");
}
}
if (hasKeyOnly && hasKeyValue) {
Logging.deprecationPrint("You are using a deprecated syntax of PRE" + kind + ":" + formula + " ... Each item in the list should have a target value, e.g.: PRE" + kind + ":1,First=99,Second=5");
}
} else {
// 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,Alertness, or 2,TYPE=ItemCreation, or 1,Reflex=7 or 3,Knowledge%=2 or 4,TYPE.Craft=5
Prerequisite subreq = prereq;
if (elementsLength > 1) {
for (int i = 1; i < elements.length; ++i) {
if ("CHECKMULT".equalsIgnoreCase(elements[i])) {
continue;
}
if (elements[i].indexOf('=') >= 0) {
// i.e. TYPE=ItemCreation or Reflex=7
String[] tokens = elements[i].split("=");
int valueIdx = tokens.length - 1;
try {
// i.e. Reflex=7 or TYPE.Craft=5
int iOper = Integer.parseInt(tokens[valueIdx]);
if (numRequired != 1) {
//
// If we would lose the required number of matches,
// then make this a PREMULT
//
prereq.setOperator(PrerequisiteOperator.GTEQ);
prereq.setOperand(Integer.toString(numRequired));
prereq.setKind(null);
subreq = new Prerequisite();
prereq.addPrerequisite(subreq);
subreq.setCountMultiples(true);
}
subreq.setOperand(Integer.toString(iOper));
String requirementKey = getRequirementKey(tokens);
subreq.setKey(requirementKey);
} catch (NumberFormatException nfe) {
if (tokens[valueIdx].equals("ANY")) {
if (isAnyLegal()) {
subreq.setOperand(tokens[valueIdx]);
subreq.setKey(getRequirementKey(tokens));
} else {
throw new PersistenceLayerException("Prerequisites of kind " + kind + " do not support 'ANY'");
}
} else {
// i.e. TYPE=ItemCreation
subreq.setOperand(elements[0]);
subreq.setKey(elements[i]);
}
}
} else {
if (requiresValue()) {
throw new PersistenceLayerException("Prerequisites of kind " + kind + " require a target value, e.g. Key=Value");
}
String assumed = getAssumedValue();
if (assumed == null) {
subreq.setOperand(elements[0]);
} else {
Logging.deprecationPrint("Old syntax detected: " + "Prerequisites of kind " + kind + " now require a target value, " + "e.g. Key=Value. Assuming Value=" + assumed);
subreq.setOperand(assumed);
}
subreq.setKey(elements[i]);
}
break;
}
} else {
subreq.setOperand(elements[0]);
}
subreq.setKind(kind.toLowerCase());
subreq.setOperator(PrerequisiteOperator.GTEQ);
}
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PCClassLoader method processSubLevelLines.
private void processSubLevelLines(LoadContext context, PCClass cl, PCClass sc) {
for (DeferredLine dl : sc.getSafeListFor(ListKey.SUB_CLASS_LEVEL)) {
context.setSourceURI(dl.source.getURI());
String lstLine = dl.lstLine;
try {
int tabLoc = lstLine.indexOf(SystemLoader.TAB_DELIM);
String lineIdentifier;
String restOfLine;
if (tabLoc == -1) {
lineIdentifier = lstLine;
restOfLine = null;
} else {
lineIdentifier = lstLine.substring(0, tabLoc);
restOfLine = lstLine.substring(tabLoc + 1);
}
parseFullClassLevelLine(context, dl.source, sc, lineIdentifier, restOfLine);
} catch (PersistenceLayerException ple) {
Logging.log(Logging.LST_ERROR, "Error parsing " + sc.getClass().getSimpleName() + " line: " + cl.getKeyName() + " " + sc.getKeyName() + " " + lstLine, ple);
}
}
}
Aggregations