The "params" Map of TemplateDirectiveModel

Grégory Joseph <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Hi,

I'm currently writing a bunch of TemplateDirectiveModel implementations, and as part of the parameters validation, I was wondering if it was considered "safe" or even good practice to manipulate the params map. What I had in mind is: get/convert parameters and remove them from the map as I go (using utility method which take care of checking types, throwing exception with adequate messages etc - see below); once I fetched all my parameters, I should be able to do something along the lines of:
if (!params.isEmpty())
  throw new TemplateModelException("unsupported parameter(s): " + params);

Any thought or advice ? Would this be a decent way to go, or is there something easier I could do ?

Cheers,

-greg

ps: for the record: all my directives extend a custom Abstract one, which has a _param() method (see below) and other simpler protected methods which eventually all delegate to _param(), such as:

    protected String mandatoryString(Map params, String key) throws TemplateModelException {
        return _param(params, key, TemplateScalarModel.class, true).getAsString();
    }

    // [...] other param* methods [...]

    protected <MT extends TemplateModel> MT _param(Map<String, TemplateModel> params, String key, Class<MT> type, boolean isMandatory) throws TemplateModelException {
        if (isMandatory && !params.containsKey(key)) {
            throw new TemplateModelException("The '" + key + "' parameter is mandatory.");

        }
        final TemplateModel m = params.get(key);
        if (m != null && !type.isAssignableFrom(m.getClass())) {
            throw new TemplateModelException("The '" + key + "' parameter must be a " + type.getSimpleName() + " and is a " + m.getClass().getSimpleName() + ".");
        }
        if (m == null && params.containsKey(key)) {
            // parameter is passed but null value ... (happens with content.nonExistingSubNode apparently)
            throw new TemplateModelException("The '" + key + "' parameter was passed but not or wrongly specified.");
        }

        return (MT) m;
    }




------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.