001/**
002 *
003 * Copyright the original author or authors
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.smackx.pubsub;
018
019import java.util.Locale;
020
021import org.jivesoftware.smackx.pubsub.packet.PubSubNamespace;
022
023/**
024 * Defines all the possible element types as defined for all the pubsub
025 * schemas in all 3 namespaces.
026 * 
027 * @author Robin Collier
028 */
029public enum PubSubElementType
030{
031    CREATE("create", PubSubNamespace.BASIC),
032    DELETE("delete", PubSubNamespace.OWNER),
033    DELETE_EVENT("delete", PubSubNamespace.EVENT),
034    CONFIGURE("configure", PubSubNamespace.BASIC),
035    CONFIGURE_OWNER("configure", PubSubNamespace.OWNER),
036    CONFIGURATION("configuration", PubSubNamespace.EVENT),
037    OPTIONS("options", PubSubNamespace.BASIC),
038    DEFAULT("default", PubSubNamespace.OWNER),  
039    ITEMS("items", PubSubNamespace.BASIC),
040    ITEMS_EVENT("items", PubSubNamespace.EVENT),
041    ITEM("item", PubSubNamespace.BASIC),
042    ITEM_EVENT("item", PubSubNamespace.EVENT),
043    PUBLISH("publish", PubSubNamespace.BASIC),
044    PUBLISH_OPTIONS("publish-options", PubSubNamespace.BASIC), 
045    PURGE_OWNER("purge", PubSubNamespace.OWNER),
046    PURGE_EVENT("purge", PubSubNamespace.EVENT),
047    RETRACT("retract", PubSubNamespace.BASIC), 
048    AFFILIATIONS("affiliations", PubSubNamespace.BASIC), 
049    SUBSCRIBE("subscribe", PubSubNamespace.BASIC), 
050    SUBSCRIPTION("subscription", PubSubNamespace.BASIC),
051    SUBSCRIPTIONS("subscriptions", PubSubNamespace.BASIC), 
052    UNSUBSCRIBE("unsubscribe", PubSubNamespace.BASIC);
053
054    private final String eName;
055    private final PubSubNamespace nSpace;
056
057    private PubSubElementType(String elemName, PubSubNamespace ns)
058    {
059        eName = elemName;
060        nSpace = ns;
061    }
062
063    public PubSubNamespace getNamespace()
064    {
065        return nSpace;
066    }
067
068    public String getElementName()
069    {
070        return eName;
071    }
072
073    public static PubSubElementType valueOfFromElemName(String elemName, String namespace)
074    {
075        int index = namespace.lastIndexOf('#');
076        String fragment = (index == -1 ? null : namespace.substring(index+1));
077
078        if (fragment != null)
079        {
080            return valueOf((elemName + '_' + fragment).toUpperCase(Locale.US));
081        }
082        return valueOf(elemName.toUpperCase(Locale.US).replace('-', '_'));
083    }
084
085}