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        this(formType, null, submitForm);
042    }
043
044    /**
045     * Create a {@link FormNode} which contains the specified form, which is
046     * associated with the specified node.
047     *
048     * @param formType The type of form being sent
049     * @param nodeId The node the form is associated with
050     * @param submitForm The form
051     */
052    public FormNode(FormNodeType formType, String nodeId, DataForm submitForm) {
053        super(formType.getNodeElement(), nodeId);
054        configForm = submitForm;
055    }
056
057    /**
058     * Get the Form that is to be sent, or was retrieved from the server.
059     *
060     * @return The form
061     */
062    public DataForm getForm() {
063        return configForm;
064    }
065
066    @Override
067    protected void addXml(XmlStringBuilder xml) {
068        if (configForm == null) {
069            xml.closeEmptyElement();
070            return;
071        }
072
073        xml.rightAngleBracket();
074        xml.append(configForm);
075        xml.closeElement(this);
076    }
077
078}