/** Serializes the object to XML */ public void toXml(XmppStreamWriter out, Serializable object) throws IOException, XMLStreamException { DataForm form = (DataForm) object; out.writeStartElement("", getLocalName(), getNamespaceURI()); out.writeNamespace("", getNamespaceURI()); if (form.getType() != null) out.writeAttribute("type", form.getType()); if (form.getTitle() != null) { out.writeStartElement("title"); out.writeCharacters(form.getTitle()); out.writeEndElement(); // </title> } DataInstructions[] instructions = form.getInstructions(); if (instructions != null) { for (DataInstructions instruction : instructions) { toXml(out, instruction); } } DataField[] fields = form.getField(); if (fields != null) { for (DataField field : fields) { toXml(out, field); } } if (form.getReported() != null) toXml(out, form.getReported()); DataItem[] items = form.getItem(); if (items != null) { for (DataItem item : items) { toXml(out, item); } } out.writeEndElement(); // </form> }
/** Deserializes the object from XML */ public Serializable fromXml(XmppStreamReader in) throws IOException, XMLStreamException { boolean isFinest = log.isLoggable(Level.FINEST); String type = in.getAttributeValue(null, "type"); DataForm form = new DataForm(type); ArrayList<DataField> fieldList = new ArrayList<DataField>(); ArrayList<DataItem> itemList = new ArrayList<DataItem>(); ArrayList<DataInstructions> instructionsList = new ArrayList<DataInstructions>(); int tag = in.nextTag(); while (tag > 0) { if (isFinest) debug(in); if (XMLStreamReader.END_ELEMENT == tag) { form.setFieldList(fieldList); form.setItemList(itemList); form.setInstructionsList(instructionsList); return form; } if (XMLStreamReader.START_ELEMENT == tag && "field".equals(in.getLocalName())) { fieldList.add(parseField(in)); } else if (XMLStreamReader.START_ELEMENT == tag && "item".equals(in.getLocalName())) { itemList.add(parseItem(in)); } else if (XMLStreamReader.START_ELEMENT == tag && "reported".equals(in.getLocalName())) { form.setReported(parseReported(in)); } else if (XMLStreamReader.START_ELEMENT == tag && "title".equals(in.getLocalName())) { String title = in.getElementText(); form.setTitle(title); skipToEnd(in, "title"); } else if (XMLStreamReader.START_ELEMENT == tag && "instructions".equals(in.getLocalName())) { String value = in.getElementText(); instructionsList.add(new DataInstructions(value)); skipToEnd(in, "instructions"); } else if (XMLStreamReader.START_ELEMENT == tag) { log.finer(this + " <" + in.getLocalName() + "> is an unknown tag"); skipToEnd(in, in.getLocalName()); } tag = in.nextTag(); } return null; }
/** @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here DataForm.main(args); }
/** * Calculates the <tt>String</tt> for a specific <tt>DiscoverInfo</tt> which is to be hashed in * order to compute the ver string for that <tt>DiscoverInfo</tt>. * * @param discoverInfo the <tt>DiscoverInfo</tt> for which the <tt>String</tt> to be hashed in * order to compute its ver string is to be calculated * @return the <tt>String</tt> for <tt>discoverInfo</tt> which is to be hashed in order to compute * its ver string */ private static String calculateEntityCapsString(DiscoverInfo discoverInfo) { StringBuilder bldr = new StringBuilder(); // Add identities { Iterator<DiscoverInfo.Identity> identities = discoverInfo.getIdentities(); SortedSet<DiscoverInfo.Identity> is = new TreeSet<DiscoverInfo.Identity>( new Comparator<DiscoverInfo.Identity>() { public int compare(DiscoverInfo.Identity i1, DiscoverInfo.Identity i2) { int category = i1.getCategory().compareTo(i2.getCategory()); if (category != 0) return category; int type = i1.getType().compareTo(i2.getType()); if (type != 0) return type; /* * TODO Sort by xml:lang. * * Since sort by xml:lang is currently missing, * use the last supported sort criterion i.e. * type. */ return type; } }); if (identities != null) while (identities.hasNext()) is.add(identities.next()); for (DiscoverInfo.Identity i : is) { bldr.append(i.getCategory()) .append('/') .append(i.getType()) .append("//") .append(i.getName()) .append('<'); } } // Add features { Iterator<DiscoverInfo.Feature> features = getDiscoverInfoFeatures(discoverInfo); SortedSet<String> fs = new TreeSet<String>(); if (features != null) while (features.hasNext()) fs.add(features.next().getVar()); for (String f : fs) bldr.append(f).append('<'); } DataForm extendedInfo = (DataForm) discoverInfo.getExtension("x", "jabber:x:data"); if (extendedInfo != null) { synchronized (extendedInfo) { SortedSet<FormField> fs = new TreeSet<FormField>( new Comparator<FormField>() { public int compare(FormField f1, FormField f2) { return f1.getVariable().compareTo(f2.getVariable()); } }); FormField formType = null; for (Iterator<FormField> fieldsIter = extendedInfo.getFields(); fieldsIter.hasNext(); ) { FormField f = fieldsIter.next(); if (!f.getVariable().equals("FORM_TYPE")) fs.add(f); else formType = f; } // Add FORM_TYPE values if (formType != null) formFieldValuesToCaps(formType.getValues(), bldr); // Add the other values for (FormField f : fs) { bldr.append(f.getVariable()).append('<'); formFieldValuesToCaps(f.getValues(), bldr); } } } return bldr.toString(); }