View Javadoc
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.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   * Analysis representation of a java file.
35   */
36  public class ThemisFile
37          implements MetisFieldItem {
38      /**
39       * Report fields.
40       */
41      private static final MetisFieldSet<ThemisFile> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisFile.class);
42  
43      /*
44       * Declare Fields.
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       * The java suffix.
53       */
54      public static final String SFX_JAVA = ".java";
55  
56      /**
57       * The location of the file.
58       */
59      private final File theLocation;
60  
61      /**
62       * The name of the file.
63       */
64      private final String theName;
65  
66      /**
67       * The class list.
68       */
69      private final List<ThemisClassInstance> theClasses;
70  
71      /**
72       * The contents.
73       */
74      private ThemisNodeCompilationUnit theContents;
75  
76      /**
77       * Constructor.
78       *
79       * @param pFile the file to analyse
80       */
81      ThemisFile(final File pFile) {
82          /* Store the parameters */
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       * Obtain the name of the fileClass.
100      *
101      * @return the name
102      */
103     public String getName() {
104         return theName;
105     }
106 
107     /**
108      * Obtain the location of the fileClass.
109      *
110      * @return the location
111      */
112     public String getLocation() {
113         return theLocation.getAbsolutePath();
114     }
115 
116     /**
117      * Obtain the contents.
118      *
119      * @return the contents
120      */
121     public ThemisNodeCompilationUnit getContents() {
122         return theContents;
123     }
124 
125     /**
126      * Obtain the classList.
127      *
128      * @return the classList
129      */
130     public List<ThemisClassInstance> getClasses() {
131         return theClasses;
132     }
133 
134     @Override
135     public String toString() {
136         return theName;
137     }
138 
139     /**
140      * Process the file.
141      *
142      * @param pParser the parser
143      * @throws OceanusException on error
144      */
145     void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
146         /* Set the current file */
147         pParser.setCurrentFile(theLocation);
148 
149         /* Parse the file */
150         theContents = (ThemisNodeCompilationUnit) pParser.parseJavaFile();
151 
152         /* Check that we have a class that is the same name as the file */
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         /* Obtain a copy of the classList from the parser */
159         theClasses.addAll(pParser.getClasses());
160     }
161 }