1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
URI subclass:#HierarchicalURI
|
1257
|
4 |
instanceVariableNames:'authority isAbsolute isDirectory pathSegments query fragment'
|
1005
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Resources'
|
|
8 |
!
|
|
9 |
|
|
10 |
!HierarchicalURI class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
documentation
|
|
13 |
"
|
|
14 |
Hierarchical URI as defined in RFC2396:
|
|
15 |
|
|
16 |
<scheme:[//authority][/absolute_path][?query][#fragment]>
|
|
17 |
|
|
18 |
[author:]
|
|
19 |
Stefan Vogel (stefan@zwerg)
|
|
20 |
|
|
21 |
[see also:]
|
|
22 |
|
|
23 |
[instance variables:]
|
|
24 |
|
|
25 |
[class variables:]
|
|
26 |
"
|
|
27 |
!
|
|
28 |
|
|
29 |
examples
|
|
30 |
"
|
|
31 |
|
|
32 |
more examples to be added:
|
|
33 |
[exBegin]
|
1254
|
34 |
|u1 u2|
|
|
35 |
|
|
36 |
u1 := URI fromString:'file:/phys/exept/tmp/'.
|
|
37 |
u2 := u1 construct:'test.txt'.
|
|
38 |
Transcript showCR:u1.
|
|
39 |
Transcript showCR:u2.
|
|
40 |
[exEnd]
|
|
41 |
|
|
42 |
[exBegin]
|
|
43 |
|u1 u2|
|
|
44 |
|
|
45 |
u1 := URI fromString:'file:/phys/exept/tmp'.
|
|
46 |
u2 := u1 construct:'test.txt'.
|
|
47 |
Transcript showCR:u1.
|
|
48 |
Transcript showCR:u2.
|
1005
|
49 |
[exEnd]
|
|
50 |
"
|
|
51 |
! !
|
|
52 |
|
|
53 |
!HierarchicalURI class methodsFor:'instance creation'!
|
|
54 |
|
|
55 |
new
|
|
56 |
|
|
57 |
^ self basicNew initialize
|
|
58 |
!
|
|
59 |
|
|
60 |
scheme:aSchemeString fromString:aString
|
|
61 |
"parse the hierarchical information.
|
|
62 |
schemeString is ignored here"
|
|
63 |
|
|
64 |
^ self new scheme:aSchemeString;
|
|
65 |
fromString:aString
|
|
66 |
! !
|
|
67 |
|
|
68 |
!HierarchicalURI methodsFor:'accessing'!
|
|
69 |
|
|
70 |
authority
|
|
71 |
"return the value of the instance variable 'authority' (automatically generated)"
|
|
72 |
|
|
73 |
^ authority
|
|
74 |
!
|
|
75 |
|
|
76 |
authority:something
|
|
77 |
"set the value of the instance variable 'authority' (automatically generated)"
|
|
78 |
|
|
79 |
authority := something.
|
|
80 |
!
|
|
81 |
|
1254
|
82 |
examples
|
|
83 |
"
|
|
84 |
|
|
85 |
more examples to be added:
|
|
86 |
[exBegin]
|
|
87 |
URI fromString:'file:/phys/exept/home/tm/tmp'
|
|
88 |
[exEnd]
|
|
89 |
"
|
|
90 |
!
|
|
91 |
|
1005
|
92 |
fragment
|
|
93 |
"return the value of the instance variable 'fragment' (automatically generated)"
|
|
94 |
|
|
95 |
^ fragment
|
|
96 |
!
|
|
97 |
|
|
98 |
fragment:something
|
|
99 |
"set the value of the instance variable 'fragment' (automatically generated)"
|
|
100 |
|
|
101 |
fragment := something.
|
|
102 |
!
|
|
103 |
|
|
104 |
isAbsolute
|
|
105 |
"return the value of the instance variable 'isAbsolute' (automatically generated)"
|
|
106 |
|
|
107 |
^ isAbsolute
|
|
108 |
!
|
|
109 |
|
1257
|
110 |
isDirectory
|
|
111 |
"return the value of the instance variable 'isDirectory' (automatically generated)"
|
|
112 |
|
|
113 |
^ isDirectory ? false
|
|
114 |
!
|
|
115 |
|
1005
|
116 |
pathSegements
|
|
117 |
"return the value of the instance variable 'pathSegements' (automatically generated)"
|
|
118 |
|
|
119 |
^ pathSegments
|
|
120 |
!
|
|
121 |
|
|
122 |
pathSegements:something
|
|
123 |
"set the value of the instance variable 'pathSegements' (automatically generated)"
|
|
124 |
|
|
125 |
pathSegments := something.
|
|
126 |
!
|
|
127 |
|
|
128 |
query
|
|
129 |
"return the value of the instance variable 'query' (automatically generated)"
|
|
130 |
|
|
131 |
^ query
|
|
132 |
!
|
|
133 |
|
|
134 |
query:something
|
|
135 |
"set the value of the instance variable 'query' (automatically generated)"
|
|
136 |
|
|
137 |
query := something.
|
|
138 |
! !
|
|
139 |
|
|
140 |
!HierarchicalURI methodsFor:'accessing-details'!
|
|
141 |
|
|
142 |
host
|
|
143 |
"answer the host part of authority"
|
|
144 |
|
|
145 |
|start end|
|
|
146 |
|
|
147 |
authority isNil ifTrue:[
|
|
148 |
^ nil
|
|
149 |
].
|
|
150 |
start := (authority indexOf:$@) + 1.
|
|
151 |
end := (authority indexOf:$: startingAt:start) - 1.
|
|
152 |
end == -1 ifTrue:[
|
|
153 |
end := authority size
|
|
154 |
].
|
|
155 |
^ authority copyFrom:start to:end
|
|
156 |
|
|
157 |
"
|
|
158 |
(self fromString:'ftp://stefan@www.exept.de:80/test') host
|
|
159 |
(self fromString:'ftp://www.exept.de:80/test') host
|
|
160 |
(self fromString:'ftp://www.exept.de/test') host
|
|
161 |
"
|
|
162 |
!
|
|
163 |
|
|
164 |
password
|
|
165 |
"answer the user part of authority"
|
|
166 |
|
|
167 |
|end start|
|
|
168 |
|
|
169 |
authority isNil ifTrue:[
|
|
170 |
^ nil
|
|
171 |
].
|
|
172 |
end := authority indexOf:$@.
|
|
173 |
end == 0 ifTrue:[
|
|
174 |
^ nil
|
|
175 |
].
|
|
176 |
start := authority indexOf:$:.
|
|
177 |
(start == 0 or:[start > end]) ifTrue:[
|
|
178 |
^ nil
|
|
179 |
].
|
|
180 |
^ authority copyFrom:start+1 to:end-1
|
|
181 |
|
|
182 |
"
|
|
183 |
(self fromString:'ftp://stefan@www.exept.de:80/test') password
|
|
184 |
(self fromString:'ftp://stefan:pass@www.exept.de:80/test') password
|
|
185 |
(self fromString:'ftp://www.exept.de:80/test') password
|
|
186 |
"
|
|
187 |
!
|
|
188 |
|
|
189 |
port
|
|
190 |
"answer the port part of authority"
|
|
191 |
|
|
192 |
|start|
|
|
193 |
|
|
194 |
authority isNil ifTrue:[
|
|
195 |
^ self defaultPort
|
|
196 |
].
|
|
197 |
start := authority indexOf:$@.
|
|
198 |
start := authority indexOf:$: startingAt:start.
|
|
199 |
start == 0 ifTrue:[
|
|
200 |
^ self defaultPort
|
|
201 |
].
|
|
202 |
|
|
203 |
^ authority copyFrom:start+1
|
|
204 |
|
|
205 |
"
|
|
206 |
(self fromString:'ftp://stefan@www.exept.de:80/test') port
|
|
207 |
(self fromString:'ftp://www.exept.de:80/test') port
|
|
208 |
(self fromString:'ftp://www.exept.de/test') port
|
|
209 |
"
|
|
210 |
!
|
|
211 |
|
|
212 |
user
|
|
213 |
"answer the user part of authority"
|
|
214 |
|
|
215 |
|end end1|
|
|
216 |
|
|
217 |
authority isNil ifTrue:[
|
|
218 |
^ nil
|
|
219 |
].
|
|
220 |
end := authority indexOf:$@.
|
|
221 |
end == 0 ifTrue:[
|
|
222 |
^ nil
|
|
223 |
].
|
|
224 |
end1 := authority indexOf:$:.
|
|
225 |
end1 ~~ 0 ifTrue:[
|
|
226 |
end := end min:end1.
|
|
227 |
].
|
|
228 |
^ authority copyTo:end-1
|
|
229 |
|
|
230 |
"
|
|
231 |
(self fromString:'ftp://stefan@www.exept.de:80/test') user
|
|
232 |
(self fromString:'ftp://stefan:pass@www.exept.de:80/test') user
|
|
233 |
(self fromString:'ftp://www.exept.de:80/test') user
|
|
234 |
"
|
|
235 |
! !
|
|
236 |
|
1254
|
237 |
!HierarchicalURI methodsFor:'copying'!
|
|
238 |
|
|
239 |
postCopy
|
|
240 |
|
|
241 |
pathSegments := pathSegments copy
|
|
242 |
! !
|
|
243 |
|
1005
|
244 |
!HierarchicalURI methodsFor:'defaults'!
|
|
245 |
|
|
246 |
defaultPort
|
|
247 |
"answer the default port for the given scheme.
|
|
248 |
Concrete subclasses redefine this method"
|
|
249 |
|
|
250 |
^ nil
|
|
251 |
! !
|
|
252 |
|
|
253 |
!HierarchicalURI methodsFor:'escape'!
|
|
254 |
|
|
255 |
unEscape
|
|
256 |
"convert escaped characters (such as %20 for ' ') to their native
|
|
257 |
representation"
|
|
258 |
|
|
259 |
authority := self class unEscape:authority readStream.
|
|
260 |
pathSegments keysAndValuesDo:[:i :v|
|
|
261 |
pathSegments at:i put:(self class unEscape:v readStream).
|
|
262 |
].
|
|
263 |
query := self class unEscape:query readStream.
|
|
264 |
fragment := self class unEscape:fragment readStream.
|
|
265 |
! !
|
|
266 |
|
|
267 |
!HierarchicalURI methodsFor:'initialize'!
|
|
268 |
|
|
269 |
fromString:aString
|
|
270 |
|
|
271 |
|i i1 separator|
|
|
272 |
|
|
273 |
(aString startsWith:'//') ifTrue:[
|
1268
|
274 |
i := aString indexOfAny:'/?#' startingAt:3.
|
1005
|
275 |
i == 0 ifTrue:[
|
|
276 |
authority := aString copyFrom:3.
|
|
277 |
^ self.
|
|
278 |
] ifFalse:[
|
|
279 |
separator := aString at:i.
|
|
280 |
authority := aString copyFrom:3 to:i-1.
|
|
281 |
].
|
1267
|
282 |
(isAbsolute := (aString at:i) == $/).
|
1005
|
283 |
] ifFalse:[
|
|
284 |
(isAbsolute := aString startsWith:$/) ifTrue:[
|
|
285 |
i := 1.
|
|
286 |
] ifFalse:[
|
|
287 |
i := 0.
|
|
288 |
]
|
|
289 |
].
|
|
290 |
|
|
291 |
[
|
|
292 |
i1 := aString indexOfAny:'/?#' startingAt:i+1.
|
1254
|
293 |
i1 == 0 ifTrue:[ |path|
|
|
294 |
path := aString copyFrom:i+1.
|
|
295 |
path size ~~ 0 ifTrue:[
|
|
296 |
pathSegments add:path.
|
1257
|
297 |
] ifFalse:[
|
|
298 |
(aString at:i) == $/ ifTrue:[
|
|
299 |
isDirectory := true
|
|
300 |
].
|
1254
|
301 |
].
|
1005
|
302 |
^ self.
|
|
303 |
] ifFalse:[
|
|
304 |
separator := aString at:i1.
|
|
305 |
pathSegments add:(aString copyFrom:i+1 to:i1-1).
|
1267
|
306 |
isAbsolute ifTrue:[
|
|
307 |
pathSegments size == 1 ifTrue:[
|
1268
|
308 |
(pathSegments first startsWith:$~) ifTrue:[
|
1267
|
309 |
isAbsolute := false
|
|
310 |
].
|
|
311 |
].
|
|
312 |
].
|
1005
|
313 |
i := i1.
|
|
314 |
].
|
|
315 |
] doWhile:[separator == $/].
|
|
316 |
|
|
317 |
separator == $? ifTrue:[
|
|
318 |
i1 := aString indexOf:$# startingAt:i+1.
|
|
319 |
i1 == 0 ifTrue:[
|
|
320 |
query := aString copyFrom:i+1.
|
|
321 |
^ self.
|
|
322 |
] ifFalse:[
|
|
323 |
separator := aString at:i1.
|
|
324 |
query := aString copyFrom:i+1 to:i1-1.
|
|
325 |
i := i1.
|
|
326 |
].
|
|
327 |
].
|
|
328 |
separator == $# ifTrue:[
|
|
329 |
fragment := aString copyFrom:i+1.
|
|
330 |
].
|
|
331 |
|
|
332 |
"
|
1267
|
333 |
self new fromString:'//exept~/tmp'
|
|
334 |
self new fromString:'~/tmp'
|
|
335 |
self new fromString:'/~/tmp'
|
1257
|
336 |
self new fromString:'//authority/path1/path2/'
|
1005
|
337 |
self new fromString:'//authority/path1/path2?query'
|
|
338 |
self new fromString:'//authority/path1/path2?query#fragment'
|
|
339 |
self new fromString:'/path1/path2?query#fragment'
|
|
340 |
self new fromString:'/path1/path2#fragment'
|
|
341 |
self new fromString:'path1/path2#fragment'
|
|
342 |
"
|
|
343 |
!
|
|
344 |
|
|
345 |
initialize
|
|
346 |
|
|
347 |
pathSegments := OrderedCollection new.
|
|
348 |
! !
|
|
349 |
|
|
350 |
!HierarchicalURI methodsFor:'printing & storing'!
|
|
351 |
|
1268
|
352 |
directoryPath
|
|
353 |
"answer the directory path part of the URI"
|
|
354 |
|
|
355 |
|aStream|
|
|
356 |
|
|
357 |
aStream := WriteStream on:''.
|
|
358 |
|
|
359 |
pathSegments size ~~ 0 ifTrue: [
|
|
360 |
self isAbsolute ifTrue:[
|
|
361 |
aStream nextPut:$/.
|
|
362 |
].
|
|
363 |
pathSegments size > 1 ifTrue:[
|
|
364 |
(pathSegments copyFrom:2) do:[:p|
|
|
365 |
self class escape:p allow:'~;:@&=+",' on:aStream
|
|
366 |
] separatedBy:[
|
|
367 |
aStream nextPut:$/
|
|
368 |
].
|
|
369 |
].
|
|
370 |
].
|
|
371 |
|
|
372 |
^ aStream contents
|
|
373 |
!
|
|
374 |
|
1005
|
375 |
path
|
|
376 |
"answer the path part of the URI"
|
|
377 |
|
|
378 |
|aStream|
|
|
379 |
|
|
380 |
aStream := WriteStream on:''.
|
|
381 |
|
|
382 |
pathSegments size ~~ 0 ifTrue: [
|
1268
|
383 |
self isAbsolute ifTrue:[
|
1005
|
384 |
aStream nextPut:$/.
|
|
385 |
].
|
|
386 |
pathSegments do:[:p|
|
1268
|
387 |
self class escape:p allow:'~;:@&=+",' on:aStream
|
1005
|
388 |
] separatedBy:[
|
|
389 |
aStream nextPut:$/
|
|
390 |
].
|
|
391 |
].
|
|
392 |
query notNil ifTrue: [
|
|
393 |
aStream nextPut:$?.
|
|
394 |
self class escape:query allow:nil on:aStream
|
|
395 |
].
|
|
396 |
fragment notNil ifTrue: [
|
|
397 |
aStream nextPut:$#.
|
|
398 |
self class escape:fragment allow:nil on:aStream
|
|
399 |
].
|
|
400 |
|
|
401 |
^ aStream contents
|
|
402 |
!
|
|
403 |
|
|
404 |
printOn:aStream
|
|
405 |
|
|
406 |
self printOn:aStream escape:false
|
|
407 |
!
|
|
408 |
|
|
409 |
printOn:aStream escape:doEscape
|
|
410 |
"print the URI on aStream. If doEscape is set, escape special
|
|
411 |
characters"
|
|
412 |
|
|
413 |
super printOn:aStream.
|
|
414 |
authority notNil ifTrue: [
|
|
415 |
aStream nextPutAll:'//'.
|
|
416 |
doEscape ifTrue:[
|
1268
|
417 |
self class escape:authority allow:'~$,;:@&=+' on:aStream
|
1005
|
418 |
] ifFalse:[
|
|
419 |
aStream nextPutAll:authority
|
|
420 |
]
|
|
421 |
].
|
|
422 |
pathSegments size ~~ 0 ifTrue: [
|
1268
|
423 |
self isAbsolute ifTrue:[
|
1005
|
424 |
aStream nextPut:$/.
|
|
425 |
].
|
|
426 |
pathSegments do:[:p|
|
|
427 |
doEscape ifTrue:[
|
1268
|
428 |
self class escape:p allow:'~;:@&=+",' on:aStream
|
1005
|
429 |
] ifFalse:[
|
|
430 |
aStream nextPutAll:p
|
|
431 |
]
|
|
432 |
] separatedBy:[
|
|
433 |
aStream nextPut:$/
|
|
434 |
].
|
|
435 |
].
|
|
436 |
query notNil ifTrue: [
|
|
437 |
aStream nextPut:$?.
|
|
438 |
doEscape ifTrue:[
|
|
439 |
self class escape:query allow:nil on:aStream
|
|
440 |
] ifFalse:[
|
|
441 |
aStream nextPutAll:query
|
|
442 |
]
|
|
443 |
|
|
444 |
].
|
|
445 |
fragment notNil ifTrue: [
|
|
446 |
aStream nextPut:$#.
|
|
447 |
doEscape ifTrue:[
|
|
448 |
self class escape:fragment allow:nil on:aStream
|
|
449 |
] ifFalse:[
|
|
450 |
aStream nextPutAll:fragment
|
|
451 |
]
|
|
452 |
].
|
|
453 |
! !
|
|
454 |
|
|
455 |
!HierarchicalURI methodsFor:'resolution'!
|
|
456 |
|
|
457 |
addComponent:aString
|
|
458 |
"concatenate aString to my path"
|
|
459 |
|
|
460 |
(aString = '..'
|
|
461 |
and:[pathSegments size ~~ 0
|
|
462 |
and:[pathSegments first ~= '..']]) ifTrue:[
|
|
463 |
pathSegments removeLast.
|
|
464 |
] ifFalse:[
|
|
465 |
pathSegments add:aString
|
|
466 |
].
|
|
467 |
!
|
|
468 |
|
|
469 |
construct:aString
|
|
470 |
"concatenate anUri to my path"
|
|
471 |
|
|
472 |
^ self copy
|
|
473 |
addComponent:aString;
|
|
474 |
yourself.
|
|
475 |
! !
|
|
476 |
|
|
477 |
!HierarchicalURI class methodsFor:'documentation'!
|
|
478 |
|
|
479 |
version
|
1268
|
480 |
^ '$Header: /cvs/stx/stx/libbasic2/HierarchicalURI.st,v 1.5 2003-07-11 12:47:04 stefan Exp $'
|
1005
|
481 |
! !
|