1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.themis.parser.project;
18
19 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
20 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
21 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
22 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
23 import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
24 import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisClassInstance;
25 import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisNodeInstance;
26 import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
27 import io.github.tonywasher.joceanus.themis.parser.node.ThemisNodeCompilationUnit;
28
29 import java.io.File;
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34
35
36 public class ThemisFile
37 implements MetisFieldItem {
38
39
40
41 private static final MetisFieldSet<ThemisFile> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisFile.class);
42
43
44
45
46 static {
47 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_NAME, ThemisFile::getName);
48 FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_CLASSES, ThemisFile::getClasses);
49 }
50
51
52
53
54 public static final String SFX_JAVA = ".java";
55
56
57
58
59 private final File theLocation;
60
61
62
63
64 private final String theName;
65
66
67
68
69 private final List<ThemisClassInstance> theClasses;
70
71
72
73
74 private ThemisNodeCompilationUnit theContents;
75
76
77
78
79
80
81 ThemisFile(final File pFile) {
82
83 theLocation = pFile;
84 theName = pFile.getName().replace(SFX_JAVA, "");
85 theClasses = new ArrayList<>();
86 }
87
88 @Override
89 public MetisFieldSet<ThemisFile> getDataFieldSet() {
90 return FIELD_DEFS;
91 }
92
93 @Override
94 public String formatObject(final OceanusDataFormatter pFormatter) {
95 return toString();
96 }
97
98
99
100
101
102
103 public String getName() {
104 return theName;
105 }
106
107
108
109
110
111
112 public String getLocation() {
113 return theLocation.getAbsolutePath();
114 }
115
116
117
118
119
120
121 public ThemisNodeCompilationUnit getContents() {
122 return theContents;
123 }
124
125
126
127
128
129
130 public List<ThemisClassInstance> getClasses() {
131 return theClasses;
132 }
133
134 @Override
135 public String toString() {
136 return theName;
137 }
138
139
140
141
142
143
144
145 void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
146
147 pParser.setCurrentFile(theLocation);
148
149
150 theContents = (ThemisNodeCompilationUnit) pParser.parseJavaFile();
151
152
153 final ThemisClassInstance myClass = theContents.getContents();
154 if (!theName.equals(myClass.getName())) {
155 throw pParser.buildException("Incorrect name for class in file", ((ThemisNodeInstance) myClass).getNode());
156 }
157
158
159 theClasses.addAll(pParser.getClasses());
160 }
161 }