1/**
2 * All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 *     http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14package org.jivesoftware.smackx.pubsub;
15
16import org.jivesoftware.smackx.Form;
17
18/**
19 * Generic packet extension which represents any pubsub form that is
20 * parsed from the incoming stream or being sent out to the server.
21 *
22 * Form types are defined in {@link FormNodeType}.
23 *
24 * @author Robin Collier
25 */
26public class FormNode extends NodeExtension
27{
28	private Form configForm;
29
30	/**
31	 * Create a {@link FormNode} which contains the specified form.
32	 *
33	 * @param formType The type of form being sent
34	 * @param submitForm The form
35	 */
36	public FormNode(FormNodeType formType, Form submitForm)
37	{
38		super(formType.getNodeElement());
39
40		if (submitForm == null)
41			throw new IllegalArgumentException("Submit form cannot be null");
42		configForm = submitForm;
43	}
44
45	/**
46	 * Create a {@link FormNode} which contains the specified form, which is
47	 * associated with the specified node.
48	 *
49	 * @param formType The type of form being sent
50	 * @param nodeId The node the form is associated with
51	 * @param submitForm The form
52	 */
53	public FormNode(FormNodeType formType, String nodeId, Form submitForm)
54	{
55		super(formType.getNodeElement(), nodeId);
56
57		if (submitForm == null)
58			throw new IllegalArgumentException("Submit form cannot be null");
59		configForm = submitForm;
60	}
61
62	/**
63	 * Get the Form that is to be sent, or was retrieved from the server.
64	 *
65	 * @return The form
66	 */
67	public Form getForm()
68	{
69		return configForm;
70	}
71
72	@Override
73	public String toXML()
74	{
75		if (configForm == null)
76		{
77			return super.toXML();
78		}
79		else
80		{
81			StringBuilder builder = new StringBuilder("<");
82			builder.append(getElementName());
83
84			if (getNode() != null)
85			{
86				builder.append(" node='");
87				builder.append(getNode());
88				builder.append("'>");
89			}
90			else
91				builder.append('>');
92			builder.append(configForm.getDataFormToSend().toXML());
93			builder.append("</");
94			builder.append(getElementName() + '>');
95			return builder.toString();
96		}
97	}
98
99}
100