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.oceanus.profile.OceanusProfile;
24  import io.github.tonywasher.joceanus.tethys.api.thread.TethysUIThreadStatusReport;
25  import io.github.tonywasher.joceanus.themis.parser.base.ThemisDataResource;
26  import io.github.tonywasher.joceanus.themis.parser.base.ThemisParserDef;
27  import io.github.tonywasher.joceanus.themis.parser.maven.ThemisMavenId;
28  import io.github.tonywasher.joceanus.themis.parser.maven.ThemisMavenParser;
29  import io.github.tonywasher.joceanus.themis.parser.maven.ThemisMavenPom;
30  
31  import java.io.File;
32  import java.util.ArrayList;
33  import java.util.LinkedHashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * Project.
39   */
40  public class ThemisProject
41          implements MetisFieldItem {
42      /**
43       * Report fields.
44       */
45      private static final MetisFieldSet<ThemisProject> FIELD_DEFS = MetisFieldSet.newFieldSet(ThemisProject.class);
46  
47      /*
48       * Declare Fields.
49       */
50      static {
51          FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_NAME, ThemisProject::getName);
52          FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_MODULES, ThemisProject::getModules);
53          FIELD_DEFS.declareLocalField(ThemisDataResource.DATA_PARSEDMODULES, ThemisProject::getParsedModules);
54      }
55  
56      /**
57       * The project name.
58       */
59      private final String theName;
60  
61      /**
62       * The location.
63       */
64      private final File theLocation;
65  
66      /**
67       * The parser.
68       */
69      private final ThemisParserDef theParser;
70  
71      /**
72       * The module map.
73       */
74      private final Map<ThemisMavenId, ThemisModule> theModules;
75  
76      /**
77       * The dependencies.
78       */
79      private final List<ThemisMavenId> theDependencies;
80  
81      /**
82       * The list of local modules.
83       */
84      private final List<ThemisMavenPom> theParsedModules;
85  
86      /**
87       * Constructor.
88       *
89       * @param pParser   the parser
90       * @param pLocation the project location
91       * @throws OceanusException on error
92       */
93      public ThemisProject(final ThemisParserDef pParser,
94                           final File pLocation) throws OceanusException {
95          /* Store the parser and location */
96          theParser = pParser;
97          theLocation = pLocation;
98  
99          /* Create the list */
100         theModules = new LinkedHashMap<>();
101 
102         /* Build new Maven Map */
103         final ThemisMavenParser myParser = new ThemisMavenParser(theParser.getReporter(), theLocation);
104         theName = myParser.getName();
105         theParsedModules = myParser.getParsedModules();
106         theDependencies = myParser.getProjectDependencies();
107         for (ThemisMavenPom myModule : theParsedModules) {
108             theModules.put(myModule.getId(), new ThemisModule(myModule));
109         }
110 
111         /* Remove own mavenIds from dependency list */
112         theDependencies.removeIf(theModules::containsKey);
113     }
114 
115     @Override
116     public MetisFieldSet<ThemisProject> getDataFieldSet() {
117         return FIELD_DEFS;
118     }
119 
120     @Override
121     public String formatObject(final OceanusDataFormatter pFormatter) {
122         return toString();
123     }
124 
125     /**
126      * Obtain the name.
127      *
128      * @return the name
129      */
130     public String getName() {
131         return theName;
132     }
133 
134     /**
135      * Obtain the location.
136      *
137      * @return the location
138      */
139     File getLocation() {
140         return theLocation;
141     }
142 
143     /**
144      * Obtain the modules.
145      *
146      * @return the modules
147      */
148     public List<ThemisModule> getModules() {
149         return new ArrayList<>(theModules.values());
150     }
151 
152     /**
153      * Obtain the modules.
154      *
155      * @return the modules
156      */
157     private List<ThemisMavenPom> getParsedModules() {
158         return theParsedModules;
159     }
160 
161     /**
162      * Obtain the dependencies.
163      *
164      * @return the dependencies
165      */
166     public List<ThemisMavenId> getDependencies() {
167         return theDependencies;
168     }
169 
170     @Override
171     public String toString() {
172         return theName;
173     }
174 
175     /**
176      * parse the java code.
177      *
178      * @throws OceanusException on error
179      */
180     public void parseJavaCode() throws OceanusException {
181         /* Obtain the reporter */
182         final TethysUIThreadStatusReport myReport = theParser.getReporter();
183         myReport.initTask(ThemisDataResource.TASK_PARSECODE);
184         myReport.setNumStages(theModules.size());
185 
186         /* Obtain the active profile */
187         OceanusProfile myTask = myReport.getActiveTask();
188         myTask = myTask.startTask(ThemisDataResource.TASK_PARSECODE);
189 
190         /* Loop through the modules */
191         for (ThemisModule myModule : theModules.values()) {
192             /* Process the module */
193             final String myName = myModule.getName();
194             myTask.startTask(myName);
195             myReport.setNewStage(myName);
196             myReport.setNumSteps(1);
197             myReport.setNextStep();
198             myModule.parseJavaCode(theParser);
199         }
200 
201         /* End the task */
202         myTask.end();
203     }
204 }