use of pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface in project pcgen by PCGen.
the class PreCompatibilityToken method unparse.
@Override
public String[] unparse(LoadContext context, ConcretePrereqObject obj) {
Changes<Prerequisite> changes = context.getObjectContext().getPrerequisiteChanges(obj);
if (changes == null || changes.isEmpty()) {
return null;
}
Set<String> set = new TreeSet<>();
for (Prerequisite p : changes.getAdded()) {
String kind = p.getKind();
final StringWriter capture = new StringWriter();
try {
PrerequisiteWriterInterface writer = factory.getWriter(kind);
writer.write(capture, p);
} catch (PersistenceLayerException e) {
Logging.errorPrint("Error in Compatibility Token", e);
}
String output = capture.toString();
int colonLoc = output.indexOf(':');
boolean outInvert = output.startsWith("!");
if (invert ^ outInvert) {
continue;
}
String key = output.substring(0, colonLoc);
if (tokenName.equalsIgnoreCase(key)) {
set.add(output.substring(colonLoc + 1));
}
}
if (set.isEmpty()) {
return null;
}
return set.toArray(new String[set.size()]);
}
use of pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface in project pcgen by PCGen.
the class AbstractPreRoundRobin method runSimpleRoundRobin.
public static void runSimpleRoundRobin(String s, String d) {
try {
Prerequisite p = PreParserFactory.getInstance().parse(s);
PrerequisiteWriterInterface writer = PrerequisiteWriterFactory.getInstance().getWriter(p.getKind());
if (writer == null) {
fail("Could not find Writer for: " + p.getKind());
}
StringWriter w = new StringWriter();
writer.write(w, p);
assertEquals(d, w.toString());
/*
* Now try new system
*/
LoadContext context = new EditorLoadContext();
CDOMObject obj = new ObjectCache();
int colonLoc = s.indexOf(':');
String key = s.substring(0, colonLoc);
String value = s.substring(colonLoc + 1);
if (context.processToken(obj, key, value)) {
context.commit();
} else {
context.rollback();
Logging.replayParsedMessages();
fail();
}
Logging.clearParseMessages();
Collection<String> output = context.unparse(obj);
if (output == null || output.isEmpty()) {
// Uh Oh
fail("Unable to unparse: " + key + ":" + value);
}
assertEquals(1, output.size());
assertEquals(d, output.iterator().next());
} catch (PersistenceLayerException e) {
e.printStackTrace();
fail(e.getLocalizedMessage());
}
}
use of pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface in project pcgen by PCGen.
the class PreAbilityRoundRobin method testCombineSubNegative.
@Override
public void testCombineSubNegative() {
String original = "!PREMULT:2,[!PRE" + getBaseString() + ":1," + getPrefix() + "Foo],[!PRE" + getBaseString() + ":1," + getPrefix() + "Spot]";
String consolidatedPre = "PRE" + getBaseString() + ":1," + getPrefix() + "Foo,Spot";
try {
Prerequisite p = PreParserFactory.getInstance().parse(original);
PrerequisiteWriterInterface writer = PrerequisiteWriterFactory.getInstance().getWriter(p.getKind());
if (writer == null) {
fail("Could not find Writer for: " + p.getKind());
}
StringWriter w = new StringWriter();
writer.write(w, p);
boolean consolidated = w.toString().equals(consolidatedPre);
boolean separate = w.toString().equals(original);
assertTrue(consolidated || separate);
} catch (PersistenceLayerException e) {
fail(e.getLocalizedMessage());
}
}
use of pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface in project pcgen by PCGen.
the class PreAbilityRoundRobin method testCombineSubCheckMult.
public void testCombineSubCheckMult() {
// runSimpleRoundRobin("PREMULT:2,[!PRE" + getBaseString() + ":1,"
// + "CHECKMULT," + getPrefix() + "Foo],[!PRE" + getBaseString()
// + ":1," + "CHECKMULT," + getPrefix() + "Spot]", "!PRE"
// + getBaseString() + ":1," + "CHECKMULT," + getPrefix()
// + "Foo,Spot");
String original = "PREMULT:2,[!PRE" + getBaseString() + ":1," + "CHECKMULT," + getPrefix() + "Foo],[!PRE" + getBaseString() + ":1," + "CHECKMULT," + getPrefix() + "Spot]";
String consolidatedPre = "!PRE" + getBaseString() + ":1," + "CHECKMULT," + getPrefix() + "Foo,Spot";
try {
Prerequisite p = PreParserFactory.getInstance().parse(original);
PrerequisiteWriterInterface writer = PrerequisiteWriterFactory.getInstance().getWriter(p.getKind());
if (writer == null) {
fail("Could not find Writer for: " + p.getKind());
}
StringWriter w = new StringWriter();
writer.write(w, p);
boolean consolidated = w.toString().equals(consolidatedPre);
boolean separate = w.toString().equals(original);
assertTrue(consolidated || separate);
} catch (PersistenceLayerException e) {
fail(e.getLocalizedMessage());
}
}
use of pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface in project pcgen by PCGen.
the class PreAbilityRoundRobin method testCombineSubSub.
@Override
public void testCombineSubSub() {
String original = "PREMULT:2,[!PRE" + getBaseString() + ":1," + getPrefix() + "Foo (Bar)],[!PRE" + getBaseString() + ":1," + getPrefix() + "Spot (Check)]";
String consolidatedPre = "!PRE" + getBaseString() + ":1," + getPrefix() + "Foo (Bar),Spot (Check)";
try {
Prerequisite p = PreParserFactory.getInstance().parse(original);
PrerequisiteWriterInterface writer = PrerequisiteWriterFactory.getInstance().getWriter(p.getKind());
if (writer == null) {
fail("Could not find Writer for: " + p.getKind());
}
StringWriter w = new StringWriter();
writer.write(w, p);
boolean consolidated = w.toString().equals(consolidatedPre);
boolean separate = w.toString().equals(original);
assertTrue(consolidated || separate);
} catch (PersistenceLayerException e) {
fail(e.getLocalizedMessage());
}
}
Aggregations