You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by be...@apache.org on 2009/05/20 22:43:38 UTC
svn commit: r776845 - in
/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp: protocol/
stanza/dataforms/
Author: berndf
Date: Wed May 20 20:43:38 2009
New Revision: 776845
URL: http://svn.apache.org/viewvc?rev=776845&view=rev
Log:
[vysper] first take on data forms (VYSPER-61)
Added:
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataForm.java
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataFormEncoder.java
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Field.java
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Option.java
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/package.html
Modified:
mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java
Modified: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java?rev=776845&r1=776844&r2=776845&view=diff
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java (original)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/protocol/NamespaceURIs.java Wed May 20 20:43:38 2009
@@ -43,6 +43,7 @@
// extension namespaces
public static final String JABBER_IQ_VERSION = "jabber:iq:version";
public static final String JABBER_IQ_TIME = "jabber:iq:time";
+ public static final String JABBER_X_DATA = "jabber:x:data";
public static final String URN_XMPP_TIME = "urn:xmpp:time";
public static final String VCARD_TEMP = "vcard-temp";
public static final String XEP0030_SERVICE_DISCOVERY_ITEMS = "http://jabber.org/protocol/disco#items";
Added: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataForm.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataForm.java?rev=776845&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataForm.java (added)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataForm.java Wed May 20 20:43:38 2009
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.xmpp.stanza.dataforms;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ */
+public class DataForm {
+
+ public static enum Type {
+
+ cancel, form, result, submit;
+
+ public String value() {
+ return name();
+ }
+
+ }
+
+ protected Type type;
+ protected List<String> instructions = new ArrayList<String>();
+ protected String title;
+ protected final List<Field> fields = new ArrayList<Field>();
+ protected final List<List<Field>> items = new ArrayList<List<Field>>();
+
+ public Type getType() {
+ return type;
+ }
+
+ public void setType(Type type) {
+ this.type = type;
+ }
+
+ public Iterator<String> getInstructionIterator() {
+ return instructions.iterator();
+ }
+
+ public void addInstruction(String instructions) {
+ this.instructions.add(instructions);
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public Iterator<Field> getFieldIterator() {
+ return fields.iterator();
+ }
+
+ public void addField(Field field) {
+ fields.add(field);
+ }
+
+ public Iterator<List<Field>> getItemIterator() {
+ return items.iterator();
+ }
+
+ public void addItem(List<Field> item) {
+ items.add(item);
+ }
+
+ public Iterator<Field> getReportedIterator() {
+ List<Field> reportedFields = new ArrayList<Field>();
+
+ if (items.size() == 0) return reportedFields.iterator();
+
+ // reported fields are implicitly defined by item fields
+ // here, reported fields are only taken from the first item.
+ // there is no consistency check if the remaining fields report the same fields,
+ // as the spec requires (XEP-004#3.4, last sentence).
+ List<Field> fieldPrototype = items.get(0);
+ for (Field field : fieldPrototype) {
+ reportedFields.add(new Field(field.getLabel(), field.getType(), field.getVar()));
+ }
+
+ return reportedFields.iterator();
+ }
+
+}
Added: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataFormEncoder.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataFormEncoder.java?rev=776845&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataFormEncoder.java (added)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/DataFormEncoder.java Wed May 20 20:43:38 2009
@@ -0,0 +1,143 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.xmpp.stanza.dataforms;
+
+import org.apache.vysper.xmpp.protocol.NamespaceURIs;
+import org.apache.vysper.xmpp.xmlfragment.Attribute;
+import org.apache.vysper.xmpp.xmlfragment.XMLElement;
+import org.apache.vysper.xmpp.xmlfragment.XMLFragment;
+import org.apache.vysper.xmpp.xmlfragment.XMLText;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * makes XMPP out of a Data Form
+ */
+public class DataFormEncoder {
+
+ public static XMLElement getXML(DataForm dataForm) {
+
+ List<XMLFragment> childElements = new ArrayList<XMLFragment>();
+
+ Iterator<String> instructionIterator = dataForm.getInstructionIterator();
+ while (instructionIterator.hasNext()) {
+ String instruction = instructionIterator.next();
+ if (instruction == null) continue;
+ childElements.add(createTextOnlyElement("instructions", instruction));
+ }
+
+ if (dataForm.getTitle() != null) {
+ childElements.add(createTextOnlyElement("title", dataForm.getTitle()));
+ }
+
+ if (dataForm.getType() == DataForm.Type.form) {
+ // reported element, containing the reported items
+ ArrayList<XMLFragment> reportedFields = new ArrayList<XMLFragment>();
+ Iterator<Field> reportedIterator = dataForm.getReportedIterator();
+ while (reportedIterator.hasNext()) {
+ Field field = reportedIterator.next();
+ reportedFields.add(encodeField(field));
+ }
+ XMLElement reportedElement = new XMLElement("reported", null, (Attribute[])null, reportedFields);
+ childElements.add(reportedElement);
+
+ // all item elements with their values
+ Iterator<List<Field>> itemIterator = dataForm.getItemIterator();
+ while (itemIterator.hasNext()) {
+ ArrayList<XMLFragment> itemFields = new ArrayList<XMLFragment>();
+ List<Field> itemField = itemIterator.next();
+ for (Field field : itemField) {
+ itemFields.add(encodeField(field));
+ }
+ XMLElement itemElement = new XMLElement("item", null, (Attribute[])null, itemFields);
+ childElements.add(itemElement);
+ }
+ } else {
+ // all fields
+ Iterator<Field> fieldIterator = dataForm.getFieldIterator();
+ while (fieldIterator.hasNext()) {
+ Field field = fieldIterator.next();
+ childElements.add(encodeField(field));
+ }
+ }
+
+ List<Attribute> attributes = new ArrayList<Attribute>();
+ attributes.add(new Attribute("type", dataForm.getType().value()));
+
+ return new XMLElement("x", NamespaceURIs.JABBER_X_DATA, attributes, childElements);
+ }
+
+ protected static XMLElement encodeField(Field field) {
+
+ ArrayList<XMLFragment> fieldElements = new ArrayList<XMLFragment>();
+
+ List<Attribute> fieldAttributes = new ArrayList<Attribute>();
+ if (field.getVar() != null) {
+ fieldAttributes.add(new Attribute("var", field.getVar()));
+ }
+ if (field.getLabel() != null) {
+ fieldAttributes.add(new Attribute("label", field.getLabel()));
+ }
+ if (field.getType() != null) {
+ fieldAttributes.add(new Attribute("type", field.getType().value()));
+ }
+
+ ArrayList<XMLFragment> descFragment = new ArrayList<XMLFragment>();
+ if (field.getDesc() != null) {
+ descFragment.add(new XMLText(field.getDesc()));
+ }
+ fieldElements.add(new XMLElement("desc", null, (Attribute[])null, descFragment));
+
+ if (field.isRequired()) {
+ fieldElements.add(createEmptyElement("required"));
+ }
+
+ Iterator<String> valueIterator = field.getValueIterator();
+ while (valueIterator.hasNext()) {
+ String value = valueIterator.next();
+ XMLElement valueElement = createTextOnlyElement("value", value);
+ fieldElements.add(valueElement);
+ }
+
+ Iterator<Option> optionIterator = field.getOptions();
+ while (optionIterator.hasNext()) {
+ Option option = optionIterator.next();
+
+ Attribute[] attributes = option.getLabel() == null ? null : new Attribute[]{new Attribute("label", option.getLabel())};
+ XMLFragment[] elements = option.getValue() == null ? null : new XMLFragment[]{new XMLText(option.getValue())};
+
+ XMLElement optionElement = new XMLElement("option", null, attributes, elements);
+ fieldElements.add(optionElement);
+ }
+
+ return new XMLElement("field", null, fieldAttributes, fieldElements);
+
+ }
+
+ protected static XMLElement createEmptyElement(String elementName) {
+ return new XMLElement(elementName, null, (Attribute[])null, (XMLFragment[])null);
+ }
+
+ protected static XMLElement createTextOnlyElement(String elementName, String text) {
+ return new XMLElement(elementName, null, (Attribute[])null, new XMLFragment[]{new XMLText(text)});
+ }
+}
Added: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Field.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Field.java?rev=776845&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Field.java (added)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Field.java Wed May 20 20:43:38 2009
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.xmpp.stanza.dataforms;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ */
+public class Field {
+
+ public static enum Type {
+
+ BOOLEAN("boolean"),
+ FIXED("fixed"),
+ HIDDEN("hidden"),
+ JID_MULTI("jid-multi"),
+ JID_SINGLE("jid-single"),
+ LIST_MULTI("list-multi"),
+ LIST_SINGLE("list-single"),
+ TEXT_MULTI("text-multi"),
+ TEXT_PRIVATE("text-private"),
+ TEXT_SINGLE("text-single");
+
+ private final String value;
+
+ Type(String value) {
+ this.value = value;
+ }
+
+ public String value() {
+ return value;
+ }
+ }
+
+ protected String label;
+ protected Type type;
+ protected String var;
+ protected String desc;
+ protected boolean required = false;
+ protected final List<Option> options = new ArrayList<Option>();
+ protected final List<String> values = new ArrayList<String>();
+
+ public Field(String label, Type type, String var) {
+ this.label = label;
+ this.type = type;
+ this.var = var;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public void setLabel(String label) {
+ this.label = label;
+ }
+
+ public Type getType() {
+ return type;
+ }
+
+ public void setType(Type type) {
+ this.type = type;
+ }
+
+ public String getVar() {
+ return var;
+ }
+
+ public void setVar(String var) {
+ this.var = var;
+ }
+
+ public String getDesc() {
+ return desc;
+ }
+
+ public void setDesc(String desc) {
+ this.desc = desc;
+ }
+
+ public boolean isRequired() {
+ return required;
+ }
+
+ public void setRequired(boolean required) {
+ this.required = required;
+ }
+
+ public Iterator<String> getValueIterator() {
+ return values.iterator();
+ }
+
+ public void addValue(String value) {
+ this.values.add(value);
+ }
+
+ public Iterator<Option> getOptions() {
+ return options.iterator();
+ }
+
+ public void addOption(Option option) {
+ this.options.add(option);
+ }
+}
Added: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Option.java
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Option.java?rev=776845&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Option.java (added)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/Option.java Wed May 20 20:43:38 2009
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.vysper.xmpp.stanza.dataforms;
+
+/**
+ */
+public class Option {
+
+ protected String label;
+ protected String value;
+
+ public Option(String label, String value) {
+ this.label = label;
+ this.value = value;
+ }
+
+ public String getLabel() {
+ return label;
+ }
+
+ public String getValue() {
+ return value;
+ }
+}
Added: mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/package.html
URL: http://svn.apache.org/viewvc/mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/package.html?rev=776845&view=auto
==============================================================================
--- mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/package.html (added)
+++ mina/sandbox/vysper/trunk/src/main/java/org/apache/vysper/xmpp/stanza/dataforms/package.html Wed May 20 20:43:38 2009
@@ -0,0 +1,24 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body>
+This package provides support for XEP-0004: Data Forms, see http://xmpp.org/extensions/xep-0004.html .
+</body>
+</html>