use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class AbstractPrerequisiteWriter method checkValidOperator.
protected void checkValidOperator(Prerequisite prereq, PrerequisiteOperator[] comparators) throws PersistenceLayerException {
StringBuilder comparatorString = new StringBuilder(25);
for (int i = 0; i < comparators.length; i++) {
PrerequisiteOperator comparator = comparators[i];
if (prereq.getOperator().equals(comparators[i])) {
return;
}
if (i > 0) {
comparatorString.append(", ");
}
comparatorString.append(comparator);
}
String kind = prereq.getKind();
if (kind == null) {
kind = "<NULL>";
}
throw new PersistenceLayerException("Cannot write token: LST syntax only supports " + comparatorString.toString() + " operators for PRE" + kind.toUpperCase() + ": " + prereq.toString());
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class PrerequisiteMultWriter method write.
@Override
public void write(Writer writer, Prerequisite prereq) throws PersistenceLayerException {
checkValidOperator(prereq, operatorsHandled());
try {
Prerequisite subreq;
//
if (isSpecialCase(prereq)) {
handleSpecialCase(writer, prereq);
return;
}
if (isNegatedPreability(prereq)) {
handleNegatedPreAbility(writer, prereq);
return;
}
if (prereq.getPrerequisiteCount() != 0) {
subreq = prereq.getPrerequisites().get(0);
final PrerequisiteWriterInterface test = PrerequisiteWriterFactory.getInstance().getWriter(subreq.getKind());
if ((test != null) && (test instanceof AbstractPrerequisiteWriter) && ((AbstractPrerequisiteWriter) test).specialCase(writer, prereq)) {
return;
}
}
if (prereq.getOperator().equals(PrerequisiteOperator.LT)) {
writer.write('!');
}
writer.write("PREMULT:");
writer.write(prereq.getOperand());
writer.write(',');
int i = 0;
for (Prerequisite pre : prereq.getPrerequisites()) {
if (i > 0) {
writer.write(',');
}
writer.write('[');
PrerequisiteWriterFactory factory = PrerequisiteWriterFactory.getInstance();
PrerequisiteWriterInterface w = factory.getWriter(pre.getKind());
if (w != null) {
w.write(writer, pre);
} else {
writer.write("unrecognized kind:" + pre.getKind());
}
writer.write(']');
i++;
}
} catch (IOException e) {
throw new PersistenceLayerException(e.getMessage());
}
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class ValuesToken method parseNonEmptyToken.
@Override
protected ParseResult parseNonEmptyToken(LoadContext context, KitTable kitTable, String value) {
ParsingSeparator sep = new ParsingSeparator(value, '|');
sep.addGroupingPair('[', ']');
sep.addGroupingPair('(', ')');
while (sep.hasNext()) {
String thing = sep.next();
if (thing.isEmpty()) {
return new ParseResult.Fail(getTokenName() + " arguments has invalid pipe separator: " + value, context);
}
KitGear optionInfo = new KitGear();
for (String s : thing.split("[\\[\\]]")) {
if (s.isEmpty()) {
continue;
}
int colonLoc = s.indexOf(':');
if (colonLoc == -1) {
return new ParseResult.Fail("Expected colon in Value item: " + s + " within: " + value, context);
}
String key = s.substring(0, colonLoc);
String thingValue = s.substring(colonLoc + 1);
try {
boolean passed = context.processToken(optionInfo, key, thingValue);
if (!passed) {
return new ParseResult.Fail("Failure in token: " + key, context);
}
} catch (PersistenceLayerException e) {
return new ParseResult.Fail("Failure in token: " + key + " " + e.getMessage(), context);
}
}
if (!sep.hasNext()) {
return new ParseResult.Fail("Odd token count in Value: " + value, context);
}
String range = sep.next();
if (!processRange(kitTable, optionInfo, range)) {
return new ParseResult.Fail("Invalid Range in Value: " + range + " within " + value, context);
}
}
return ParseResult.SUCCESS;
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class AbilityToken method parseToken.
@Override
public ParseResult parseToken(LoadContext context, KitLevelAbility kitAbility, String value) {
if (!value.startsWith("PROMPT:")) {
return new ParseResult.Fail("Expected " + getTokenName() + " to start with PROMPT: " + value, context);
}
StringTokenizer st = new StringTokenizer(value, Constants.PIPE);
String first = st.nextToken();
PersistentTransitionChoice<?> ptc;
try {
ptc = Compatibility.processOldAdd(context, first);
} catch (PersistenceLayerException e) {
return new ParseResult.Fail(e.getMessage(), context);
}
if (ptc == null) {
return new ParseResult.Fail("Error was in " + getTokenName() + ' ' + value, context);
}
kitAbility.setAdd(ptc);
while (st.hasMoreTokens()) {
String choiceString = st.nextToken();
if (!choiceString.startsWith("CHOICE:")) {
return new ParseResult.Fail("Expected " + getTokenName() + " choice string to start with CHOICE: " + value, context);
}
String choice = choiceString.substring(7);
if (first.equals("FEAT") && !choice.startsWith("CATEGORY=")) {
/*
* In the case of FEAT, need to provide the context (since
* persistence assumes this CATEGORY= exists)
*/
choice = "CATEGORY=FEAT|" + choice;
}
/*
* TODO This is load order dependent, this really should be storing
* references into kitAbility, not a String - thpr Dec 8 2012
*/
if (ptc.decodeChoice(context, choice) == null) {
return new ParseResult.Fail(choiceString + " is not a valid selection for ADD:" + first, context);
}
kitAbility.addChoice(choice);
}
return ParseResult.SUCCESS;
}
use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.
the class AdddomainsToken method unparse.
@Override
public String[] unparse(LoadContext context, PCClass pcc) {
AssociatedChanges<CDOMReference<Domain>> changes = context.getListContext().getChangesInList(getTokenName(), pcc, PCClass.ALLOWED_DOMAINS);
Collection<CDOMReference<Domain>> removedItems = changes.getRemoved();
if (removedItems != null && !removedItems.isEmpty() || changes.includesGlobalClear()) {
context.addWriteMessage(getTokenName() + " does not support .CLEAR");
return null;
}
MapToList<CDOMReference<Domain>, AssociatedPrereqObject> mtl = changes.getAddedAssociations();
if (mtl == null || mtl.isEmpty()) {
return null;
}
PrerequisiteWriter prereqWriter = new PrerequisiteWriter();
Set<String> set = new TreeSet<>();
Set<String> noPrereqSet = new TreeSet<>();
for (CDOMReference<Domain> domain : mtl.getKeySet()) {
for (AssociatedPrereqObject assoc : mtl.getListFor(domain)) {
StringBuilder sb = new StringBuilder(domain.getLSTformat(false));
List<Prerequisite> prereqs = assoc.getPrerequisiteList();
if (prereqs == null || prereqs.isEmpty()) {
noPrereqSet.add(sb.toString());
continue;
}
for (Prerequisite prereq : prereqs) {
sb.append(Constants.PIPE);
StringWriter swriter = new StringWriter();
try {
prereqWriter.write(swriter, prereq);
} catch (PersistenceLayerException e) {
context.addWriteMessage("Error writing Prerequisite: " + e);
return null;
}
sb.append(swriter.toString());
}
set.add(sb.toString());
}
}
if (!noPrereqSet.isEmpty()) {
set.add(StringUtil.join(noPrereqSet, Constants.PIPE));
}
return set.toArray(new String[set.size()]);
}
Aggregations