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("en");
071            if (descriptiveText == null) {
072                descriptiveText = getDescriptiveText("");
073            }
074        }
075        return descriptiveText;
076    }
077
078    /**
079     * Get the descriptive test of this SASLFailure.
080     * <p>
081     * Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
082     * </p>
083     * 
084     * @param xmllang the language.
085     * @return the descriptive text or null.
086     */
087    public String getDescriptiveText(String xmllang) {
088        return descriptiveTexts.get(xmllang);
089    }
090
091    /**
092     * Returns the first stanza(/packet) extension that matches the specified element name and
093     * namespace, or <tt>null</tt> if it doesn't exist. 
094     *
095     * @param elementName the XML element name of the stanza(/packet) extension.
096     * @param namespace the XML element namespace of the stanza(/packet) extension.
097     * @return the extension, or <tt>null</tt> if it doesn't exist.
098     */
099    public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
100        return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
101    }
102
103    protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
104        for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
105            String xmllang = entry.getKey();
106            String text = entry.getValue();
107            xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
108                    .xmllangAttribute(xmllang).rightAngleBracket();
109            xml.escape(text);
110            xml.closeElement("text");
111        }
112        for (ExtensionElement packetExtension : extensions) {
113            xml.append(packetExtension.toXML());
114        }
115    }
116
117    public static abstract class Builder<B extends Builder<B>> {
118        protected String textNamespace;
119        protected Map<String, String> descriptiveTexts;
120        protected List<ExtensionElement> extensions;
121
122        public B setDescriptiveTexts(Map<String, String> descriptiveTexts) {
123            if (this.descriptiveTexts == null) {
124                this.descriptiveTexts = descriptiveTexts;
125            }
126            else {
127                this.descriptiveTexts.putAll(descriptiveTexts);
128            }
129            return getThis();
130        }
131
132        public B setDescriptiveEnText(String descriptiveEnText) {
133            if (descriptiveTexts == null) {
134                descriptiveTexts = new HashMap<>();
135            }
136            descriptiveTexts.put("en", descriptiveEnText);
137            return getThis();
138        }
139
140        public B setTextNamespace(String textNamespace) {
141            this.textNamespace = textNamespace;
142            return getThis();
143        }
144
145        public B setExtensions(List<ExtensionElement> extensions) {
146            if (this.extensions == null) {
147                this.extensions = extensions;
148            }
149            else {
150                this.extensions.addAll(extensions);
151            }
152            return getThis();
153        }
154
155        public B addExtension(ExtensionElement extension) {
156            if (extensions == null) {
157                extensions = new ArrayList<>();
158            }
159            extensions.add(extension);
160            return getThis();
161        }
162
163        protected abstract B getThis();
164    }
165}