author | Claus Gittinger <cg@exept.de> |
Fri, 29 Aug 2003 21:33:23 +0200 | |
changeset 1309 | c752d54f4e09 |
parent 1276 | fd3f0d37513c |
child 1310 | 948ed141b0b7 |
permissions | -rw-r--r-- |
1309 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
1005 | 13 |
"{ Package: 'stx:libbasic2' }" |
14 |
||
15 |
Object subclass:#URI |
|
16 |
instanceVariableNames:'scheme' |
|
17 |
classVariableNames:'' |
|
18 |
poolDictionaries:'' |
|
19 |
category:'Resources' |
|
20 |
! |
|
21 |
||
1309 | 22 |
!URI class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
27 |
All Rights Reserved |
|
28 |
||
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! ! |
|
1005 | 37 |
|
38 |
!URI class methodsFor:'instance creation'! |
|
39 |
||
40 |
fromString:aString |
|
41 |
"create an URI from a given String" |
|
42 |
||
1251 | 43 |
|i scheme rest| |
44 |
||
45 |
i := aString indexOf:$:. |
|
46 |
i == 0 ifTrue:[ |
|
47 |
self error:('Missing scheme in: %1!!' bindWith:aString) |
|
48 |
]. |
|
49 |
scheme := aString copyFrom:1 to:i-1. |
|
50 |
rest := aString copyFrom:i+1. |
|
51 |
||
52 |
^ (self classForScheme:scheme) scheme:scheme fromString:rest |
|
53 |
||
1249 | 54 |
|
55 |
||
56 |
" |
|
57 |
self fromString:'' |
|
1268 | 58 |
self fromString:'file:~' |
59 |
self fromString:'ftp://exept.exept.de/~stefan/bla' |
|
1249 | 60 |
" |
61 |
! |
|
62 |
||
63 |
fromString:aString onError:exceptionBlock |
|
64 |
"create an URI from a given String" |
|
65 |
||
1251 | 66 |
|uri| |
1249 | 67 |
|
1251 | 68 |
Error handle:[:ex| |
69 |
exceptionBlock value. |
|
70 |
] do:[ |
|
71 |
uri := self fromString:aString |
|
72 |
]. |
|
1249 | 73 |
|
1251 | 74 |
^ uri |
1249 | 75 |
|
76 |
" |
|
1251 | 77 |
self fromString:'' onError:nil |
1249 | 78 |
" |
1005 | 79 |
! ! |
80 |
||
81 |
!URI class methodsFor:'accessing'! |
|
82 |
||
1254 | 83 |
attributes |
84 |
||
85 |
|dict| |
|
86 |
||
87 |
dict := Dictionary new. |
|
88 |
dict at:#MIME put:'text/plain'. |
|
89 |
^ dict |
|
90 |
! |
|
91 |
||
1005 | 92 |
classForScheme:aString |
93 |
"find a class for a given scheme name aString" |
|
94 |
||
95 |
|s| |
|
96 |
||
97 |
s := aString asLowercase. |
|
98 |
||
99 |
^ self allSubclasses detect:[:cls| |schemes| |
|
100 |
schemes := cls schemes. |
|
101 |
schemes size ~~ 0 and:[schemes includes:s] |
|
102 |
] ifNone:[HierarchicalURI] |
|
1249 | 103 |
|
104 |
" |
|
105 |
self classForScheme:'file' |
|
106 |
" |
|
1005 | 107 |
! |
108 |
||
109 |
schemes |
|
110 |
"answer the schemes supported by an URI-class. |
|
111 |
Concrete subclasses redefine this to answer an array of scheme names" |
|
112 |
||
113 |
^ nil |
|
1256 | 114 |
! |
115 |
||
116 |
transferBufferSize |
|
117 |
^ 8 * 1024 |
|
1005 | 118 |
! ! |
119 |
||
120 |
!URI class methodsFor:'escape'! |
|
121 |
||
122 |
escape:aString allow:additionalCharacters on:aStream |
|
123 |
||
124 |
|val| |
|
125 |
||
126 |
aString do:[:c| |
|
127 |
((c isLetterOrDigit) |
|
128 |
or:[('-_.!!~*''()' includes:c) |
|
129 |
or:[additionalCharacters notNil |
|
130 |
and:[additionalCharacters includes:c]]] |
|
131 |
) ifTrue:[ |
|
132 |
aStream nextPut:c |
|
133 |
] ifFalse:[ |
|
134 |
val := c asciiValue. |
|
135 |
aStream nextPut:$%; |
|
136 |
nextPut:(Character digitValue:val//16); |
|
137 |
nextPut:(Character digitValue:val\\16). |
|
138 |
]. |
|
139 |
]. |
|
140 |
||
141 |
||
142 |
" |
|
143 |
self escape:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
144 |
allow:nil on:TextCollector open |
|
145 |
||
146 |
self escape:'Ein text mit blanks und @ & #' |
|
147 |
allow:nil on:TextCollector open |
|
148 |
" |
|
149 |
! |
|
150 |
||
151 |
unEscape:aStream |
|
152 |
"convert escape sequences to the original characters" |
|
153 |
||
154 |
|s c1 c2| |
|
155 |
||
156 |
s := WriteStream on:''. |
|
157 |
||
158 |
[aStream atEnd] whileFalse:[ |
|
159 |
c1 := aStream next. |
|
160 |
c1 == $% ifTrue:[ |
|
161 |
c1 := aStream next. |
|
162 |
c1 isNil ifTrue:[ |
|
163 |
self error:'escape sequence incomplete' |
|
164 |
]. |
|
165 |
c2 := aStream next. |
|
166 |
c2 isNil ifTrue:[ |
|
167 |
self error:'escape sequence incomplete' |
|
168 |
]. |
|
169 |
c1 := c1 digitValue. |
|
170 |
c2 := c2 digitValue. |
|
171 |
(c1 > 15 or:[c2 > 15]) ifTrue:[ |
|
172 |
self error:'escape sequence: expect hex digit' |
|
173 |
]. |
|
174 |
c1 := Character value:c1*16 + c2. |
|
175 |
]. |
|
176 |
s nextPut:c1. |
|
177 |
]. |
|
178 |
||
179 |
^ s contents. |
|
180 |
||
181 |
" |
|
182 |
self unEscape:(self escape:' &%@ ' allow:nil) readStream |
|
183 |
" |
|
184 |
! ! |
|
185 |
||
186 |
!URI methodsFor:'accessing'! |
|
187 |
||
188 |
scheme |
|
189 |
"return primary scheme of the class. |
|
190 |
Concrete subclasses may redefine this" |
|
191 |
||
192 |
|schemes| |
|
193 |
||
194 |
schemes := self class schemes. |
|
195 |
^ schemes size ~~ 0 ifTrue:[ |
|
196 |
schemes at:1 |
|
197 |
] ifFalse:[ |
|
198 |
nil |
|
199 |
] |
|
200 |
! |
|
201 |
||
202 |
scheme:aString |
|
203 |
"set the scheme. This is a noop here and may be |
|
204 |
defined by subclasses" |
|
205 |
||
206 |
^ self |
|
207 |
! ! |
|
208 |
||
209 |
!URI methodsFor:'comparing'! |
|
210 |
||
211 |
= anURI |
|
212 |
^ self class == anURI class and:[self asString == anURI asString] |
|
213 |
! |
|
214 |
||
215 |
hash |
|
216 |
^ self asString hash |
|
217 |
! ! |
|
218 |
||
219 |
!URI methodsFor:'converting'! |
|
220 |
||
221 |
asURI |
|
222 |
||
223 |
^ self |
|
224 |
! ! |
|
225 |
||
226 |
!URI methodsFor:'printing & storing'! |
|
227 |
||
228 |
printOn:aStream |
|
229 |
||
230 |
|scheme| |
|
231 |
||
232 |
scheme := self scheme. |
|
233 |
scheme size ~~ 0 ifTrue:[ |
|
234 |
aStream nextPutAll:scheme; nextPut:$: |
|
235 |
]. |
|
1276 | 236 |
! |
237 |
||
238 |
publicPrintOn:aStream |
|
239 |
"print, but omit password information. |
|
240 |
Subclasses which know about secret information have to redefine this" |
|
241 |
||
242 |
^ self printOn:aStream |
|
1005 | 243 |
! ! |
244 |
||
245 |
!URI methodsFor:'subclass responsibility'! |
|
246 |
||
1270
01f4dc0293cd
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
247 |
exists |
01f4dc0293cd
Working version: tilde-expansion, patterns
Stefan Vogel <sv@exept.de>
parents:
1268
diff
changeset
|
248 |
"return true if the resource represented by this uri exists" |
1254 | 249 |
|
250 |
^ self subclassResponsibility |
|
251 |
! |
|
252 |
||
1005 | 253 |
readStreamDo:aBlock |
254 |
||
255 |
"evaluate a block with the read stream as first argument |
|
256 |
and a dictionary containing attributes as second argument. |
|
257 |
The stream is closed after aBlock has been evaluated. |
|
258 |
Attributes may be the mime type (key #MIME)" |
|
259 |
||
260 |
^ self subclassResponsibility |
|
1254 | 261 |
! |
262 |
||
263 |
readStreamsDo:aBlock |
|
264 |
||
265 |
"evaluate the block with a Collection of streams as first argument |
|
266 |
and a dictionary containing attributes as second argument, |
|
267 |
- a collection with a stream on a single file, |
|
268 |
- or a collection with streams on a directorie's files, but not recursive |
|
269 |
||
270 |
The streams are closed after aBlock has been evaluated. |
|
271 |
Attributes may be the mime type (key #MIME)" |
|
272 |
||
273 |
^ self subclassResponsibility |
|
274 |
! |
|
275 |
||
276 |
writeStreamDo:aBlock |
|
277 |
||
278 |
"evaluate a block with the write stream as first argument |
|
279 |
and a dictionary containing attributes as second argument. |
|
280 |
The stream is closed after aBlock has been evaluated. |
|
281 |
Attributes may be the mime type (key #MIME)" |
|
282 |
||
283 |
^ self subclassResponsibility |
|
1005 | 284 |
! ! |
285 |
||
1276 | 286 |
!URI methodsFor:'testing'! |
287 |
||
288 |
isRemote |
|
289 |
"return true, if this is a remote URI" |
|
290 |
||
291 |
^ false |
|
292 |
! ! |
|
293 |
||
1005 | 294 |
!URI class methodsFor:'documentation'! |
295 |
||
296 |
version |
|
1309 | 297 |
^ '$Header: /cvs/stx/stx/libbasic2/URI.st,v 1.9 2003-08-29 19:33:21 cg Exp $' |
1005 | 298 |
! ! |