3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.remote
.view
;
13 import java
.util
.Arrays
;
14 import java
.util
.HashSet
;
15 import java
.util
.Locale
;
18 import org
.springframework
.beans
.BeansException
;
19 import org
.springframework
.beans
.factory
.BeanFactory
;
20 import org
.springframework
.context
.support
.AbstractApplicationContext
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.util
.AntPathMatcher
;
23 import org
.springframework
.util
.Assert
;
24 import org
.springframework
.util
.PathMatcher
;
25 import org
.springframework
.web
.servlet
.View
;
26 import org
.springframework
.web
.servlet
.view
.XmlViewResolver
;
28 import com
.sun
.xml
.bind
.v2
.runtime
.unmarshaller
.XsiNilLoader
.Array
;
31 * Resolves views by using a {@link PathMatcher} to match the view name to the bean names.
33 * Dy default the {@link AntPathMatcher} is used which supports Ant-style path patterns.
34 * The mapping matches views using the following rules:
36 * <li>? matches one character
37 * <li>* matches zero or more characters
38 * <li>** matches zero or more 'directories' in a path
43 public class PatternViewResolver
extends XmlViewResolver
{
44 private final Set
<String
> viewSet
= new HashSet
<String
>();
46 private PathMatcher pathMatcher
= new AntPathMatcher();
48 public void setPathMatcher(PathMatcher pathMatcher
) {
49 Assert
.notNull(pathMatcher
, "PathMatcher must not be null");
50 this.pathMatcher
= pathMatcher
;
53 public PathMatcher
getPathMatcher() {
54 return this.pathMatcher
;
58 public View
resolveViewName(String viewName
, Locale locale
) throws Exception
{
60 if (this.viewSet
.contains(viewName
)) {
61 return super.resolveViewName(viewName
, locale
);
64 String bestPathMatch
= null;
65 for (String registeredPath
: this.viewSet
) {
66 if (getPathMatcher().match(registeredPath
, viewName
) &&
67 (bestPathMatch
== null || bestPathMatch
.length() < registeredPath
.length())) {
68 bestPathMatch
= registeredPath
;
71 if (bestPathMatch
!= null) {
72 return super.resolveViewName(bestPathMatch
, locale
);
79 protected synchronized BeanFactory
initFactory() throws BeansException
{
80 AbstractApplicationContext beanFactory
= (AbstractApplicationContext
)super.initFactory();
81 String
[] beanDefinitionNames
= beanFactory
.getBeanDefinitionNames();
82 for(String beanDefinitionName
: beanDefinitionNames
) {
83 viewSet
.add(beanDefinitionName
);
84 viewSet
.addAll(Arrays
.asList(beanFactory
.getAliases(beanDefinitionName
)));