1 /*
2 * Themis: Java Project Framework
3 * Copyright 2012-2026. Tony Washer
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6 * use this file except in compliance with the License. You may obtain a copy
7 * of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 */
17 package io.github.tonywasher.joceanus.themis.parser.proj;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisClassInstance;
21 import io.github.tonywasher.joceanus.themis.parser.base.ThemisInstance.ThemisNodeInstance;
22 import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
23 import io.github.tonywasher.joceanus.themis.parser.node.ThemisNodeCompilationUnit;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 /**
30 * Analysis representation of a java file.
31 */
32 public class ThemisFile {
33 /**
34 * The java suffix.
35 */
36 public static final String SFX_JAVA = ".java";
37
38 /**
39 * The location of the file.
40 */
41 private final File theLocation;
42
43 /**
44 * The name of the file.
45 */
46 private final String theName;
47
48 /**
49 * The class list.
50 */
51 private final List<ThemisClassInstance> theClasses;
52
53 /**
54 * The contents.
55 */
56 private ThemisNodeCompilationUnit theContents;
57
58 /**
59 * Constructor.
60 *
61 * @param pFile the file to analyse
62 */
63 ThemisFile(final File pFile) {
64 /* Store the parameters */
65 theLocation = pFile;
66 theName = pFile.getName().replace(SFX_JAVA, "");
67 theClasses = new ArrayList<>();
68 }
69
70 /**
71 * Obtain the name of the fileClass.
72 *
73 * @return the name
74 */
75 public String getName() {
76 return theName;
77 }
78
79 /**
80 * Obtain the location of the fileClass.
81 *
82 * @return the location
83 */
84 public String getLocation() {
85 return theLocation.getAbsolutePath();
86 }
87
88 /**
89 * Obtain the contents.
90 *
91 * @return the contents
92 */
93 public ThemisNodeCompilationUnit getContents() {
94 return theContents;
95 }
96
97 /**
98 * Obtain the classList.
99 *
100 * @return the classList
101 */
102 public List<ThemisClassInstance> getClasses() {
103 return theClasses;
104 }
105
106 @Override
107 public String toString() {
108 return theName;
109 }
110
111 /**
112 * Process the file.
113 *
114 * @param pParser the parser
115 * @throws OceanusException on error
116 */
117 void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
118 /* Set the current file */
119 pParser.setCurrentFile(theLocation);
120
121 /* Parse the file */
122 theContents = (ThemisNodeCompilationUnit) pParser.parseJavaFile();
123
124 /* Check that we have a class that is the same name as the file */
125 final ThemisClassInstance myClass = theContents.getContents();
126 if (!theName.equals(myClass.getName())) {
127 throw pParser.buildException("Incorrect name for class in file", ((ThemisNodeInstance) myClass).getNode());
128 }
129
130 /* Obtain a copy of the classList from the parser */
131 theClasses.addAll(pParser.getClasses());
132 }
133 }