Search in sources :

Example 51 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class WeaponProfToken method unparse.

@Override
public String[] unparse(LoadContext context, CDOMObject obj) {
    List<String> list = new ArrayList<>();
    Changes<ChooseSelectionActor<?>> listChanges = context.getObjectContext().getListChanges(obj, ListKey.NEW_CHOOSE_ACTOR);
    Changes<WeaponProfProvider> changes = context.getObjectContext().getListChanges(obj, ListKey.WEAPONPROF);
    QualifiedObject<Boolean> deityweap = context.getObjectContext().getObject(obj, ObjectKey.HAS_DEITY_WEAPONPROF);
    Collection<WeaponProfProvider> added = changes.getAdded();
    Collection<ChooseSelectionActor<?>> listAdded = listChanges.getAdded();
    boolean foundAny = false;
    boolean foundOther = false;
    if (listAdded != null && !listAdded.isEmpty()) {
        foundOther = true;
        for (ChooseSelectionActor<?> cra : listAdded) {
            if (cra.getSource().equals(getTokenName())) {
                try {
                    list.add(cra.getLstFormat());
                } catch (PersistenceLayerException e) {
                    context.addWriteMessage("Error writing Prerequisite: " + e);
                    return null;
                }
            }
        }
    }
    if (deityweap != null && deityweap.getRawObject()) {
        foundOther = true;
        StringBuilder sb = new StringBuilder();
        sb.append("DEITYWEAPONS");
        if (deityweap.hasPrerequisites()) {
            sb.append('|');
            sb.append(context.getPrerequisiteString(deityweap.getPrerequisiteList()));
        }
        list.add(sb.toString());
    }
    if (added != null) {
        for (WeaponProfProvider wpp : added) {
            if (!wpp.isValid()) {
                context.addWriteMessage("Non-sensical " + "WeaponProfProvider in " + getFullName() + ": Had invalid contents");
                return null;
            }
            StringBuilder sb = new StringBuilder(wpp.getLstFormat());
            List<Prerequisite> prereqs = wpp.getPrerequisiteList();
            if (prereqs != null && !prereqs.isEmpty()) {
                if (prereqs.size() > 1) {
                    context.addWriteMessage("Error: " + obj.getClass().getSimpleName() + " had more than one Prerequisite for " + getFullName());
                    return null;
                }
                sb.append('|');
                sb.append(context.getPrerequisiteString(prereqs));
            }
            String lstFormat = sb.toString();
            boolean isUnconditionalAll = Constants.LST_ALL.equals(lstFormat);
            foundAny |= isUnconditionalAll;
            foundOther |= !isUnconditionalAll;
            list.add(lstFormat);
        }
    }
    if (foundAny && foundOther) {
        context.addWriteMessage("Non-sensical " + getFullName() + ": Contains ANY and a specific reference: " + list);
        return null;
    }
    if (list.isEmpty()) {
        // Empty indicates no Token
        return null;
    }
    return list.toArray(new String[list.size()]);
}
Also used : ArrayList(java.util.ArrayList) ChooseSelectionActor(pcgen.cdom.base.ChooseSelectionActor) WeaponProfProvider(pcgen.cdom.helper.WeaponProfProvider) PersistenceLayerException(pcgen.persistence.PersistenceLayerException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 52 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class PreStatParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    try {
        // PREMULT
        prereq.setKind(null);
        // Get the comparator type STATGTEQ, STAT, STATNEQ etc.
        String compType = kind.substring(4);
        if (compType.isEmpty()) {
            compType = "gteq";
        }
        String[] tokens = formula.split(",|\\|");
        int currToken = 0;
        // Get the minimum match count
        String aString = tokens[currToken++];
        try {
            prereq.setOperand(Integer.toString(Integer.parseInt(aString)));
        } catch (NumberFormatException e) {
            Logging.errorPrint("Badly formed PRESTAT attribute: " + aString);
            prereq.setOperand("1");
        }
        while (currToken < tokens.length) {
            aString = tokens[currToken++];
            final int idxEquals = aString.lastIndexOf('=');
            if (idxEquals < 3) {
                throw new PersistenceLayerException("PRE" + kindsHandled()[0] + " formula '" + formula + "' is not valid.");
            }
            final String stat = aString.substring(0, Math.min(3, idxEquals));
            Prerequisite statPrereq = new Prerequisite();
            statPrereq.setKind("stat");
            statPrereq.setKey(stat);
            statPrereq.setOperator(compType);
            statPrereq.setOperand(aString.substring(idxEquals + 1));
            prereq.addPrerequisite(statPrereq);
        }
        if ((prereq.getPrerequisiteCount() == 1) && (prereq.getOperator() == PrerequisiteOperator.GTEQ) && prereq.getOperand().equals("1")) {
            prereq = prereq.getPrerequisites().get(0);
        }
        if (invertResult) {
            prereq.setOperator(prereq.getOperator().invert());
        }
    } catch (PrerequisiteException pe) {
        throw new PersistenceLayerException("Unable to parse the prerequisite :'" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
    }
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) PrerequisiteException(pcgen.core.prereq.PrerequisiteException) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 53 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class PreTypeParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    int aNum;
    final StringTokenizer aTok = new StringTokenizer(formula, ",");
    String aString = aTok.nextToken();
    try {
        aNum = Integer.parseInt(aString);
    } catch (NumberFormatException nfe) {
        throw new PersistenceLayerException(formula + " must start with a number in PRETYPE", nfe);
    }
    // Parse new style syntax
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setOperand(Integer.toString(aNum));
    //
    if (aTok.countTokens() == 1) {
        prereq.setOperator(PrerequisiteOperator.EQ);
        prereq.setKey(aTok.nextToken());
    } else {
        // PREMULT
        prereq.setKind(null);
        while (aTok.hasMoreTokens()) {
            Prerequisite subreq = new Prerequisite();
            subreq.setOperator(PrerequisiteOperator.EQ);
            subreq.setKind("type");
            subreq.setKey(aTok.nextToken());
            prereq.addPrerequisite(subreq);
        }
    }
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    //prereq.setDeprecated();
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) StringTokenizer(java.util.StringTokenizer) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 54 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class PreVariableParser method parse.

/**
	 * Parse the pre req list
	 *
	 * @param kind The kind of the prerequisite (less the "PRE" prefix)
	 * @param formula The body of the prerequisite.
	 * @param invertResult Whether the prerequisite should invert the result.
	 * @param overrideQualify
	 *           if set true, this prerequisite will be enforced in spite
	 *           of any "QUALIFY" tag that may be present.
	 * @return PreReq
	 * @throws PersistenceLayerException
	 */
@Override
public Prerequisite parse(String kind, String formula, boolean invertResult, boolean overrideQualify) throws PersistenceLayerException {
    Prerequisite prereq = super.parse(kind, formula, invertResult, overrideQualify);
    prereq.setKind("var");
    // Get the comparator type SIZEGTEQ, BSIZE, SIZENEQ etc.
    String compType = kind.substring(3);
    if (compType.isEmpty()) {
        compType = "gteq";
    }
    ParsingSeparator ps = new ParsingSeparator(formula, ',');
    ps.addGroupingPair('[', ']');
    ps.addGroupingPair('(', ')');
    try {
        int count = 0;
        while (ps.hasNext()) {
            String first = ps.next();
            if (!ps.hasNext()) {
                throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. Incorrect parameter count (must be even)");
            }
            String second = ps.next();
            Prerequisite subreq;
            if (!ps.hasNext() && count == 0) {
                subreq = prereq;
            } else {
                // PREMULT
                prereq.setKind(null);
                subreq = new Prerequisite();
                subreq.setKind("var");
                prereq.addPrerequisite(subreq);
                count++;
            }
            subreq.setOperator(compType.intern());
            subreq.setKey(first.intern());
            subreq.setOperand(second.intern());
        }
        if (count > 0) {
            prereq.setOperand(Integer.toString(count));
        }
    } catch (PrerequisiteException pe) {
        throw new PersistenceLayerException("Unable to parse prerequisite 'PRE" + kind + ':' + formula + "'. " + pe.getLocalizedMessage());
    }
    if (invertResult) {
        prereq.setOperator(prereq.getOperator().invert());
    }
    prereq.setOverrideQualify(overrideQualify);
    return prereq;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) PrerequisiteException(pcgen.core.prereq.PrerequisiteException) ParsingSeparator(pcgen.base.text.ParsingSeparator) Prerequisite(pcgen.core.prereq.Prerequisite)

Example 55 with PersistenceLayerException

use of pcgen.persistence.PersistenceLayerException in project pcgen by PCGen.

the class BonusConvertPlugin method processBonus.

private String processBonus(TokenProcessEvent tpe, String key, String value) {
    try {
        LoadContext context = tpe.getContext();
        CDOMObject obj = tpe.getPrimary();
        if (context.processToken(obj, key, value)) {
            context.commit();
        } else {
            context.rollback();
            Logging.replayParsedMessages();
        }
        Logging.clearParseMessages();
        Collection<String> output = context.unparse(obj);
        if (output == null || output.isEmpty()) {
            // Uh Oh
            return ("Unable to unparse: " + key + ':' + value);
        }
        boolean needTab = false;
        for (String s : output) {
            if (needTab) {
                tpe.append('\t');
            }
            needTab = true;
            tpe.append(s);
        }
        tpe.consume();
    } catch (PersistenceLayerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : PersistenceLayerException(pcgen.persistence.PersistenceLayerException) CDOMObject(pcgen.cdom.base.CDOMObject) EditorLoadContext(pcgen.rules.context.EditorLoadContext) LoadContext(pcgen.rules.context.LoadContext)

Aggregations

PersistenceLayerException (pcgen.persistence.PersistenceLayerException)180 IOException (java.io.IOException)74 Prerequisite (pcgen.core.prereq.Prerequisite)62 StringWriter (java.io.StringWriter)17 StringTokenizer (java.util.StringTokenizer)17 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)15 LoadContext (pcgen.rules.context.LoadContext)14 URI (java.net.URI)12 Campaign (pcgen.core.Campaign)11 ParseResult (pcgen.rules.persistence.token.ParseResult)11 PCClass (pcgen.core.PCClass)10 CampaignSourceEntry (pcgen.persistence.lst.CampaignSourceEntry)10 PrerequisiteWriterInterface (pcgen.persistence.lst.output.prereq.PrerequisiteWriterInterface)10 URISyntaxException (java.net.URISyntaxException)8 TreeSet (java.util.TreeSet)8 CDOMReference (pcgen.cdom.base.CDOMReference)8 PlayerCharacter (pcgen.core.PlayerCharacter)8 PrerequisiteException (pcgen.core.prereq.PrerequisiteException)8 PCClassLoader (pcgen.persistence.lst.PCClassLoader)8