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 |
|
1254
|
132 |
|attributes fn files|
|
|
133 |
|
|
134 |
fn := self asFilename.
|
|
135 |
files := OrderedCollection new.
|
|
136 |
attributes := self class attributes.
|
|
137 |
fn isDirectory ifTrue:[
|
|
138 |
attributes at:#requestDirectory put:true.
|
|
139 |
(DirectoryContents directoryNamed:fn pathName) filesDo:[:aFile|
|
|
140 |
files add:aFile
|
|
141 |
].
|
|
142 |
] ifFalse:[
|
|
143 |
attributes at:#requestDirectory put:false.
|
|
144 |
files add:fn.
|
|
145 |
].
|
1005
|
146 |
|
1254
|
147 |
files do:[:aFile| |baseName stream|
|
|
148 |
[
|
|
149 |
baseName := aFile baseName.
|
|
150 |
attributes at:#fileSize put:(aFile fileSize).
|
|
151 |
attributes at:#baseName put:baseName.
|
|
152 |
(self pathSegements includes:baseName) ifTrue:[
|
1258
|
153 |
attributes at:#uriInfo put:self.
|
1254
|
154 |
] ifFalse:[
|
1258
|
155 |
attributes at:#uriInfo put:((self copy) addComponent:baseName).
|
1254
|
156 |
].
|
|
157 |
stream := aFile readStream.
|
1258
|
158 |
aBlock value:stream value:attributes.
|
1254
|
159 |
] ensure:[
|
|
160 |
stream notNil ifTrue:[stream close]
|
|
161 |
].
|
1258
|
162 |
doRemoveSource == true ifTrue:[
|
|
163 |
aFile remove
|
|
164 |
].
|
1254
|
165 |
].
|
|
166 |
|
|
167 |
"
|
|
168 |
(URI fromString:'file:/home/tm/tmp')
|
|
169 |
readStreamsDo:[:stream :attributes |
|
|
170 |
Transcript showCR:(attributes at:#baseName).
|
|
171 |
Transcript showCR:(attributes at:#fileSize).
|
|
172 |
Transcript showCR:(attributes at:#requestDirectory).
|
|
173 |
Transcript showCR:(attributes at:#uriInfo).
|
|
174 |
].
|
|
175 |
"
|
|
176 |
!
|
|
177 |
|
|
178 |
writeStreamDo:aBlock
|
|
179 |
"evaluate a block with the write stream as first argument
|
|
180 |
and a dictionary containing attributes as second argument.
|
|
181 |
The stream is closed after aBlock has been evaluated.
|
|
182 |
Attributes may be the mime type (key #MIME)"
|
|
183 |
|
|
184 |
|stream|
|
|
185 |
|
|
186 |
^ [
|
|
187 |
stream := self asFilename writeStream.
|
|
188 |
aBlock value:stream value:self class attributes
|
|
189 |
] ensure:[
|
|
190 |
stream notNil ifTrue:[stream close]
|
|
191 |
].
|
|
192 |
|
|
193 |
"
|
|
194 |
(URI fromString:'file:/home/tm/tmp')
|
|
195 |
readStreamsDo:[:stream :attributes|
|
|
196 |
Transcript showCR:(attributes at:#MIME).
|
|
197 |
Transcript showCR:(stream isWritable).
|
|
198 |
].
|
|
199 |
"
|
1258
|
200 |
!
|
|
201 |
|
|
202 |
writeStreamDo:aBlock create:doCreate
|
|
203 |
"evaluate a block with the write stream as first argument
|
|
204 |
and a dictionary containing attributes as second argument.
|
|
205 |
The stream is closed after aBlock has been evaluated.
|
|
206 |
Attributes may be the mime type (key #MIME)"
|
|
207 |
|
|
208 |
|stream fileName|
|
|
209 |
|
|
210 |
fileName := self asFilename.
|
|
211 |
[
|
|
212 |
Stream streamErrorSignal handle:[:ex|
|
|
213 |
doCreate ifFalse:[
|
|
214 |
ex reject
|
|
215 |
].
|
|
216 |
(fileName directory) recursiveMakeDirectory.
|
|
217 |
stream := fileName writeStream.
|
|
218 |
] do:[
|
|
219 |
stream := fileName writeStream.
|
|
220 |
].
|
|
221 |
aBlock value:stream value:self class attributes
|
|
222 |
] ensure:[
|
|
223 |
stream notNil ifTrue:[stream close]
|
|
224 |
].
|
|
225 |
|
|
226 |
"
|
|
227 |
(URI fromString:'file:/home/tm/tmp')
|
|
228 |
readStreamsDo:[:stream :attributes|
|
|
229 |
Transcript showCR:(attributes at:#MIME).
|
|
230 |
Transcript showCR:(stream isWritable).
|
|
231 |
].
|
|
232 |
"
|
1005
|
233 |
! !
|
|
234 |
|
|
235 |
!FileURI class methodsFor:'documentation'!
|
|
236 |
|
|
237 |
version
|
1258
|
238 |
^ '$Header: /cvs/stx/stx/libbasic2/FileURI.st,v 1.3 2003-07-09 12:55:42 tm Exp $'
|
1005
|
239 |
! !
|