001/**
002 *
003 * Copyright 2019 Florian Schmaus
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.xmlelement.element;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.StandardExtensionElement;
022import org.jivesoftware.smack.packet.XmlEnvironment;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025import org.jivesoftware.smackx.xdata.FormField;
026import org.jivesoftware.smackx.xdata.FormFieldChildElement;
027
028public class DataFormsXmlElement implements FormFieldChildElement {
029
030    public static final String ELEMENT = "wrapper";
031
032    public static final String NAMESPACE = "urn:xmpp:xml-element";
033
034    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
035
036    private final StandardExtensionElement payload;
037
038    public DataFormsXmlElement(StandardExtensionElement payload) {
039        this.payload = payload;
040    }
041
042    @Override
043    public QName getQName() {
044        return QNAME;
045    }
046
047    @Override
048    public String getElementName() {
049        return ELEMENT;
050    }
051
052    @Override
053    public String getNamespace() {
054        return NAMESPACE;
055    }
056
057    @Override
058    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
059        XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);
060        if (payload == null) {
061            return xml.closeEmptyElement();
062        }
063
064        xml.rightAngleBracket();
065
066        xml.append(payload.toXML());
067
068        xml.closeElement(this);
069        return xml;
070    }
071
072    public static DataFormsXmlElement from(FormField formField) {
073        return (DataFormsXmlElement) formField.getFormFieldChildElement(QNAME);
074    }
075}