@@ -26,8 +26,61 @@ var FileUtils = {
26
26
. getService ( Components . interfaces . nsIProperties )
27
27
. get ( "TmpD" , Components . interfaces . nsILocalFile ) ;
28
28
} ,
29
-
30
- getUnicodeConverter : function ( encoding ) {
29
+
30
+ getFile : function ( path ) {
31
+ var file = Components . classes [ '@mozilla.org/file/local;1' ] . createInstance ( Components . interfaces . nsILocalFile ) ;
32
+ file . initWithPath ( path ) ;
33
+ return file ;
34
+ } ,
35
+
36
+ fileExists : function ( path ) {
37
+ return path !== null && path . length > 0 && this . getFile ( path ) . exists ( ) ;
38
+ } ,
39
+
40
+ createFolder : function ( folder ) {
41
+ if ( folder && ! folder . exists ( ) ) {
42
+ folder . create ( Components . interfaces . nsIFile . DIRECTORY_TYPE , 0755 ) ;
43
+ }
44
+ return folder ;
45
+ } ,
46
+
47
+ getSeleniumIDEFolder : function ( relativePath , create ) {
48
+ var folder = this . getProfileDir ( ) ;
49
+ folder . append ( "selenium-ide" ) ;
50
+ if ( relativePath ) {
51
+ folder . appendRelativePath ( relativePath ) ;
52
+ }
53
+ return create ? this . createFolder ( folder ) : folder ;
54
+ } ,
55
+
56
+ getStoredFile : function ( relativePath , prefix , suffix ) {
57
+ var folder = this . getSeleniumIDEFolder ( relativePath , true ) ;
58
+ var filename = ( prefix || '' ) + ( suffix || '' ) ;
59
+ if ( filename . length > 0 ) {
60
+ folder . append ( filename ) ;
61
+ }
62
+ return folder ;
63
+ } ,
64
+
65
+ _pad2 : function ( num ) {
66
+ return num < 10 ? '0' + num : num ;
67
+ } ,
68
+
69
+ getTimeStamp : function ( ) {
70
+ var d = new Date ( ) ;
71
+ return d . getFullYear ( ) +
72
+ '-' + this . _pad2 ( d . getMonth ( ) + 1 ) +
73
+ '-' + this . _pad2 ( d . getDate ( ) ) +
74
+ '_' + this . _pad2 ( d . getHours ( ) ) +
75
+ '-' + this . _pad2 ( d . getMinutes ( ) ) +
76
+ '-' + this . _pad2 ( d . getSeconds ( ) ) ;
77
+ } ,
78
+
79
+ getStoredTimeStampedFile : function ( relativePath , prefix , suffix ) {
80
+ return this . getStoredFile ( relativePath , ( prefix || '' ) + this . getTimeStamp ( ) , suffix ) ;
81
+ } ,
82
+
83
+ getUnicodeConverter : function ( encoding ) {
31
84
var unicodeConverter = Components . classes [ "@mozilla.org/intl/scriptableunicodeconverter" ] . createInstance ( Components . interfaces . nsIScriptableUnicodeConverter ) ;
32
85
try {
33
86
unicodeConverter . charset = encoding ;
@@ -36,20 +89,40 @@ var FileUtils = {
36
89
}
37
90
return unicodeConverter ;
38
91
} ,
39
-
40
- openFileOutputStream : function ( file ) {
92
+
93
+ openFileOutputStream : function ( file ) {
41
94
var stream = Components . classes [ "@mozilla.org/network/file-output-stream;1" ] . createInstance ( Components . interfaces . nsIFileOutputStream ) ;
42
95
stream . init ( file , 0x02 | 0x08 | 0x20 , 420 , 0 ) ;
43
96
return stream ;
44
97
} ,
45
98
46
- openFileInputStream : function ( file ) {
47
- var stream = Components . classes [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Components . interfaces . nsIFileInputStream ) ;
48
- stream . init ( file , 0x01 , 00004 , 0 ) ;
49
- var sis = Components . classes [ "@mozilla.org/scriptableinputstream;1" ] . createInstance ( Components . interfaces . nsIScriptableInputStream ) ;
50
- sis . init ( stream ) ;
51
- return sis ;
52
- } ,
99
+ openUTF8FileOutputStream : function ( file ) {
100
+ var converter = Components . classes [ "@mozilla.org/intl/converter-output-stream;1" ] . createInstance ( Components . interfaces . nsIConverterOutputStream ) ;
101
+ converter . init ( this . openFileOutputStream ( file ) , "UTF-8" , 0 , 0 ) ;
102
+ return converter ;
103
+ } ,
104
+
105
+ writeUTF8nsIFile : function ( file , data ) {
106
+ this . createFolder ( file . parent ) ;
107
+ var converter = this . openUTF8FileOutputStream ( file ) ;
108
+ converter . writeString ( data ) ;
109
+ converter . close ( ) ;
110
+ return file . path ;
111
+ } ,
112
+
113
+ writeUTF8File : function ( filename , data ) {
114
+ return this . writeUTF8nsIFile ( this . getFile ( filename ) , data ) ;
115
+ } ,
116
+
117
+ writeStoredFile : function ( filename , data ) {
118
+ //StoredFiles are stored in the selenium ide folder under the Firefox profile
119
+ return this . writeUTF8nsIFile ( this . getSeleniumIDEFolder ( filename ) , data ) ;
120
+ } ,
121
+
122
+ writeJSONStoredFile : function ( filename , data ) {
123
+ //StoredFiles are stored in the selenium ide folder under the Firefox profile
124
+ return this . writeStoredFile ( filename , JSON . stringify ( data ) ) ;
125
+ } ,
53
126
54
127
openURLInputStream : function ( url ) {
55
128
const ioService = Components . classes [ '@mozilla.org/network/io-service;1' ] . getService ( Components . interfaces . nsIIOService ) ;
@@ -59,35 +132,53 @@ var FileUtils = {
59
132
return sis ;
60
133
} ,
61
134
135
+ readURL : function ( url ) {
136
+ var stream = this . openURLInputStream ( url ) ;
137
+ var content = stream . read ( stream . available ( ) ) ;
138
+ stream . close ( ) ;
139
+ return content ;
140
+ } ,
141
+
142
+ openFileInputStream : function ( file ) {
143
+ var stream = Components . classes [ "@mozilla.org/network/file-input-stream;1" ] . createInstance ( Components . interfaces . nsIFileInputStream ) ;
144
+ stream . init ( file , 0x01 , 00004 , 0 ) ;
145
+ var sis = Components . classes [ "@mozilla.org/scriptableinputstream;1" ] . createInstance ( Components . interfaces . nsIScriptableInputStream ) ;
146
+ sis . init ( stream ) ;
147
+ return sis ;
148
+ } ,
149
+
62
150
readFile : function ( file ) {
63
151
var stream = this . openFileInputStream ( file ) ;
64
152
var content = stream . read ( stream . available ( ) ) ;
65
153
stream . close ( ) ;
66
154
return content ;
67
155
} ,
68
156
69
- readURL : function ( url ) {
70
- var stream = this . openURLInputStream ( url ) ;
71
- var content = stream . read ( stream . available ( ) ) ;
72
- stream . close ( ) ;
73
- return content ;
74
- } ,
75
-
76
- getFile : function ( path ) {
77
- var file = Components . classes [ '@mozilla.org/file/local;1' ] . createInstance ( Components . interfaces . nsILocalFile ) ;
78
- file . initWithPath ( path ) ;
79
- return file ;
80
- } ,
157
+ readStoredFile : function ( filename , defaultData ) {
158
+ //StoredFiles are stored in the selenium ide folder under the Firefox profile
159
+ var file = this . getSeleniumIDEFolder ( filename ) ;
160
+ if ( file . exists ( ) ) {
161
+ var conv = this . getUnicodeConverter ( "UTF-8" ) ;
162
+ return conv . ConvertToUnicode ( this . readFile ( file ) ) ;
163
+ }
164
+ return defaultData ;
165
+ } ,
81
166
82
- fileExists : function ( path ) {
83
- return path !== null && path . length > 0 && this . getFile ( path ) . exists ( ) ;
84
- } ,
167
+ readJSONStoredFile : function ( filename , defaultData ) {
168
+ //StoredFiles are stored in the selenium ide folder under the Firefox profile
169
+ var data = this . readStoredFile ( filename ) ;
170
+ return data ? JSON . parse ( data ) : defaultData ;
171
+ } ,
85
172
86
173
fileURI : function ( file ) {
87
174
return Components . classes [ "@mozilla.org/network/io-service;1" ] . getService ( Components . interfaces . nsIIOService ) .
88
175
newFileURI ( file ) . spec ;
89
176
} ,
90
177
178
+ getSafeFilename : function ( filename ) {
179
+ return filename ? filename . replace ( / [ ^ _ ~ , . 0 - 9 A - Z a - z - ] / g, '-' ) : '' ;
180
+ } ,
181
+
91
182
splitPath : function ( file ) {
92
183
var max = 100 ;
93
184
var result = [ ] ;
@@ -139,4 +230,4 @@ var FileUtils = {
139
230
mergedFile . appendRelativePath ( cleanPath . join ( fileSep ) ) ;
140
231
return mergedFile ;
141
232
}
142
- }
233
+ } ;
0 commit comments