Red Hat Web Application Framework 6.1 Manuale Utente

Pagina di 230
Chapter 5. WAF Component: Presentation
23
5.2.2.3. Pattern Generators
The
com.arsdigita.templating.PatternGenerator
interface provides the mechanism for in-
troducing new placeholders in the pattern based stylesheet resolver. This interface contains the single
method:
public String[] generateValues(String name,
HttpServletRequest request);
This method may use any available state information to generate a list of values for the placeholder.
The elements in the array should be ordered in decreasing specificity. If there are no possible values for
a placeholder, then an empty array can be returned, causing the entire abstract path to be thrown away.
As a hypothetical example, consider a pattern generator for switching based on the user’s browser:
public class BrowserPatternGenerator implements PatternGenerator {
public final static String GENERIC = "generic";
public String[] generateValues(String key,
HttpServletRequest req) {
String useragent = req.getHeader("user-agent");
if (useragent != null &&
useragent.indexOf("MSIE")

-1) {
return new String[] { "ie", GENERIC };
} elseif (useragent != null &&
useragent.indexOf("Mozilla")

-1) {
return new String[] { "mozilla", GENERIC };
} else {
return new String[] { GENERIC };
}
}
}
Example 5-1. Web Browser Triggered Pattern Generator
Once the new pattern generator class has been implemented, it needs to be registered
with the resolver by calling the static
registerPatternGenerator
method in
com.arsdigita.templating.PatternStylesheetResolver
, supplying the name of the
placeholder key. This registration is best done in the static initializer block of a core class, such as a
servlet.
static {
PatternStylesheetResolver.registerPatternGenerator(
"browser",
new BrowserPatternGenerator()
);
}
Available Patterns
application
This pattern generator expands to the current application package key. ie, the value returned by a
call to
com.arsdigita.web.Web.getConfig().getApplication().getKey()
.
host
This pattern generator expands to the hostname and port number for the current servlet container,
as returned by
com.arsdigita.web.Web.getConfig().getHost()
.