2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
9 package eu
.etaxonomy
.cdm
.remote
.view
;
11 import java
.util
.Arrays
;
12 import java
.util
.HashSet
;
13 import java
.util
.Locale
;
16 import org
.springframework
.beans
.BeansException
;
17 import org
.springframework
.beans
.factory
.BeanFactory
;
18 import org
.springframework
.context
.support
.AbstractApplicationContext
;
19 import org
.springframework
.util
.AntPathMatcher
;
20 import org
.springframework
.util
.Assert
;
21 import org
.springframework
.util
.PathMatcher
;
22 import org
.springframework
.web
.servlet
.View
;
23 import org
.springframework
.web
.servlet
.view
.XmlViewResolver
;
26 * Resolves views by using a {@link PathMatcher} to match the view name to the bean names.
28 * By default the {@link AntPathMatcher} is used which supports Ant-style path patterns.
29 * The mapping matches views using the following rules:
31 * <li>? matches one character
32 * <li>* matches zero or more characters
33 * <li>** matches zero or more 'directories' in a path
37 public class PatternViewResolver
extends XmlViewResolver
{
39 private final Set
<String
> viewSet
= new HashSet
<>();
41 private PathMatcher pathMatcher
= new AntPathMatcher();
43 public void setPathMatcher(PathMatcher pathMatcher
) {
44 Assert
.notNull(pathMatcher
, "PathMatcher must not be null");
45 this.pathMatcher
= pathMatcher
;
48 public PathMatcher
getPathMatcher() {
49 return this.pathMatcher
;
53 public View
resolveViewName(String viewName
, Locale locale
) throws Exception
{
55 if (this.viewSet
.contains(viewName
)) {
56 return super.resolveViewName(viewName
, locale
);
59 String bestPathMatch
= null;
60 for (String registeredPath
: this.viewSet
) {
61 if (getPathMatcher().match(registeredPath
, viewName
) &&
62 (bestPathMatch
== null || bestPathMatch
.length() < registeredPath
.length())) {
63 bestPathMatch
= registeredPath
;
66 if (bestPathMatch
!= null) {
67 return super.resolveViewName(bestPathMatch
, locale
);
74 protected synchronized BeanFactory
initFactory() throws BeansException
{
75 AbstractApplicationContext beanFactory
= (AbstractApplicationContext
)super.initFactory();
76 String
[] beanDefinitionNames
= beanFactory
.getBeanDefinitionNames();
77 for(String beanDefinitionName
: beanDefinitionNames
) {
78 viewSet
.add(beanDefinitionName
);
79 viewSet
.addAll(Arrays
.asList(beanFactory
.getAliases(beanDefinitionName
)));