@Override public TemplateModel wrap(Object obj) throws TemplateModelException { // So our existence builtins work as expected. if (obj == UNDEFINED_INSTANCE || obj == UniqueTag.NOT_FOUND) { return null; } // UniqueTag.NULL_VALUE represents intentionally set null in Rhino, and // BeansWrapper#nullModel also represents intentionally returned null. // I [A.Sz.] am fairly certain that this value is never passed out of // any of the Rhino code back to clients, but is instead always being // converted back to null. However, since this object is available to // any 3rd party Scriptable implementations as well, they might return // it, so we'll just be on the safe side, and handle it. if (obj == UniqueTag.NULL_VALUE) { return super.wrap(null); } // So, say, a JavaAdapter for FreeMarker interfaces works if (obj instanceof Wrapper) { obj = ((Wrapper) obj).unwrap(); } return super.wrap(obj); }
public TemplateModel next() throws TemplateModelException { return wrapper.wrap(iterator.next()); }
public TemplateCollectionModel keys() throws TemplateModelException { return (TemplateCollectionModel) wrapper.wrap(scriptable.getIds()); }
public TemplateModel get(int index) throws TemplateModelException { Object retval = ScriptableObject.getProperty(scriptable, index); if (retval instanceof Function) { return new RhinoFunctionModel((Function) retval, scriptable, wrapper); } else { return wrapper.wrap(retval); } }
public TemplateModel get(String key) throws TemplateModelException { Object retval = ScriptableObject.getProperty(scriptable, key); if (retval instanceof Function) { return new RhinoFunctionModel((Function) retval, scriptable, wrapper); } else { return wrapper.wrap(retval); } }
/** * 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); }
public TemplateCollectionModel values() throws TemplateModelException { Object[] ids = scriptable.getIds(); Object[] values = new Object[ids.length]; for (int i = 0; i < values.length; i++) { Object id = ids[i]; if (id instanceof Number) { values[i] = ScriptableObject.getProperty(scriptable, ((Number) id).intValue()); } else { values[i] = ScriptableObject.getProperty(scriptable, String.valueOf(id)); } } return (TemplateCollectionModel) wrapper.wrap(values); }
public Object exec(List args) throws TemplateModelException { if (args.isEmpty()) { throw new TemplateModelException("This method must have at least one argument, the name of the class to instantiate."); } String classname = args.get(0).toString(); Class cl = null; try { cl = ClassUtil.forName(classname); } catch (Exception e) { throw new TemplateModelException(e.getMessage()); } BeansWrapper bw = BeansWrapper.getDefaultInstance(); Object obj = bw.newInstance(cl, args.subList(1, args.size())); return bw.wrap(obj); } }
private Object callConstructor(Class cl) throws _ObjectBuilderSettingEvaluationException { if (hasNoParameters()) { // No need to create ObjectWrapper try { return cl.newInstance(); } catch (Exception e) { throw new _ObjectBuilderSettingEvaluationException( "Failed to call " + cl.getName() + " 0-argument constructor", e); } } else { BeansWrapper ow = env.getObjectWrapper(); List/*<TemplateModel>*/ tmArgs = new ArrayList(positionalParamValues.size()); for (int i = 0; i < positionalParamValues.size(); i++) { try { tmArgs.add(ow.wrap(positionalParamValues.get(i))); } catch (TemplateModelException e) { throw new _ObjectBuilderSettingEvaluationException("Failed to wrap arg #" + (i + 1), e); } } try { return ow.newInstance(cl, tmArgs); } catch (Exception e) { throw new _ObjectBuilderSettingEvaluationException( "Failed to call " + cl.getName() + " constructor", e); } } }
public TemplateModel get(String key) throws TemplateModelException { Object bean = scope == ANY_SCOPE ? pageContext.findAttribute(key) : pageContext.getAttribute(key, scope); return BeansWrapper.getDefaultInstance().wrap(bean); }
public Object exec(List arguments) throws TemplateModelException { Context cx = Context.getCurrentContext(); Object[] args = arguments.toArray(); BeansWrapper wrapper = getWrapper(); for (int i = 0; i < args.length; i++) { args[i] = wrapper.unwrap((TemplateModel) args[i]); } return wrapper.wrap(((Function) getScriptable()).call(cx, ScriptableObject.getTopLevelScope(fnThis), fnThis, args)); } }
elFunctionModel = beansWrapper.wrap(null, functionMethod); } catch (Exception e) { throw new TldParsingSAXException(
resultModel = wrapper.wrap(((Field) desc).get(object));
public TemplateModel wrap(Object object) throws TemplateModelException { // check for feature collection if (object instanceof FeatureCollection) { // create a model with just one variable called 'features' SimpleHash map = new SimpleHash(); map.put( "features", templateFeatureCollectionFactory.createTemplateFeatureCollection( (FeatureCollection) object, this)); map.put("type", wrap(((FeatureCollection) object).getSchema())); return map; } else if (object instanceof ComplexType) { return buildType((ComplexType) object); } else if (object instanceof Feature) { return buildComplex((Feature) object); } return super.wrap(object); }
public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj == null) { return super.wrap(null);
@Override public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj instanceof TemplateContext) { return new TemplateContextAdapter((TemplateContext) obj, this); } if (obj instanceof Renderable) { return new RenderableModel((Renderable) obj, this); } if (userDefinedWrapper != null) { return userDefinedWrapper.wrap(obj); } else { return super.wrap(obj); } } }
@Override public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj instanceof TemplateContext) { return new TemplateContextAdapter((TemplateContext) obj, this); } if (obj instanceof Renderable) { return new RenderableModel((Renderable) obj, this); } if (userDefinedWrapper != null) { return userDefinedWrapper.wrap(obj); } else { return super.wrap(obj); } } }
@Override public TemplateModel get(final String key) throws TemplateModelException { try { // GIT Issue#357: Accessing db in freemarker template throws exception // When content store is accessed with key "db" then wrap the ContentStore with BeansWrapper and return to template. // All methods on db are then accessible in template. Eg: ${db.getPublishedPostsByTag(tagName).size()} if(key.equals(Crawler.Attributes.DB)) { BeansWrapperBuilder bwb = new BeansWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS); BeansWrapper bw = bwb.build(); return bw.wrap(db); } return extractors.extractAndTransform(db, key, eagerModel.toMap(), new TemplateEngineAdapter<TemplateModel>() { @Override public TemplateModel adapt(String key, Object extractedValue) { if(key.equals(Crawler.Attributes.ALLTAGS)) { return new SimpleCollection((Collection) extractedValue, wrapper); } else if(key.equals(Crawler.Attributes.PUBLISHED_DATE)) { return new SimpleDate((Date) extractedValue, TemplateDateModel.UNKNOWN); } else { // All other cases, as far as I know, are document collections return new SimpleSequence((Collection) extractedValue, wrapper); } } }); } catch(NoModelExtractorException e) { return eagerModel.get(key); } }