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 |
|
1254
|
213 |
pathExists
|
|
214 |
|
|
215 |
"returns true if path exists"
|
|
216 |
|
|
217 |
^ self subclassResponsibility
|
|
218 |
!
|
|
219 |
|
1005
|
220 |
readStreamDo:aBlock
|
|
221 |
|
|
222 |
"evaluate a block with the read stream as first argument
|
|
223 |
and a dictionary containing attributes as second argument.
|
|
224 |
The stream is closed after aBlock has been evaluated.
|
|
225 |
Attributes may be the mime type (key #MIME)"
|
|
226 |
|
|
227 |
^ self subclassResponsibility
|
1254
|
228 |
!
|
|
229 |
|
|
230 |
readStreamsDo:aBlock
|
|
231 |
|
|
232 |
"evaluate the block with a Collection of streams as first argument
|
|
233 |
and a dictionary containing attributes as second argument,
|
|
234 |
- a collection with a stream on a single file,
|
|
235 |
- or a collection with streams on a directorie's files, but not recursive
|
|
236 |
|
|
237 |
The streams are closed after aBlock has been evaluated.
|
|
238 |
Attributes may be the mime type (key #MIME)"
|
|
239 |
|
|
240 |
^ self subclassResponsibility
|
|
241 |
!
|
|
242 |
|
|
243 |
writeStreamDo:aBlock
|
|
244 |
|
|
245 |
"evaluate a block with the write stream as first argument
|
|
246 |
and a dictionary containing attributes as second argument.
|
|
247 |
The stream is closed after aBlock has been evaluated.
|
|
248 |
Attributes may be the mime type (key #MIME)"
|
|
249 |
|
|
250 |
^ self subclassResponsibility
|
1005
|
251 |
! !
|
|
252 |
|
|
253 |
!URI class methodsFor:'documentation'!
|
|
254 |
|
|
255 |
version
|
1268
|
256 |
^ '$Header: /cvs/stx/stx/libbasic2/URI.st,v 1.6 2003-07-11 12:47:46 stefan Exp $'
|
1005
|
257 |
! !
|