Search in sources :

Example 1 with PrerequisiteWriterInterface

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()]);
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) TreeSet(java.util.TreeSet) PrerequisiteWriterInterface(pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 2 with PrerequisiteWriterInterface

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());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) EditorLoadContext(pcgen.rules.context.EditorLoadContext) StringWriter(java.io.StringWriter) CDOMObject(pcgen.cdom.base.CDOMObject) ObjectCache(pcgen.cdom.inst.ObjectCache) EditorLoadContext(pcgen.rules.context.EditorLoadContext) LoadContext(pcgen.rules.context.LoadContext) PrerequisiteWriterInterface(pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 3 with PrerequisiteWriterInterface

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());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) PrerequisiteWriterInterface(pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 4 with PrerequisiteWriterInterface

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());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) PrerequisiteWriterInterface(pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 5 with PrerequisiteWriterInterface

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());
    }
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringWriter(java.io.StringWriter) PrerequisiteWriterInterface(pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface) Prerequisite(pcgen.core.prereq.Prerequisite)

Aggregations

StringWriter (java.io.StringWriter)10 Prerequisite (pcgen.core.prereq.Prerequisite)10 PersistenceLayerException (pcgen.persistence.PersistenceLayerException)10 PrerequisiteWriterInterface (pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface)10 TreeSet (java.util.TreeSet)1 CDOMObject (pcgen.cdom.base.CDOMObject)1 ObjectCache (pcgen.cdom.inst.ObjectCache)1 EditorLoadContext (pcgen.rules.context.EditorLoadContext)1 LoadContext (pcgen.rules.context.LoadContext)1