/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. @import "CPObject.j"
  23. @import "CPString.j"
  24. CPURLNameKey = @"CPURLNameKey";
  25. CPURLLocalizedNameKey = @"CPURLLocalizedNameKey";
  26. CPURLIsRegularFileKey = @"CPURLIsRegularFileKey";
  27. CPURLIsDirectoryKey = @"CPURLIsDirectoryKey";
  28. CPURLIsSymbolicLinkKey = @"CPURLIsSymbolicLinkKey";
  29. CPURLIsVolumeKey = @"CPURLIsVolumeKey";
  30. CPURLIsPackageKey = @"CPURLIsPackageKey";
  31. CPURLIsSystemImmutableKey = @"CPURLIsSystemImmutableKey";
  32. CPURLIsUserImmutableKey = @"CPURLIsUserImmutableKey";
  33. CPURLIsHiddenKey = @"CPURLIsHiddenKey";
  34. CPURLHasHiddenExtensionKey = @"CPURLHasHiddenExtensionKey";
  35. CPURLCreationDateKey = @"CPURLCreationDateKey";
  36. CPURLContentAccessDateKey = @"CPURLContentAccessDateKey";
  37. CPURLContentModificationDateKey = @"CPURLContentModificationDateKey";
  38. CPURLAttributeModificationDateKey = @"CPURLAttributeModificationDateKey";
  39. CPURLLinkCountKey = @"CPURLLinkCountKey";
  40. CPURLParentDirectoryURLKey = @"CPURLParentDirectoryURLKey";
  41. CPURLVolumeURLKey = @"CPURLTypeIdentifierKey";
  42. CPURLTypeIdentifierKey = @"CPURLTypeIdentifierKey";
  43. CPURLLocalizedTypeDescriptionKey = @"CPURLLocalizedTypeDescriptionKey";
  44. CPURLLabelNumberKey = @"CPURLLabelNumberKey";
  45. CPURLLabelColorKey = @"CPURLLabelColorKey";
  46. CPURLLocalizedLabelKey = @"CPURLLocalizedLabelKey";
  47. CPURLEffectiveIconKey = @"CPURLEffectiveIconKey";
  48. CPURLCustomIconKey = @"CPURLCustomIconKey";
  49. @implementation CPURL : CPObject
  50. {
  51. }
  52. + (id)alloc
  53. {
  54. var result = new CFURL();
  55. result.isa = [self class];
  56. return result;
  57. }
  58. - (id)init
  59. {
  60. return nil;
  61. }
  62. - (id)initWithScheme:(CPString)aScheme host:(CPString)aHost path:(CPString)aPath
  63. {
  64. var URLString = (aScheme ? aScheme + ":" : "") + (aHost ? aHost + "//" : "") + (aPath || "");
  65. return [self initWithString:URLString];
  66. }
  67. - (id)initWithString:(CPString)URLString
  68. {
  69. return [self initWithString:URLString relativeToURL:nil];
  70. }
  71. + (id)URLWithString:(CPString)URLString
  72. {
  73. return [[self alloc] initWithString:URLString];
  74. }
  75. - (id)initWithString:(CPString)URLString relativeToURL:(CPURL)aBaseURL
  76. {
  77. var result = new CFURL(URLString, aBaseURL);
  78. result.isa = [self class];
  79. return result;
  80. }
  81. + (id)URLWithString:(CPString)URLString relativeToURL:(CPURL)aBaseURL
  82. {
  83. return [[self alloc] initWithString:URLString relativeToURL:aBaseURL];
  84. }
  85. - (CPURL)absoluteURL
  86. {
  87. return self.absoluteURL();
  88. }
  89. - (CPURL)baseURL
  90. {
  91. return self.baseURL();
  92. }
  93. - (CPString)absoluteString
  94. {
  95. return self.absoluteString();
  96. }
  97. // if absolute, returns same as absoluteString
  98. - (CPString)relativeString
  99. {
  100. return self.string();
  101. }
  102. - (CPString)path
  103. {
  104. return [self absoluteURL].path();
  105. }
  106. - (CPArray)pathComponents
  107. {
  108. var components = self.pathComponents();
  109. return [components copy];
  110. }
  111. // if absolute, returns the same as path
  112. - (CPString)relativePath
  113. {
  114. return self.path();
  115. }
  116. - (CPString)scheme
  117. {
  118. return self.scheme();
  119. }
  120. - (CPString)user
  121. {
  122. return [self absoluteURL].user();
  123. }
  124. - (CPString)password
  125. {
  126. return [self absoluteURL].password();
  127. }
  128. - (CPString)host
  129. {
  130. return [self absoluteURL].domain();
  131. }
  132. - (Number)port
  133. {
  134. var portNumber = [self absoluteURL].portNumber();
  135. if (portNumber === -1)
  136. return nil;
  137. return portNumber;
  138. }
  139. - (CPString)parameterString
  140. {
  141. return self.queryString();
  142. }
  143. - (CPString)fragment
  144. {
  145. return self.fragment();
  146. }
  147. - (BOOL)isEqual:(id)anObject
  148. {
  149. if (self === anObject)
  150. return YES;
  151. if (!anObject || ![anObject isKindOfClass:[CPURL class]])
  152. return NO;
  153. return [self isEqualToURL:anObject];
  154. }
  155. - (BOOL)isEqualToURL:(id)aURL
  156. {
  157. if (self === aURL)
  158. return YES;
  159. // Is checking if baseURL isEqual correct? Does "identical" mean same object or equivalent values?
  160. return [[self absoluteString] isEqual:[aURL absoluteString]];
  161. }
  162. - (CPString)lastPathComponent
  163. {
  164. return [self absoluteURL].lastPathComponent();
  165. }
  166. - (CPString)pathExtension
  167. {
  168. return self.pathExtension();
  169. }
  170. - (CPURL)URLByDeletingLastPathComponent
  171. {
  172. var result = self.createCopyDeletingLastPathComponent();
  173. result.isa = [self class];
  174. return result;
  175. }
  176. - (CPURL)standardizedURL
  177. {
  178. return self.standardizedURL();
  179. }
  180. - (BOOL)isFileURL
  181. {
  182. return [self scheme] === "file";
  183. }
  184. - (CPString)description
  185. {
  186. return [self absoluteString];
  187. }
  188. - (id)resourceValueForKey:(CPString)aKey
  189. {
  190. return self.resourcePropertyForKey(aKey);
  191. }
  192. - (id)setResourceValue:(id)anObject forKey:(CPString)aKey
  193. {
  194. return self.setResourcePropertyForKey(aKey, anObject);
  195. }
  196. - (CPData)staticResourceData
  197. {
  198. return self.staticResourceData();
  199. }
  200. @end
  201. var CPURLURLStringKey = @"CPURLURLStringKey",
  202. CPURLBaseURLKey = @"CPURLBaseURLKey";
  203. @implementation CPURL (CPCoding)
  204. - (id)initWithCoder:(CPCoder)aCoder
  205. {
  206. return [self initWithString:[aCoder decodeObjectForKey:CPURLURLStringKey]
  207. relativeToURL:[aCoder decodeObjectForKey:CPURLBaseURLKey]];
  208. }
  209. - (void)encodeWithCoder:(CPCoder)aCoder
  210. {
  211. [aCoder encodeObject:self._baseURL forKey:CPURLBaseURLKey];
  212. [aCoder encodeObject:self._string forKey:CPURLURLStringKey];
  213. }
  214. @end
  215. CFURL.prototype.isa = [CPURL class];