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