Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<module>tiles-ognl</module>
<module>tiles-compat</module>
<module>tiles-extras</module>
<module>tiles-examples</module>
<module>assembly</module>
<module>tiles-test-pom</module>
</modules>
Expand Down
43 changes: 43 additions & 0 deletions tiles-examples/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?xml version="1.0"?>
<!--
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-parent</artifactId>
<version>3.0.7-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>tiles-examples</artifactId>
<packaging>pom</packaging>
<name>Tiles - Examples</name>
<description>Extras for Tiles, including non-standard, non-generic, non-identifiable components that may be useful for Tiles users.
</description>
<modules>
<module>tiles-elognl</module>
</modules>
<build>
</build>
</project>
115 changes: 115 additions & 0 deletions tiles-examples/tiles-elognl/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?xml version="1.0"?>
<!--
/*
* $Id$
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<parent>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-examples</artifactId>
<version>3.0.7-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>tiles-elognl</artifactId>
<packaging>jar</packaging>
<name>Tiles - EL OGNL Freemarker support</name>
<description>Extras for Tiles, including non-standard, non-generic, non-identifiable components that may be useful for Tiles users.
</description>

<properties>
<tiles.osgi.symbolicName>org.apache.tiles.extras</tiles.osgi.symbolicName>
</properties>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>LICENSE.txt</exclude>
<exclude>NOTICE.txt</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>LICENSE.txt</include>
<include>NOTICE.txt</include>
</includes>
<targetPath>META-INF</targetPath>
</resource>
</resources>
</build>

<dependencies>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-freemarker</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-el</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.tiles</groupId>
<artifactId>tiles-ognl</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
package org.apache.tiles.elognl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import javax.el.ArrayELResolver;
import javax.el.BeanELResolver;
import javax.el.CompositeELResolver;
import javax.el.ELResolver;
import javax.el.ListELResolver;
import javax.el.MapELResolver;
import javax.el.ResourceBundleELResolver;

import org.apache.tiles.TilesContainer;
import org.apache.tiles.el.ELAttributeEvaluator;
import org.apache.tiles.el.JspExpressionFactoryFactory;
import org.apache.tiles.el.ScopeELResolver;
import org.apache.tiles.el.TilesContextBeanELResolver;
import org.apache.tiles.el.TilesContextELResolver;
import org.apache.tiles.evaluator.AttributeEvaluatorFactory;
import org.apache.tiles.evaluator.BasicAttributeEvaluatorFactory;
import org.apache.tiles.factory.BasicTilesContainerFactory;
import org.apache.tiles.factory.TilesContainerFactoryException;
import org.apache.tiles.freemarker.TilesSharedVariableFactory;
import org.apache.tiles.locale.LocaleResolver;
import org.apache.tiles.ognl.AnyScopePropertyAccessor;
import org.apache.tiles.ognl.DelegatePropertyAccessor;
import org.apache.tiles.ognl.NestedObjectDelegatePropertyAccessor;
import org.apache.tiles.ognl.OGNLAttributeEvaluator;
import org.apache.tiles.ognl.PropertyAccessorDelegateFactory;
import org.apache.tiles.ognl.ScopePropertyAccessor;
import org.apache.tiles.ognl.TilesApplicationContextNestedObjectExtractor;
import org.apache.tiles.ognl.TilesContextPropertyAccessorDelegateFactory;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.ApplicationResource;
import org.apache.tiles.request.Request;
import org.apache.tiles.request.freemarker.render.FreemarkerRenderer;
import org.apache.tiles.request.freemarker.render.FreemarkerRendererBuilder;
import org.apache.tiles.request.freemarker.servlet.SharedVariableLoaderFreemarkerServlet;
import org.apache.tiles.request.render.BasicRendererFactory;
import org.apache.tiles.request.render.ChainedDelegateRenderer;
import org.apache.tiles.request.render.Renderer;

import ognl.OgnlException;
import ognl.OgnlRuntime;
import ognl.PropertyAccessor;

public class ElOgnlTilesContainerFactory extends BasicTilesContainerFactory {

/**
* The freemarker renderer name.
*/
private static final String FREEMARKER_RENDERER_NAME = "freemarker";

/** {@inheritDoc} */
@Override
protected List<ApplicationResource> getSources(ApplicationContext applicationContext) {
Collection<ApplicationResource> webINFSet = applicationContext.getResources("/WEB-INF/tiles.xml");
//applicationContext.getResources("/WEB-INF/**/tiles*.xml");

List<ApplicationResource> filteredResources = new ArrayList<ApplicationResource>();
if (webINFSet != null) {
for (ApplicationResource resource : webINFSet) {
if (Locale.ROOT.equals(resource.getLocale())) {
filteredResources.add(resource);
}
}
}
return filteredResources;
}


/**
* Creates the EL evaluator.
*
* @param applicationContext The Tiles application context.
* @return The EL evaluator.
*/
private ELAttributeEvaluator createELEvaluator(ApplicationContext applicationContext) {
ELAttributeEvaluator evaluator = new ELAttributeEvaluator();
JspExpressionFactoryFactory efFactory = new JspExpressionFactoryFactory();
efFactory.setApplicationContext(applicationContext);
evaluator.setExpressionFactory(efFactory.getExpressionFactory());
ELResolver elResolver = new CompositeELResolver() {
{
BeanELResolver beanElResolver = new BeanELResolver(false);
add(new ScopeELResolver());
add(new TilesContextELResolver(beanElResolver));
add(new TilesContextBeanELResolver());
add(new ArrayELResolver(false));
add(new ListELResolver(false));
add(new MapELResolver(false));
add(new ResourceBundleELResolver());
add(beanElResolver);
}
};
evaluator.setResolver(elResolver);
return evaluator;
}

/**
* Creates the OGNL evaluator.
*
* @return The OGNL evaluator.
*/
private OGNLAttributeEvaluator createOGNLEvaluator() {
try {
PropertyAccessor objectPropertyAccessor = OgnlRuntime.getPropertyAccessor(Object.class);
PropertyAccessor applicationContextPropertyAccessor = new NestedObjectDelegatePropertyAccessor<Request>(
new TilesApplicationContextNestedObjectExtractor(), objectPropertyAccessor);
PropertyAccessor anyScopePropertyAccessor = new AnyScopePropertyAccessor();
PropertyAccessor scopePropertyAccessor = new ScopePropertyAccessor();
PropertyAccessorDelegateFactory<Request> factory = new TilesContextPropertyAccessorDelegateFactory(
objectPropertyAccessor, applicationContextPropertyAccessor, anyScopePropertyAccessor,
scopePropertyAccessor);
PropertyAccessor tilesRequestAccessor = new DelegatePropertyAccessor<Request>(factory);
OgnlRuntime.setPropertyAccessor(Request.class, tilesRequestAccessor);
return new OGNLAttributeEvaluator();
} catch (OgnlException e) {
throw new TilesContainerFactoryException("Cannot initialize OGNL evaluator", e);
}
}


/** {@inheritDoc} */
@Override
protected void registerAttributeRenderers(
BasicRendererFactory rendererFactory,
ApplicationContext applicationContext,
TilesContainer container,
AttributeEvaluatorFactory attributeEvaluatorFactory) {
super.registerAttributeRenderers(rendererFactory, applicationContext, container, attributeEvaluatorFactory);

FreemarkerRenderer freemarkerRenderer = FreemarkerRendererBuilder
.createInstance()
.setApplicationContext(applicationContext)
.setParameter("TemplatePath", "/")
.setParameter("NoCache", "true")
.setParameter("ContentType", "text/html")
.setParameter("template_update_delay", "0")
.setParameter("default_encoding", "ISO-8859-1")
.setParameter("number_format", "0.##########")
.setParameter(SharedVariableLoaderFreemarkerServlet.CUSTOM_SHARED_VARIABLE_FACTORIES_INIT_PARAM,
"tiles," + TilesSharedVariableFactory.class.getName()).build();
rendererFactory.registerRenderer(FREEMARKER_RENDERER_NAME, freemarkerRenderer);
}

/** {@inheritDoc} */
@Override
protected Renderer createDefaultAttributeRenderer(BasicRendererFactory rendererFactory,
ApplicationContext applicationContext, TilesContainer container,
AttributeEvaluatorFactory attributeEvaluatorFactory) {

ChainedDelegateRenderer retValue = new ChainedDelegateRenderer();
retValue.addAttributeRenderer(rendererFactory.getRenderer(DEFINITION_RENDERER_NAME));
retValue.addAttributeRenderer(rendererFactory.getRenderer(FREEMARKER_RENDERER_NAME));
retValue.addAttributeRenderer(rendererFactory.getRenderer(TEMPLATE_RENDERER_NAME));
retValue.addAttributeRenderer(rendererFactory.getRenderer(STRING_RENDERER_NAME));
return retValue;
}


/** {@inheritDoc} */
@Override
protected AttributeEvaluatorFactory createAttributeEvaluatorFactory(ApplicationContext applicationContext,
LocaleResolver resolver) {
BasicAttributeEvaluatorFactory attributeEvaluatorFactory = new BasicAttributeEvaluatorFactory(
createELEvaluator(applicationContext));
attributeEvaluatorFactory.registerAttributeEvaluator("OGNL", createOGNLEvaluator());

return attributeEvaluatorFactory;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.apache.tiles.elognl;

import javax.servlet.ServletContext;

import org.apache.tiles.startup.AbstractTilesInitializer;
import org.apache.tiles.factory.AbstractTilesContainerFactory;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletApplicationContext;

public class ElOgnlTilesInitializer extends AbstractTilesInitializer {

/** {@inheritDoc} */
@Override
protected ApplicationContext createTilesApplicationContext(
ApplicationContext preliminaryContext) {
return new ServletApplicationContext(
(ServletContext) preliminaryContext.getContext());
}


/** {@inheritDoc} */
@Override
protected AbstractTilesContainerFactory createContainerFactory(
ApplicationContext context) {
return new ElOgnlTilesContainerFactory();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.tiles.elognl;

import org.apache.tiles.startup.TilesInitializer;
import org.apache.tiles.web.startup.AbstractTilesListener;

public class ElOgnlTilesListener extends AbstractTilesListener {

/** {@inheritDoc} */
@Override
protected TilesInitializer createTilesInitializer() {
return new ElOgnlTilesInitializer();
}

}
Loading