PubSubNamespace.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  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.smackx.pubsub.packet;

  18. import java.util.Locale;

  19. /**
  20.  * Defines all the valid namespaces that are used with the {@link PubSub} packet
  21.  * as defined by the specification.
  22.  *
  23.  * @author Robin Collier
  24.  */
  25. public enum PubSubNamespace
  26. {
  27.     BASIC(null),
  28.     ERROR("errors"),
  29.     EVENT("event"),
  30.     OWNER("owner");
  31.    
  32.     private String fragment;
  33.    
  34.     private PubSubNamespace(String fragment)
  35.     {
  36.         this.fragment = fragment;
  37.     }
  38.    
  39.     public String getXmlns()
  40.     {
  41.         String ns = PubSub.NAMESPACE;
  42.        
  43.         if (fragment != null)
  44.             ns += '#' + fragment;
  45.        
  46.         return ns;
  47.     }
  48.    
  49.     public String getFragment()
  50.     {
  51.         return fragment;
  52.     }

  53.     public static PubSubNamespace valueOfFromXmlns(String ns)
  54.     {
  55.         int index = ns.lastIndexOf('#');
  56.        
  57.         if (index != -1)
  58.         {
  59.             String suffix = ns.substring(ns.lastIndexOf('#')+1);
  60.             return valueOf(suffix.toUpperCase(Locale.US));
  61.         }
  62.         else
  63.             return BASIC;
  64.     }
  65. }