001/**
002 *
003 * Copyright 2003-2007 Jive Software.
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 */
017
018package org.jivesoftware.smack.filter;
019
020import org.jivesoftware.smack.packet.ExtensionElement;
021import org.jivesoftware.smack.packet.Stanza;
022import org.jivesoftware.smack.util.StringUtils;
023
024/**
025 * Filters for stanzas with a particular type of stanza extension.
026 *
027 * @author Matt Tucker
028 */
029public class StanzaExtensionFilter implements StanzaFilter {
030
031    private final String elementName;
032    private final String namespace;
033
034    /**
035     * Creates a new stanza extension filter. Stanzas will pass the filter if
036     * they have a stanza extension that matches the specified element name
037     * and namespace.
038     *
039     * @param elementName the XML element name of the stanza extension.
040     * @param namespace the XML namespace of the stanza extension.
041     */
042    public StanzaExtensionFilter(String elementName, String namespace) {
043        StringUtils.requireNotNullNorEmpty(namespace, "namespace must not be null nor empty");
044
045        this.elementName = elementName;
046        this.namespace = namespace;
047    }
048
049    /**
050     * Creates a new stanza extension filter. Stanzas will pass the filter if they have a stanza
051     * extension that matches the specified namespace.
052     *
053     * @param namespace the XML namespace of the stanza extension.
054     */
055    public StanzaExtensionFilter(String namespace) {
056        this(null, namespace);
057    }
058
059    /**
060     * Creates a new stanza extension filter for the given stanza extension.
061     *
062     * @param packetExtension TODO javadoc me please
063     */
064    public StanzaExtensionFilter(ExtensionElement packetExtension) {
065        this(packetExtension.getElementName(), packetExtension.getNamespace());
066    }
067
068    @Override
069    public boolean accept(Stanza packet) {
070        return packet.hasExtension(elementName, namespace);
071    }
072
073    @Override
074    public String toString() {
075        return getClass().getSimpleName() + ": element=" + elementName + " namespace=" + namespace;
076    }
077}