KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > typehandler > BinaryHandler


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.bridge.jsp.taglib.typehandler;
12
13 import javax.servlet.jsp.JspTagException JavaDoc;
14
15 import java.util.*;
16 import org.mmbase.bridge.*;
17 import org.mmbase.bridge.jsp.taglib.*;
18 import org.mmbase.bridge.jsp.taglib.edit.FormTag;
19 import org.mmbase.bridge.jsp.taglib.util.*;
20 import org.mmbase.util.logging.Logger;
21 import org.mmbase.util.logging.Logging;
22 import org.mmbase.util.functions.*;
23 import org.mmbase.util.*;
24 import org.mmbase.util.transformers.Xml;
25 import org.mmbase.datatypes.*;
26 import javax.servlet.jsp.PageContext JavaDoc;
27 import org.apache.commons.fileupload.FileItem;
28
29 /**
30  * @javadoc
31  *
32  * @author Gerard van de Looi
33  * @author Michiel Meeuwissen
34  * @since MMBase-1.8 (was named ByteHandler previously)
35  * @version $Id: BinaryHandler.java,v 1.4 2006/07/06 15:02:40 michiel Exp $
36  */

37
38 public class BinaryHandler extends AbstractTypeHandler {
39     private static final Logger log = Logging.getLoggerInstance(ByteHandler.class);
40     /**
41      * Constructor
42      * @param tag
43      */

44     public BinaryHandler(FieldInfoTag tag) {
45         super(tag);
46     }
47
48     /**
49      * @see TypeHandler#htmlInput(Node, Field, boolean)
50      */

51     public String JavaDoc htmlInput(Node node, Field field, boolean search) throws JspTagException JavaDoc {
52         StringBuffer JavaDoc show = new StringBuffer JavaDoc();
53         if (node != null) {
54             Function gui = node.getFunction("gui");
55             Parameters args = gui.createParameters();
56             args.set("field", field.getName());
57             args.set(Parameter.LANGUAGE, tag.getLocale().getLanguage());
58             args.set("session", tag.getSessionName());
59             PageContext JavaDoc pc = tag.getContextTag().getPageContext();
60             args.set(Parameter.RESPONSE, pc.getResponse());
61             args.set(Parameter.REQUEST, pc.getRequest());
62             args.set(Parameter.LOCALE, tag.getLocale());
63             show.append("" + gui.getFunctionValue(args));
64         }
65         show.append("<input class=\"" + getClasses(field) + "\" type=\"").append(search ? "text" : "file").append("\" name=\"").append(prefix(field.getName())).append("\" id=\"").append(prefixID(field.getName())).append("\" ");
66         addExtraAttributes(show);
67         show.append("/>");
68         return show.toString();
69
70     }
71
72
73     /**
74      * Returns the field value as specified by the client's post.
75      */

76     protected Object JavaDoc getFieldValue(Field field) throws JspTagException JavaDoc {
77         if (MultiPart.isMultipart(tag.getPageContext())) {
78             ContextContainer cc = tag.getContextProvider().getContextContainer();
79             ContextTag ct = tag.getContextTag();
80             FileItem bytes = ct.getFileItem(prefix(field.getName()));
81             return bytes;
82         } else {
83             return null;
84         }
85     }
86
87     public String JavaDoc checkHtmlInput(Node node, Field field, boolean errors) throws JspTagException JavaDoc {
88         Object JavaDoc fieldValue = getFieldValue(field);
89
90         if (fieldValue != null) {
91             DataType dt = field.getDataType();
92             Collection col = dt.validate(fieldValue, node, field);
93             if (col.size() == 0) {
94                 // do actually set the field, because some datatypes need cross-field checking
95
// also in an mm:form, you can simply commit.
96
if (node != null && ! field.isReadOnly()) {
97                     setValue(node, field, (FileItem) fieldValue);
98                 }
99                 if (errors) {
100                     return "<div id=\"" + prefixError(field.getName()) + "\" class=\"mm_check_noerror\"> </div>";
101                 } else {
102                     return "";
103                 }
104             } else {
105                 FormTag form = (FormTag) tag.findParentTag(FormTag.class, null, false);
106                 if (form != null) {
107                     form.setValid(false);
108                 }
109                 if (errors) {
110                     StringBuffer JavaDoc show = new StringBuffer JavaDoc("<div id=\"");
111                     show.append(prefixError(field.getName()));
112                     show.append("\" class=\"mm_check_error\">");
113                     Locale locale = tag.getLocale();
114                     Iterator i = col.iterator();
115                     while (i.hasNext()) {
116                         LocalizedString error = (LocalizedString) i.next();
117                         show.append("<span>");
118                         Xml.XMLEscape(error.get(locale), show);
119                         show.append("</span>");
120                     }
121                     show.append("</div>");
122                     return show.toString();
123                 } else {
124                     return "";
125                 }
126             }
127         } else {
128             return "";
129         }
130     }
131
132     /**
133      * Sets the binary value. Also tries to set some fields which are generaly associated with the binary.
134      * This could actually be moved to commit-processors of those fields.
135      */

136     protected void setValue(Node node, Field field, FileItem bytes) throws JspTagException JavaDoc {
137         if (bytes.getSize() > 0) {
138             String JavaDoc fileName = bytes.getName();
139             String JavaDoc fileType = bytes.getContentType();
140
141             try {
142                 node.setInputStreamValue(field.getName(), bytes.getInputStream(), bytes.getSize());
143             } catch (java.io.IOException JavaDoc ioe) {
144                 throw new TaglibException(ioe);
145             }
146             ContextContainer cc = tag.getContextProvider().getContextContainer();
147             log.debug("Filename : " + fileName);
148             NodeManager nm = node.getNodeManager();
149             // follwing stuff should probably be moved to commit-processors of the fields themselves.
150
if (nm.hasField("mimetype") && (fileType != null) && (! fileType.equals("")) &&
151                 cc.find(tag.getPageContext(), prefix("mimetype")) == null
152                 ) {
153                 node.setValueWithoutProcess("mimetype", fileType);
154             }
155             Object JavaDoc specFileName = cc.find(tag.getPageContext(), prefix("filename"));
156             if (nm.hasField("filename") &&
157                 fileName != null &&
158                 (! fileName.equals("")) &&
159                 (specFileName == null || specFileName.equals("") || specFileName.equals(node.getStringValue("filename")))
160                 ) {
161                 node.setValueWithoutProcess("filename", fileName);
162             }
163             if (nm.hasField("size") &&
164                 cc.find(tag.getPageContext(), prefix("size")) == null
165                 ) {
166                 node.setLongValue("size", bytes.getSize());
167             }
168             if (nm.hasField("filesize") &&
169                 cc.find(tag.getPageContext(), prefix("filesize")) == null
170                 ) {
171                 node.setLongValue("filesize", bytes.getSize());
172             }
173         }
174     }
175
176     /**
177      * @see TypeHandler#useHtmlInput(Node, Field)
178      */

179     public boolean useHtmlInput(Node node, Field field) throws JspTagException JavaDoc {
180         FileItem bytes = (FileItem) getFieldValue(field);
181         if (bytes == null){
182             throw new BridgeException("getBytes(" + prefix(field.getName()) + ") returned null (node= " + node.getNumber() +") field=(" + field + ") (Was your form enctype='multipart/form-data' ?");
183         }
184         setValue(node, field, bytes);
185
186
187         return true;
188     }
189
190     /**
191      * @see TypeHandler#whereHtmlInput(Field)
192      */

193     public String JavaDoc whereHtmlInput(Field field) throws JspTagException JavaDoc {
194         log.error("Don't know what to do with byte[]");
195         return super.whereHtmlInput(field);
196     }
197
198 }
199
Popular Tags