Project

General

Profile

Download (2.76 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
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.
9
*/
10

    
11
package eu.etaxonomy.cdm.remote.view;
12

    
13
import java.util.Arrays;
14
import java.util.HashSet;
15
import java.util.Locale;
16
import java.util.Set;
17

    
18
import org.springframework.beans.BeansException;
19
import org.springframework.beans.factory.BeanFactory;
20
import org.springframework.context.support.AbstractApplicationContext;
21
import org.springframework.util.AntPathMatcher;
22
import org.springframework.util.Assert;
23
import org.springframework.util.PathMatcher;
24
import org.springframework.web.servlet.View;
25
import org.springframework.web.servlet.view.XmlViewResolver;
26

    
27
/**
28
 * Resolves views by using a {@link PathMatcher} to match the view name to the bean names.
29
 * 
30
 * Dy default the {@link AntPathMatcher} is used which supports Ant-style path patterns. 
31
 * The mapping matches views using the following rules:
32
 * <ul>
33
 *   <li>? matches one character
34
 *   <li>* matches zero or more characters
35
 *   <li>** matches zero or more 'directories' in a path 
36
 *  </ul>
37
 * @author ben.clark
38
 */
39
public class PatternViewResolver extends XmlViewResolver {
40
	
41
	private final Set<String> viewSet = new HashSet<String>();
42
	
43
	private PathMatcher pathMatcher = new AntPathMatcher();
44
	
45
	
46
	
47
	public void setPathMatcher(PathMatcher pathMatcher) {
48
		Assert.notNull(pathMatcher, "PathMatcher must not be null");
49
		this.pathMatcher = pathMatcher;
50
	}
51
	
52
	public PathMatcher getPathMatcher() {
53
		return this.pathMatcher;
54
	}
55
	
56
	@Override
57
	public View resolveViewName(String viewName, Locale locale)  throws Exception {
58
		// Direct match?
59
		if (this.viewSet.contains(viewName)) {
60
			return super.resolveViewName(viewName, locale);
61
		}
62
		// Pattern match?
63
		String bestPathMatch = null;
64
		for (String registeredPath : this.viewSet) {
65
			if (getPathMatcher().match(registeredPath, viewName) &&
66
					(bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) {
67
				bestPathMatch = registeredPath;
68
			}
69
		}
70
		if (bestPathMatch != null) {
71
			return super.resolveViewName(bestPathMatch, locale);
72
		}
73
		// No view found...
74
		return null;
75
	}
76
	
77
	@Override
78
	 protected synchronized BeanFactory initFactory() throws BeansException {
79
		AbstractApplicationContext beanFactory = (AbstractApplicationContext)super.initFactory();
80
		String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
81
		for(String beanDefinitionName : beanDefinitionNames) {
82
			viewSet.add(beanDefinitionName);
83
			viewSet.addAll(Arrays.asList(beanFactory.getAliases(beanDefinitionName)));
84
		}
85
		return beanFactory;
86
	}
87

    
88
}
(7-7/10)