1
1
package org .ternlang .studio .project ;
2
2
3
+ import org .simpleframework .http .Path ;
4
+ import org .ternlang .studio .common .FileDirectory ;
5
+
3
6
import java .io .File ;
4
7
import java .util .ArrayList ;
5
8
import java .util .Collections ;
6
9
import java .util .List ;
7
10
8
- import org .simpleframework .http .Path ;
9
- import org .ternlang .studio .common .FileDirectory ;
10
-
11
11
public class FileSystem {
12
12
13
13
private final FileDirectory directory ;
14
-
14
+
15
15
public FileSystem (FileDirectory directory ) {
16
16
this .directory = directory ;
17
17
}
18
-
18
+
19
19
public File getFile (Path path ) {
20
20
String projectPath = path .getPath (2 ); // /<project-name>/<project-path> or /default/blah.tern
21
21
File rootPath = directory .getBasePath ();
22
22
String realPath = projectPath .replace ('/' , File .separatorChar );
23
23
return new File (rootPath , realPath );
24
24
}
25
-
25
+
26
26
public File getFile (String path ) {
27
27
File rootPath = directory .getBasePath ();
28
28
String realPath = path .replace ('/' , File .separatorChar );
29
29
return new File (rootPath , realPath );
30
30
}
31
-
31
+
32
32
public void writeAsString (String path , String resource ) throws Exception {
33
33
byte [] octets = resource .getBytes ("UTF-8" );
34
34
writeAsByteArray (path , octets );
35
35
}
36
-
36
+
37
37
public void writeAsByteArray (String path , byte [] resource ) throws Exception {
38
38
File rootPath = directory .getBasePath ();
39
39
FilePersister .writeAsByteArray (rootPath , path , resource );
40
40
}
41
-
41
+
42
42
public String readAsString (String path ) throws Exception {
43
43
File rootPath = directory .getBasePath ();
44
44
return FilePersister .readAsString (rootPath , path );
45
45
}
46
-
46
+
47
47
public byte [] readAsByteArray (String path ) throws Exception {
48
48
File rootPath = directory .getBasePath ();
49
49
return FilePersister .readAsByteArray (rootPath , path );
50
50
}
51
-
51
+
52
52
public FileData readFile (Path path ) throws Exception {
53
53
String projectPath = path .getPath (2 ); // /<project-name>/<project-path> or /default/blah.tern
54
54
return readFile (projectPath );
55
55
}
56
-
56
+
57
57
public FileData readFile (String path ) throws Exception {
58
58
long time = System .currentTimeMillis ();
59
59
File rootPath = directory .getBasePath ();
60
60
String realPath = path .replace ('/' , File .separatorChar );
61
61
File projectFile = new File (rootPath , realPath );
62
-
63
- if (projectFile .exists ()) {
62
+
63
+ if (projectFile .exists ()) {
64
64
String canonicalPath = projectFile .getCanonicalPath ();
65
-
66
- if (canonicalPath .endsWith (realPath )) {
65
+
66
+ if (canonicalPath .endsWith (realPath )) {
67
67
return new FileData (this , path , projectFile , time );
68
68
}
69
- }
69
+ }
70
70
return new FileData (this , path , null , time );
71
71
}
72
-
72
+
73
73
public FileData readFile (File file ) throws Exception {
74
74
long time = System .currentTimeMillis ();
75
75
File rootPath = directory .getBasePath ();
76
76
String basePath = rootPath .getCanonicalPath ();
77
77
String absolutePath = file .getCanonicalPath ();
78
78
String relativePath = absolutePath .replace (basePath , "" ).replace (File .separatorChar , '/' );
79
-
80
- if (file .exists ()) {
79
+
80
+ if (file .exists ()) {
81
81
return new FileData (this , relativePath , file , time );
82
- }
82
+ }
83
83
return new FileData (this , relativePath , null , time );
84
84
}
85
-
85
+
86
86
public List <FileData > readFiles (File file ) throws Exception {
87
87
File [] list = file .listFiles ();
88
-
89
- if (list != null ) {
88
+
89
+ if (list != null ) {
90
90
List <FileData > result = new ArrayList <FileData >(list .length );
91
-
92
- for (int i = 0 ; i < list .length ; i ++) {
91
+
92
+ for (int i = 0 ; i < list .length ; i ++) {
93
93
FileData fileData = readFile (list [i ]);
94
94
result .add (fileData );
95
95
}
96
96
return Collections .unmodifiableList (result );
97
- }
97
+ }
98
98
return Collections .emptyList ();
99
-
99
+
100
100
}
101
-
102
101
}
0 commit comments