@Override protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) { ParsingSeparator sep = new ParsingSeparator(value, '|'); String activeValue = sep.next(); Formula count; if (!sep.hasNext()) { count = FormulaFactory.ONE; } else { count = FormulaFactory.getFormulaFor(activeValue); if (!count.isValid()) { return new ParseResult.Fail( "Count in " + getTokenName() + " was not valid: " + count.toString()); } if (count.isStatic() && count.resolve(null, "").doubleValue() <= 0) { return new ParseResult.Fail("Count in " + getFullName() + " must be > 0"); } activeValue = sep.next(); } if (sep.hasNext()) { return new ParseResult.Fail(getFullName() + " had too many pipe separated items: " + value); } ParseResult pr = checkSeparatorsAndNonEmpty(',', activeValue); if (!pr.passed()) { return pr; } List<CDOMReference<Language>> refs = new ArrayList<CDOMReference<Language>>(); StringTokenizer tok = new StringTokenizer(activeValue, Constants.COMMA); while (tok.hasMoreTokens()) { String tokText = tok.nextToken(); CDOMReference<Language> lang = TokenUtilities.getReference(context, LANGUAGE_CLASS, tokText); if (lang == null) { return new ParseResult.Fail( " Error was encountered while parsing " + getFullName() + ": " + value + " had an invalid reference: " + tokText); } refs.add(lang); } ReferenceChoiceSet<Language> rcs = new ReferenceChoiceSet<Language>(refs); if (!rcs.getGroupingState().isValid()) { return new ParseResult.Fail( "Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value); } ChoiceSet<Language> cs = new ChoiceSet<Language>(getTokenName(), rcs); cs.setTitle("Language Choice"); PersistentTransitionChoice<Language> tc = new ConcretePersistentTransitionChoice<Language>(cs, count); context.getObjectContext().addToList(obj, ListKey.ADD, tc); tc.setChoiceActor(this); return ParseResult.SUCCESS; }
@Test public void testUnparseBadCount() throws PersistenceLayerException { AbilityRefChoiceSet arcs = build("TestWP1"); ChoiceSet<CNAbilitySelection> cs = new ChoiceSet<CNAbilitySelection>(getSubTokenName(), arcs, true); cs.setTitle("Select for removal"); PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<CNAbilitySelection>(cs, null); tc.setChoiceActor(subtoken); primaryProf.addToListFor(ListKey.REMOVE, tc); assertBadUnparse(); }
@Test public void testUnparseMultipleParen() throws PersistenceLayerException { AbilityRefChoiceSet arcs = build("TestWP1 (Foo)", "TestWP2 (Bar)"); ChoiceSet<CNAbilitySelection> cs = new ChoiceSet<CNAbilitySelection>(getSubTokenName(), arcs, true); cs.setTitle("Select for removal"); PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<CNAbilitySelection>(cs, FormulaFactory.ONE); tc.setChoiceActor(subtoken); primaryProf.addToListFor(ListKey.REMOVE, tc); String[] unparsed = getToken().unparse(primaryContext, primaryProf); expectSingle(unparsed, "FEAT|TestWP1 (Foo)" + getJoinCharacter() + "TestWP2 (Bar)"); }
@Override public boolean process(LoadContext context, PCTemplate obj) { List<CDOMReference<WeaponProf>> weaponbonus = obj.getListFor(ListKey.WEAPONBONUS); if (weaponbonus != null) { ReferenceChoiceSet<WeaponProf> rcs = new ReferenceChoiceSet<WeaponProf>(weaponbonus); ChoiceSet<WeaponProf> cs = new ChoiceSet<WeaponProf>(getTokenName(), rcs); cs.setTitle("Bonus WeaponProf Choice"); PersistentTransitionChoice<WeaponProf> tc = new ConcretePersistentTransitionChoice<WeaponProf>(cs, FormulaFactory.ONE); context.getObjectContext().addToList(obj, ListKey.ADD, tc); tc.setChoiceActor(this); } return true; }
@Override public boolean process(LoadContext context, PCTemplate pct) { List<CDOMReference<Ability>> list = pct.getListFor(ListKey.FEAT_TOKEN_LIST); if (list != null && !list.isEmpty()) { AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(AbilityCategory.FEAT, list, Nature.AUTOMATIC); ChoiceSet<CategorizedAbilitySelection> cs = new ChoiceSet<CategorizedAbilitySelection>(getTokenName(), rcs); cs.setTitle("Feat Choice"); PersistentTransitionChoice<CategorizedAbilitySelection> tc = new ConcretePersistentTransitionChoice<CategorizedAbilitySelection>( cs, FormulaFactory.ONE); context.getObjectContext().put(pct, ObjectKey.TEMPLATE_FEAT, tc); tc.setChoiceActor(this); } return true; }
@Test public void testUnparseNullInList() throws PersistenceLayerException { List<CDOMReference<Ability>> list = new ArrayList<CDOMReference<Ability>>(); Ability ab = construct(primaryContext, "TestWP1"); CDOMDirectSingleRef<Ability> ar = CDOMDirectSingleRef.getRef(ab); list.add(ar); list.add(null); AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(AbilityCategory.FEAT, list, Nature.NORMAL); AbilityRefChoiceSet arcs = rcs; ChoiceSet<CNAbilitySelection> cs = new ChoiceSet<CNAbilitySelection>(getSubTokenName(), arcs, true); cs.setTitle("Select for removal"); PersistentTransitionChoice<CNAbilitySelection> tc = new ConcretePersistentTransitionChoice<CNAbilitySelection>(cs, FormulaFactory.ONE); tc.setChoiceActor(subtoken); primaryProf.addToListFor(ListKey.REMOVE, tc); try { getToken().unparse(primaryContext, primaryProf); fail(); } catch (NullPointerException e) { // Yep! } }
@Override protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject obj, String value) { AbilityCategory category = AbilityCategory.FEAT; Nature nature = Nature.NORMAL; ParsingSeparator sep = new ParsingSeparator(value, '|'); String activeValue = sep.next(); Formula count; if (!sep.hasNext()) { count = FormulaFactory.ONE; } else { count = FormulaFactory.getFormulaFor(activeValue); if (!count.isValid()) { return new ParseResult.Fail( "Count in " + getTokenName() + " was not valid: " + count.toString(), context); } if (!count.isValid()) { return new ParseResult.Fail( "Count in " + getTokenName() + " was not valid: " + count.toString(), context); } if (count.isStatic() && count.resolveStatic().doubleValue() <= 0) { return new ParseResult.Fail("Count in " + getFullName() + " must be > 0", context); } activeValue = sep.next(); } if (sep.hasNext()) { return new ParseResult.Fail( getFullName() + " had too many pipe separated items: " + value, context); } if (isEmpty(activeValue) || hasIllegalSeparator(',', activeValue)) { return ParseResult.INTERNAL_ERROR; } List<CDOMReference<Ability>> refs = new ArrayList<CDOMReference<Ability>>(); List<PrimitiveChoiceSet<CategorizedAbilitySelection>> pcs = new ArrayList<PrimitiveChoiceSet<CategorizedAbilitySelection>>(); ParsingSeparator tok = new ParsingSeparator(activeValue, ','); boolean foundAny = false; boolean foundOther = false; ReferenceManufacturer<Ability> rm = context.ref.getManufacturer(ABILITY_CLASS, AbilityCategory.FEAT); while (tok.hasNext()) { CDOMReference<Ability> ab = null; String token = tok.next(); if (Constants.LST_CHOICE.equals(token) || Constants.LST_ANY.equals(token)) { foundAny = true; ab = rm.getAllReference(); } else if (token.startsWith("CLASS.") || token.startsWith("CLASS=")) { String className = token.substring(6); if (className.length() == 0) { return new ParseResult.Fail( getTokenName() + " must have Class name after " + token, context); } CDOMSingleRef<PCClass> pcc = context.ref.getCDOMReference(PCCLASS_CLASS, className); AbilityFromClassChoiceSet acs = new AbilityFromClassChoiceSet(pcc); pcs.add(acs); } else { foundOther = true; ab = TokenUtilities.getTypeOrPrimitive(rm, token); if (ab == null) { return new ParseResult.Fail( " Error was encountered while parsing " + getTokenName() + ": " + value + " had an invalid reference: " + token, context); } } if (ab != null) { refs.add(ab); } } if (foundAny && foundOther) { return new ParseResult.Fail( "Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + value, context); } if (!refs.isEmpty()) { AbilityRefChoiceSet rcs = new AbilityRefChoiceSet(category, refs, nature); pcs.add(rcs); } if (pcs.isEmpty()) { return new ParseResult.Fail( "Internal Error: " + getFullName() + " did not have any references: " + value, context); } PrimitiveChoiceSet<CategorizedAbilitySelection> ascs; if (pcs.size() == 1) { ascs = pcs.get(0); } else { ascs = new CompoundOrChoiceSet<CategorizedAbilitySelection>(pcs, Constants.COMMA); } ChoiceSet<CategorizedAbilitySelection> cs = new ChoiceSet<CategorizedAbilitySelection>(getTokenName(), ascs, true); cs.setTitle("Select for removal"); PersistentTransitionChoice<CategorizedAbilitySelection> tc = new ConcretePersistentTransitionChoice<CategorizedAbilitySelection>(cs, count); context.getObjectContext().addToList(obj, ListKey.REMOVE, tc); tc.allowStack(true); tc.setChoiceActor(this); return ParseResult.SUCCESS; }