1005
|
1 |
"{ Package: 'stx:libbasic2' }"
|
|
2 |
|
|
3 |
HierarchicalURI subclass:#FileURI
|
|
4 |
instanceVariableNames:''
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Resources'
|
|
8 |
!
|
|
9 |
|
|
10 |
|
|
11 |
!FileURI class methodsFor:'instance creation'!
|
|
12 |
|
|
13 |
fromFilename:aFilename
|
|
14 |
"create an URI based on an a filename"
|
|
15 |
|
|
16 |
^ self new fromFilename:aFilename
|
|
17 |
! !
|
|
18 |
|
|
19 |
!FileURI class methodsFor:'accessing'!
|
|
20 |
|
|
21 |
schemes
|
|
22 |
"answer the list of supported schemes"
|
|
23 |
|
|
24 |
^ #(file)
|
|
25 |
! !
|
|
26 |
|
|
27 |
!FileURI methodsFor:'converting'!
|
|
28 |
|
|
29 |
asFilename
|
|
30 |
"answer the receiver represented as filename"
|
|
31 |
|
|
32 |
^ authority notNil ifTrue:[
|
|
33 |
Filename remoteHost:authority rootComponents:pathSegments.
|
|
34 |
] ifFalse:[
|
|
35 |
Filename rootComponents:pathSegments
|
|
36 |
].
|
|
37 |
! !
|
|
38 |
|
|
39 |
!FileURI methodsFor:'initialize'!
|
|
40 |
|
|
41 |
fromFilename:aFilename
|
|
42 |
"create an URI based on an a filename"
|
|
43 |
|
|
44 |
|components|
|
|
45 |
|
|
46 |
components := aFilename components.
|
|
47 |
aFilename isAbsolute ifTrue:[
|
|
48 |
(components size > 3 and:[(components at:2) size == 0]) ifTrue:[
|
|
49 |
"this is a MS-Windows network path: \\host\path"
|
|
50 |
authority := components at:3.
|
|
51 |
pathSegments := components copyFrom:4.
|
|
52 |
] ifFalse:[
|
|
53 |
"this is an absolute path"
|
|
54 |
isAbsolute := true.
|
|
55 |
pathSegments := components copyFrom:2.
|
|
56 |
].
|
|
57 |
] ifFalse:[
|
|
58 |
"this is a relative path"
|
|
59 |
isAbsolute := false.
|
|
60 |
pathSegments := components.
|
|
61 |
]
|
|
62 |
|
|
63 |
"
|
1258
|
64 |
self fromFilename:'/a/b/c' asFilename
|
|
65 |
self fromFilename:'//a/b/c' asFilename
|
|
66 |
self fromFilename:'a/b/c' asFilename
|
1005
|
67 |
"
|
|
68 |
! !
|
|
69 |
|
1254
|
70 |
!FileURI methodsFor:'queries'!
|
|
71 |
|
|
72 |
pathExists
|
|
73 |
|
|
74 |
^ self asFilename exists
|
|
75 |
! !
|
|
76 |
|
1005
|
77 |
!FileURI methodsFor:'stream access'!
|
|
78 |
|
|
79 |
readStream
|
|
80 |
|
|
81 |
^ self asFilename readStream
|
|
82 |
|
|
83 |
"
|
|
84 |
'file:/etc/group' asURI readStream contents
|
|
85 |
"
|
|
86 |
!
|
|
87 |
|
|
88 |
readStreamDo:aBlock
|
|
89 |
"evaluate a block with the read stream as first argument
|
|
90 |
and a dictionary containing attributes as second argument.
|
|
91 |
The stream is closed after aBlock has been evaluated."
|
|
92 |
|
1254
|
93 |
|attributes file stream|
|
1005
|
94 |
|
1254
|
95 |
attributes := self class attributes.
|
|
96 |
file := self asFilename.
|
|
97 |
attributes at:#fileSize put:(file fileSize).
|
|
98 |
attributes at:#baseName put:file baseName.
|
|
99 |
attributes at:#uriInfo put:self printString.
|
1005
|
100 |
|
1254
|
101 |
^ [
|
|
102 |
stream := file readStream.
|
1005
|
103 |
aBlock value:stream value:attributes
|
|
104 |
] ensure:[
|
|
105 |
stream notNil ifTrue:[stream close]
|
|
106 |
].
|
|
107 |
|
|
108 |
"
|
|
109 |
'file:/etc/group' asURI readStreamDo:[:stream :attributes|
|
|
110 |
stream contents
|
|
111 |
addFirst:attributes printString;
|
|
112 |
yourself
|
|
113 |
].
|
|
114 |
"
|
|
115 |
!
|
|
116 |
|
1254
|
117 |
readStreamsDo:aBlock
|
|
118 |
"evaluate the block with a Collection of streams as first argument
|
|
119 |
and a dictionary containing attributes as second argument,
|
|
120 |
- a collection with a stream on a single file,
|
|
121 |
- or a collection with streams on a directorie's files, but not recursive"
|
|
122 |
|
1258
|
123 |
self readStreamsDo:aBlock thenRemove:false.
|
|
124 |
!
|
|
125 |
|
|
126 |
readStreamsDo:aBlock thenRemove:doRemoveSource
|
|
127 |
"evaluate the block with a Collection of streams as first argument
|
|
128 |
and a dictionary containing attributes as second argument,
|
|
129 |
- a collection with a stream on a single file,
|
|
130 |
- or a collection with streams on a directorie's files, but not recursive"
|
|
131 |
|
1266
|
132 |
|attributes fn files list dirPath|
|
1254
|
133 |
|
|
134 |
fn := self asFilename.
|
|
135 |
files := OrderedCollection new.
|
1264
|
136 |
list := OrderedCollection new.
|
1254
|
137 |
attributes := self class attributes.
|
1264
|
138 |
|
1254
|
139 |
fn isDirectory ifTrue:[
|
1266
|
140 |
dirPath := fn pathName.
|
1254
|
141 |
attributes at:#requestDirectory put:true.
|
1266
|
142 |
(DirectoryContents directoryNamed:dirPath) filesDo:[:aFile|
|
1254
|
143 |
files add:aFile
|
|
144 |
].
|
|
145 |
] ifFalse:[
|
|
146 |
attributes at:#requestDirectory put:false.
|
|
147 |
files add:fn.
|
|
148 |
].
|
1005
|
149 |
|
1264
|
150 |
fn isDirectory ifFalse:[ |bName|
|
|
151 |
bName := fn baseName.
|
|
152 |
(bName startsWith:'*') ifTrue:[
|
|
153 |
files removeAll.
|
|
154 |
fn := fn directory.
|
1266
|
155 |
dirPath := fn pathName.
|
1264
|
156 |
attributes at:#requestDirectory put:true.
|
1266
|
157 |
(DirectoryContents directoryNamed:dirPath) filesDo:[:aFile|
|
1264
|
158 |
files add:aFile
|
|
159 |
].
|
|
160 |
].
|
|
161 |
(bName startsWith:'*.') ifTrue:[ |rest|
|
|
162 |
rest := bName restAfter:$*.
|
|
163 |
(rest includesString:'*') ifTrue:[
|
|
164 |
self error:'can''t resolve path:', self printString
|
|
165 |
].
|
|
166 |
files := files select:[:aFile| aFile pathName endsWith:rest ]
|
|
167 |
].
|
|
168 |
].
|
|
169 |
|
1254
|
170 |
files do:[:aFile| |baseName stream|
|
|
171 |
[
|
|
172 |
baseName := aFile baseName.
|
|
173 |
attributes at:#fileSize put:(aFile fileSize).
|
|
174 |
attributes at:#baseName put:baseName.
|
|
175 |
(self pathSegements includes:baseName) ifTrue:[
|
1258
|
176 |
attributes at:#uriInfo put:self.
|
1266
|
177 |
] ifFalse:[ |uri col|
|
|
178 |
uri := self copy.
|
|
179 |
col := self pathSegements copy.
|
|
180 |
col removeLast.
|
|
181 |
col add:baseName.
|
|
182 |
uri pathSegements:col.
|
|
183 |
attributes at:#uriInfo put:uri.
|
1254
|
184 |
].
|
|
185 |
stream := aFile readStream.
|
1258
|
186 |
aBlock value:stream value:attributes.
|
1254
|
187 |
] ensure:[
|
|
188 |
stream notNil ifTrue:[stream close]
|
|
189 |
].
|
1258
|
190 |
doRemoveSource == true ifTrue:[
|
|
191 |
aFile remove
|
|
192 |
].
|
1254
|
193 |
].
|
|
194 |
|
|
195 |
"
|
|
196 |
(URI fromString:'file:/home/tm/tmp')
|
|
197 |
readStreamsDo:[:stream :attributes |
|
|
198 |
Transcript showCR:(attributes at:#baseName).
|
|
199 |
Transcript showCR:(attributes at:#fileSize).
|
|
200 |
Transcript showCR:(attributes at:#requestDirectory).
|
|
201 |
Transcript showCR:(attributes at:#uriInfo).
|
|
202 |
].
|
|
203 |
"
|
|
204 |
!
|
|
205 |
|
|
206 |
writeStreamDo:aBlock
|
|
207 |
"evaluate a block with the write stream as first argument
|
|
208 |
and a dictionary containing attributes as second argument.
|
|
209 |
The stream is closed after aBlock has been evaluated.
|
|
210 |
Attributes may be the mime type (key #MIME)"
|
|
211 |
|
|
212 |
|stream|
|
|
213 |
|
|
214 |
^ [
|
|
215 |
stream := self asFilename writeStream.
|
|
216 |
aBlock value:stream value:self class attributes
|
|
217 |
] ensure:[
|
|
218 |
stream notNil ifTrue:[stream close]
|
|
219 |
].
|
|
220 |
|
|
221 |
"
|
|
222 |
(URI fromString:'file:/home/tm/tmp')
|
|
223 |
readStreamsDo:[:stream :attributes|
|
|
224 |
Transcript showCR:(attributes at:#MIME).
|
|
225 |
Transcript showCR:(stream isWritable).
|
|
226 |
].
|
|
227 |
"
|
1258
|
228 |
!
|
|
229 |
|
|
230 |
writeStreamDo:aBlock create:doCreate
|
|
231 |
"evaluate a block with the write stream as first argument
|
|
232 |
and a dictionary containing attributes as second argument.
|
|
233 |
The stream is closed after aBlock has been evaluated.
|
|
234 |
Attributes may be the mime type (key #MIME)"
|
|
235 |
|
|
236 |
|stream fileName|
|
|
237 |
|
|
238 |
fileName := self asFilename.
|
|
239 |
[
|
|
240 |
Stream streamErrorSignal handle:[:ex|
|
|
241 |
doCreate ifFalse:[
|
|
242 |
ex reject
|
|
243 |
].
|
|
244 |
(fileName directory) recursiveMakeDirectory.
|
|
245 |
stream := fileName writeStream.
|
|
246 |
] do:[
|
|
247 |
stream := fileName writeStream.
|
|
248 |
].
|
|
249 |
aBlock value:stream value:self class attributes
|
|
250 |
] ensure:[
|
|
251 |
stream notNil ifTrue:[stream close]
|
|
252 |
].
|
|
253 |
|
|
254 |
"
|
|
255 |
(URI fromString:'file:/home/tm/tmp')
|
|
256 |
readStreamsDo:[:stream :attributes|
|
|
257 |
Transcript showCR:(attributes at:#MIME).
|
|
258 |
Transcript showCR:(stream isWritable).
|
|
259 |
].
|
|
260 |
"
|
1005
|
261 |
! !
|
|
262 |
|
|
263 |
!FileURI class methodsFor:'documentation'!
|
|
264 |
|
|
265 |
version
|
1266
|
266 |
^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.5 2003-07-10 12:07:29 tm Exp $'
|
1005
|
267 |
! !
|