001    /*
002            Copyright (C) 2003 Adam Olsen
003    
004            This program is free software; you can redistribute it and/or modify
005            it under the terms of the GNU General Public License as published by
006            the Free Software Foundation; either version 1, or (at your option)
007            any later version.
008    
009            This program is distributed in the hope that it will be useful,
010            but WITHOUT ANY WARRANTY; without even the implied warranty of
011            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012            GNU General Public License for more details.
013    
014            You should have received a copy of the GNU General Public License
015            along with this program; if not, write to the Free Software
016            Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017    */
018    
019    package com.valhalla.jbother.jabber;
020    
021    /**
022     * Parses a JID into its various parts
023     *
024     * @author Adam Olsen
025     * @version 1.0
026    */
027    public class ParsedBuddyInfo
028    {
029            private String from, userId, server, resource;
030    
031            /**
032             * Creates the ParsedBuddyInfo and parses the information
033             * @param from the address to parse
034            */
035            public ParsedBuddyInfo( String from )
036            {
037                    this.from = from;
038    
039                    parseInfo();
040            }
041    
042            /**
043             * Parses the address
044            */
045            private void parseInfo()
046            {
047                    if( from == null ) return;
048                    if( from.indexOf( "@" ) > -1 )
049                    {
050                            if( from.indexOf( "/" ) > -1 )
051                                    resource = from.substring( from.indexOf( "/" ) + 1 );
052    
053                            userId = from.replaceAll( "/.*", "" );
054                            server = userId.substring( userId.indexOf( "@" ) + 1 );
055                    }
056                    else {
057                            userId = from;
058                            server = from;
059                            resource = "";
060                    }
061    
062                    if( resource == null || resource.equals( "" ) ) resource = "N/A";
063                    from = from.replaceAll( "/.*", "" );
064            }
065    
066            /**
067             * Gets the address without the resource
068             * @return the bare XMPPP address without the resource
069            */
070            public String getBareAddress() { return from; }
071    
072            /**
073             * Gets the user ID - without resource if this is not a transport or agent
074             * @return the user id
075            */
076            public String getUserId() { return userId; }
077    
078            /**
079             * Gets the server that the user is on
080             * @return the server the user is on
081            */
082            public String getServer() { return server; }
083    
084            /**
085             * Gets the resource
086             * @return the resource, or an empty string if there isn't one or the user is an agent/transport
087            */
088            public String getResource() { return resource; }
089    }