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