001/**
002 *
003 * Copyright © 2014-2020 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 */
017
018package org.jivesoftware.smack.packet;
019
020import org.jivesoftware.smack.util.XmlStringBuilder;
021
022/**
023 * The stream open <b>tag</b>.
024 */
025public final class StreamOpen extends AbstractStreamOpen {
026    public static final String UNPREFIXED_ELEMENT = "stream";
027
028    public static final String ELEMENT = "stream:" + UNPREFIXED_ELEMENT;
029
030    public StreamOpen(CharSequence to) {
031       this(to, null, null, null, StreamContentNamespace.client);
032    }
033
034    public StreamOpen(CharSequence to, CharSequence from, String id) {
035        this(to, from, id, "en", StreamContentNamespace.client);
036    }
037
038    public StreamOpen(CharSequence to, CharSequence from, String id, String lang) {
039        super(to, from, id, lang);
040    }
041
042    public StreamOpen(CharSequence to, CharSequence from, String id, String lang, StreamContentNamespace ns) {
043        super(to, from, id, lang, ns);
044    }
045
046    @Override
047    public String getNamespace() {
048        return contentNamespace;
049    }
050
051    @Override
052    public String getElementName() {
053        return ELEMENT;
054    }
055
056    @Override
057    public XmlStringBuilder toXML(XmlEnvironment enclosingXmlEnvironment) {
058        XmlStringBuilder xml = new XmlStringBuilder();
059        xml.halfOpenElement(getElementName());
060
061        String namespace = CLIENT_NAMESPACE;
062        // We always want to state 'xmlns' for stream open tags.
063        if (enclosingXmlEnvironment != null) {
064            namespace = enclosingXmlEnvironment.getEffectiveNamespaceOrUse(CLIENT_NAMESPACE);
065        }
066        xml.attribute("xmlns", namespace);
067
068        xml.attribute("to", to);
069        xml.attribute("xmlns:stream", "http://etherx.jabber.org/streams");
070        xml.attribute("version", VERSION);
071        xml.optAttribute("from", from);
072        xml.optAttribute("id", id);
073        xml.xmllangAttribute(lang);
074        xml.rightAngleBracket();
075        return xml;
076    }
077
078    public enum StreamContentNamespace {
079        client,
080        server,
081    }
082}