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.packet;
018
019/**
020 * Defines all the valid namespaces that are used with the {@link PubSub} packet
021 * as defined by the specification.
022 *
023 * @author Robin Collier
024 */
025public enum PubSubNamespace {
026    basic(null),
027    error("errors"),
028    event("event"),
029    owner("owner");
030
031    private final String fragment;
032    private final String fullNamespace;
033
034    PubSubNamespace(String fragment) {
035        this.fragment = fragment;
036        if (fragment != null) {
037            fullNamespace = PubSub.NAMESPACE + '#' + fragment;
038        } else {
039            fullNamespace = PubSub.NAMESPACE;
040        }
041    }
042
043    public String getXmlns() {
044        return fullNamespace;
045    }
046
047    public String getFragment() {
048        return fragment;
049    }
050
051    public static PubSubNamespace valueOfFromXmlns(String ns) {
052        int index = ns.lastIndexOf('#');
053
054        if (index != -1) {
055            // We found an extended namespace.
056            if (index > ns.length()) {
057                throw new IllegalArgumentException(ns + " is not a valid PubSub namespace");
058            }
059            String suffix = ns.substring(index + 1);
060            return valueOf(suffix);
061        }
062
063        if (!PubSub.NAMESPACE.equals(ns)) {
064            throw new IllegalArgumentException(ns + " is not a valid PubSub namespace");
065        }
066        return basic;
067    }
068}