StropheCappuccino API 1.0.0
/tmp/tempDoc.doc/TNStropheJID.j
Go to the documentation of this file.
00001 /*
00002  * TNStropheJID.j
00003  *
00004  * Copyright (C) 2010  Antoine Mercadal <antoine.mercadal@inframonde.eu>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 3.0 of the License, or (at your option) any later version.
00010  *
00011  * This library 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 GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 
00022 TNStropheJIDExceptionJID = @"TNStropheJIDExceptionJID";
00023 
00024 
00028 @implementation TNStropheJID : CPObject
00029 {
00030     BOOL        _isServer;
00031     CPString    _domain;
00032     CPString    _node;
00033     CPString    _resource;
00034 }
00035 
00036 #pragma mark -
00037 #pragma mark Class methods
00038 
00039 + (void)initialize
00040 {
00041     [self exposeBinding:@"domain"];
00042     [self exposeBinding:@"node"];
00043     [self exposeBinding:@"resource"];
00044 }
00045 
00051 + (TNStropheJID)stropheJIDWithNode:(CPString)aNode domain:(CPString)aDomain resource:(CPString)aResource
00052 {
00053     return [[TNStropheJID alloc] initWithNode:aNode domain:aDomain resource:aResource];
00054 }
00055 
00061 + (TNStropheJID)stropheJIDWithNode:(CPString)aNode domain:(CPString)aDomain
00062 {
00063     return [[TNStropheJID alloc] initWithNode:aNode domain:aDomain];
00064 }
00065 
00071 + (TNStropheJID)stropheJIDWithString:(CPString)aStringJID
00072 {
00073     return [[TNStropheJID alloc] initWithString:aStringJID];
00074 }
00075 
00076 
00077 #pragma mark -
00078 #pragma mark Initialization
00079 
00086 - (TNStropheJID)initWithNode:(CPString)aNode domain:(CPString)aDomain resource:(CPString)aResource
00087 {
00088     if (self = [super init])
00089     {
00090         [self setNode:aNode];
00091         [self setDomain:aDomain];
00092         [self setResource:aResource];
00093 
00094         _isServer   = (!aDomain && !aResource);
00095 
00096         if ((_node && _node.indexOf(" ") != -1) || (_domain && _domain.indexOf(" ") != -1) || (_resource && _resource.indexOf(" ") != -1))
00097             [CPException raise:TNStropheJIDExceptionJID reason:@"Given information are not a valid JID."];
00098     }
00099 
00100     return self;
00101 }
00102 
00107 - (TNStropheJID)initWithNode:(CPString)aNode domain:(CPString)aDomain
00108 {
00109     return [self initWithNode:aNode domain:aDomain resource:nil];
00110 }
00111 
00116 - (TNStropheJID)initWithString:(CPString)aStringJID
00117 {
00118     if (!aStringJID)
00119         return;
00120 
00121     var node = aStringJID.split("@")[0],
00122         domain,
00123         resource;
00124 
00125     if (aStringJID.indexOf("@") != -1) //this is a server
00126     {
00127         domain = aStringJID.split("@")[1].split("/")[0],
00128         resource = aStringJID.split("/")[1];
00129     }
00130     if (!node)
00131         [CPException raise:TNStropheJIDExceptionJID reason:aStringJID + @" is not a valid JID"];
00132 
00133     return [self initWithNode:node domain:domain resource:resource];
00134 }
00135 
00136 
00137 #pragma mark -
00138 #pragma mark Setters and Getters
00139 
00143 - (CPString)bare
00144 {
00145     if (_domain)
00146         return _node + @"@" + _domain;
00147     else
00148         return _node;
00149 }
00150 
00155 - (void)setBare:(CPString)aBareJID
00156 {
00157     var node = aBareJID.split("@")[0],
00158         domain = aBareJID.split("@")[1].split("/")[0];
00159 
00160     if (!node || !domain)
00161         [CPException raise:TNStropheJIDExceptionJID reason:aBareJID + @" is not a valid JID"];
00162 
00163     [self setNode:node];
00164     [self setDomain:domain];
00165 }
00166 
00170 - (CPString)full
00171 {
00172     if (_resource)
00173         return [self bare] + "/" + _resource;
00174     else
00175         return [self bare];
00176 }
00177 
00181 - (void)setFull:(CPString)aFullJID
00182 {
00183     [self setBare:aFullJID];
00184 
00185     var resource = aFullJID.split("/")[1];
00186 
00187     if (!resource)
00188         [CPException raise:TNStropheJIDExceptionJID reason:aFullJID + @" is not a valid JID"];
00189 
00190     [self setResource:resource]
00191 }
00192 
00196 - (CPString)description
00197 {
00198     return [self stringValue];
00199 }
00200 
00204 - (CPString)stringValue
00205 {
00206     return [self full];
00207 }
00208 
00211 - (CPString)uppercaseString
00212 {
00213     return [[self stringValue] uppercaseString];
00214 }
00215 
00218 - (CPString)lowercaseString
00219 {
00220     return [[self stringValue] lowercaseString];
00221 }
00222 
00223 #pragma mark -
00224 #pragma mark Operations
00225 
00229 - (BOOL)equals:(TNStropheJID)anotherJID
00230 {
00231     return [self fullEquals:anotherJID];
00232 }
00233 
00237 - (BOOL)fullEquals:(TNStropheJID)anotherJID
00238 {
00239     return ([self bareEquals:anotherJID] && ([[anotherJID resource] uppercaseString] === [[self resource] uppercaseString]))
00240 }
00241 
00245 - (BOOL)bareEquals:(TNStropheJID)anotherJID
00246 {
00247     return (([[anotherJID node] uppercaseString] === [[self node] uppercaseString]) && ([[anotherJID domain] uppercaseString] === [[self domain] uppercaseString]));
00248 }
00249 
00253 - (CPComparisonResult)compare:(TNStropheJID)anotherJID
00254 {
00255     var stringRepA = [self stringValue],
00256         stringRepB = [anotherJID stringValue];
00257 
00258     return [stringRepA compare:stringRepB];
00259 }
00260 
00261 @end
00262 
00263 
00264 @implementation TNStropheJID (CPCodingCompliance)
00265 
00266 - (id)initWithCoder:(CPCoder)aCoder
00267 {
00268     if (self = [super init])
00269     {
00270         _node       = [aCoder decodeObjectForKey:@"_node"];
00271         _domain     = [aCoder decodeObjectForKey:@"_domain"];
00272         _resource   = [aCoder decodeObjectForKey:@"_resource"];
00273     }
00274 
00275     return self;
00276 }
00277 
00278 - (void)encodeWithCoder:(CPCoder)aCoder
00279 {
00280     [aCoder encodeObject:_node forKey:@"_node"];
00281 
00282     if (_domain)
00283         [aCoder encodeObject:_domain forKey:@"_domain"];
00284 
00285     if (_resource)
00286         [aCoder encodeObject:_resource forKey:@"_resource"];
00287 }
00288 
00289 @end
00290 
00291 @implementation TNStropheJID (CPSynthesizedAccessors)
00292 
00296 - (BOOL)isServer
00297 {
00298     return _isServer;
00299 }
00300 
00304 - (CPString)domain
00305 {
00306     return _domain;
00307 }
00308 
00312 - (void)setDomain:(CPString)aValue
00313 {
00314     _domain = aValue;
00315 }
00316 
00320 - (CPString)node
00321 {
00322     return _node;
00323 }
00324 
00328 - (void)setNode:(CPString)aValue
00329 {
00330     _node = aValue;
00331 }
00332 
00336 - (CPString)resource
00337 {
00338     return _resource;
00339 }
00340 
00344 - (void)setResource:(CPString)aValue
00345 {
00346     _resource = aValue;
00347 }
00348 
00349 @end
 All Classes Namespaces Files Functions Variables