Home | History | Annotate | Download | only in jbosh
      1 /*
      2  * Copyright 2009 Mike Cumings
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.kenai.jbosh;
     18 
     19 /**
     20  * Qualified name of an attribute of the wrapper element.  This class is
     21  * analagous to the {@code javax.xml.namespace.QName} class.
     22  * Each qualified name consists of a namespace URI and a local name.
     23  * <p/>
     24  * Instances of this class are immutable and thread-safe.
     25  */
     26 public final class BodyQName {
     27 
     28     /**
     29      * BOSH namespace URI.
     30      */
     31     static final String BOSH_NS_URI =
     32             "http://jabber.org/protocol/httpbind";
     33 
     34     /**
     35      * Namespace URI.
     36      */
     37     private final QName qname;
     38 
     39     /**
     40      * Private constructor to prevent direct construction.
     41      *
     42      * @param wrapped QName instance to wrap
     43      */
     44     private BodyQName(
     45             final QName wrapped) {
     46         qname = wrapped;
     47     }
     48 
     49     /**
     50      * Creates a new qualified name using a namespace URI and local name.
     51      *
     52      * @param uri namespace URI
     53      * @param local local name
     54      * @return BodyQName instance
     55      */
     56     public static BodyQName create(
     57             final String uri,
     58             final String local) {
     59         return createWithPrefix(uri, local, null);
     60     }
     61 
     62     /**
     63      * Creates a new qualified name using a namespace URI and local name
     64      * along with an optional prefix.
     65      *
     66      * @param uri namespace URI
     67      * @param local local name
     68      * @param prefix optional prefix or @{code null} for no prefix
     69      * @return BodyQName instance
     70      */
     71     public static BodyQName createWithPrefix(
     72             final String uri,
     73             final String local,
     74             final String prefix) {
     75         if (uri == null || uri.length() == 0) {
     76             throw(new IllegalArgumentException(
     77                     "URI is required and may not be null/empty"));
     78         }
     79         if (local == null || local.length() == 0) {
     80             throw(new IllegalArgumentException(
     81                     "Local arg is required and may not be null/empty"));
     82         }
     83         if (prefix == null || prefix.length() == 0) {
     84             return new BodyQName(new QName(uri, local));
     85         } else {
     86             return new BodyQName(new QName(uri, local, prefix));
     87         }
     88     }
     89 
     90     /**
     91      * Get the namespace URI of this qualified name.
     92      *
     93      * @return namespace uri
     94      */
     95     public String getNamespaceURI() {
     96         return qname.getNamespaceURI();
     97     }
     98 
     99     /**
    100      * Get the local part of this qualified name.
    101      *
    102      * @return local name
    103      */
    104     public String  getLocalPart() {
    105         return qname.getLocalPart();
    106     }
    107 
    108     /**
    109      * Get the optional prefix used with this qualified name, or {@code null}
    110      * if no prefix has been assiciated.
    111      *
    112      * @return prefix, or {@code null} if no prefix was supplied
    113      */
    114     public String getPrefix() {
    115         return qname.getPrefix();
    116     }
    117 
    118     /**
    119      * {@inheritDoc}
    120      */
    121     @Override
    122     public boolean equals(final Object obj) {
    123         if (obj instanceof BodyQName) {
    124             BodyQName other = (BodyQName) obj;
    125             return qname.equals(other.qname);
    126         } else {
    127             return false;
    128         }
    129     }
    130 
    131     /**
    132      * {@inheritDoc}
    133      */
    134     @Override
    135     public int hashCode() {
    136         return qname.hashCode();
    137     }
    138 
    139     ///////////////////////////////////////////////////////////////////////////
    140     // Package-private methods:
    141 
    142     /**
    143      * Creates a new qualified name using the BOSH namespace URI and local name.
    144      *
    145      * @param local local name
    146      * @return BodyQName instance
    147      */
    148     static BodyQName createBOSH(
    149             final String local) {
    150         return createWithPrefix(BOSH_NS_URI, local, null);
    151     }
    152 
    153     /**
    154      * Convenience method to compare this qualified name with a
    155      * {@code javax.xml.namespace.QName}.
    156      *
    157      * @param otherName QName to compare to
    158      * @return @{code true} if the qualified name is the same, {@code false}
    159      *  otherwise
    160      */
    161     boolean equalsQName(final QName otherName) {
    162         return qname.equals(otherName);
    163     }
    164 
    165 }
    166