/** * Wraps the passed object into a template model using this object's object * wrapper. * @param obj the object to wrap * @return the template model that wraps the object * @throws TemplateModelException if the wrapper does not know how to * wrap the passed object. */ protected final TemplateModel wrap(Object obj) throws TemplateModelException { return objectWrapper.wrap(obj); }
private TemplateModel wrap(Object obj) throws TemplateModelException { return (obj instanceof TemplateModel) ? (TemplateModel) obj : objectWrapper.wrap(obj); }
public TemplateModel get(String key) throws TemplateModelException { return wrapper.wrap(request.getAttribute(key)); }
public TemplateModel get(String key) throws TemplateModelException { return wrapper.wrap(servletctx.getAttribute(key)); }
public TemplateModel next() throws TemplateModelException { try { return wrapper.wrap(it.next()); } catch (NoSuchElementException e) { throw new TemplateModelException("The collection has no more items.", e); } }
public TemplateModel get(String key) throws TemplateModelException { checkSessionExistence(); return wrapper.wrap(session != null ? session.getAttribute(key) : null); }
public TemplateCollectionModel values() throws TemplateModelException { return (TemplateCollectionModel) wrapper.getOuterIdentity().wrap(map.values()); }
protected TemplateModel wrap(Object obj) throws TemplateModelException { return wrapper.getOuterIdentity().wrap(obj); }
private void setSharedVariablesFromRewrappableSharedVariables() throws TemplateModelException { if (rewrappableSharedVariables == null) return; for (Iterator it = rewrappableSharedVariables.entrySet().iterator(); it.hasNext(); ) { Map.Entry/*<String, Object>*/ ent = (Entry) it.next(); String name = (String) ent.getKey(); Object value = ent.getValue(); TemplateModel valueAsTM; if (value instanceof TemplateModel) { valueAsTM = (TemplateModel) value; } else { valueAsTM = getObjectWrapper().wrap(value); } sharedVariables.put(name, valueAsTM); } }
public TemplateCollectionModel keys() throws TemplateModelException { return (TemplateCollectionModel) wrapper.getOuterIdentity().wrap(map.keySet()); }
/** * Invokes the specified method, wrapping the return value. The specialty * of this method is that if the return value is null, and the return type * of the invoked method is void, {@link TemplateModel#NOTHING} is returned. * @param object the object to invoke the method on * @param method the method to invoke * @param args the arguments to the method * @return the wrapped return value of the method. * @throws InvocationTargetException if the invoked method threw an exception * @throws IllegalAccessException if the method can't be invoked due to an * access restriction. * @throws TemplateModelException if the return value couldn't be wrapped * (this can happen if the wrapper has an outer identity or is subclassed, * and the outer identity or the subclass throws an exception. Plain * BeansWrapper never throws TemplateModelException). */ TemplateModel invokeMethod(Object object, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException, TemplateModelException { // [2.4]: Java's Method.invoke truncates numbers if the target type has not enough bits to hold the value. // There should at least be an option to check this. Object retval = method.invoke(object, args); return method.getReturnType() == void.class ? TemplateModel.NOTHING : getOuterIdentity().wrap(retval); }
/** * A hook that Jython uses. */ public void __setitem__(String key, Object o) throws TemplateException { setGlobalVariable(key, getObjectWrapper().wrap(o)); }
/** * Called for an object that isn't considered to be of a "basic" Java type, like for an application specific type, * or for a W3C DOM node. In its default implementation, W3C {@link Node}-s will be wrapped as {@link NodeModel}-s * (allows DOM tree traversal), Jython objects will be delegated to the {@code JythonWrapper}, others will be * wrapped using {@link BeansWrapper#wrap(Object)}. * * <p> * When you override this method, you should first decide if you want to wrap the object in a custom way (and if so * then do it and return with the result), and if not, then you should call the super method (assuming the default * behavior is fine with you). */ protected TemplateModel handleUnknownType(Object obj) throws TemplateModelException { if (obj instanceof Node) { return wrapDomNode(obj); } if (JYTHON_WRAPPER != null && JYTHON_OBJ_CLASS.isInstance(obj)) { return JYTHON_WRAPPER.wrap(obj); } return super.wrap(obj); }
/** * Returns the field or method named by the <tt>key</tt> * parameter. */ public TemplateModel get(String key) throws TemplateModelException { Object model = map.get(key); // Simple method, overloaded method or final field -- these have cached // template models if (model instanceof TemplateModel) return (TemplateModel) model; // Non-final field; this must be evaluated on each call. if (model instanceof Field) { try { return wrapper.getOuterIdentity().wrap(((Field) model).get(null)); } catch (IllegalAccessException e) { throw new TemplateModelException( "Illegal access for field " + key + " of class " + clazz.getName()); } } throw new TemplateModelException( "No such key: " + key + " in class " + clazz.getName()); }
/** * Adds shared variable to the configuration; It uses {@link Configurable#getObjectWrapper()} to wrap the * {@code value}, so it's important that the object wrapper is set before this. * * <p>This method is <b>not</b> thread safe; use it with the same restrictions as those that modify setting values. * * <p>The added value should be thread safe, if you are running templates from multiple threads with this * configuration. * * @throws TemplateModelException If some of the variables couldn't be wrapped via {@link #getObjectWrapper()}. * * @see #setSharedVaribles(Map) * @see #setSharedVariable(String,TemplateModel) * @see #setAllSharedVariables(TemplateHashModelEx) */ public void setSharedVariable(String name, Object value) throws TemplateModelException { setSharedVariable(name, getObjectWrapper().wrap(value)); }
public Object exec(List args) throws TemplateModelException { int argCnt = args.size(); checkMethodArgCount(argCnt, 1, 2); String splitString = (String) args.get(0); long flags = argCnt > 1 ? RegexpHelper.parseFlagString((String) args.get(1)) : 0; String[] result = null; if ((flags & RegexpHelper.RE_FLAG_REGEXP) == 0) { RegexpHelper.checkNonRegexpFlags(key, flags); result = StringUtil.split(s, splitString, (flags & RegexpHelper.RE_FLAG_CASE_INSENSITIVE) != 0); } else { Pattern pattern = RegexpHelper.getPattern(splitString, (int) flags); result = pattern.split(s); } return ObjectWrapper.DEFAULT_WRAPPER.wrap(result); } }
private static Object unwrap(TemplateModel model, boolean permissive) throws TemplateModelException { Environment env = Environment.getCurrentEnvironment(); TemplateModel nullModel = null; if (env != null) { ObjectWrapper wrapper = env.getObjectWrapper(); if (wrapper != null) { nullModel = wrapper.wrap(null); } } return unwrap(model, nullModel, permissive); }
dataModelHash = new SimpleHash(wrapper); } else { TemplateModel wrappedDataModel = wrapper.wrap(dataModel); if (wrappedDataModel instanceof TemplateHashModel) { dataModelHash = (TemplateHashModel) wrappedDataModel;
@Override public void setAttribute(String name, Object value, int scope) { switch(scope) { case PAGE_SCOPE: { try { environment.setGlobalVariable(name, wrapper.wrap(value)); break; } catch (TemplateModelException e) { throw new UndeclaredThrowableException(e); } } case REQUEST_SCOPE: { getRequest().setAttribute(name, value); break; } case SESSION_SCOPE: { getSession(true).setAttribute(name, value); break; } case APPLICATION_SCOPE: { getServletContext().setAttribute(name, value); break; } default: { throw new IllegalArgumentException("Invalid scope " + scope); } } }
map.put(field.getName(), wrapper.getOuterIdentity().wrap(field.get(null))); } catch (IllegalAccessException e) {