author | Claus Gittinger <cg@exept.de> |
Tue, 25 Jun 2019 11:09:04 +0200 | |
changeset 5048 | 63f42cfbaf59 |
parent 4881 | bdff8a3ce9d6 |
child 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 |
||
4307 | 73 |
^ [self fromString:aString] on:Error do:[exceptionBlock value] |
1249 | 74 |
|
1776 | 75 |
" |
76 |
self fromString:'' onError:nil |
|
77 |
" |
|
4307 | 78 |
|
79 |
"Modified (format): / 08-02-2017 / 19:16:55 / stefan" |
|
1005 | 80 |
! ! |
81 |
||
82 |
!URI class methodsFor:'accessing'! |
|
83 |
||
1254 | 84 |
attributes |
85 |
||
86 |
|dict| |
|
87 |
||
88 |
dict := Dictionary new. |
|
89 |
dict at:#MIME put:'text/plain'. |
|
90 |
^ dict |
|
91 |
! |
|
92 |
||
1005 | 93 |
classForScheme:aString |
94 |
"find a class for a given scheme name aString" |
|
95 |
||
96 |
|s| |
|
97 |
||
4721 | 98 |
"/ because it is so common: |
99 |
aString = 'http' ifTrue:[^ HttpURI]. |
|
100 |
aString = 'https' ifTrue:[^ HttpsURI]. |
|
101 |
||
1005 | 102 |
s := aString asLowercase. |
4721 | 103 |
^ self allSubclasses |
104 |
detect:[:cls| |
|
105 |
|schemes| |
|
106 |
schemes := cls schemes. |
|
107 |
schemes size ~~ 0 and:[schemes includes:s] |
|
108 |
] |
|
109 |
ifNone:[HierarchicalURI] |
|
1249 | 110 |
|
111 |
" |
|
112 |
self classForScheme:'file' |
|
113 |
" |
|
4721 | 114 |
|
115 |
"Modified: / 28-08-2018 / 09:05:41 / Claus Gittinger" |
|
1005 | 116 |
! |
117 |
||
118 |
schemes |
|
119 |
"answer the schemes supported by an URI-class. |
|
120 |
Concrete subclasses redefine this to answer an array of scheme names" |
|
121 |
||
122 |
^ nil |
|
1256 | 123 |
! |
124 |
||
125 |
transferBufferSize |
|
126 |
^ 8 * 1024 |
|
1005 | 127 |
! ! |
128 |
||
129 |
!URI class methodsFor:'escape'! |
|
130 |
||
131 |
escape:aString allow:additionalCharacters on:aStream |
|
4356 | 132 |
aString do:[:c| |
133 |
|val| |
|
1005 | 134 |
|
135 |
((c isLetterOrDigit) |
|
136 |
or:[('-_.!!~*''()' includes:c) |
|
137 |
or:[additionalCharacters notNil |
|
138 |
and:[additionalCharacters includes:c]]] |
|
139 |
) ifTrue:[ |
|
140 |
aStream nextPut:c |
|
141 |
] ifFalse:[ |
|
1415
3ef6a2c42611
Use #codePoint instead of deprecated #asciiValue
Stefan Vogel <sv@exept.de>
parents:
1310
diff
changeset
|
142 |
val := c codePoint. |
1005 | 143 |
aStream nextPut:$%; |
144 |
nextPut:(Character digitValue:val//16); |
|
145 |
nextPut:(Character digitValue:val\\16). |
|
146 |
]. |
|
147 |
]. |
|
148 |
||
149 |
||
150 |
" |
|
151 |
self escape:'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' |
|
152 |
allow:nil on:TextCollector open |
|
153 |
||
154 |
self escape:'Ein text mit blanks und @ & #' |
|
155 |
allow:nil on:TextCollector open |
|
4356 | 156 |
" |
157 |
||
158 |
"Modified (format): / 06-03-2017 / 15:59:47 / stefan" |
|
1005 | 159 |
! |
160 |
||
161 |
unEscape:aStream |
|
162 |
"convert escape sequences to the original characters" |
|
163 |
||
164 |
|s c1 c2| |
|
165 |
||
166 |
s := WriteStream on:''. |
|
167 |
||
168 |
[aStream atEnd] whileFalse:[ |
|
169 |
c1 := aStream next. |
|
170 |
c1 == $% ifTrue:[ |
|
171 |
c1 := aStream next. |
|
172 |
c1 isNil ifTrue:[ |
|
173 |
self error:'escape sequence incomplete' |
|
174 |
]. |
|
175 |
c2 := aStream next. |
|
176 |
c2 isNil ifTrue:[ |
|
177 |
self error:'escape sequence incomplete' |
|
178 |
]. |
|
179 |
c1 := c1 digitValue. |
|
180 |
c2 := c2 digitValue. |
|
181 |
(c1 > 15 or:[c2 > 15]) ifTrue:[ |
|
182 |
self error:'escape sequence: expect hex digit' |
|
183 |
]. |
|
184 |
c1 := Character value:c1*16 + c2. |
|
185 |
]. |
|
186 |
s nextPut:c1. |
|
187 |
]. |
|
188 |
||
189 |
^ s contents. |
|
190 |
||
191 |
" |
|
2023 | 192 |
self unEscape:(self escape:' &%@ ' ) |
1005 | 193 |
" |
194 |
! ! |
|
195 |
||
196 |
!URI methodsFor:'accessing'! |
|
197 |
||
2874 | 198 |
contents |
199 |
|data| |
|
200 |
||
201 |
self readStreamDo:[:s | |
|
202 |
data := s contents. |
|
203 |
s close. |
|
204 |
]. |
|
205 |
^ data |
|
206 |
! |
|
207 |
||
3170 | 208 |
method |
209 |
"for compatibility with outdated URL class" |
|
210 |
||
211 |
^ self scheme |
|
212 |
! |
|
213 |
||
1005 | 214 |
scheme |
215 |
"return primary scheme of the class. |
|
216 |
Concrete subclasses may redefine this" |
|
217 |
||
218 |
|schemes| |
|
219 |
||
1865 | 220 |
scheme notNil ifTrue:[ |
221 |
^ scheme |
|
222 |
]. |
|
223 |
||
1005 | 224 |
schemes := self class schemes. |
225 |
^ schemes size ~~ 0 ifTrue:[ |
|
226 |
schemes at:1 |
|
227 |
] ifFalse:[ |
|
228 |
nil |
|
229 |
] |
|
230 |
! |
|
231 |
||
232 |
scheme:aString |
|
1865 | 233 |
scheme := aString |
1005 | 234 |
! ! |
235 |
||
236 |
!URI methodsFor:'comparing'! |
|
237 |
||
238 |
= anURI |
|
239 |
^ self class == anURI class and:[self asString == anURI asString] |
|
240 |
! |
|
241 |
||
242 |
hash |
|
243 |
^ self asString hash |
|
244 |
! ! |
|
245 |
||
246 |
!URI methodsFor:'converting'! |
|
247 |
||
248 |
asURI |
|
2956 | 249 |
"return myself - I am an URI" |
1005 | 250 |
|
251 |
^ self |
|
252 |
! ! |
|
253 |
||
254 |
!URI methodsFor:'printing & storing'! |
|
255 |
||
256 |
printOn:aStream |
|
257 |
||
258 |
|scheme| |
|
259 |
||
260 |
scheme := self scheme. |
|
261 |
scheme size ~~ 0 ifTrue:[ |
|
262 |
aStream nextPutAll:scheme; nextPut:$: |
|
263 |
]. |
|
1276 | 264 |
! |
265 |
||
266 |
publicPrintOn:aStream |
|
267 |
"print, but omit password information. |
|
268 |
Subclasses which know about secret information have to redefine this" |
|
269 |
||
270 |
^ self printOn:aStream |
|
1005 | 271 |
! ! |
272 |
||
273 |
!URI methodsFor:'subclass responsibility'! |
|
274 |
||
4881 | 275 |
readStreamDo:aBlock |
276 |
"evaluate a block with the read stream as first argument |
|
277 |
and a dictionary containing attributes as second argument. |
|
278 |
The stream is closed after aBlock has been evaluated." |
|
279 |
||
280 |
self subclassResponsibility |
|
281 |
||
282 |
"Created: / 18-03-2019 / 11:02:45 / Claus Gittinger" |
|
283 |
! |
|
284 |
||
1254 | 285 |
readStreamsDo:aBlock |
286 |
"evaluate the block with a Collection of streams as first argument |
|
2874 | 287 |
and a dictionary containing attributes as optional second argument, |
1254 | 288 |
- a collection with a stream on a single file, |
289 |
- or a collection with streams on a directorie's files, but not recursive |
|
290 |
||
291 |
The streams are closed after aBlock has been evaluated. |
|
292 |
Attributes may be the mime type (key #MIME)" |
|
293 |
||
294 |
^ self subclassResponsibility |
|
4881 | 295 |
|
296 |
"Modified (format): / 18-03-2019 / 11:02:48 / Claus Gittinger" |
|
1005 | 297 |
! ! |
298 |
||
1276 | 299 |
!URI methodsFor:'testing'! |
300 |
||
4586 | 301 |
isFileScheme |
302 |
^ self method isNil or:[self method = 'file'] |
|
303 |
! |
|
304 |
||
1276 | 305 |
isRemote |
306 |
"return true, if this is a remote URI" |
|
307 |
||
308 |
^ false |
|
309 |
! ! |
|
310 |
||
1005 | 311 |
!URI class methodsFor:'documentation'! |
312 |
||
313 |
version |
|
4307 | 314 |
^ '$Header$' |
1005 | 315 |
! ! |
2874 | 316 |