protected void declarePragma()

in src/main/java/org/apache/commons/jexl3/parser/JexlParser.java [538:575]


    protected void declarePragma(final String key, final Object value) {
        final JexlFeatures features = getFeatures();
        if (!features.supportsPragma()) {
            throwFeatureException(JexlFeatures.PRAGMA, getToken(0));
        }
        if (PRAGMA_IMPORT.equals(key) && !features.supportsImportPragma()) {
            throwFeatureException(JexlFeatures.IMPORT_PRAGMA, getToken(0));
        }
        if (pragmas == null) {
            pragmas = new TreeMap<>();
        }
        // declaring a namespace
        final Predicate<String> ns = features.namespaceTest();
        if (ns != null && key.startsWith(PRAGMA_JEXLNS)) {
            if (!features.supportsNamespacePragma()) {
                throwFeatureException(JexlFeatures.NS_PRAGMA, getToken(0));
            }
            // jexl.namespace.***
            final String nsname = key.substring(PRAGMA_JEXLNS.length());
            if (!nsname.isEmpty()) {
                if (namespaces == null) {
                    namespaces = new HashSet<>();
                }
                namespaces.add(nsname);
            }
        }
        // merge new value into a set created on the fly if key is already mapped
        pragmas.merge(key, value, (previous, newValue)->{
            if (previous instanceof Set<?>) {
                ((Set<Object>) previous).add(newValue);
                return previous;
            }
            final Set<Object> values = new LinkedHashSet<>();
            values.add(previous);
            values.add(newValue);
            return values;
        });
    }