KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > bridge > jsp > taglib > edit > SetFieldTag


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 package org.mmbase.bridge.jsp.taglib.edit;
11
12 import javax.servlet.jsp.*;
13 import org.mmbase.bridge.*;
14 import org.mmbase.bridge.jsp.taglib.*;
15 import org.mmbase.bridge.jsp.taglib.util.Attribute;
16 import org.mmbase.util.logging.*;
17 import org.mmbase.util.transformers.*;
18 import org.mmbase.util.Casting;
19
20 /**
21  * The SetFieldTag can be used as a child of a 'NodeProvider' tag or inside a
22  * FieldProvider.
23  *
24  * @author Michiel Meeuwissen
25  * @author Jaco de Groot
26  * @version $Id: SetFieldTag.java,v 1.34 2005/11/23 10:29:39 michiel Exp $
27  */

28
29 public class SetFieldTag extends FieldTag { // but it is not a writer
30
private static final Logger log = Logging.getLoggerInstance(SetFieldTag.class);
31
32     protected String JavaDoc convert (String JavaDoc s) throws JspTagException {
33         return s;
34     }
35
36     private String JavaDoc body = null;
37     private Attribute valueId = Attribute.NULL;
38
39     public int doStartTag() throws JspTagException {
40         setFieldVar(name.getString(this));
41         return EVAL_BODY_BUFFERED;
42     }
43
44     public int doAfterBody() throws JspTagException {
45         if (bodyContent != null) body = bodyContent.getString();
46         return SKIP_BODY;
47     }
48     public void setValueid(String JavaDoc v) throws JspTagException {
49         valueId = getAttribute(v);
50     }
51
52     private static final ByteToCharTransformer base64 = new Base64();
53     /**
54      * Set the value of the field.
55      */

56     public int doEndTag() throws JspTagException {
57         setFieldVar();
58         Node node = getNode();
59         if (field == null) {
60             throw new JspTagException("Cannot set field '" + name.getString(this) + "' for node '" + node + "' (it does not exist?)");
61         }
62         int type = field.getType();
63
64         Object JavaDoc value;
65         String JavaDoc refid = valueId.getString(this);
66         if (body != null) {
67             if (! refid.equals("")) throw new JspTagException("Cannot use both body and referid attribute on setfield tag");
68             value = body;
69         } else if (! refid.equals("")) {
70             value = getObject(refid);
71         } else {
72             value = "";
73         }
74
75         if ((field != null) && (type == Field.TYPE_BYTE)) {
76             // if the field type is a byte[] then we expect a BASE64 encoded String, unless value is actualy a byte[].
77
if (value instanceof byte[]) {
78                 node.setByteValue(fieldName, (byte[]) value);
79             } else if (value instanceof org.apache.commons.fileupload.FileItem) {
80                 node.setByteValue(fieldName, ((org.apache.commons.fileupload.FileItem) value).get());
81             } else {
82                 node.setByteValue(fieldName, base64.transformBack(Casting.toString(value)));
83             }
84         } else {
85             String JavaDoc newValue = convert(Casting.toString(value));
86             // a bit of hackery to make it more likely that actually a right type is fed to the core.
87
// E.g. if you use ExprCalc to set an integer field, that would not work otherwise (because always double like '1.0')
88
switch(type) {
89             case Field.TYPE_NODE:
90             case Field.TYPE_INTEGER:
91                 value = Casting.toInteger(newValue);
92                 break;
93             case Field.TYPE_LONG:
94                 value = Casting.toInteger(newValue);
95                 break;
96             default: // rest should go ok in core
97
value = newValue;
98             }
99
100             
101             if (log.isDebugEnabled()) {
102                 log.debug("Setting field " + fieldName + " to " + value);
103             }
104             
105             node.setValue(fieldName, value);
106
107             if (getId() != null) {
108                 getContextProvider().getContextContainer().register(getId(), value);
109             }
110         }
111
112         return EVAL_PAGE;
113     }
114 }
115
Popular Tags