001/**
002 *
003 * Copyright 2003-2005 Jive Software, 2017 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.ExtensionElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025public final class JingleError implements ExtensionElement {
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(String enclosingNamespace) {
059        XmlStringBuilder xml = new XmlStringBuilder(this);
060        xml.closeEmptyElement();
061        return xml;
062    }
063
064    /**
065     * Returns a Action instance associated with the String value.
066     */
067    public static JingleError fromString(String value) {
068        value = value.toLowerCase(Locale.US);
069        switch (value) {
070        case "out-of-order":
071            return OUT_OF_ORDER;
072        case "unknown-session":
073            return UNKNOWN_SESSION;
074        case "tie-break":
075            return TIE_BREAK;
076        case "unsupported-info":
077            return UNSUPPORTED_INFO;
078        default:
079            throw new IllegalArgumentException();
080        }
081    }
082
083    @Override
084    public String toString() {
085        return getMessage();
086    }
087
088    @Override
089    public String getElementName() {
090        return errorName;
091    }
092
093    @Override
094    public String getNamespace() {
095        return NAMESPACE;
096    }
097
098}