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 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    private final Form configForm;
031
032    /**
033     * Create a {@link FormNode} which contains the specified form.
034     *
035     * @param formType The type of form being sent
036     * @param submitForm The form
037     */
038    public FormNode(FormNodeType formType, Form submitForm) {
039        super(formType.getNodeElement());
040
041        if (submitForm == null)
042            throw new IllegalArgumentException("Submit form cannot be null");
043        configForm = submitForm;
044    }
045
046    /**
047     * Create a {@link FormNode} which contains the specified form, which is
048     * associated with the specified node.
049     *
050     * @param formType The type of form being sent
051     * @param nodeId The node the form is associated with
052     * @param submitForm The form
053     */
054    public FormNode(FormNodeType formType, String nodeId, Form submitForm) {
055        super(formType.getNodeElement(), nodeId);
056
057        if (submitForm == null)
058            throw new IllegalArgumentException("Submit form cannot be null");
059        configForm = submitForm;
060    }
061
062    /**
063     * Get the Form that is to be sent, or was retrieved from the server.
064     *
065     * @return The form
066     */
067    public Form getForm() {
068        return configForm;
069    }
070
071    @Override
072    public CharSequence toXML(String enclosingNamespace) {
073        if (configForm == null) {
074            return super.toXML(enclosingNamespace);
075        }
076        else {
077            StringBuilder builder = new StringBuilder("<");
078            builder.append(getElementName());
079
080            if (getNode() != null) {
081                builder.append(" node='");
082                builder.append(getNode());
083                builder.append("'>");
084            }
085            else
086                builder.append('>');
087            builder.append(configForm.getDataFormToSend().toXML(null));
088            builder.append("</");
089            builder.append(getElementName() + '>');
090            return builder.toString();
091        }
092    }
093
094}