Hello everybody ...
I was playing with the file upload, it seems that it works for me ...
Might be somebody will find it usefull. Changes in brief:
ChibaBean:
updateControlValue(String id, String contentType, String filename,
byte[] data)
- to be called from the EnvironmentAdapter
Upload, Filename, Mediatype:
setValue(...)
- Upload.setValue will convert the byte[] array to the
base64 or hex char stream, that will be stored into
the proper instance element, then it will try to locate
xforms:filename and xform:mediatype elements and
invoke theirs setValue() ...
You need the commons-codec-1.2.jar from jakarta.
------------------------------------------------------------------------
diff -urw src/org/chiba/xml/xforms/ChibaBean.java
src-new/org/chiba/xml/xforms/ChibaBean.java
--- src/org/chiba/xml/xforms/ChibaBean.java 2004-02-13 14:54:07.000000000
+0200
+++ src-new/org/chiba/xml/xforms/ChibaBean.java 2004-02-12 11:45:41.000000000
+0200
@@ -114,6 +114,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
+import org.chiba.xml.xforms.ui.Upload;
/**
* Chiba Facade Class.
@@ -523,6 +524,26 @@
}
/**
+ * This method emulates the setting of a new value through an UI control.
The value will only be changed if
+ * there was a change to the data. This method mainly exists to allow the
separation of the actual UI handling.
+ * Applications have to call this method to propagate their UI value
changes to the Chiba processor.
+ *
+ * @param id the id of the control
+ * @param newValue the new value for the control
+ * @throws XFormsException
+ */
+ public void updateControlValue(String id, String contentType, String filename, byte[] data)
+ throws XFormsException {
+ AbstractFormControl control = (AbstractFormControl)
getContainer().lookup(id);
+ if (!(control instanceof Upload)) {
+ LOGGER.warn("Only Update control can be updated with file data");
+ return;
+ }
+ Upload uploadControl = (Upload) control;
+ uploadControl.setValue(contentType, filename, data);
+ }
+
+ /**
* Finishes processor operation.
*
* @throws XFormsException if no document container is present
diff -urw src/org/chiba/xml/xforms/ui/Filename.java
src-new/org/chiba/xml/xforms/ui/Filename.java
--- src/org/chiba/xml/xforms/ui/Filename.java 2004-02-13 14:54:07.000000000
+0200
+++ src-new/org/chiba/xml/xforms/ui/Filename.java 2004-02-13
09:27:36.000000000 +0200
@@ -97,6 +97,8 @@
import org.apache.log4j.Category;
import org.chiba.xml.xforms.Model;
+import org.chiba.xml.xforms.events.EventFactory;
+import org.chiba.xml.xforms.exception.XFormsException;
import org.w3c.dom.Element;
/**
@@ -118,6 +120,18 @@
super(element, model);
}
+ public void setValue(String value) throws XFormsException {
+ if (isBound()) {
+
this.model.getInstance(getInstanceId()).setNodeValue(getLocationPath(), value);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.RECALCULATE, null);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REVALIDATE, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.VALUE_CHANGED, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.DOM_FOCUS_IN, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.DOM_FOCUS_OUT, null);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REFRESH, null);
+ }
+ }
+
/**
* Returns the logger object.
*
diff -urw src/org/chiba/xml/xforms/ui/Mediatype.java
src-new/org/chiba/xml/xforms/ui/Mediatype.java
--- src/org/chiba/xml/xforms/ui/Mediatype.java 2004-02-13 14:54:07.000000000
+0200
+++ src-new/org/chiba/xml/xforms/ui/Mediatype.java 2004-02-13
09:27:31.000000000 +0200
@@ -97,6 +97,8 @@
import org.apache.log4j.Category;
import org.chiba.xml.xforms.Model;
+import org.chiba.xml.xforms.events.EventFactory;
+import org.chiba.xml.xforms.exception.XFormsException;
import org.w3c.dom.Element;
/**
@@ -118,6 +120,18 @@
super(element, model);
}
+ public void setValue(String value) throws XFormsException {
+ if (isBound()) {
+
this.model.getInstance(getInstanceId()).setNodeValue(getLocationPath(), value);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.RECALCULATE, null);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REVALIDATE, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.VALUE_CHANGED, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.DOM_FOCUS_IN, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.DOM_FOCUS_OUT, null);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REFRESH, null);
+ }
+ }
+
/**
* Returns the logger object.
*
diff -urw src/org/chiba/xml/xforms/ui/Upload.java
src-new/org/chiba/xml/xforms/ui/Upload.java
--- src/org/chiba/xml/xforms/ui/Upload.java 2004-02-13 14:54:07.000000000
+0200
+++ src-new/org/chiba/xml/xforms/ui/Upload.java 2004-02-13 14:45:54.000000000
+0200
@@ -114,10 +114,21 @@
*/
package org.chiba.xml.xforms.ui;
+import java.io.InputStream;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Category;
+import org.apache.xerces.dom.ElementImpl;
+import org.chiba.xml.xforms.BindingResolver;
+import org.chiba.xml.xforms.Instance;
import org.chiba.xml.xforms.Model;
+import org.chiba.xml.xforms.ModelItem;
+import org.chiba.xml.xforms.NamespaceCtx;
+import org.chiba.xml.xforms.events.EventFactory;
import org.chiba.xml.xforms.exception.XFormsException;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
/**
* Implementation of XForms Upload Element.
@@ -147,7 +158,57 @@
* @param value the value to be set.
*/
public void setValue(String value) throws XFormsException {
- LOGGER.warn("Update control not yet implemented");
+ LOGGER.warn("Update control cannot be set with this method.");
+ }
+
+ /**
+ * Sets the value of this form control.
+ * <p>
+ * The bound instance data is updated and the event sequence for this
control
+ * is executed. Event sequences are described in Chapter 4.6 of XForms 1.0
Recommendation.
+ *
+ * @param value the value to be set.
+ */
+ public void setValue(String contentType, String filename, byte[] data)
+ throws XFormsException {
+ if (isBound()) {
+ // check data binding ...
+ String dataValue = null;
+ String datatype = getModelBinding().getDatatype();
+ if (datatype.equalsIgnoreCase("base64Binary")) {
+ dataValue = new String(Base64.encodeBase64(data));
+ } else if(datatype.equalsIgnoreCase("hexBinary")) {
+ dataValue = new String(Hex.encodeHex(data));
+ } else {
+ LOGGER.warn("Unsupported datatype: "+datatype);
+ return;
+ }
+ Instance instance = this.model.getInstance(getInstanceId());
+ instance.setNodeValue(getLocationPath(), dataValue);
+
+ // update children ...
+ NodeList nodes = getElement().getChildNodes();
+ if (nodes != null && nodes.getLength() > 0) {
+ for(int i=0; i<nodes.getLength(); i++) {
+ Node node = nodes.item(i);
+ if(node.getNodeType() != Node.ELEMENT_NODE)
+ continue;
+ Object userData = ((ElementImpl) node).getUserData();
+ if (userData != null) {
+ if (userData instanceof Mediatype) {
+ ((Mediatype) userData).setValue(contentType);
+ } else if (userData instanceof Filename) {
+ ((Filename) userData).setValue(filename);
+ }
+ }
+ }
+ }
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.RECALCULATE, null);
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REVALIDATE, null);
+ this.model.getContainer().dispatch(this.target,
EventFactory.VALUE_CHANGED, null);
+ //todo: DOMFocusIN, DOMFocusOUT ?
+ this.model.getContainer().dispatch(this.model.getTarget(),
EventFactory.REFRESH, null);
+ }
}
/**