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