001/**
002 *
003 * Copyright 2003-2005 Jive Software, 2017-2021 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.smackx.jingle.element;
019
020import java.util.Locale;
021
022import org.jivesoftware.smack.packet.XmlElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025public final class JingleError implements XmlElement {
026
027    public static String NAMESPACE = "urn:xmpp:jingle:errors:1";
028
029    public static final JingleError OUT_OF_ORDER = new JingleError("out-of-order");
030
031    public static final JingleError TIE_BREAK = new JingleError("tie-break");
032
033    public static final JingleError UNKNOWN_SESSION = new JingleError("unknown-session");
034
035    public static final JingleError UNSUPPORTED_INFO = new JingleError("unsupported-info");
036
037    private final String errorName;
038
039    /**
040     * Creates a new error with the specified code and errorName.
041     *
042     * @param errorName a name describing the error.
043     */
044    private JingleError(final String errorName) {
045        this.errorName = errorName;
046    }
047
048    /**
049     * Returns the name of the Jingle error.
050     *
051     * @return the name of the error.
052     */
053    public String getMessage() {
054        return errorName;
055    }
056
057    @Override
058    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
059        XmlStringBuilder xml = new XmlStringBuilder(this);
060        xml.closeEmptyElement();
061        return xml;
062    }
063
064    /**
065     * Returns a error instance associated with the String value.
066     *
067     * @param value the input String.
068     * @return the jingle error instance associated with the input String.
069     */
070    public static JingleError fromString(String value) {
071        value = value.toLowerCase(Locale.US);
072        switch (value) {
073        case "out-of-order":
074            return OUT_OF_ORDER;
075        case "unknown-session":
076            return UNKNOWN_SESSION;
077        case "tie-break":
078            return TIE_BREAK;
079        case "unsupported-info":
080            return UNSUPPORTED_INFO;
081        default:
082            throw new IllegalArgumentException();
083        }
084    }
085
086    @Override
087    public String toString() {
088        return getMessage();
089    }
090
091    @Override
092    public String getElementName() {
093        return errorName;
094    }
095
096    @Override
097    public String getNamespace() {
098        return NAMESPACE;
099    }
100
101}