Hubiquitus Android  0.3
Android client for hubiquitus protocol
JabberID.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Novedia Group 2012.
00003  *
00004  *     This file is part of Hubiquitus.
00005  *
00006  *     Hubiquitus is free software: you can redistribute it and/or modify
00007  *     it under the terms of the GNU General Public License as published by
00008  *     the Free Software Foundation, either version 3 of the License, or
00009  *     (at your option) any later version.
00010  *
00011  *     Hubiquitus is distributed in the hope that it will be useful,
00012  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *     GNU General Public License for more details.
00015  *
00016  *     You should have received a copy of the GNU General Public License
00017  *     along with Hubiquitus.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 package org.hubiquitus.hapi.structures;
00021 
00022 import java.util.regex.Matcher;
00023 import java.util.regex.Pattern;
00024 
00032 public class JabberID {
00033          
00034          
00035          private String username = "";
00036          private String domain = "";
00037          private String resource = "";
00038          
00044          public JabberID(String jid) throws Exception {
00045                   this.setJID(jid);
00046          }
00047          
00051          public String getBareJID() {
00052                   return this.username + "@" + this.domain;
00053          }
00054          
00058          public String getFullJID() {
00059                   if(resource != "")
00060                            return this.username + "@" + this.domain + "/" + this.resource;
00061                   else
00062                            return getBareJID();
00063          }
00064 
00070          public void setJID(String jid) throws Exception {
00071                   if (jid != null) {
00072                            Pattern pattern = Pattern.compile("^(?:([^@/<>'\"]+)@)([^@/<>'\"]+)(?:/([^/<>'\"]*))?$");
00073                            Matcher matcher = pattern.matcher(jid);
00074                            if (matcher.matches() && matcher.find()) {
00075                                     setUsername(matcher.group(1));
00076                                     setDomain(matcher.group(2));
00077                                     
00078                                     if (matcher.groupCount() >= 3) {
00079                                              setResource(matcher.group(3));
00080                                     }
00081                            } else {
00082                                     throw new Exception();
00083                            }
00084                   } else {
00085                            throw new Exception();
00086                   }
00087          }
00088          
00089          /* Getter & setter */
00090          
00091          public String getUsername() {
00092                   return username;
00093          }
00094 
00095          public void setUsername(String username) {
00096                   if(username != null)
00097                            this.username = username;
00098                   else 
00099                            this.username = "";
00100          }
00101 
00102          public String getDomain() {
00103                   return domain;
00104          }
00105 
00106          
00107          public void setDomain(String domain) {
00108                   if(domain != null)
00109                            this.domain = domain;
00110                   else 
00111                            this.domain = "";
00112          }
00113 
00114          public String getResource() {
00115                   return this.resource;
00116          }
00117 
00118          public void setResource(String resource) {
00119                   if(resource != null)
00120                            this.resource = resource;
00121                   else 
00122                            this.resource = "";
00123          }
00124 
00125          @Override
00126          public String toString() {
00127                   return "JabberID [username=" + username + ", domain=" + domain
00128                                     + ", ressources=" + resource + "]";
00129          }
00130 
00131          @Override
00132          public int hashCode() {
00133                   final int prime = 31;
00134                   int result = 1;
00135                   result = prime * result + ((domain == null) ? 0 : domain.hashCode());
00136                   result = prime * result
00137                                     + ((resource == null) ? 0 : resource.hashCode());
00138                   result = prime * result
00139                                     + ((username == null) ? 0 : username.hashCode());
00140                   return result;
00141          }
00142 
00143          @Override
00144          public boolean equals(Object obj) {
00145                   if (this == obj)
00146                            return true;
00147                   if (obj == null)
00148                            return false;
00149                   if (getClass() != obj.getClass())
00150                            return false;
00151                   JabberID other = (JabberID) obj;
00152                   if (domain == null) {
00153                            if (other.domain != null)
00154                                     return false;
00155                   } else if (!domain.equals(other.domain))
00156                            return false;
00157                   if (resource == null) {
00158                            if (other.resource != null)
00159                                     return false;
00160                   } else if (!resource.equals(other.resource))
00161                            return false;
00162                   if (username == null) {
00163                            if (other.username != null)
00164                                     return false;
00165                   } else if (!username.equals(other.username))
00166                            return false;
00167                   return true;
00168          }
00169          
00170 }
00171 
 All Classes Namespaces Files Functions Variables