001/**
002 *
003 * Copyright 2014 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.smack.packet;
018
019import java.util.Collections;
020import java.util.List;
021import java.util.Locale;
022import java.util.Map;
023
024import org.jivesoftware.smack.util.PacketUtil;
025import org.jivesoftware.smack.util.XmlStringBuilder;
026
027public class AbstractError {
028
029    private final String textNamespace;
030    protected final Map<String, String> descriptiveTexts;
031    private final List<ExtensionElement> extensions;
032
033
034    protected AbstractError(Map<String, String> descriptiveTexts) {
035        this(descriptiveTexts, null);
036    }
037
038    protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
039        this(descriptiveTexts, null, extensions);
040    }
041
042    protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
043        if (descriptiveTexts != null) {
044            this.descriptiveTexts = descriptiveTexts;
045        } else {
046            this.descriptiveTexts = Collections.emptyMap();
047        }
048        this.textNamespace = textNamespace;
049        if (extensions != null) {
050            this.extensions = extensions;
051        } else {
052            this.extensions = Collections.emptyList();
053        }
054    }
055
056    /**
057     * Get the descriptive text of this SASLFailure.
058     * <p>
059     * Returns the descriptive text of this SASLFailure in the system default language if possible. May return null.
060     * </p>
061     * 
062     * @return the descriptive text or null.
063     */
064    public String getDescriptiveText() {
065        String defaultLocale = Locale.getDefault().getLanguage();
066        String descriptiveText = getDescriptiveText(defaultLocale);
067        if (descriptiveText == null) {
068            descriptiveText = getDescriptiveText("");
069        }
070        return descriptiveText;
071    }
072
073    /**
074     * Get the descriptive test of this SASLFailure.
075     * <p>
076     * Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
077     * </p>
078     * 
079     * @param xmllang the language.
080     * @return the descriptive text or null.
081     */
082    public String getDescriptiveText(String xmllang) {
083        return descriptiveTexts.get(xmllang);
084    }
085
086    /**
087     * Returns the first stanza(/packet) extension that matches the specified element name and
088     * namespace, or <tt>null</tt> if it doesn't exist. 
089     *
090     * @param elementName the XML element name of the stanza(/packet) extension.
091     * @param namespace the XML element namespace of the stanza(/packet) extension.
092     * @return the extension, or <tt>null</tt> if it doesn't exist.
093     */
094    public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
095        return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
096    }
097
098    protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
099        for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
100            String xmllang = entry.getKey();
101            String text = entry.getValue();
102            xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
103                    .xmllangAttribute(xmllang).rightAngleBracket();
104            xml.escape(text);
105            xml.closeElement("text");
106        }
107        for (ExtensionElement packetExtension : extensions) {
108            xml.append(packetExtension.toXML());
109        }
110    }
111}