001/**
002 *
003 * Copyright 2019 Paul Schaub
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.message_fastening.element;
018
019import org.jivesoftware.smack.packet.NamedElement;
020import org.jivesoftware.smack.packet.XmlEnvironment;
021import org.jivesoftware.smack.util.XmlStringBuilder;
022
023/**
024 * Child element of {@link FasteningElement}.
025 * Reference to a top level element in the stanza that contains the {@link FasteningElement}.
026 */
027public class ExternalElement implements NamedElement {
028
029    public static final String ELEMENT = "external";
030    public static final String ATTR_NAME = "name";
031    public static final String ATTR_ELEMENT_NAMESPACE = "element-namespace";
032
033    private final String name;
034    private final String elementNamespace;
035
036    /**
037     * Create a new {@link ExternalElement} that references a top level element with the given name.
038     *
039     * @param name name of the top level element
040     */
041    public ExternalElement(String name) {
042        this(name, null);
043    }
044
045    /**
046     * Create a new {@link ExternalElement} that references a top level element with the given name and namespace.
047     *
048     * @param name name of the top level element
049     * @param elementNamespace namespace of the top level element
050     */
051    public ExternalElement(String name, String elementNamespace) {
052        this.name = name;
053        this.elementNamespace = elementNamespace;
054    }
055
056    @Override
057    public String getElementName() {
058        return ELEMENT;
059    }
060
061    @Override
062    public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
063        XmlStringBuilder xml = new XmlStringBuilder(this);
064        xml.attribute(ATTR_NAME, getName());
065        xml.optAttribute(ATTR_ELEMENT_NAMESPACE, getElementNamespace());
066        return xml.closeEmptyElement();
067    }
068
069    /**
070     * Name of the referenced top level element, eg. 'body'.
071     * @return element name
072     */
073    public String getName() {
074        return name;
075    }
076
077    /**
078     * Namespace of the referenced top level element, eg. 'urn:example:lik'.
079     * @return element namespace
080     */
081    public String getElementNamespace() {
082        return elementNamespace;
083    }
084}