public SectionWriter getSectionWriter() { try { AdapterTemplateModel model = (AdapterTemplateModel) Environment.getCurrentEnvironment().getGlobalVariable( "_info"); if( model != null ) { return (SectionWriter) model.getAdaptedObject(SectionWriter.class); } return null; } catch( TemplateModelException e ) { throw new SectionsRuntimeException(e); } }
@Override public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException { JSCallable function = (JSCallable) jsObject; RenderContext context = getSectionWriter(); context.preRender(function); Object[] exprArgs = new Object[args.size()]; int i = 0; for( Object obj : args ) { if( obj instanceof AdapterTemplateModel ) { Object adapted = ((AdapterTemplateModel) obj).getAdaptedObject(Object.class); exprArgs[i] = adapted; } else { exprArgs[i] = new ScriptExpression(obj.toString()); } i++; } return function.getExpressionForCall(context, JSUtils.convertExpressions(exprArgs)); }
public SectionWriter getSectionWriter() { try { return (SectionWriter) ((AdapterTemplateModel) Environment.getCurrentEnvironment().getGlobalVariable( "_info")) //$NON-NLS-1$ .getAdaptedObject(SectionWriter.class); } catch( TemplateModelException e ) { throw new SectionsRuntimeException(e); } }
@Override @SuppressWarnings("unchecked") public Object exec(List list) throws TemplateModelException { RenderContext info = getSectionWriter(); Object section = list.get(0); String tag = list.get(1).toString(); if( section instanceof AdapterTemplateModel ) { Object wrapped = ((AdapterTemplateModel) section).getAdaptedObject(Object.class); if( wrapped instanceof HtmlComponentState ) { return new TagRenderer(tag, ((HtmlComponentState) wrapped)); } if( wrapped instanceof SectionId ) { SectionId sectionId = (SectionId) wrapped; AbstractRenderedComponent<HtmlComponentState> component = (AbstractRenderedComponent<HtmlComponentState>) info .getSectionForId(sectionId); component.setRendererType(info, "null"); //$NON-NLS-1$ ResultListCollector results = new ResultListCollector(false); try { info.processEvent(new RenderEvent(info, sectionId.getSectionId(), results)); } catch( Exception e ) { throw new RuntimeException(e); } HtmlComponentState state = component.getState(info); TagRenderer renderer = new TagRenderer(tag, state); state.fireRendererCallback(info, renderer); return renderer; } } throw new SectionsRuntimeException("Needs to be a HtmlComponentState or AbstractRenderedComponent"); //$NON-NLS-1$ }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { UserBean user = null; Object userModel = args.get(0); if( userModel instanceof AdapterTemplateModel ) { Object wrapped = ((AdapterTemplateModel) userModel).getAdaptedObject(Object.class); if( wrapped instanceof UserBean ) { user = (UserBean) wrapped; } } if( user == null && userModel instanceof TemplateScalarModel ) { String userId = ((TemplateScalarModel) userModel).getAsString(); if( userId.equals("$LoggedInUser") ) //$NON-NLS-1$ { user = CurrentUser.getDetails(); } else { user = userService.getInformationForUser(userId); } } String format = Format.DEFAULT_USER_BEAN_FORMAT; if( args.size() > 1 ) { Object formatModel = args.get(1); if( formatModel instanceof TemplateScalarModel ) { format = ((TemplateScalarModel) formatModel).getAsString(); } } return new SimpleScalar(Utils.ent(Format.format(user, format))); }
@Override public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { String name = Directives.getStringParam(env, params, "name"); name = Overrides.BLOCK_NAME_PRE+name; TemplateModel tm = env.getCurrentNamespace().get(name); if(tm instanceof AdapterTemplateModel) { Object o = ((AdapterTemplateModel)tm).getAdaptedObject(null); if(o instanceof TemplateDirectiveBody) { ((TemplateDirectiveBody)o).render(env.getOut()); return; } } else if(tm instanceof TemplateDirectiveBody) { ((TemplateDirectiveBody)tm).render(env.getOut()); return; } else if(tm instanceof TemplateScalarModel) { String tpl = ((TemplateScalarModel)tm).getAsString(); if(tpl!=null) { env.getOut().append(tpl); return; } } if(body!=null) body.render(env.getOut()); }
/** * 获得RequestContext * * ViewResolver中的exposeSpringMacroHelpers必须为true * * @param env * @return * @throws TemplateException */ public static RequestContext getContext(Environment env) throws TemplateException { TemplateModel ctx = env .getGlobalVariable(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE); if (ctx instanceof AdapterTemplateModel) { return (RequestContext) ((AdapterTemplateModel) ctx) .getAdaptedObject(RequestContext.class); } else { throw new TemplateModelException("RequestContext '" + SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE + "' not found in DataModel."); } }
/** * 标签中获得站点 * * @param env * @return * @throws TemplateModelException */ public static CmsSite getSite(Environment env) throws TemplateModelException { TemplateModel model = env.getGlobalVariable(SITE); if (model instanceof AdapterTemplateModel) { return (CmsSite) ((AdapterTemplateModel) model) .getAdaptedObject(CmsSite.class); } else { throw new TemplateModelException("'" + SITE + "' not found in DataModel"); } }
/** * Tells whether an object is instance of a class. * <p> * {@inheritDoc} */ @Override public TemplateModel exec(List arguments) throws TemplateModelException { try { Object target = ((AdapterTemplateModel) arguments.get(0)) .getAdaptedObject(Object.class); Class<?> clazz = Class.forName(((TemplateScalarModel) arguments.get(1)) .getAsString()); return new BooleanModel(clazz.isAssignableFrom(target .getClass()), wrapper); } catch (Exception ex) { throw new TemplateModelException("Could execute instanceof method.", ex); } }
@Override @SuppressWarnings("unchecked") public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { Object section = params.get("section"); //$NON-NLS-1$ if( section == null ) { throw new SectionsRuntimeException("Section is null"); //$NON-NLS-1$ } if( section instanceof AdapterTemplateModel ) { Object wrapped = ((AdapterTemplateModel) section).getAdaptedObject(Object.class); if( wrapped instanceof SectionId ) { SectionId sectionId = (SectionId) wrapped; RenderContext context = getSectionWriter(); AbstractRenderedComponent<HtmlComponentState> component = (AbstractRenderedComponent<HtmlComponentState>) context .getSectionForId(sectionId); component.setRendererType(context, "null"); //$NON-NLS-1$ ResultListCollector results = new ResultListCollector(false); try { context.processEvent(new RenderEvent(context, sectionId.getSectionId(), results)); } catch( Exception e ) { throw new RuntimeException(e); } HtmlComponentState state = component.getState(context); TagRenderer renderer = createTagRenderer((HtmlMutableListState) state, env, params, body, loopVars); state.setBeenRendered(true); state.fireRendererCallback(context, renderer); setupStyles(renderer, params); try( SectionWriter writer = new SectionWriter(env.getOut(), context) ) { writer.render(renderer); } } else { throw new SectionsRuntimeException("Need a sectionId"); //$NON-NLS-1$ } } }
@NonNullByDefault(false) @SuppressWarnings({"unchecked", "nls", "rawtypes"}) @Override public void execute(Environment env, Map params, TemplateModel[] arg2, TemplateDirectiveBody body) throws TemplateException, IOException { RenderContext info = getSectionWriter(); try { Object model = params.get("section"); //$NON-NLS-1$ if( model instanceof AdapterTemplateModel ) { Object wrapped = ((AdapterTemplateModel) model).getAdaptedObject(Object.class); if( wrapped != null ) { SectionRenderable renderable = getRenderable(wrapped, params); if( renderable == null ) { return; } if( renderable instanceof StyleableRenderer ) { ((StyleableRenderer) renderable).setStyles(getParam("style", params), getParam("class", params), getParam("id", params)); } if( renderable instanceof NestedRenderable && body != null && getBodyField(env) != null ) { ((NestedRenderable) renderable).setNestedRenderable(new BodyDirectiveRenderable(body)); } SectionWriter writer = new SectionWriter(env.getOut(), info); writer.preRender(renderable); renderable.realRender(writer); } } else { throw new RuntimeException("'section' parameter to render macro was not of the appropriate type (was " + model.getClass() + "). Perhaps you named your section property the same as a method name?"); } } catch( Exception e ) { throw Throwables.propagate(e); } }