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