Search in sources :

Example 1 with CaseInsensitiveString

use of pcgen.base.lang.CaseInsensitiveString in project pcgen by PCGen.

the class InfoModel method get.

/**
	 * Acts as a hash for producing the contents of this model.
	 * 
	 * @see freemarker.template.TemplateHashModel#get(java.lang.String)
	 */
@Override
public TemplateModel get(String key) throws TemplateModelException {
    CaseInsensitiveString cis = new CaseInsensitiveString(key);
    MessageFormat info = cdo.get(MapKey.INFO, cis);
    StringBuffer sb = new StringBuffer(100);
    if (info != null) {
        info.format(getVars(cis), sb, null);
    } else {
        //TODO: This should actually throw an error but we can't for
        //now due to it breaking too many thing...
        //So we are just logging it for now.
        //--Connor Petty
        Logging.errorPrint("CDOMObject [" + cdo.getDisplayName() + "] does not have INFO of type " + key);
    //			throw new TemplateModelException(
    //				"CDOMObject did not have INFO of type " + key);
    }
    return FacetLibrary.getFacet(ObjectWrapperFacet.class).wrap(id, sb.toString());
}
Also used : MessageFormat(java.text.MessageFormat) ObjectWrapperFacet(pcgen.cdom.facet.ObjectWrapperFacet) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 2 with CaseInsensitiveString

use of pcgen.base.lang.CaseInsensitiveString in project pcgen by PCGen.

the class AbstractReferenceManufacturer method validateDuplicates.

private boolean validateDuplicates() {
    boolean returnGood = true;
    for (CaseInsensitiveString second : duplicates.getKeySet()) {
        List<T> list = duplicates.getListFor(second);
        T good = active.get(second.toString());
        /*
			 * CONSIDER Should get CDOMObject reference out of here :(
			 */
        if (good instanceof CDOMObject) {
            CDOMObject cdo = (CDOMObject) good;
            for (int i = 0; i < list.size(); i++) {
                T dupe = list.get(i);
                if (cdo.isCDOMEqual((CDOMObject) dupe)) {
                    for (Iterator<WeakReference<T>> it = manufactured.iterator(); it.hasNext(); ) {
                        WeakReference<T> wr = it.next();
                        T mfg = wr.get();
                        if (mfg == null) {
                            it.remove();
                        } else // Yes this is instance equality, not .equals
                        if (mfg == good) {
                            forgetObject(good);
                            break;
                        }
                    }
                }
            }
        }
        if (duplicates.containsListFor(second)) {
            Logging.errorPrint("More than one " + factory.getReferenceDescription() + " with key/name " + good.getKeyName() + " was built");
            List<T> dupes = duplicates.getListFor(second);
            StringBuilder sb = new StringBuilder(1000);
            sb.append("Sources: ");
            sb.append(good.isInternal() ? "<internal>" : good.getSourceURI());
            for (T dupe : dupes) {
                sb.append(", ").append(dupe.isInternal() ? "<internal>" : dupe.getSourceURI());
                if (!dupe.getKeyName().equals(good.getKeyName())) {
                    Logging.errorPrint("Key case differed for " + dupe.getKeyName());
                }
            }
            Logging.errorPrint(sb.toString());
            returnGood = false;
        }
    }
    return returnGood;
}
Also used : CDOMObject(pcgen.cdom.base.CDOMObject) WeakReference(java.lang.ref.WeakReference) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 3 with CaseInsensitiveString

use of pcgen.base.lang.CaseInsensitiveString in project pcgen by PCGen.

the class AbstractReferenceManufacturer method getObject.

/**
	 * Gets the object represented by the given identifier. Will return null if
	 * an object with the given identifier is not present in this
	 * AbstractReferenceManufacturer.
	 * 
	 * Note that this is testing *object* presence. This will not return an
	 * object if a reference for the given identifier has been requested; it
	 * will only return true if an object with the given identifier has actually
	 * been constructed by or imported into this AbstractReferenceManufacturer.
	 * 
	 * @param key
	 *            identifier of the object to be returned
	 * @return The object stored in this AbstractReferenceManufacturer with the
	 *         given identifier, or null if this AbstractReferenceManufacturer
	 *         does not contain an object with the given identifier.
	 */
@Override
public T getObject(String key) {
    T po = active.get(key);
    if (po != null) {
        List<T> list = duplicates.getListFor(new CaseInsensitiveString(key));
        if ((list != null) && !list.isEmpty()) {
            Logging.errorPrint("Reference to Constructed " + factory.getReferenceDescription() + " " + key + " is ambiguous");
            StringBuilder sb = new StringBuilder(1000);
            sb.append("Locations: ");
            sb.append(po.getSourceURI());
            for (T dupe : list) {
                sb.append(", ");
                sb.append(dupe.getSourceURI());
            }
            Logging.errorPrint(sb.toString());
        }
        return po;
    }
    return null;
}
Also used : CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 4 with CaseInsensitiveString

use of pcgen.base.lang.CaseInsensitiveString in project pcgen by PCGen.

the class VisionType method buildMap.

private static void buildMap() {
    typeMap = new HashMap<>();
    Field[] fields = VisionType.class.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        int mod = fields[i].getModifiers();
        if (Modifier.isStatic(mod) && Modifier.isFinal(mod) && Modifier.isPublic(mod)) {
            try {
                Object o = fields[i].get(null);
                if (o instanceof VisionType) {
                    typeMap.put(new CaseInsensitiveString(fields[i].getName()), (VisionType) o);
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                //TODO Why throw an InternalError? Wouldn't an assert false be better? JK070115
                throw new InternalError();
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Example 5 with CaseInsensitiveString

use of pcgen.base.lang.CaseInsensitiveString in project pcgen by PCGen.

the class InfoLst method parseNonEmptyToken.

@Override
protected ParseResult parseNonEmptyToken(LoadContext context, CDOMObject cdo, String value) {
    if (value.charAt(0) == '|') {
        return new ParseResult.Fail(getTokenName() + " arguments may not start with PIPE : " + value);
    }
    if (value.charAt(value.length() - 1) == '|') {
        return new ParseResult.Fail(getTokenName() + " arguments may not end with PIPE : " + value);
    }
    int pipeLoc = value.indexOf(Constants.PIPE);
    if (pipeLoc == -1) {
        return new ParseResult.Fail(getTokenName() + " expecting '|', format is: InfoName|Info value was: " + value, context);
    }
    String key = value.substring(0, pipeLoc);
    //key length 0 caught by charAt(0) test above
    String val = value.substring(pipeLoc + 1);
    if (val.isEmpty()) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty value, " + "format is: InfoName|Info value was: " + value, context);
    }
    if (val.startsWith(Constants.PIPE)) {
        return new ParseResult.Fail(getTokenName() + " expecting non-empty value, " + "format is: InfoName|Info value was: " + value, context);
    }
    try {
        MessageFormat mf = new MessageFormat(val);
        CaseInsensitiveString cis = new CaseInsensitiveString(key);
        context.getObjectContext().put(cdo, MapKey.INFO, cis, mf);
    } catch (IllegalArgumentException e) {
        return new ParseResult.Fail(getTokenName() + " expected a valid MessageFormat, but received error: " + e.getMessage() + " when parsing: " + value, context);
    }
    return ParseResult.SUCCESS;
}
Also used : MessageFormat(java.text.MessageFormat) ParseResult(pcgen.rules.persistence.token.ParseResult) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString) CaseInsensitiveString(pcgen.base.lang.CaseInsensitiveString)

Aggregations

CaseInsensitiveString (pcgen.base.lang.CaseInsensitiveString)12 MessageFormat (java.text.MessageFormat)4 TreeSet (java.util.TreeSet)2 CDOMObject (pcgen.cdom.base.CDOMObject)2 WeakReference (java.lang.ref.WeakReference)1 Field (java.lang.reflect.Field)1 LegalScope (pcgen.base.formula.base.LegalScope)1 ObjectWrapperFacet (pcgen.cdom.facet.ObjectWrapperFacet)1 ResultFacet (pcgen.cdom.facet.analysis.ResultFacet)1 VariableContext (pcgen.rules.context.VariableContext)1 ParseResult (pcgen.rules.persistence.token.ParseResult)1