001/**
002 *
003 * Copyright 2014-2015 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.ArrayList;
020import java.util.Collections;
021import java.util.HashMap;
022import java.util.List;
023import java.util.Locale;
024import java.util.Map;
025
026import org.jivesoftware.smack.util.PacketUtil;
027import org.jivesoftware.smack.util.XmlStringBuilder;
028
029public class AbstractError {
030
031    protected final String textNamespace;
032    protected final Map<String, String> descriptiveTexts;
033    protected final List<ExtensionElement> extensions;
034
035
036    protected AbstractError(Map<String, String> descriptiveTexts) {
037        this(descriptiveTexts, null);
038    }
039
040    protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
041        this(descriptiveTexts, null, extensions);
042    }
043
044    protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
045        if (descriptiveTexts != null) {
046            this.descriptiveTexts = descriptiveTexts;
047        } else {
048            this.descriptiveTexts = Collections.emptyMap();
049        }
050        this.textNamespace = textNamespace;
051        if (extensions != null) {
052            this.extensions = extensions;
053        } else {
054            this.extensions = Collections.emptyList();
055        }
056    }
057
058    /**
059     * Get the descriptive text of this SASLFailure.
060     * <p>
061     * Returns the descriptive text of this SASLFailure in the system default language if possible. May return null.
062     * </p>
063     * 
064     * @return the descriptive text or null.
065     */
066    public String getDescriptiveText() {
067        String defaultLocale = Locale.getDefault().getLanguage();
068        String descriptiveText = getDescriptiveText(defaultLocale);
069        if (descriptiveText == null) {
070            descriptiveText = getDescriptiveText("");
071        }
072        return descriptiveText;
073    }
074
075    /**
076     * Get the descriptive test of this SASLFailure.
077     * <p>
078     * Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
079     * </p>
080     * 
081     * @param xmllang the language.
082     * @return the descriptive text or null.
083     */
084    public String getDescriptiveText(String xmllang) {
085        return descriptiveTexts.get(xmllang);
086    }
087
088    /**
089     * Returns the first stanza(/packet) extension that matches the specified element name and
090     * namespace, or <tt>null</tt> if it doesn't exist. 
091     *
092     * @param elementName the XML element name of the stanza(/packet) extension.
093     * @param namespace the XML element namespace of the stanza(/packet) extension.
094     * @return the extension, or <tt>null</tt> if it doesn't exist.
095     */
096    public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
097        return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
098    }
099
100    protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
101        for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
102            String xmllang = entry.getKey();
103            String text = entry.getValue();
104            xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
105                    .xmllangAttribute(xmllang).rightAngleBracket();
106            xml.escape(text);
107            xml.closeElement("text");
108        }
109        for (ExtensionElement packetExtension : extensions) {
110            xml.append(packetExtension.toXML());
111        }
112    }
113
114    public static abstract class Builder<B extends Builder<B>> {
115        protected String textNamespace;
116        protected Map<String, String> descriptiveTexts;
117        protected List<ExtensionElement> extensions;
118
119        public B setDescriptiveTexts(Map<String, String> descriptiveTexts) {
120            if (this.descriptiveTexts == null) {
121                this.descriptiveTexts = descriptiveTexts;
122            }
123            else {
124                this.descriptiveTexts.putAll(descriptiveTexts);
125            }
126            return getThis();
127        }
128
129        public B setDescriptiveEnText(String descriptiveEnText) {
130            if (descriptiveTexts == null) {
131                descriptiveTexts = new HashMap<>();
132            }
133            descriptiveTexts.put("en", descriptiveEnText);
134            return getThis();
135        }
136
137        public B setTextNamespace(String textNamespace) {
138            this.textNamespace = textNamespace;
139            return getThis();
140        }
141
142        public B setExtensions(List<ExtensionElement> extensions) {
143            if (this.extensions == null) {
144                this.extensions = extensions;
145            }
146            else {
147                this.extensions.addAll(extensions);
148            }
149            return getThis();
150        }
151
152        public B addExtension(ExtensionElement extension) {
153            if (extensions == null) {
154                extensions = new ArrayList<>();
155            }
156            extensions.add(extension);
157            return getThis();
158        }
159
160        protected abstract B getThis();
161    }
162}