private FormField parseField(XmlPullParser parser) throws Exception { boolean done = false; FormField formField = new FormField(parser.getAttributeValue("", "var")); formField.setLabel(parser.getAttributeValue("", "label")); formField.setType(parser.getAttributeValue("", "type")); while (!done) { int eventType = parser.next(); if (eventType == XmlPullParser.START_TAG) { if (parser.getName().equals("desc")) { formField.setDescription(parser.nextText()); } else if (parser.getName().equals("value")) { formField.addValue(parser.nextText()); } else if (parser.getName().equals("required")) { formField.setRequired(true); } else if (parser.getName().equals("option")) { formField.addOption(parseOption(parser)); } } else if (eventType == XmlPullParser.END_TAG) { if (parser.getName().equals("field")) { done = true; } } } return formField; }
if (field != null) { if (!FormField.TYPE_JID_MULTI.equals(field.getType()) && !FormField.TYPE_LIST_MULTI.equals(field.getType()) && !FormField.TYPE_LIST_SINGLE.equals(field.getType()) && !FormField.TYPE_TEXT_MULTI.equals(field.getType()) && !FormField.TYPE_HIDDEN.equals(field.getType())) { throw new IllegalArgumentException("This field only accept list of values."); field.resetValues(); field.addValues(values);
buf.append("<field"); if (getLabel() != null) { buf.append(" label=\"").append(getLabel()).append("\""); if (getVariable() != null) { buf.append(" var=\"").append(getVariable()).append("\""); if (getType() != null) { buf.append(" type=\"").append(getType()).append("\""); if (getDescription() != null) { buf.append("<desc>").append(getDescription()).append("</desc>"); if (isRequired()) { buf.append("<required/>"); for (Iterator<String> i = getValues(); i.hasNext();) { buf.append("<value>").append(i.next()).append("</value>"); for (Iterator i = getOptions(); i.hasNext();) { buf.append(((Option) i.next()).toXML());
/** * Returns true if this DataForm has at least one FORM_TYPE field which is * hidden. This method is used for sanity checks. * * @return */ public boolean hasHiddenFromTypeField() { boolean found = false; for (FormField f : fields) { if (f.getVariable().equals("FORM_TYPE") && f.getType() != null && f.getType().equals("hidden")) found = true; } return found; }
private DataForm createDefaultInitiationForm() { DataForm form = new DataForm(Form.TYPE_FORM); FormField field = new FormField(STREAM_DATA_FIELD_NAME); field.setType(FormField.TYPE_LIST_SINGLE); if (!IBB_ONLY) { field.addOption(new FormField.Option(Socks5BytestreamManager.NAMESPACE)); } field.addOption(new FormField.Option(InBandBytestreamManager.NAMESPACE)); form.addField(field); return form; } }
if (field.getVariable() != null) { FormField newField = new FormField(field.getVariable()); newField.setType(field.getType()); form.addField(newField); if (FormField.TYPE_HIDDEN.equals(field.getType())) { for (Iterator<String> it=field.getValues();it.hasNext();) { values.add(it.next()); form.setAnswer(field.getVariable(), values);
columns.add(new Column(field.getLabel(), field.getVariable(), field.getType())); for (Iterator<String> it=field.getValues(); it.hasNext();) { values.add(it.next()); fieldList.add(new Field(field.getVariable(), values));
/** * Sets the default value as the value of a given form's field. The field whose variable matches * the requested variable will be completed with its default value. If no field could be found * for the specified variable then an exception will be raised. * * @param variable the variable to complete with its default value. * @throws IllegalStateException if the form is not of type "submit". * @throws IllegalArgumentException if the form does not include the specified variable. */ public void setDefaultAnswer(String variable) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } FormField field = getField(variable); if (field != null) { // Clear the old values field.resetValues(); // Set the default value for (Iterator it = field.getValues(); it.hasNext();) { field.addValue((String) it.next()); } } else { throw new IllegalArgumentException("Couldn't find a field for the specified variable."); } }
private static String getSingleValue(FormField formField) { Iterator<String> values = formField.getValues(); while (values.hasNext()) { return values.next(); } return ""; }
/** * Sets a new int value to a given form's field. The field whose variable matches the * requested variable will be completed with the specified value. If no field could be found * for the specified variable then an exception will be raised. * * @param variable the variable name that was completed. * @param value the int value that was answered. * @throws IllegalStateException if the form is not of type "submit". * @throws IllegalArgumentException if the form does not include the specified variable or * if the answer type does not correspond with the field type. */ public void setAnswer(String variable, int value) { FormField field = getField(variable); if (field == null) { throw new IllegalArgumentException("Field not found for the specified variable name."); } if (!FormField.TYPE_TEXT_MULTI.equals(field.getType()) && !FormField.TYPE_TEXT_PRIVATE.equals(field.getType()) && !FormField.TYPE_TEXT_SINGLE.equals(field.getType())) { throw new IllegalArgumentException("This field is not of type int."); } setAnswer(field, value); }
/** * Sets a new Object value to a given form's field. In fact, the object representation * (i.e. #toString) will be the actual value of the field.<p> * * If the value to set to the field is not a basic type (e.g. String, boolean, int, etc.) you * will need to use {@link #setAnswer(String, String))} where the String value is the * String representation of the object.<p> * * Before setting the new value to the field we will check if the form is of type submit. If * the form isn't of type submit means that it's not possible to complete the form and an * exception will be thrown. * * @param field the form field that was completed. * @param value the Object value that was answered. The object representation will be the * actual value. * @throws IllegalStateException if the form is not of type "submit". */ private void setAnswer(FormField field, Object value) { if (!isSubmitType()) { throw new IllegalStateException("Cannot set an answer if the form is not of type " + "\"submit\""); } field.resetValues(); field.addValue(value.toString()); }
/** * Creates the initiation acceptance packet to forward to the stream * initiator. * * @param streamInitiationOffer The offer from the stream initiator to connect for a stream. * @param namespaces The namespace that relates to the accepted means of transfer. * @return The response to be forwarded to the initiator. */ public StreamInitiation createInitiationAccept( StreamInitiation streamInitiationOffer, String[] namespaces) { StreamInitiation response = new StreamInitiation(); response.setTo(streamInitiationOffer.getFrom()); response.setFrom(streamInitiationOffer.getTo()); response.setType(IQ.Type.RESULT); response.setPacketID(streamInitiationOffer.getPacketID()); DataForm form = new DataForm(Form.TYPE_SUBMIT); FormField field = new FormField( FileTransferNegotiator.STREAM_DATA_FIELD_NAME); for (String namespace : namespaces) { field.addValue(namespace); } form.addField(field); response.setFeatureNegotiationForm(form); return response; }
public int compare(FormField f1, FormField f2) { return f1.getVariable().compareTo(f2.getVariable()); } });
result.append(formField.getVariable()); result.append(':'); Iterator<String> values = formField.getValues(); StringBuilder valuesBuilder = new StringBuilder();
private StreamNegotiator getNegotiator(final FormField field) throws XMPPException { String variable; boolean isByteStream = false; boolean isIBB = false; for (Iterator it = field.getOptions(); it.hasNext();) { variable = ((FormField.Option) it.next()).getValue(); if (variable.equals(BYTE_STREAM) && !IBB_ONLY) { isByteStream = true; } else if (variable.equals(INBAND_BYTE_STREAM)) { isIBB = true; } } if (!isByteStream && !isIBB) { XMPPError error = new XMPPError(400); throw new XMPPException("No acceptable transfer mechanism", error); } if (isByteStream && isIBB && field.getType().equals(FormField.TYPE_LIST_MULTI)) { return new FaultTolerantNegotiator(connection, byteStreamTransferManager, inbandTransferManager); } else if (isByteStream) { return byteStreamTransferManager; } else { return inbandTransferManager; } }
boolean isByteStream = false; boolean isIBB = false; for (Iterator<FormField.Option> it = field.getOptions(); it.hasNext();) { variable = it.next().getValue(); if (variable.equals(Socks5BytestreamManager.NAMESPACE) && !IBB_ONLY) {
/** * * @param info * @return true if the packet extensions is ill-formed */ protected static boolean verifyPacketExtensions(DiscoverInfo info) { List<FormField> foundFormTypes = new LinkedList<FormField>(); for (Iterator<PacketExtension> i = info.getExtensions().iterator(); i.hasNext();) { PacketExtension pe = i.next(); if (pe.getNamespace().equals(Form.NAMESPACE)) { DataForm df = (DataForm) pe; for (Iterator<FormField> it = df.getFields(); it.hasNext();) { FormField f = it.next(); if (f.getVariable().equals("FORM_TYPE")) { for (FormField fft : foundFormTypes) { if (f.equals(fft)) return true; } foundFormTypes.add(f); } } } } return false; }
if (field.getVariable() != null) { FormField newField = new FormField(field.getVariable()); newField.setType(field.getType()); form.addField(newField); if (FormField.TYPE_HIDDEN.equals(field.getType())) { for (Iterator<String> it=field.getValues();it.hasNext();) { values.add(it.next()); form.setAnswer(field.getVariable(), values);
columns.add(new Column(field.getLabel(), field.getVariable(), field.getType())); for (Iterator it=field.getValues(); it.hasNext();) { values.add(it.next()); fieldList.add(new Field(field.getVariable(), values));