KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > support > ResourceArrayPropertyEditor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.io.support;
18
19 import java.beans.PropertyEditorSupport JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.springframework.core.io.Resource;
28 import org.springframework.util.SystemPropertyUtils;
29
30 /**
31  * Editor for {@link org.springframework.core.io.Resource} arrays, to
32  * automatically convert <code>String</code> location patterns
33  * (e.g. <code>"file:C:/my*.txt"</code> or <code>"classpath*:myfile.txt"</code>)
34  * to <code>Resource</code> array properties. Can also translate a collection
35  * or array of location patterns into a merged Resource array.
36  *
37  * <p>The path may contain <code>${...}</code> placeholders, to be resolved
38  * as system properties: e.g. <code>${user.dir}</code>.
39  *
40  * <p>Delegates to a {@link ResourcePatternResolver},
41  * by default using a {@link PathMatchingResourcePatternResolver}.
42  *
43  * @author Juergen Hoeller
44  * @since 1.1.2
45  * @see org.springframework.core.io.Resource
46  * @see ResourcePatternResolver
47  * @see PathMatchingResourcePatternResolver
48  * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
49  * @see System#getProperty(String)
50  */

51 public class ResourceArrayPropertyEditor extends PropertyEditorSupport JavaDoc {
52
53     private final ResourcePatternResolver resourcePatternResolver;
54
55
56     /**
57      * Create a new ResourceArrayPropertyEditor with a default
58      * PathMatchingResourcePatternResolver.
59      * @see PathMatchingResourcePatternResolver
60      */

61     public ResourceArrayPropertyEditor() {
62         this.resourcePatternResolver = new PathMatchingResourcePatternResolver();
63     }
64
65     /**
66      * Create a new ResourceArrayPropertyEditor with the given ResourcePatternResolver.
67      * @param resourcePatternResolver the ResourcePatternResolver to use
68      */

69     public ResourceArrayPropertyEditor(ResourcePatternResolver resourcePatternResolver) {
70         this.resourcePatternResolver = resourcePatternResolver;
71     }
72
73
74     /**
75      * Treat the given text as location pattern and convert it to a Resource array.
76      */

77     public void setAsText(String JavaDoc text) {
78         String JavaDoc pattern = resolvePath(text).trim();
79         try {
80             setValue(this.resourcePatternResolver.getResources(pattern));
81         }
82         catch (IOException JavaDoc ex) {
83             throw new IllegalArgumentException JavaDoc(
84                 "Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
85         }
86     }
87
88     /**
89      * Treat the given value as collection or array and convert it to a Resource array.
90      * Considers String elements as location patterns, and takes Resource elements as-is.
91      */

92     public void setValue(Object JavaDoc value) throws IllegalArgumentException JavaDoc {
93         if (value instanceof Collection JavaDoc || (value instanceof Object JavaDoc[] && !(value instanceof Resource[]))) {
94             Collection JavaDoc input = (value instanceof Collection JavaDoc ? (Collection JavaDoc) value : Arrays.asList((Object JavaDoc[]) value));
95             List JavaDoc merged = new ArrayList JavaDoc();
96             for (Iterator JavaDoc it = input.iterator(); it.hasNext();) {
97                 Object JavaDoc element = it.next();
98                 if (element instanceof String JavaDoc) {
99                     // A location pattern: resolve it into a Resource array.
100
// Might point to a single resource or to multiple resources.
101
String JavaDoc pattern = resolvePath((String JavaDoc) element).trim();
102                     try {
103                         Resource[] resources = this.resourcePatternResolver.getResources(pattern);
104                         for (int i = 0; i < resources.length; i++) {
105                             Resource resource = resources[i];
106                             if (!merged.contains(resource)) {
107                                 merged.add(resource);
108                             }
109                         }
110                     }
111                     catch (IOException JavaDoc ex) {
112                         throw new IllegalArgumentException JavaDoc(
113                                 "Could not resolve resource location pattern [" + pattern + "]: " + ex.getMessage());
114                     }
115                 }
116                 else if (element instanceof Resource) {
117                     // A Resource object: add it to the result.
118
if (!merged.contains(element)) {
119                         merged.add(element);
120                     }
121                 }
122                 else {
123                     throw new IllegalArgumentException JavaDoc("Cannot convert element [" + element + "] to [" +
124                             Resource.class.getName() + "]: only location String and Resource object supported");
125                 }
126             }
127             super.setValue(merged.toArray(new Resource[merged.size()]));
128         }
129
130         else {
131             // An arbitrary value: probably a String or a Resource array.
132
// setAsText will be called for a String; a Resource array will be used as-is.
133
super.setValue(value);
134         }
135     }
136
137     /**
138      * Resolve the given path, replacing placeholders with
139      * corresponding system property values if necessary.
140      * @param path the original file path
141      * @return the resolved file path
142      * @see org.springframework.util.SystemPropertyUtils#resolvePlaceholders
143      */

144     protected String JavaDoc resolvePath(String JavaDoc path) {
145         return SystemPropertyUtils.resolvePlaceholders(path);
146     }
147
148 }
149
Popular Tags