1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
URI subclass:#HierarchicalURI
|
|
4 |
instanceVariableNames:'authority isAbsolute pathSegments query fragment'
|
|
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 |
|
|
110 |
pathSegements
|
|
111 |
"return the value of the instance variable 'pathSegements' (automatically generated)"
|
|
112 |
|
|
113 |
^ pathSegments
|
|
114 |
!
|
|
115 |
|
|
116 |
pathSegements:something
|
|
117 |
"set the value of the instance variable 'pathSegements' (automatically generated)"
|
|
118 |
|
|
119 |
pathSegments := something.
|
|
120 |
!
|
|
121 |
|
|
122 |
query
|
|
123 |
"return the value of the instance variable 'query' (automatically generated)"
|
|
124 |
|
|
125 |
^ query
|
|
126 |
!
|
|
127 |
|
|
128 |
query:something
|
|
129 |
"set the value of the instance variable 'query' (automatically generated)"
|
|
130 |
|
|
131 |
query := something.
|
|
132 |
! !
|
|
133 |
|
|
134 |
!HierarchicalURI methodsFor:'accessing-details'!
|
|
135 |
|
|
136 |
host
|
|
137 |
"answer the host part of authority"
|
|
138 |
|
|
139 |
|start end|
|
|
140 |
|
|
141 |
authority isNil ifTrue:[
|
|
142 |
^ nil
|
|
143 |
].
|
|
144 |
start := (authority indexOf:$@) + 1.
|
|
145 |
end := (authority indexOf:$: startingAt:start) - 1.
|
|
146 |
end == -1 ifTrue:[
|
|
147 |
end := authority size
|
|
148 |
].
|
|
149 |
^ authority copyFrom:start to:end
|
|
150 |
|
|
151 |
"
|
|
152 |
(self fromString:'ftp://stefan@www.exept.de:80/test') host
|
|
153 |
(self fromString:'ftp://www.exept.de:80/test') host
|
|
154 |
(self fromString:'ftp://www.exept.de/test') host
|
|
155 |
"
|
|
156 |
!
|
|
157 |
|
|
158 |
password
|
|
159 |
"answer the user part of authority"
|
|
160 |
|
|
161 |
|end start|
|
|
162 |
|
|
163 |
authority isNil ifTrue:[
|
|
164 |
^ nil
|
|
165 |
].
|
|
166 |
end := authority indexOf:$@.
|
|
167 |
end == 0 ifTrue:[
|
|
168 |
^ nil
|
|
169 |
].
|
|
170 |
start := authority indexOf:$:.
|
|
171 |
(start == 0 or:[start > end]) ifTrue:[
|
|
172 |
^ nil
|
|
173 |
].
|
|
174 |
^ authority copyFrom:start+1 to:end-1
|
|
175 |
|
|
176 |
"
|
|
177 |
(self fromString:'ftp://stefan@www.exept.de:80/test') password
|
|
178 |
(self fromString:'ftp://stefan:pass@www.exept.de:80/test') password
|
|
179 |
(self fromString:'ftp://www.exept.de:80/test') password
|
|
180 |
"
|
|
181 |
!
|
|
182 |
|
|
183 |
port
|
|
184 |
"answer the port part of authority"
|
|
185 |
|
|
186 |
|start|
|
|
187 |
|
|
188 |
authority isNil ifTrue:[
|
|
189 |
^ self defaultPort
|
|
190 |
].
|
|
191 |
start := authority indexOf:$@.
|
|
192 |
start := authority indexOf:$: startingAt:start.
|
|
193 |
start == 0 ifTrue:[
|
|
194 |
^ self defaultPort
|
|
195 |
].
|
|
196 |
|
|
197 |
^ authority copyFrom:start+1
|
|
198 |
|
|
199 |
"
|
|
200 |
(self fromString:'ftp://stefan@www.exept.de:80/test') port
|
|
201 |
(self fromString:'ftp://www.exept.de:80/test') port
|
|
202 |
(self fromString:'ftp://www.exept.de/test') port
|
|
203 |
"
|
|
204 |
!
|
|
205 |
|
|
206 |
user
|
|
207 |
"answer the user part of authority"
|
|
208 |
|
|
209 |
|end end1|
|
|
210 |
|
|
211 |
authority isNil ifTrue:[
|
|
212 |
^ nil
|
|
213 |
].
|
|
214 |
end := authority indexOf:$@.
|
|
215 |
end == 0 ifTrue:[
|
|
216 |
^ nil
|
|
217 |
].
|
|
218 |
end1 := authority indexOf:$:.
|
|
219 |
end1 ~~ 0 ifTrue:[
|
|
220 |
end := end min:end1.
|
|
221 |
].
|
|
222 |
^ authority copyTo:end-1
|
|
223 |
|
|
224 |
"
|
|
225 |
(self fromString:'ftp://stefan@www.exept.de:80/test') user
|
|
226 |
(self fromString:'ftp://stefan:pass@www.exept.de:80/test') user
|
|
227 |
(self fromString:'ftp://www.exept.de:80/test') user
|
|
228 |
"
|
|
229 |
! !
|
|
230 |
|
1254
|
231 |
!HierarchicalURI methodsFor:'copying'!
|
|
232 |
|
|
233 |
postCopy
|
|
234 |
|
|
235 |
pathSegments := pathSegments copy
|
|
236 |
! !
|
|
237 |
|
1005
|
238 |
!HierarchicalURI methodsFor:'defaults'!
|
|
239 |
|
|
240 |
defaultPort
|
|
241 |
"answer the default port for the given scheme.
|
|
242 |
Concrete subclasses redefine this method"
|
|
243 |
|
|
244 |
^ nil
|
|
245 |
! !
|
|
246 |
|
|
247 |
!HierarchicalURI methodsFor:'escape'!
|
|
248 |
|
|
249 |
unEscape
|
|
250 |
"convert escaped characters (such as %20 for ' ') to their native
|
|
251 |
representation"
|
|
252 |
|
|
253 |
authority := self class unEscape:authority readStream.
|
|
254 |
pathSegments keysAndValuesDo:[:i :v|
|
|
255 |
pathSegments at:i put:(self class unEscape:v readStream).
|
|
256 |
].
|
|
257 |
query := self class unEscape:query readStream.
|
|
258 |
fragment := self class unEscape:fragment readStream.
|
|
259 |
! !
|
|
260 |
|
|
261 |
!HierarchicalURI methodsFor:'initialize'!
|
|
262 |
|
|
263 |
fromString:aString
|
|
264 |
|
|
265 |
|i i1 separator|
|
|
266 |
|
|
267 |
(aString startsWith:'//') ifTrue:[
|
|
268 |
i := aString indexOfAny:'/?#' startingAt:3.
|
|
269 |
i == 0 ifTrue:[
|
|
270 |
authority := aString copyFrom:3.
|
|
271 |
^ self.
|
|
272 |
] ifFalse:[
|
|
273 |
separator := aString at:i.
|
|
274 |
authority := aString copyFrom:3 to:i-1.
|
|
275 |
].
|
|
276 |
isAbsolute := true.
|
|
277 |
] ifFalse:[
|
|
278 |
(isAbsolute := aString startsWith:$/) ifTrue:[
|
|
279 |
i := 1.
|
|
280 |
] ifFalse:[
|
|
281 |
i := 0.
|
|
282 |
]
|
|
283 |
].
|
|
284 |
|
|
285 |
[
|
|
286 |
i1 := aString indexOfAny:'/?#' startingAt:i+1.
|
1254
|
287 |
i1 == 0 ifTrue:[ |path|
|
|
288 |
path := aString copyFrom:i+1.
|
|
289 |
path size ~~ 0 ifTrue:[
|
|
290 |
pathSegments add:path.
|
|
291 |
].
|
1005
|
292 |
^ self.
|
|
293 |
] ifFalse:[
|
|
294 |
separator := aString at:i1.
|
|
295 |
pathSegments add:(aString copyFrom:i+1 to:i1-1).
|
|
296 |
i := i1.
|
|
297 |
].
|
|
298 |
] doWhile:[separator == $/].
|
|
299 |
|
|
300 |
separator == $? ifTrue:[
|
|
301 |
i1 := aString indexOf:$# startingAt:i+1.
|
|
302 |
i1 == 0 ifTrue:[
|
|
303 |
query := aString copyFrom:i+1.
|
|
304 |
^ self.
|
|
305 |
] ifFalse:[
|
|
306 |
separator := aString at:i1.
|
|
307 |
query := aString copyFrom:i+1 to:i1-1.
|
|
308 |
i := i1.
|
|
309 |
].
|
|
310 |
].
|
|
311 |
separator == $# ifTrue:[
|
|
312 |
fragment := aString copyFrom:i+1.
|
|
313 |
].
|
|
314 |
|
|
315 |
"
|
|
316 |
self new fromString:'//authority/path1/path2'
|
|
317 |
self new fromString:'//authority/path1/path2?query'
|
|
318 |
self new fromString:'//authority/path1/path2?query#fragment'
|
|
319 |
self new fromString:'/path1/path2?query#fragment'
|
|
320 |
self new fromString:'/path1/path2#fragment'
|
|
321 |
self new fromString:'path1/path2#fragment'
|
|
322 |
"
|
|
323 |
!
|
|
324 |
|
|
325 |
initialize
|
|
326 |
|
|
327 |
pathSegments := OrderedCollection new.
|
|
328 |
! !
|
|
329 |
|
|
330 |
!HierarchicalURI methodsFor:'printing & storing'!
|
|
331 |
|
|
332 |
path
|
|
333 |
"answer the path part of the URI"
|
|
334 |
|
|
335 |
|aStream|
|
|
336 |
|
|
337 |
aStream := WriteStream on:''.
|
|
338 |
|
|
339 |
pathSegments size ~~ 0 ifTrue: [
|
|
340 |
isAbsolute ifTrue:[
|
|
341 |
aStream nextPut:$/.
|
|
342 |
].
|
|
343 |
pathSegments do:[:p|
|
|
344 |
self class escape:p allow:';:@&=+",' on:aStream
|
|
345 |
] separatedBy:[
|
|
346 |
aStream nextPut:$/
|
|
347 |
].
|
|
348 |
].
|
|
349 |
query notNil ifTrue: [
|
|
350 |
aStream nextPut:$?.
|
|
351 |
self class escape:query allow:nil on:aStream
|
|
352 |
].
|
|
353 |
fragment notNil ifTrue: [
|
|
354 |
aStream nextPut:$#.
|
|
355 |
self class escape:fragment allow:nil on:aStream
|
|
356 |
].
|
|
357 |
|
|
358 |
^ aStream contents
|
|
359 |
!
|
|
360 |
|
|
361 |
printOn:aStream
|
|
362 |
|
|
363 |
self printOn:aStream escape:false
|
|
364 |
!
|
|
365 |
|
|
366 |
printOn:aStream escape:doEscape
|
|
367 |
"print the URI on aStream. If doEscape is set, escape special
|
|
368 |
characters"
|
|
369 |
|
|
370 |
super printOn:aStream.
|
|
371 |
authority notNil ifTrue: [
|
|
372 |
aStream nextPutAll:'//'.
|
|
373 |
doEscape ifTrue:[
|
|
374 |
self class escape:authority allow:'$,;:@&=+' on:aStream
|
|
375 |
] ifFalse:[
|
|
376 |
aStream nextPutAll:authority
|
|
377 |
]
|
|
378 |
].
|
|
379 |
pathSegments size ~~ 0 ifTrue: [
|
|
380 |
isAbsolute ifTrue:[
|
|
381 |
aStream nextPut:$/.
|
|
382 |
].
|
|
383 |
pathSegments do:[:p|
|
|
384 |
doEscape ifTrue:[
|
|
385 |
self class escape:p allow:';:@&=+",' on:aStream
|
|
386 |
] ifFalse:[
|
|
387 |
aStream nextPutAll:p
|
|
388 |
]
|
|
389 |
] separatedBy:[
|
|
390 |
aStream nextPut:$/
|
|
391 |
].
|
|
392 |
].
|
|
393 |
query notNil ifTrue: [
|
|
394 |
aStream nextPut:$?.
|
|
395 |
doEscape ifTrue:[
|
|
396 |
self class escape:query allow:nil on:aStream
|
|
397 |
] ifFalse:[
|
|
398 |
aStream nextPutAll:query
|
|
399 |
]
|
|
400 |
|
|
401 |
].
|
|
402 |
fragment notNil ifTrue: [
|
|
403 |
aStream nextPut:$#.
|
|
404 |
doEscape ifTrue:[
|
|
405 |
self class escape:fragment allow:nil on:aStream
|
|
406 |
] ifFalse:[
|
|
407 |
aStream nextPutAll:fragment
|
|
408 |
]
|
|
409 |
].
|
|
410 |
! !
|
|
411 |
|
|
412 |
!HierarchicalURI methodsFor:'resolution'!
|
|
413 |
|
|
414 |
addComponent:aString
|
|
415 |
"concatenate aString to my path"
|
|
416 |
|
|
417 |
(aString = '..'
|
|
418 |
and:[pathSegments size ~~ 0
|
|
419 |
and:[pathSegments first ~= '..']]) ifTrue:[
|
|
420 |
pathSegments removeLast.
|
|
421 |
] ifFalse:[
|
|
422 |
pathSegments add:aString
|
|
423 |
].
|
|
424 |
!
|
|
425 |
|
|
426 |
construct:aString
|
|
427 |
"concatenate anUri to my path"
|
|
428 |
|
|
429 |
^ self copy
|
|
430 |
addComponent:aString;
|
|
431 |
yourself.
|
|
432 |
! !
|
|
433 |
|
|
434 |
!HierarchicalURI class methodsFor:'documentation'!
|
|
435 |
|
|
436 |
version
|
1254
|
437 |
^ '$Header: /cvs/stx/stx/libbasic2/HierarchicalURI.st,v 1.2 2003-06-24 14:10:39 tm Exp $'
|
1005
|
438 |
! !
|