|
| 1 | +package org.apache.tiles.elognl; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Locale; |
| 7 | + |
| 8 | +import javax.el.ArrayELResolver; |
| 9 | +import javax.el.BeanELResolver; |
| 10 | +import javax.el.CompositeELResolver; |
| 11 | +import javax.el.ELResolver; |
| 12 | +import javax.el.ListELResolver; |
| 13 | +import javax.el.MapELResolver; |
| 14 | +import javax.el.ResourceBundleELResolver; |
| 15 | + |
| 16 | +import org.apache.tiles.TilesContainer; |
| 17 | +import org.apache.tiles.el.ELAttributeEvaluator; |
| 18 | +import org.apache.tiles.el.JspExpressionFactoryFactory; |
| 19 | +import org.apache.tiles.el.ScopeELResolver; |
| 20 | +import org.apache.tiles.el.TilesContextBeanELResolver; |
| 21 | +import org.apache.tiles.el.TilesContextELResolver; |
| 22 | +import org.apache.tiles.evaluator.AttributeEvaluatorFactory; |
| 23 | +import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory; |
| 24 | +import org.apache.tiles.factory.BasicTilesContainerFactory; |
| 25 | +import org.apache.tiles.factory.TilesContainerFactoryException; |
| 26 | +import org.apache.tiles.freemarker.TilesSharedVariableFactory; |
| 27 | +import org.apache.tiles.locale.LocaleResolver; |
| 28 | +import org.apache.tiles.ognl.AnyScopePropertyAccessor; |
| 29 | +import org.apache.tiles.ognl.DelegatePropertyAccessor; |
| 30 | +import org.apache.tiles.ognl.NestedObjectDelegatePropertyAccessor; |
| 31 | +import org.apache.tiles.ognl.OGNLAttributeEvaluator; |
| 32 | +import org.apache.tiles.ognl.PropertyAccessorDelegateFactory; |
| 33 | +import org.apache.tiles.ognl.ScopePropertyAccessor; |
| 34 | +import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor; |
| 35 | +import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory; |
| 36 | +import org.apache.tiles.request.ApplicationContext; |
| 37 | +import org.apache.tiles.request.ApplicationResource; |
| 38 | +import org.apache.tiles.request.Request; |
| 39 | +import org.apache.tiles.request.freemarker.render.FreemarkerRenderer; |
| 40 | +import org.apache.tiles.request.freemarker.render.FreemarkerRendererBuilder; |
| 41 | +import org.apache.tiles.request.freemarker.servlet.SharedVariableLoaderFreemarkerServlet; |
| 42 | +import org.apache.tiles.request.render.BasicRendererFactory; |
| 43 | +import org.apache.tiles.request.render.ChainedDelegateRenderer; |
| 44 | +import org.apache.tiles.request.render.Renderer; |
| 45 | + |
| 46 | +import ognl.OgnlException; |
| 47 | +import ognl.OgnlRuntime; |
| 48 | +import ognl.PropertyAccessor; |
| 49 | + |
| 50 | +public class ElOgnlTilesContainerFactory extends BasicTilesContainerFactory { |
| 51 | + |
| 52 | + /** |
| 53 | + * The freemarker renderer name. |
| 54 | + */ |
| 55 | + private static final String FREEMARKER_RENDERER_NAME = "freemarker"; |
| 56 | + |
| 57 | + /** {@inheritDoc} */ |
| 58 | + @Override |
| 59 | + protected List<ApplicationResource> getSources(ApplicationContext applicationContext) { |
| 60 | + Collection<ApplicationResource> webINFSet = applicationContext.getResources("/WEB-INF/tiles.xml"); |
| 61 | + //applicationContext.getResources("/WEB-INF/**/tiles*.xml"); |
| 62 | + |
| 63 | + List<ApplicationResource> filteredResources = new ArrayList<ApplicationResource>(); |
| 64 | + if (webINFSet != null) { |
| 65 | + for (ApplicationResource resource : webINFSet) { |
| 66 | + if (Locale.ROOT.equals(resource.getLocale())) { |
| 67 | + filteredResources.add(resource); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return filteredResources; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + /** |
| 76 | + * Creates the EL evaluator. |
| 77 | + * |
| 78 | + * @param applicationContext The Tiles application context. |
| 79 | + * @return The EL evaluator. |
| 80 | + */ |
| 81 | + private ELAttributeEvaluator createELEvaluator(ApplicationContext applicationContext) { |
| 82 | + ELAttributeEvaluator evaluator = new ELAttributeEvaluator(); |
| 83 | + JspExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory(); |
| 84 | + efFactory.setApplicationContext(applicationContext); |
| 85 | + evaluator.setExpressionFactory(efFactory.getExpressionFactory()); |
| 86 | + ELResolver elResolver = new CompositeELResolver() { |
| 87 | + { |
| 88 | + BeanELResolver beanElResolver = new BeanELResolver(false); |
| 89 | + add(new ScopeELResolver()); |
| 90 | + add(new TilesContextELResolver(beanElResolver)); |
| 91 | + add(new TilesContextBeanELResolver()); |
| 92 | + add(new ArrayELResolver(false)); |
| 93 | + add(new ListELResolver(false)); |
| 94 | + add(new MapELResolver(false)); |
| 95 | + add(new ResourceBundleELResolver()); |
| 96 | + add(beanElResolver); |
| 97 | + } |
| 98 | + }; |
| 99 | + evaluator.setResolver(elResolver); |
| 100 | + return evaluator; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Creates the OGNL evaluator. |
| 105 | + * |
| 106 | + * @return The OGNL evaluator. |
| 107 | + */ |
| 108 | + private OGNLAttributeEvaluator createOGNLEvaluator() { |
| 109 | + try { |
| 110 | + PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class); |
| 111 | + PropertyAccessor applicationContextPropertyAccessor = new NestedObjectDelegatePropertyAccessor<Request>( |
| 112 | + new TilesApplicationContextNestedObjectExtractor(), objectPropertyAccessor); |
| 113 | + PropertyAccessor anyScopePropertyAccessor = new AnyScopePropertyAccessor(); |
| 114 | + PropertyAccessor scopePropertyAccessor = new ScopePropertyAccessor(); |
| 115 | + PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory( |
| 116 | + objectPropertyAccessor, applicationContextPropertyAccessor, anyScopePropertyAccessor, |
| 117 | + scopePropertyAccessor); |
| 118 | + PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<Request>(factory); |
| 119 | + OgnlRuntime.setPropertyAccessor(Request.class, tilesRequestAccessor); |
| 120 | + return new OGNLAttributeEvaluator(); |
| 121 | + } catch (OgnlException e) { |
| 122 | + throw new TilesContainerFactoryException("Cannot initialize OGNL evaluator", e); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + |
| 127 | + /** {@inheritDoc} */ |
| 128 | + @Override |
| 129 | + protected void registerAttributeRenderers( |
| 130 | + BasicRendererFactory rendererFactory, |
| 131 | + ApplicationContext applicationContext, |
| 132 | + TilesContainer container, |
| 133 | + AttributeEvaluatorFactory attributeEvaluatorFactory) { |
| 134 | + super.registerAttributeRenderers(rendererFactory, applicationContext, container, attributeEvaluatorFactory); |
| 135 | + |
| 136 | + FreemarkerRenderer freemarkerRenderer = FreemarkerRendererBuilder |
| 137 | + .createInstance() |
| 138 | + .setApplicationContext(applicationContext) |
| 139 | + .setParameter("TemplatePath", "/") |
| 140 | + .setParameter("NoCache", "true") |
| 141 | + .setParameter("ContentType", "text/html") |
| 142 | + .setParameter("template_update_delay", "0") |
| 143 | + .setParameter("default_encoding", "ISO-8859-1") |
| 144 | + .setParameter("number_format", "0.##########") |
| 145 | + .setParameter(SharedVariableLoaderFreemarkerServlet.CUSTOM_SHARED_VARIABLE_FACTORIES_INIT_PARAM, |
| 146 | + "tiles," + TilesSharedVariableFactory.class.getName()).build(); |
| 147 | + rendererFactory.registerRenderer(FREEMARKER_RENDERER_NAME, freemarkerRenderer); |
| 148 | + } |
| 149 | + |
| 150 | + /** {@inheritDoc} */ |
| 151 | + @Override |
| 152 | + protected Renderer createDefaultAttributeRenderer(BasicRendererFactory rendererFactory, |
| 153 | + ApplicationContext applicationContext, TilesContainer container, |
| 154 | + AttributeEvaluatorFactory attributeEvaluatorFactory) { |
| 155 | + |
| 156 | + ChainedDelegateRenderer retValue = new ChainedDelegateRenderer(); |
| 157 | + retValue.addAttributeRenderer(rendererFactory.getRenderer(DEFINITION_RENDERER_NAME)); |
| 158 | + retValue.addAttributeRenderer(rendererFactory.getRenderer(FREEMARKER_RENDERER_NAME)); |
| 159 | + retValue.addAttributeRenderer(rendererFactory.getRenderer(TEMPLATE_RENDERER_NAME)); |
| 160 | + retValue.addAttributeRenderer(rendererFactory.getRenderer(STRING_RENDERER_NAME)); |
| 161 | + return retValue; |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + /** {@inheritDoc} */ |
| 166 | + @Override |
| 167 | + protected AttributeEvaluatorFactory createAttributeEvaluatorFactory(ApplicationContext applicationContext, |
| 168 | + LocaleResolver resolver) { |
| 169 | + BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory( |
| 170 | + createELEvaluator(applicationContext)); |
| 171 | + attributeEvaluatorFactory.registerAttributeEvaluator("OGNL", createOGNLEvaluator()); |
| 172 | + |
| 173 | + return attributeEvaluatorFactory; |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | +} |
0 commit comments