View Javadoc

1   /*
2    * Copyright 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.util;
17  
18  import java.beans.PropertyEditorSupport;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  
22  import org.springframework.core.io.Resource;
23  import org.springframework.core.io.support.ResourceArrayPropertyEditor;
24  import org.springframework.util.StringUtils;
25  
26  /**
27   * Drop-in replacement for Spring's InputStreamEditor
28   * to convert one or more Resources to an InputStream.
29   * @author Matt Benson
30   */
31  public class ResourceArrayInputStreamPropertyEditor extends PropertyEditorSupport {
32  
33  	private ResourceArrayPropertyEditor resourceArrayPropertyEditor;
34  
35  	/**
36  	 * Construct a new ResourceArrayInputStreamPropertyEditor.
37  	 */
38  	public ResourceArrayInputStreamPropertyEditor() {
39  		this(new ResourceArrayPropertyEditor());
40  	}
41  
42  	/**
43  	 * Construct a new ResourceArrayInputStreamPropertyEditor.
44  	 * @param resourceArrayPropertyEditor
45  	 */
46  	public ResourceArrayInputStreamPropertyEditor(ResourceArrayPropertyEditor resourceArrayPropertyEditor) {
47  		this.resourceArrayPropertyEditor = resourceArrayPropertyEditor ;
48  	}
49  
50  	/* (non-Javadoc)
51  	 * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
52  	 */
53  	public void setAsText(String text) throws IllegalArgumentException {
54  		if (StringUtils.hasLength(text)) {
55  			String[] s = StringUtils.commaDelimitedListToStringArray(text);
56  			ArrayList l = new ArrayList(s.length);
57  			for (int i = 0; i < s.length; i++) {
58  				resourceArrayPropertyEditor.setAsText(s[i]);
59  				l.addAll(Arrays.asList((Resource[]) resourceArrayPropertyEditor.getValue()));
60  				setValue(new ResourceArrayInputStream((Resource[]) l.toArray(new Resource[l.size()])));
61  			}
62  		} else {
63  			setValue(null);
64  		}
65  	}
66  
67  }