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.smack.util.XmlStringBuilder;
020
021import org.jivesoftware.smackx.xdata.packet.DataForm;
022
023/**
024 * Generic stanza extension which represents any PubSub form that is
025 * parsed from the incoming stream or being sent out to the server.
026 *
027 * Form types are defined in {@link FormNodeType}.
028 *
029 * @author Robin Collier
030 */
031public class FormNode extends NodeExtension {
032    private final DataForm configForm;
033
034    /**
035     * Create a {@link FormNode} which contains the specified form.
036     *
037     * @param formType The type of form being sent
038     * @param submitForm The form
039     */
040    public FormNode(FormNodeType formType, DataForm submitForm) {
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, DataForm submitForm) {
057        super(formType.getNodeElement(), nodeId);
058
059        if (submitForm == null)
060            throw new IllegalArgumentException("Submit form cannot be null");
061        configForm = submitForm;
062    }
063
064    /**
065     * Get the Form that is to be sent, or was retrieved from the server.
066     *
067     * @return The form
068     */
069    public DataForm getForm() {
070        return configForm;
071    }
072
073    @Override
074    protected void addXml(XmlStringBuilder xml) {
075        if (configForm == null) {
076            xml.closeEmptyElement();
077            return;
078        }
079
080        xml.rightAngleBracket();
081        xml.append(configForm);
082        xml.closeElement(this);
083    }
084
085}