/** * 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 data the raw data. * @param filename the filename of the uploaded data. * @param mediatype the mediatype of the uploaded data. */ public void setValue(byte[] data, String filename, String mediatype) throws XFormsException { if (!isBound()) { return; } String value; String name = filename; String type = mediatype; if (data != null && data.length > 0) { // get model item datatype Validator validator = this.model.getValidator(); String datatype = getDatatype(); // convert binary data according to bound datatype if (validator.isRestricted("base64Binary", datatype)) { value = new String(Base64.encodeBase64(data, true)); } else if (validator.isRestricted("hexBinary", datatype)) { value = new String(Hex.encodeHex(data)); } else if (validator.isRestricted("anyURI", datatype)) { value = new String(data); } else { throw new XFormsBindingException( "datatype not supported by upload control", this.target, datatype); } // check mediatype if (mediatype == null || mediatype.length() == 0) { type = DEFAULT_MEDIATYPE; } } else { value = ""; name = ""; type = ""; } // update instance data Instance instance = this.model.getInstance(getInstanceId()); instance.setNodeValue(getLocationPath(), value); ModelItem item = instance.getModelItem(getLocationPath()); if (!item.isReadonly()) { item.setFilename(name); item.setMediatype(type); } // update helper elements if (this.filenameHelper != null) { this.filenameHelper.setValue(name); } if (this.mediatypeHelper != null) { this.mediatypeHelper.setValue(type); } dispatchValueChangeSequence(); }
/** * Initializes this upload. * * @throws XFormsException if the datatype of the bound is not supported. */ protected final void initializeUpload() throws XFormsException { if (isBound()) { Instance instance = this.model.getInstance(getInstanceId()); ModelItem item = instance.getModelItem(getLocationPath()); if (this.filenameHelper != null && !item.isReadonly()) { item.setFilename(this.filenameHelper.getValue()); } if (this.mediatypeHelper != null && !item.isReadonly()) { item.setMediatype(this.mediatypeHelper.getValue()); } } }