Search in sources :

Example 11 with SimpleNumber

use of freemarker.template.SimpleNumber in project freemarker by apache.

the class SunInternalXalanXPathSupport method executeQuery.

public synchronized TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException {
    if (!(context instanceof Node)) {
        if (context != null) {
            if (isNodeList(context)) {
                int cnt = ((List) context).size();
                if (cnt != 0) {
                    throw new TemplateModelException("Cannot perform an XPath query against a node set of " + cnt + " nodes. Expecting a single node." + ERRMSG_RECOMMEND_JAXEN);
                } else {
                    throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
                }
            } else {
                throw new TemplateModelException("Cannot perform an XPath query against a " + context.getClass().getName() + ". Expecting a single org.w3c.dom.Node.");
            }
        } else {
            throw new TemplateModelException(ERRMSG_EMPTY_NODE_SET);
        }
    }
    Node node = (Node) context;
    try {
        XPath xpath = new XPath(xpathQuery, null, customPrefixResolver, XPath.SELECT, null);
        int ctxtNode = xpathContext.getDTMHandleFromNode(node);
        XObject xresult = xpath.execute(xpathContext, ctxtNode, customPrefixResolver);
        if (xresult instanceof XNodeSet) {
            NodeListModel result = new NodeListModel(node);
            result.xpathSupport = this;
            NodeIterator nodeIterator = xresult.nodeset();
            Node n;
            do {
                n = nodeIterator.nextNode();
                if (n != null) {
                    result.add(n);
                }
            } while (n != null);
            return result.size() == 1 ? result.get(0) : result;
        }
        if (xresult instanceof XBoolean) {
            return ((XBoolean) xresult).bool() ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
        }
        if (xresult instanceof XNull) {
            return null;
        }
        if (xresult instanceof XString) {
            return new SimpleScalar(xresult.toString());
        }
        if (xresult instanceof XNumber) {
            return new SimpleNumber(Double.valueOf(((XNumber) xresult).num()));
        }
        throw new TemplateModelException("Cannot deal with type: " + xresult.getClass().getName());
    } catch (TransformerException te) {
        throw new TemplateModelException(te);
    }
}
Also used : XPath(com.sun.org.apache.xpath.internal.XPath) NodeIterator(org.w3c.dom.traversal.NodeIterator) TemplateModelException(freemarker.template.TemplateModelException) XNull(com.sun.org.apache.xpath.internal.objects.XNull) XNumber(com.sun.org.apache.xpath.internal.objects.XNumber) Node(org.w3c.dom.Node) XBoolean(com.sun.org.apache.xpath.internal.objects.XBoolean) SimpleScalar(freemarker.template.SimpleScalar) XNodeSet(com.sun.org.apache.xpath.internal.objects.XNodeSet) SimpleNumber(freemarker.template.SimpleNumber) XString(com.sun.org.apache.xpath.internal.objects.XString) List(java.util.List) XObject(com.sun.org.apache.xpath.internal.objects.XObject) TransformerException(javax.xml.transform.TransformerException)

Example 12 with SimpleNumber

use of freemarker.template.SimpleNumber in project ninja by ninjaframework.

the class TemplateEngineFreemarkerI18nMethod method exec.

public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() == 1 && args.get(0) instanceof StringModel && ((StringModel) args.get(0)).getWrappedObject() instanceof ConstraintViolation) {
        ConstraintViolation violation = (ConstraintViolation) ((StringModel) args.get(0)).getWrappedObject();
        String messageValue = messages.get(violation.getMessageKey(), context, result, violation.getMessageParams()).orElse(violation.getDefaultMessage());
        return new SimpleScalar(messageValue);
    } else if (args.size() == 1) {
        String messageKey = ((SimpleScalar) args.get(0)).getAsString();
        String messageValue = messages.get(messageKey, context, result).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else if (args.size() > 1) {
        List<String> strings = Lists.newArrayList();
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        String messageKey = strings.get(0);
        String messageValue = messages.get(messageKey, context, result, strings.subList(1, strings.size()).toArray()).orElse(messageKey);
        logIfMessageKeyIsMissing(messageKey, messageValue);
        return new SimpleScalar(messageValue);
    } else {
        throw new TemplateModelException("Using i18n without any key is not possible.");
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ConstraintViolation(ninja.validation.ConstraintViolation) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar)

Example 13 with SimpleNumber

use of freemarker.template.SimpleNumber in project ninja by ninjaframework.

the class TemplateEngineFreemarkerReverseRouteHelper method computeReverseRoute.

public TemplateModel computeReverseRoute(List args) throws TemplateModelException {
    if (args.size() < 2) {
        throw new TemplateModelException("Please specify at least classname and controller (2 parameters).");
    } else {
        List<String> strings = new ArrayList<>(args.size());
        for (Object o : args) {
            // We currently allow only numbers and strings as arguments
            if (o instanceof String) {
                strings.add((String) o);
            }
            if (o instanceof SimpleScalar) {
                strings.add(((SimpleScalar) o).getAsString());
            } else if (o instanceof SimpleNumber) {
                strings.add(((SimpleNumber) o).toString());
            }
        }
        try {
            Class<?> clazz = Class.forName(strings.get(0));
            Object[] parameterMap = strings.subList(2, strings.size()).toArray();
            String reverseRoute = router.getReverseRoute(clazz, strings.get(1), parameterMap);
            return new SimpleScalar(reverseRoute);
        } catch (ClassNotFoundException ex) {
            throw new TemplateModelException("Error. Cannot find class for String: " + strings.get(0));
        }
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) ArrayList(java.util.ArrayList) SimpleScalar(freemarker.template.SimpleScalar)

Aggregations

SimpleNumber (freemarker.template.SimpleNumber)13 SimpleScalar (freemarker.template.SimpleScalar)7 TemplateModelException (freemarker.template.TemplateModelException)7 List (java.util.List)4 StringModel (freemarker.ext.beans.StringModel)2 SimpleDate (freemarker.template.SimpleDate)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 GradleVersion (com.android.ide.common.repository.GradleVersion)1 ContainerUtil.newArrayList (com.intellij.util.containers.ContainerUtil.newArrayList)1 XPath (com.sun.org.apache.xpath.internal.XPath)1 XBoolean (com.sun.org.apache.xpath.internal.objects.XBoolean)1 XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)1 XNull (com.sun.org.apache.xpath.internal.objects.XNull)1 XNumber (com.sun.org.apache.xpath.internal.objects.XNumber)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1 XString (com.sun.org.apache.xpath.internal.objects.XString)1 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1