public String getAsString() throws TemplateModelException { return ((TemplateScalarModel) model).getAsString(); }
public TemplateModel getValue() throws TemplateModelException { return hash.get(((TemplateScalarModel) key).getAsString()); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Override public Object exec(final List arguments) throws TemplateModelException { List<String> args = (List<String>) arguments.stream() .map(it -> Try.apply(() -> ((TemplateScalarModel) it).getAsString()).get()) .collect(Collectors.toList()); String[] xss = args.subList(1, args.size()) .toArray(new String[arguments.size() - 1]); return env.xss(xss).apply(args.get(0)); }
public String[] keys() throws TemplateModelException { TemplateHashModelEx h = (TemplateHashModelEx) model; List list = new ArrayList(); TemplateModelIterator i = h.keys().iterator(); while (i.hasNext()) { list.add(((TemplateScalarModel) i.next()).getAsString()); } return (String[]) list.toArray(new String[list.size()]); }
public Object nextElement() { try { return ((TemplateScalarModel) it.next()).getAsString(); } catch (TemplateModelException tme) { throw new UndeclaredThrowableException(tme); } } }
public Collection getLocalVariableNames() throws TemplateModelException { HashSet result = new HashSet(); for (TemplateModelIterator it = localVars.keys().iterator(); it.hasNext(); ) { result.add(((TemplateScalarModel) it.next()).getAsString()); } return result; } }
public String getAsString() throws TemplateModelException { // Boolean should have come first... but that change would be non-BC. if (bool instanceof TemplateScalarModel) { return ((TemplateScalarModel) bool).getAsString(); } else { try { return env.formatBoolean(bool.getAsBoolean(), true); } catch (TemplateException e) { throw new TemplateModelException(e); } } } }
private static void addKeys(Set set, SimpleSequence keySeq, TemplateHashModelEx hash) throws TemplateModelException { TemplateModelIterator it = hash.keys().iterator(); while (it.hasNext()) { TemplateScalarModel tsm = (TemplateScalarModel) it.next(); if (set.add(tsm.getAsString())) { // The first occurence of the key decides the index; // this is consisten with stuff like java.util.LinkedHashSet. keySeq.add(tsm); } } }
Object obj = args.get(0); if (obj instanceof TemplateScalarModel) { subvars = new String[]{((TemplateScalarModel) obj).getAsString()}; } else if (obj instanceof TemplateSequenceModel) { TemplateSequenceModel seq = (TemplateSequenceModel) obj; try { subvars[i] = ((TemplateScalarModel) item) .getAsString(); } catch (ClassCastException e) { if (!(item instanceof TemplateScalarModel)) {
/** * @param expr {@code null} is allowed, but may results in less helpful error messages * @param env {@code null} is allowed, but may results in lower performance in classic-compatible mode */ static String modelToString(TemplateScalarModel model, Expression expr, Environment env) throws TemplateModelException { String value = model.getAsString(); if (value == null) { if (env == null) env = Environment.getCurrentEnvironment(); if (env != null && env.isClassicCompatible()) { return ""; } else { throw newModelHasStoredNullException(String.class, model, expr); } } return value; }
@Override TemplateElement[] accept(Environment env) throws TemplateException { TemplateModel mval = value.eval(env); String strval; if (mval instanceof TemplateScalarModel) { strval = ((TemplateScalarModel) mval).getAsString(); } else if (mval instanceof TemplateBooleanModel) { strval = ((TemplateBooleanModel) mval).getAsBoolean() ? "true" : "false"; } else if (mval instanceof TemplateNumberModel) { strval = ((TemplateNumberModel) mval).getAsNumber().toString(); } else { strval = value.evalAndCoerceToStringOrUnsupportedMarkup(env); } env.setSetting(key, strval); return null; }
public TemplateCollectionModel values() throws TemplateModelException { List<Object> values = new ArrayList<Object>(size()); TemplateModelIterator it = keys().iterator(); while (it.hasNext()) { String key = ((TemplateScalarModel) it.next()).getAsString(); values.add(get(key)); } return new CollectionAndSequence(new SimpleSequence(values, wrapper)); }
private boolean getBoolean(Expression exp, boolean legacyCompat) throws ParseException { TemplateModel tm = null; try { tm = exp.eval(null); } catch (Exception e) { throw new ParseException(e.getMessage() + "\nCould not evaluate expression: " + exp.getCanonicalForm(), exp, e); } if (tm instanceof TemplateBooleanModel) { try { return ((TemplateBooleanModel) tm).getAsBoolean(); } catch (TemplateModelException tme) { } } if (legacyCompat && tm instanceof TemplateScalarModel) { try { return StringUtil.getYesNo(((TemplateScalarModel) tm).getAsString()); } catch (Exception e) { throw new ParseException(e.getMessage() + "\nExpecting boolean (true/false), found: " + exp.getCanonicalForm(), exp); } } throw new ParseException("Expecting boolean (true/false) parameter", exp); }
private void initValues() throws TemplateModelException { if (values == null) { SimpleSequence seq = new SimpleSequence(size()); // Note: size() invokes initKeys() if needed. int ln = keys.size(); for (int i = 0; i < ln; i++) { seq.add(get(((TemplateScalarModel) keys.get(i)).getAsString())); } values = new CollectionAndSequence(seq); } } }
static boolean isEmpty(TemplateModel model) throws TemplateModelException { if (model instanceof BeanModel) { return ((BeanModel) model).isEmpty(); } else if (model instanceof TemplateSequenceModel) { return ((TemplateSequenceModel) model).size() == 0; } else if (model instanceof TemplateScalarModel) { String s = ((TemplateScalarModel) model).getAsString(); return (s == null || s.length() == 0); } else if (model == null) { return true; } else if (model instanceof TemplateMarkupOutputModel) { // Note: happens just after FTL string check TemplateMarkupOutputModel mo = (TemplateMarkupOutputModel) model; return mo.getOutputFormat().isEmpty(mo); } else if (model instanceof TemplateCollectionModel) { return !((TemplateCollectionModel) model).iterator().hasNext(); } else if (model instanceof TemplateHashModel) { return ((TemplateHashModel) model).isEmpty(); } else if (model instanceof TemplateNumberModel || model instanceof TemplateDateModel || model instanceof TemplateBooleanModel) { return false; } else { return true; } }
public Object getVariableValue(String namespaceURI, String prefix, String localName) throws UnresolvableException { try { TemplateModel model = Environment.getCurrentEnvironment().getVariable(localName); if (model == null) { throw new UnresolvableException("Variable \"" + localName + "\" not found."); } if (model instanceof TemplateScalarModel) { return ((TemplateScalarModel) model).getAsString(); } if (model instanceof TemplateNumberModel) { return ((TemplateNumberModel) model).getAsNumber(); } if (model instanceof TemplateDateModel) { return ((TemplateDateModel) model).getAsDate(); } if (model instanceof TemplateBooleanModel) { return Boolean.valueOf(((TemplateBooleanModel) model).getAsBoolean()); } } catch (TemplateModelException e) { throw new UndeclaredThrowableException(e); } throw new UnresolvableException( "Variable \"" + localName + "\" exists, but it's not a string, number, date, or boolean"); } };
/** * Adds all object in the hash as shared variable to the configuration; it's like doing several * {@link #setSharedVariable(String, Object)} calls, one for each hash entry. It doesn't remove the already added * shared variable before doing this. * * <p>Never use <tt>TemplateModel</tt> implementation that is not thread-safe for shared shared variable values, * if the configuration is used by multiple threads! It is the typical situation for Servlet based Web sites. * * <p>This method is <b>not</b> thread safe; use it with the same restrictions as those that modify setting values. * * @param hash a hash model whose objects will be copied to the * configuration with same names as they are given in the hash. * If a shared variable with these names already exist, it will be replaced * with those from the map. * * @see #setSharedVaribles(Map) * @see #setSharedVariable(String,Object) * @see #setSharedVariable(String,TemplateModel) */ public void setAllSharedVariables(TemplateHashModelEx hash) throws TemplateModelException { TemplateModelIterator keys = hash.keys().iterator(); TemplateModelIterator values = hash.values().iterator(); while (keys.hasNext()) { setSharedVariable(((TemplateScalarModel) keys.next()).getAsString(), values.next()); } }
@Override TemplateModel _eval(Environment env) throws TemplateException { TemplateModel model = target.eval(env); if (model instanceof TemplateNumberModel) { return new NumberFormatter((TemplateNumberModel) model, env); } else if (model instanceof TemplateDateModel) { TemplateDateModel dm = (TemplateDateModel) model; return new DateFormatter(dm, env); } else if (model instanceof SimpleScalar) { return model; } else if (model instanceof TemplateBooleanModel) { return new BooleanFormatter((TemplateBooleanModel) model, env); } else if (model instanceof TemplateScalarModel) { return new SimpleScalar(((TemplateScalarModel) model).getAsString()); } else if (env.isClassicCompatible() && model instanceof BeanModel) { return new SimpleScalar(_BeansAPI.getAsClassicCompatibleString((BeanModel) model)); } else { throw new UnexpectedTypeException( target, model, "number, date, boolean or string", new Class[] { TemplateNumberModel.class, TemplateDateModel.class, TemplateBooleanModel.class, TemplateScalarModel.class }, env); } } }
nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null); } else if (namespaces instanceof ListLiteral) { nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
@Override TemplateElement[] accept(Environment env) throws IOException, TemplateException { TemplateModel node = targetNode.eval(env); if (!(node instanceof TemplateNodeModel)) { throw new NonNodeException(targetNode, node, env); } TemplateModel nss = namespaces == null ? null : namespaces.eval(env); if (namespaces instanceof StringLiteral) { nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null); } else if (namespaces instanceof ListLiteral) { nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env); } if (nss != null) { if (nss instanceof Environment.Namespace) { SimpleSequence ss = new SimpleSequence(1); ss.add(nss); nss = ss; } else if (!(nss instanceof TemplateSequenceModel)) { if (namespaces != null) { throw new NonSequenceException(namespaces, nss, env); } else { // Should not occur throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\""); } } } env.invokeNodeHandlerFor((TemplateNodeModel) node, (TemplateSequenceModel) nss); return null; }