PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/Foundation/CPURL.j

http://github.com/cacaodev/cappuccino
Unknown | 263 lines | 215 code | 48 blank | 0 comment | 0 complexity | f180769eb6f64cc28f6e3b40ba026803 MD5 | raw file
  1/*
  2 * CPURL.j
  3 * Foundation
  4 *
  5 * Created by Francisco Tolmasky.
  6 * Copyright 2008, 280 North, Inc.
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2.1 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16 * Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 21 */
 22
 23@import "CPObject.j"
 24@import "CPString.j"
 25
 26CPURLNameKey                        = @"CPURLNameKey";
 27CPURLLocalizedNameKey               = @"CPURLLocalizedNameKey";
 28CPURLIsRegularFileKey               = @"CPURLIsRegularFileKey";
 29CPURLIsDirectoryKey                 = @"CPURLIsDirectoryKey";
 30CPURLIsSymbolicLinkKey              = @"CPURLIsSymbolicLinkKey";
 31CPURLIsVolumeKey                    = @"CPURLIsVolumeKey";
 32CPURLIsPackageKey                   = @"CPURLIsPackageKey";
 33CPURLIsSystemImmutableKey           = @"CPURLIsSystemImmutableKey";
 34CPURLIsUserImmutableKey             = @"CPURLIsUserImmutableKey";
 35CPURLIsHiddenKey                    = @"CPURLIsHiddenKey";
 36CPURLHasHiddenExtensionKey          = @"CPURLHasHiddenExtensionKey";
 37CPURLCreationDateKey                = @"CPURLCreationDateKey";
 38CPURLContentAccessDateKey           = @"CPURLContentAccessDateKey";
 39CPURLContentModificationDateKey     = @"CPURLContentModificationDateKey";
 40CPURLAttributeModificationDateKey   = @"CPURLAttributeModificationDateKey";
 41CPURLLinkCountKey                   = @"CPURLLinkCountKey";
 42CPURLParentDirectoryURLKey          = @"CPURLParentDirectoryURLKey";
 43CPURLVolumeURLKey                   = @"CPURLTypeIdentifierKey";
 44CPURLTypeIdentifierKey              = @"CPURLTypeIdentifierKey";
 45CPURLLocalizedTypeDescriptionKey    = @"CPURLLocalizedTypeDescriptionKey";
 46CPURLLabelNumberKey                 = @"CPURLLabelNumberKey";
 47CPURLLabelColorKey                  = @"CPURLLabelColorKey";
 48CPURLLocalizedLabelKey              = @"CPURLLocalizedLabelKey";
 49CPURLEffectiveIconKey               = @"CPURLEffectiveIconKey";
 50CPURLCustomIconKey                  = @"CPURLCustomIconKey";
 51
 52@implementation CPURL : CPObject
 53{
 54}
 55
 56+ (id)alloc
 57{
 58    var result = new CFURL();
 59    result.isa = [self class];
 60    return result;
 61}
 62
 63- (id)init
 64{
 65    return nil;
 66}
 67
 68- (id)initWithScheme:(CPString)aScheme host:(CPString)aHost path:(CPString)aPath
 69{
 70    var URLString = (aScheme ? aScheme + ":" : "") + (aHost ? aHost + "//" : "") + (aPath || "");
 71
 72    return [self initWithString:URLString];
 73}
 74
 75- (id)initWithString:(CPString)URLString
 76{
 77    return [self initWithString:URLString relativeToURL:nil];
 78}
 79
 80+ (id)URLWithString:(CPString)URLString
 81{
 82    return [[self alloc] initWithString:URLString];
 83}
 84
 85- (id)initWithString:(CPString)URLString relativeToURL:(CPURL)aBaseURL
 86{
 87    var result = new CFURL(URLString, aBaseURL);
 88    result.isa = [self class];
 89    return result;
 90}
 91
 92+ (id)URLWithString:(CPString)URLString relativeToURL:(CPURL)aBaseURL
 93{
 94    return [[self alloc] initWithString:URLString relativeToURL:aBaseURL];
 95}
 96
 97- (CPURL)absoluteURL
 98{
 99    return self.absoluteURL();
100}
101
102- (CPURL)baseURL
103{
104    return self.baseURL();
105}
106
107- (CPString)absoluteString
108{
109    return self.absoluteString();
110}
111
112// if absolute, returns same as absoluteString
113- (CPString)relativeString
114{
115    return self.string();
116}
117
118- (CPString)path
119{
120    return [self absoluteURL].path();
121}
122
123- (CPArray)pathComponents
124{
125    var components = self.pathComponents();
126    return [components copy];
127}
128
129// if absolute, returns the same as path
130- (CPString)relativePath
131{
132    return self.path();
133}
134
135- (CPString)scheme
136{
137    return self.scheme();
138}
139
140- (CPString)user
141{
142    return [self absoluteURL].user();
143}
144
145- (CPString)password
146{
147    return [self absoluteURL].password();
148}
149
150- (CPString)host
151{
152    return [self absoluteURL].domain();
153}
154
155- (Number)port
156{
157    var portNumber = [self absoluteURL].portNumber();
158
159    if (portNumber === -1)
160        return nil;
161
162    return portNumber;
163}
164
165- (CPString)parameterString
166{
167    return self.queryString();
168}
169
170- (CPString)fragment
171{
172    return self.fragment();
173}
174
175- (BOOL)isEqual:(id)anObject
176{
177    if (self === anObject)
178        return YES;
179
180    if (!anObject || ![anObject isKindOfClass:[CPURL class]])
181        return NO;
182
183    return [self isEqualToURL:anObject];
184}
185
186- (BOOL)isEqualToURL:(id)aURL
187{
188    if (self === aURL)
189        return YES;
190
191    // Is checking if baseURL isEqual correct? Does "identical" mean same object or equivalent values?
192    return [[self absoluteString] isEqual:[aURL absoluteString]];
193}
194
195- (CPString)lastPathComponent
196{
197    return [self absoluteURL].lastPathComponent();
198}
199
200- (CPString)pathExtension
201{
202    return self.pathExtension();
203}
204
205- (CPURL)URLByDeletingLastPathComponent
206{
207    var result = self.createCopyDeletingLastPathComponent();
208    result.isa = [self class];
209    return result;
210}
211
212- (CPURL)standardizedURL
213{
214    return self.standardizedURL();
215}
216
217- (BOOL)isFileURL
218{
219    return [self scheme] === "file";
220}
221
222- (CPString)description
223{
224    return [self absoluteString];
225}
226
227- (id)resourceValueForKey:(CPString)aKey
228{
229    return self.resourcePropertyForKey(aKey);
230}
231
232- (id)setResourceValue:(id)anObject forKey:(CPString)aKey
233{
234    return self.setResourcePropertyForKey(aKey, anObject);
235}
236
237- (CPData)staticResourceData
238{
239    return self.staticResourceData();
240}
241
242@end
243
244var CPURLURLStringKey   = @"CPURLURLStringKey",
245    CPURLBaseURLKey     = @"CPURLBaseURLKey";
246
247@implementation CPURL (CPCoding)
248
249- (id)initWithCoder:(CPCoder)aCoder
250{
251    return [self initWithString:[aCoder decodeObjectForKey:CPURLURLStringKey]
252                  relativeToURL:[aCoder decodeObjectForKey:CPURLBaseURLKey]];
253}
254
255- (void)encodeWithCoder:(CPCoder)aCoder
256{
257    [aCoder encodeObject:self._baseURL forKey:CPURLBaseURLKey];
258    [aCoder encodeObject:self._string forKey:CPURLURLStringKey];
259}
260
261@end
262
263CFURL.prototype.isa = [CPURL class];