View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
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, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.dxo.converter.impl;
17  
18  import org.apache.commons.fileupload.FileItem;
19  import org.seasar.extension.dxo.converter.ConversionContext;
20  import org.seasar.extension.dxo.converter.impl.AbstractConverter;
21  
22  /**
23   * {@link FileItem}を他の型のオブジェクトに変換するクラスの抽象クラウスです。
24   * 
25   * @author baba
26   * @since 1.0.0
27   */
28  public abstract class AbstractFileItemConverter extends AbstractConverter {
29  
30  	/**
31  	 * {@inheritDoc}
32  	 * <p>
33  	 * {@link FileItem}のクラスと{@link FileItem}配列のクラスを返します。
34  	 * </p>
35  	 */
36  	@SuppressWarnings("unchecked")
37  	public Class[] getSourceClasses() {
38  		return new Class[] { FileItem.class, FileItem[].class };
39  	}
40  
41  	/**
42  	 * {@inheritDoc}
43  	 */
44  	@SuppressWarnings("unchecked")
45  	public Object convert(final Object source, final Class destClass,
46  			final ConversionContext context) {
47  		if (source == null) {
48  			return null;
49  		}
50  		if (source instanceof FileItem) {
51  			final FileItem fileItem = (FileItem) source;
52  			return convert(fileItem);
53  		}
54  		if (source instanceof FileItem[]) {
55  			final FileItem[] fileItems = (FileItem[]) source;
56  			return convert(fileItems[0]);
57  		}
58  		return null;
59  	}
60  
61  	protected abstract Object convert(final FileItem fileItem);
62  
63  }