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        private String from, userId, server, resource;
029    
030        /**
031         * Creates the ParsedBuddyInfo and parses the information
032         * 
033         * @param from
034         *            the address to parse
035         */
036        public ParsedBuddyInfo(String from) {
037            this.from = from;
038    
039            parseInfo();
040        }
041    
042        /**
043         * Parses the address
044         */
045        private void parseInfo() {
046            if (from == null)
047                return;
048            if (from.indexOf("@") > -1) {
049                if (from.indexOf("/") > -1)
050                    resource = from.substring(from.indexOf("/") + 1);
051    
052                userId = from.replaceAll("/.*", "");
053                server = userId.substring(userId.indexOf("@") + 1);
054            } else {
055                userId = from;
056                server = from;
057                resource = "";
058            }
059    
060            if (resource == null || resource.equals(""))
061                resource = "N/A";
062            from = from.replaceAll("/.*", "");
063        }
064    
065        /**
066         * Gets the address without the resource
067         * 
068         * @return the bare XMPPP address without the resource
069         */
070        public String getBareAddress() {
071            return from;
072        }
073    
074        /**
075         * Gets the user ID - without resource if this is not a transport or agent
076         * 
077         * @return the user id
078         */
079        public String getUserId() {
080            return userId;
081        }
082    
083        /**
084         * Gets the server that the user is on
085         * 
086         * @return the server the user is on
087         */
088        public String getServer() {
089            return server;
090        }
091    
092        /**
093         * Gets the resource
094         * 
095         * @return the resource, or an empty string if there isn't one or the user
096         *         is an agent/transport
097         */
098        public String getResource() {
099            return resource;
100        }
101    }