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    CREATE("create", PubSubNamespace.basic),
031    DELETE("delete", PubSubNamespace.owner),
032    DELETE_EVENT("delete", PubSubNamespace.event),
033    CONFIGURE("configure", PubSubNamespace.basic),
034    CONFIGURE_OWNER("configure", PubSubNamespace.owner),
035    CONFIGURATION("configuration", PubSubNamespace.event),
036    OPTIONS("options", PubSubNamespace.basic),
037    DEFAULT("default", PubSubNamespace.owner),
038    ITEMS("items", PubSubNamespace.basic),
039    ITEMS_EVENT("items", PubSubNamespace.event),
040    ITEM("item", PubSubNamespace.basic),
041    ITEM_EVENT("item", PubSubNamespace.event),
042    PUBLISH("publish", PubSubNamespace.basic),
043    PUBLISH_OPTIONS("publish-options", PubSubNamespace.basic),
044    PURGE_OWNER("purge", PubSubNamespace.owner),
045    PURGE_EVENT("purge", PubSubNamespace.event),
046    RETRACT("retract", PubSubNamespace.basic),
047    AFFILIATIONS("affiliations", PubSubNamespace.basic),
048    AFFILIATIONS_OWNER("affiliations", PubSubNamespace.owner),
049    SUBSCRIBE("subscribe", PubSubNamespace.basic),
050    SUBSCRIPTION("subscription", PubSubNamespace.basic),
051    SUBSCRIPTIONS("subscriptions", PubSubNamespace.basic),
052    SUBSCRIPTIONS_OWNER("subscriptions", PubSubNamespace.owner),
053    UNSUBSCRIBE("unsubscribe", PubSubNamespace.basic);
054
055    private final String eName;
056    private final PubSubNamespace nSpace;
057
058    PubSubElementType(String elemName, PubSubNamespace ns) {
059        eName = elemName;
060        nSpace = ns;
061    }
062
063    public PubSubNamespace getNamespace() {
064        return nSpace;
065    }
066
067    public String getElementName() {
068        return eName;
069    }
070
071    public static PubSubElementType valueOfFromElemName(String elemName, String namespace) {
072        int index = namespace.lastIndexOf('#');
073        String fragment = index == -1 ? null : namespace.substring(index + 1);
074
075        if (fragment != null) {
076            return valueOf((elemName + '_' + fragment).toUpperCase(Locale.US));
077        }
078        return valueOf(elemName.toUpperCase(Locale.US).replace('-', '_'));
079    }
080
081}