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.proj;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.parser.base.ThemisChar;
21  import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
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 ThemisPackage {
32      /**
33       * The path xtra.
34       */
35      static final String PATH_XTRA = ".src.main.java".replace(ThemisChar.PERIOD, File.separatorChar);
36  
37      /**
38       * The package-info file.
39       */
40      private static final String PACKAGE_INFO = "package-info" + ThemisFile.SFX_JAVA;
41  
42      /**
43       * The package name.
44       */
45      private final String thePackage;
46  
47      /**
48       * The short name.
49       */
50      private final String theShortName;
51  
52      /**
53       * The package name.
54       */
55      private final String theParentName;
56  
57      /**
58       * The location.
59       */
60      private final File theLocation;
61  
62      /**
63       * The list of files in this package.
64       */
65      private final List<ThemisFile> theFiles;
66  
67      /**
68       * Is this a standard package?
69       */
70      private final boolean isStandard;
71  
72      /**
73       * Is this a placeHolder package?
74       */
75      private final boolean isPlaceHolder;
76  
77      /**
78       * Constructor.
79       *
80       * @param pLocation the location
81       * @param pPackage  the package name.
82       */
83      ThemisPackage(final File pLocation,
84                    final String pPackage) {
85          /* Store package name */
86          theLocation = pLocation;
87          thePackage = pPackage;
88          isStandard = pLocation.getAbsolutePath().endsWith(PATH_XTRA);
89          isPlaceHolder = false;
90  
91          /* Determine the short name */
92          final int iIndex = thePackage.lastIndexOf(ThemisChar.PERIOD);
93          theShortName = iIndex == -1 ? thePackage : thePackage.substring(iIndex + 1);
94          theParentName = iIndex == -1 ? null : thePackage.substring(0, iIndex);
95  
96          /* Create directory path and record the location */
97          final String myPath = pPackage.replace(ThemisChar.PERIOD, File.separatorChar);
98          final File myLocation = new File(pLocation, myPath);
99  
100         /* Build list of files */
101         theFiles = listFiles(myLocation);
102     }
103 
104     /**
105      * Constructor.
106      *
107      * @param pPackage the package name.
108      */
109     public ThemisPackage(final String pPackage) {
110         /* Store package name */
111         theLocation = null;
112         thePackage = pPackage;
113         isStandard = false;
114         isPlaceHolder = true;
115 
116         /* Determine the short name */
117         final int iIndex = thePackage.lastIndexOf(ThemisChar.PERIOD);
118         theShortName = iIndex == -1 ? thePackage : thePackage.substring(iIndex + 1);
119         theParentName = iIndex == -1 ? null : thePackage.substring(0, iIndex);
120 
121         /* Build empty list of files */
122         theFiles = new ArrayList<>();
123     }
124 
125     /**
126      * Obtain the package.
127      *
128      * @return the package
129      */
130     public String getPackage() {
131         return thePackage;
132     }
133 
134     /**
135      * Obtain the shortName.
136      *
137      * @return the shortName
138      */
139     public String getShortName() {
140         return theShortName;
141     }
142 
143     /**
144      * Obtain the parentName.
145      *
146      * @return the parentName
147      */
148     public String getParentName() {
149         return theParentName;
150     }
151 
152     /**
153      * Is this the root package.
154      *
155      * @return true/false
156      */
157     public boolean isRoot() {
158         return theShortName.isEmpty();
159     }
160 
161     /**
162      * Obtain the location.
163      *
164      * @return the location
165      */
166     public File getLocation() {
167         return theLocation;
168     }
169 
170     /**
171      * Obtain the files.
172      *
173      * @return the files
174      */
175     public List<ThemisFile> getFiles() {
176         return theFiles;
177     }
178 
179     /**
180      * Is this a standard package?
181      *
182      * @return true/false
183      */
184     public boolean isStandard() {
185         return isStandard;
186     }
187 
188     /**
189      * Is this a placeHolder package?
190      *
191      * @return true/false
192      */
193     public boolean isPlaceHolder() {
194         return isPlaceHolder;
195     }
196 
197     /**
198      * Build list of files.
199      *
200      * @param pLocation the location
201      * @return the list of files
202      */
203     List<ThemisFile> listFiles(final File pLocation) {
204         /* Allocate the list */
205         final List<ThemisFile> myFiles = new ArrayList<>();
206 
207         /* Loop through the entries in the directory */
208         for (File myEntry : Objects.requireNonNull(pLocation.listFiles())) {
209             /* Handle files */
210             if (!myEntry.isDirectory()) {
211                 /* Access the name of the file */
212                 final String myName = myEntry.getName();
213 
214                 /* If this is a .java that is not package-info */
215                 if (myName.endsWith(ThemisFile.SFX_JAVA)
216                         && !PACKAGE_INFO.equals(myName)) {
217                     /* Add the class */
218                     final ThemisFile myFile = new ThemisFile(myEntry);
219                     myFiles.add(myFile);
220                 }
221             }
222         }
223 
224         /* Return the files */
225         return myFiles;
226     }
227 
228     /**
229      * parse the Java Code.
230      *
231      * @param pParser the parser
232      * @throws OceanusException on error
233      */
234     void parseJavaCode(final ThemisParserDef pParser) throws OceanusException {
235         /* Set the current package */
236         pParser.setCurrentPackage(thePackage);
237 
238         /* Loop through the classes */
239         for (ThemisFile myFile : theFiles) {
240             /* Process the file */
241             myFile.parseJavaCode(pParser);
242         }
243     }
244 
245     @Override
246     public String toString() {
247         return thePackage;
248     }
249 }