StanzaExtensionFilter.java

  1. /**
  2.  *
  3.  * Copyright 2003-2007 Jive Software.
  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.filter;

  18. import org.jivesoftware.smack.packet.ExtensionElement;
  19. import org.jivesoftware.smack.packet.Stanza;
  20. import org.jivesoftware.smack.util.StringUtils;

  21. /**
  22.  * Filters for stanzas with a particular type of stanza extension.
  23.  *
  24.  * @author Matt Tucker
  25.  */
  26. public class StanzaExtensionFilter implements StanzaFilter {

  27.     private final String elementName;
  28.     private final String namespace;

  29.     /**
  30.      * Creates a new stanza extension filter. Stanzas will pass the filter if
  31.      * they have a stanza extension that matches the specified element name
  32.      * and namespace.
  33.      *
  34.      * @param elementName the XML element name of the stanza extension.
  35.      * @param namespace the XML namespace of the stanza extension.
  36.      */
  37.     public StanzaExtensionFilter(String elementName, String namespace) {
  38.         StringUtils.requireNotNullNorEmpty(namespace, "namespace must not be null nor empty");

  39.         this.elementName = elementName;
  40.         this.namespace = namespace;
  41.     }

  42.     /**
  43.      * Creates a new stanza extension filter. Stanzas will pass the filter if they have a stanza
  44.      * extension that matches the specified namespace.
  45.      *
  46.      * @param namespace the XML namespace of the stanza extension.
  47.      */
  48.     public StanzaExtensionFilter(String namespace) {
  49.         this(null, namespace);
  50.     }

  51.     /**
  52.      * Creates a new stanza extension filter for the given stanza extension.
  53.      *
  54.      * @param packetExtension TODO javadoc me please
  55.      */
  56.     public StanzaExtensionFilter(ExtensionElement packetExtension) {
  57.         this(packetExtension.getElementName(), packetExtension.getNamespace());
  58.     }

  59.     @Override
  60.     public boolean accept(Stanza packet) {
  61.         return packet.hasExtension(elementName, namespace);
  62.     }

  63.     @Override
  64.     public String toString() {
  65.         return getClass().getSimpleName() + ": element=" + elementName + " namespace=" + namespace;
  66.     }
  67. }