Project

General

Profile

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

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

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

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

    
26
/**
27
 * Resolves views by using a {@link PathMatcher} to match the view name to the bean names.
28
 *
29
 * By default the {@link AntPathMatcher} is used which supports Ant-style path patterns.
30
 * The mapping matches views using the following rules:
31
 * <ul>
32
 *   <li>? matches one character
33
 *   <li>* matches zero or more characters
34
 *   <li>** matches zero or more 'directories' in a path
35
 *  </ul>
36
 * @author ben.clark
37
 */
38
public class PatternViewResolver extends XmlViewResolver {
39

    
40
	private final Set<String> viewSet = new HashSet<String>();
41

    
42
	private PathMatcher pathMatcher = new AntPathMatcher();
43

    
44

    
45

    
46
	public void setPathMatcher(PathMatcher pathMatcher) {
47
		Assert.notNull(pathMatcher, "PathMatcher must not be null");
48
		this.pathMatcher = pathMatcher;
49
	}
50

    
51
	public PathMatcher getPathMatcher() {
52
		return this.pathMatcher;
53
	}
54

    
55
	@Override
56
	public View resolveViewName(String viewName, Locale locale)  throws Exception {
57
		// Direct match?
58
		if (this.viewSet.contains(viewName)) {
59
			return super.resolveViewName(viewName, locale);
60
		}
61
		// Pattern match?
62
		String bestPathMatch = null;
63
		for (String registeredPath : this.viewSet) {
64
			if (getPathMatcher().match(registeredPath, viewName) &&
65
					(bestPathMatch == null || bestPathMatch.length() < registeredPath.length())) {
66
				bestPathMatch = registeredPath;
67
			}
68
		}
69
		if (bestPathMatch != null) {
70
			return super.resolveViewName(bestPathMatch, locale);
71
		}
72
		// No view found...
73
		return null;
74
	}
75

    
76
	@Override
77
	 protected synchronized BeanFactory initFactory() throws BeansException {
78
		AbstractApplicationContext beanFactory = (AbstractApplicationContext)super.initFactory();
79
		String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames();
80
		for(String beanDefinitionName : beanDefinitionNames) {
81
			viewSet.add(beanDefinitionName);
82
			viewSet.addAll(Arrays.asList(beanFactory.getAliases(beanDefinitionName)));
83
		}
84
		return beanFactory;
85
	}
86

    
87
}
(7-7/10)