001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019import org.jivesoftware.smackx.xdata.Form;
020
021/**
022 * Generic stanza(/packet) extension which represents any pubsub form that is
023 * parsed from the incoming stream or being sent out to the server.
024 * 
025 * Form types are defined in {@link FormNodeType}.
026 * 
027 * @author Robin Collier
028 */
029public class FormNode extends NodeExtension
030{
031        private Form configForm;
032        
033        /**
034         * Create a {@link FormNode} which contains the specified form.
035         * 
036         * @param formType The type of form being sent
037         * @param submitForm The form
038         */
039        public FormNode(FormNodeType formType, Form submitForm)
040        {
041                super(formType.getNodeElement());
042
043                if (submitForm == null)
044                        throw new IllegalArgumentException("Submit form cannot be null");
045                configForm = submitForm;
046        }
047        
048        /**
049         * Create a {@link FormNode} which contains the specified form, which is 
050         * associated with the specified node.
051         * 
052         * @param formType The type of form being sent
053         * @param nodeId The node the form is associated with
054         * @param submitForm The form
055         */
056        public FormNode(FormNodeType formType, String nodeId, Form submitForm)
057        {
058                super(formType.getNodeElement(), nodeId);
059
060                if (submitForm == null)
061                        throw new IllegalArgumentException("Submit form cannot be null");
062                configForm = submitForm;
063        }
064        
065        /**
066         * Get the Form that is to be sent, or was retrieved from the server.
067         * 
068         * @return The form
069         */
070        public Form getForm()
071        {
072                return configForm;
073        }
074        
075        @Override
076        public CharSequence toXML()
077        {
078                if (configForm == null)
079                {
080                        return super.toXML();
081                }
082                else
083                {
084                        StringBuilder builder = new StringBuilder("<");
085                        builder.append(getElementName());
086                        
087                        if (getNode() != null)
088                        {
089                                builder.append(" node='");
090                                builder.append(getNode());
091                                builder.append("'>");
092                        }
093                        else
094                                builder.append('>');
095                        builder.append(configForm.getDataFormToSend().toXML());
096                        builder.append("</");
097                        builder.append(getElementName() + '>');
098                        return builder.toString();
099                }
100        }
101
102}