AbstractError.java

  1. /**
  2.  *
  3.  * Copyright 2014 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.packet;

  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Map;

  22. import org.jivesoftware.smack.util.PacketUtil;
  23. import org.jivesoftware.smack.util.XmlStringBuilder;

  24. public class AbstractError {

  25.     private final String textNamespace;
  26.     protected final Map<String, String> descriptiveTexts;
  27.     private final List<ExtensionElement> extensions;


  28.     protected AbstractError(Map<String, String> descriptiveTexts) {
  29.         this(descriptiveTexts, null);
  30.     }

  31.     protected AbstractError(Map<String, String> descriptiveTexts, List<ExtensionElement> extensions) {
  32.         this(descriptiveTexts, null, extensions);
  33.     }

  34.     protected AbstractError(Map<String, String> descriptiveTexts, String textNamespace, List<ExtensionElement> extensions) {
  35.         if (descriptiveTexts != null) {
  36.             this.descriptiveTexts = descriptiveTexts;
  37.         } else {
  38.             this.descriptiveTexts = Collections.emptyMap();
  39.         }
  40.         this.textNamespace = textNamespace;
  41.         if (extensions != null) {
  42.             this.extensions = extensions;
  43.         } else {
  44.             this.extensions = Collections.emptyList();
  45.         }
  46.     }

  47.     /**
  48.      * Get the descriptive text of this SASLFailure.
  49.      * <p>
  50.      * Returns the descriptive text of this SASLFailure in the system default language if possible. May return null.
  51.      * </p>
  52.      *
  53.      * @return the descriptive text or null.
  54.      */
  55.     public String getDescriptiveText() {
  56.         String defaultLocale = Locale.getDefault().getLanguage();
  57.         String descriptiveText = getDescriptiveText(defaultLocale);
  58.         if (descriptiveText == null) {
  59.             descriptiveText = getDescriptiveText("");
  60.         }
  61.         return descriptiveText;
  62.     }

  63.     /**
  64.      * Get the descriptive test of this SASLFailure.
  65.      * <p>
  66.      * Returns the descriptive text of this SASLFailure in the given language. May return null if not available.
  67.      * </p>
  68.      *
  69.      * @param xmllang the language.
  70.      * @return the descriptive text or null.
  71.      */
  72.     public String getDescriptiveText(String xmllang) {
  73.         return descriptiveTexts.get(xmllang);
  74.     }

  75.     /**
  76.      * Returns the first packet extension that matches the specified element name and
  77.      * namespace, or <tt>null</tt> if it doesn't exist.
  78.      *
  79.      * @param elementName the XML element name of the packet extension.
  80.      * @param namespace the XML element namespace of the packet extension.
  81.      * @return the extension, or <tt>null</tt> if it doesn't exist.
  82.      */
  83.     public <PE extends ExtensionElement> PE getExtension(String elementName, String namespace) {
  84.         return PacketUtil.extensionElementFrom(extensions, elementName, namespace);
  85.     }

  86.     protected void addDescriptiveTextsAndExtensions(XmlStringBuilder xml) {
  87.         for (Map.Entry<String, String> entry : descriptiveTexts.entrySet()) {
  88.             String xmllang = entry.getKey();
  89.             String text = entry.getValue();
  90.             xml.halfOpenElement("text").xmlnsAttribute(textNamespace)
  91.                     .xmllangAttribute(xmllang).rightAngleBracket();
  92.             xml.escape(text);
  93.             xml.closeElement("text");
  94.         }
  95.         for (ExtensionElement packetExtension : extensions) {
  96.             xml.append(packetExtension.toXML());
  97.         }
  98.     }
  99. }