Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jabber/jabberbeans/UserURI.java


1   package org.jabber.jabberbeans;
2   
3   import java.net.InetAddress;
4   
5   /**
6    * UserURI is a subclass of user which allows for one resource to be 
7    * specified. This makes it a complete URI.
8    *
9    * @author  David Waite <a href="mailto:mass@ufl.edu"><i>&lt;mass@ufl.edu&gt;
10   *      </i></a>
11   * @author  $Author: mass $
12   * @version $Revision: 1.1 $
13   */
14  public class UserURI
15      extends User
16  {
17      protected String resource;
18  
19      public UserURI(User u, String resource)
20      {
21    super(u.username, u.server);
22    this.resource=resource;
23      }
24      public UserURI(String username, InetAddress server, String resource)
25      {
26    super(username,server);
27    this.resource=resource;
28      }
29  
30      public static UserURI parseToURI(String initial)
31      {
32    String resource, username;
33    InetAddress server;
34  
35    //first we strip off any leading "jabber:"
36    if (initial.regionMatches(true,    /* ignore case  */
37            0,    /* offset       */
38            "jabber:",  /* other string */
39            0,    /* 2nd offset   */
40            7))    /* length       */
41        initial=initial.substring(7);
42    
43    //second, we look for an '@'
44    int location= initial.indexOf("@");
45    username=initial.substring(0,location);
46  
47    //everything after the @ becomes the new string
48    initial=initial.substring(location+1);
49  
50    //third, we look for a resource
51    location= initial.indexOf("/");
52    if (location==-1)
53        return null; //no URI can be created - no initial resource
54  
55    try
56    {  
57      server=InetAddress.getByName(initial.substring(0,location));
58    }
59    catch (Exception e)
60    {
61      return null;
62    }
63    resource=initial.substring(location+1);
64    
65    //finally, generate the URI object
66    return new UserURI(username, server, resource);
67      }
68  
69      public String toString()
70      {
71    return "jabber:" + username + "@" + server +
72        "/" + resource;
73      }
74      
75  }