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.xanalysis.parser.proj;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisChar;
21  import io.github.tonywasher.joceanus.themis.xanalysis.parser.base.ThemisXAnalysisParserDef;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Objects;
27  
28  /**
29   * Package.
30   */
31  public class ThemisXAnalysisPackage {
32      /**
33       * The package-info file.
34       */
35      private static final String PACKAGE_INFO = "package-info" + ThemisXAnalysisFile.SFX_JAVA;
36  
37      /**
38       * The package name.
39       */
40      private final String thePackage;
41  
42      /**
43       * The location.
44       */
45      private final File theLocation;
46  
47      /**
48       * The list of files in this package.
49       */
50      private final List<ThemisXAnalysisFile> theFiles;
51  
52      /**
53       * Constructor.
54       *
55       * @param pLocation the location
56       * @param pPackage  the package name.
57       * @throws OceanusException on error
58       */
59      ThemisXAnalysisPackage(final File pLocation,
60                             final String pPackage) throws OceanusException {
61          /* Store package name */
62          theLocation = pLocation;
63          thePackage = pPackage;
64  
65          /* Create directory path and record the location */
66          final String myPath = pPackage.replace(ThemisXAnalysisChar.PERIOD, File.separatorChar);
67          final File myLocation = new File(pLocation, myPath);
68  
69          /* Build list of files */
70          theFiles = listFiles(myLocation);
71      }
72  
73      /**
74       * Obtain the package.
75       *
76       * @return the package
77       */
78      public String getPackage() {
79          return thePackage;
80      }
81  
82      /**
83       * Obtain the location.
84       *
85       * @return the location
86       */
87      public File getLocation() {
88          return theLocation;
89      }
90  
91      /**
92       * Obtain the files.
93       *
94       * @return the files
95       */
96      public List<ThemisXAnalysisFile> getFiles() {
97          return theFiles;
98      }
99  
100     /**
101      * Build list of files.
102      *
103      * @param pLocation the location
104      * @return the list of files
105      */
106     List<ThemisXAnalysisFile> listFiles(final File pLocation) {
107         /* Allocate the list */
108         final List<ThemisXAnalysisFile> myClasses = new ArrayList<>();
109 
110         /* Loop through the entries in the directory */
111         for (File myFile : Objects.requireNonNull(pLocation.listFiles())) {
112             /* Handle files */
113             if (!myFile.isDirectory()) {
114                 /* Access the name of the file */
115                 final String myName = myFile.getName();
116 
117                 /* If this is a .java that is not package-info */
118                 if (myName.endsWith(ThemisXAnalysisFile.SFX_JAVA)
119                         && !PACKAGE_INFO.equals(myName)) {
120                     /* Add the class */
121                     final ThemisXAnalysisFile myClass = new ThemisXAnalysisFile(myFile);
122                     myClasses.add(myClass);
123                 }
124             }
125         }
126 
127         /* Return the classes */
128         return myClasses;
129     }
130 
131     /**
132      * parse the Java Code.
133      *
134      * @param pParser the parser
135      * @throws OceanusException on error
136      */
137     void parseJavaCode(final ThemisXAnalysisParserDef pParser) throws OceanusException {
138         /* Set the current package */
139         pParser.setCurrentPackage(thePackage);
140 
141         /* Loop through the classes */
142         for (ThemisXAnalysisFile myFile : theFiles) {
143             /* Process the file */
144             myFile.parseJavaCode(pParser);
145         }
146     }
147 
148     @Override
149     public String toString() {
150         return thePackage;
151     }
152 }