KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > convert > FileUploadConverter


1 /*
2  * ====================================================================
3  * Copyright (c) 2003 TONBELLER AG.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by
21  * TONBELLER AG (http://www.tonbeller.com)"
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28  * DISCLAIMED. IN NO EVENT SHALL THE TON BELLER AG OR
29  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
32  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  * ====================================================================
38  *
39  *
40  */

41 package com.tonbeller.wcf.convert;
42
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.apache.commons.beanutils.PropertyUtils;
47 import org.apache.commons.fileupload.FileItem;
48 import org.apache.log4j.Logger;
49 import org.w3c.dom.Element JavaDoc;
50
51 import com.tonbeller.wcf.format.Formatter;
52 import com.tonbeller.wcf.ui.FileUploadCtrl;
53 import com.tonbeller.wcf.utils.XoplonNS;
54
55 public class FileUploadConverter extends NodeConverterBase {
56
57   private static Logger logger = Logger.getLogger(FileUploadConverter.class);
58
59   public void convert(Formatter fmt, Map JavaDoc param, Map JavaDoc fileSource, Element JavaDoc element, Object JavaDoc bean)
60       throws IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
61
62     if (FileUploadCtrl.isDisabled(element))
63       return;
64
65     String JavaDoc id = FileUploadCtrl.getId(element);
66
67     FileItem [] fileItems = (FileItem[]) fileSource.get(id);
68
69     // input available
70
if (fileItems != null && fileItems.length>0) {
71       FileItem fileItem = fileItems[0];
72       try {
73         XoplonNS.removeAttribute(element, "error");
74
75         FileUploadCtrl.setFileName(element, fileItem.getName());
76
77         String JavaDoc model = FileUploadCtrl.getModelReference(element);
78         if (bean != null && model.length() > 0) {
79           PropertyUtils.setProperty(bean, model, fileItem);
80         }
81       } catch (IllegalAccessException JavaDoc e) {
82         logger.info("exception caught", e);
83         XoplonNS.setAttribute(element, "error", e.getMessage());
84         XoplonNS.setAttribute(element, "value", fileItem.getName());
85         throw e;
86       } catch (NoSuchMethodException JavaDoc e) {
87         logger.info("exception caught", e);
88         XoplonNS.setAttribute(element, "error", e.getMessage());
89         XoplonNS.setAttribute(element, "value", fileItem.getName());
90         throw e;
91       } catch (InvocationTargetException JavaDoc e) {
92         logger.info("exception caught", e);
93         XoplonNS.setAttribute(element, "error", e.getMessage());
94         XoplonNS.setAttribute(element, "value", fileItem.getName());
95         throw e;
96       }
97     }
98   }
99
100   public void convert(Formatter fmt, Object JavaDoc bean, Element JavaDoc element) throws ConvertException,
101       IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc {
102     try {
103       String JavaDoc model = FileUploadCtrl.getModelReference(element);
104       if (model.length() == 0)
105         return;
106
107       FileItem value = (FileItem) PropertyUtils.getProperty(bean, model);
108       if(value!=null)
109         FileUploadCtrl.setFileName(element, value.getName());
110       else
111         FileUploadCtrl.setFileName(element, "");
112
113     } catch (IllegalAccessException JavaDoc e) {
114       XoplonNS.setAttribute(element, "error", e.getMessage());
115       throw e;
116     } catch (NoSuchMethodException JavaDoc e) {
117       XoplonNS.setAttribute(element, "error", e.getMessage());
118       throw e;
119     } catch (InvocationTargetException JavaDoc e) {
120       XoplonNS.setAttribute(element, "error", e.getMessage());
121       throw e;
122     }
123   }
124
125 }
Popular Tags