View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.core.io.support;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.springframework.core.io.FileSystemResource;
28  import org.springframework.core.io.Resource;
29  import org.springframework.core.io.ResourceLoader;
30  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
31  
32  /**
33   * This class extends PathMatchingResourcePatternResolver in the way that
34   * it sorts the files in a folder alphabetically.
35   *
36   * @svnLink $Revision: 4091 $;$Date: 2010-01-15 12:21:07 +0100 (Fr, 15. Jan 2010) $;$Author: jonasha $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/core/io/support/OrderedPathMatchingResourcePatternResolver.java $
37   *
38   * @author Stefan Wismer (SWI)
39   */
40  public class OrderedPathMatchingResourcePatternResolver extends
41  	PathMatchingResourcePatternResolver {
42  	
43  	/**
44  	 * Is order ascending?
45  	 */
46  	protected boolean m_ascending = true;
47  	
48  	/**
49  	 * The resource comparator.
50  	 */
51  	protected final Comparator<Resource> m_resourceComparator = new Comparator<Resource>() {
52  		public int compare(Resource f1, Resource f2) {
53  			if (m_ascending) {
54  				return f1.getFilename().compareTo(f2.getFilename());
55  			} else {
56  				return -f1.getFilename().compareTo(f2.getFilename());
57  			}
58  		}
59  	};
60  	
61  	/**
62  	 * Create a PathMatchingResourcePatternResolver using a DefaultResourceLoader
63  	 * that sorts the files in a folder alphabetically.
64  	 * <p>ClassLoader access will happen via the thread context class loader.
65  	 * @see org.springframework.core.io.DefaultResourceLoader
66  	 */
67  	public OrderedPathMatchingResourcePatternResolver() {
68  		super();
69  	}
70  	
71  	/**
72  	 * Create a new PathMatchingResourcePatternResolver with a DefaultResourceLoader
73  	 * that sorts the files in a folder alphabetically.
74  	 * @param classLoader the ClassLoader to load classpath resources with,
75  	 * or <code>null</code> for using the thread context class loader
76  	 * @see org.springframework.core.io.DefaultResourceLoader
77  	 */
78  	public OrderedPathMatchingResourcePatternResolver(ClassLoader classLoader) {
79  		super(classLoader);
80  	}
81  	
82  	/**
83  	 * Create a new PathMatchingResourcePatternResolver that sorts the files in a folder alphabetically.
84  	 * <p>ClassLoader access will happen via the thread context class loader.
85  	 * @param resourceLoader the ResourceLoader to load root directories and
86  	 * actual resources with
87  	 */
88  	public OrderedPathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
89  		super(resourceLoader);
90  	}
91  	
92  	/**
93  	 * @return    whether order is ascending or not
94  	 */
95  	public boolean isAscending() {
96  		return m_ascending;
97  	}
98  
99  	/**
100 	 * @param ascending    is order ascending?
101 	 */
102 	public void setAscending(boolean ascending) {
103 		m_ascending = ascending;
104 	}
105 	
106 	/** {@inheritDoc} */
107 	@SuppressWarnings("unchecked")
108 	@Override
109 	protected Set doFindPathMatchingFileResources(Resource rootDirResource, String subPattern) throws IOException {
110 		Set<Resource> matchingResources = super.doFindPathMatchingFileResources(rootDirResource, subPattern);
111 		List<Resource> list = new ArrayList<Resource>(matchingResources);
112 		Collections.sort(list, m_resourceComparator);
113 		return new LinkedHashSet<Resource>(list);
114 	}
115 	
116 	/** {@inheritDoc} */
117 	@SuppressWarnings("unchecked")
118 	@Override
119 	protected Set doFindPathMatchingJarResources(Resource rootDirResource, String subPattern) throws IOException {
120 		Set<Resource> matchingJarResources = super.doFindPathMatchingJarResources(rootDirResource, subPattern);
121 		List<Resource> list = new ArrayList<Resource>(matchingJarResources);
122 		Collections.sort(list, m_resourceComparator);
123 		return new LinkedHashSet<Resource>(list);
124 	}
125 }