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.lethe.analysis;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisDataMap.ThemisAnalysisIntermediate;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 /**
26 * The set of imports.
27 */
28 public class ThemisAnalysisImports
29 implements ThemisAnalysisProcessed {
30 /**
31 * The imports.
32 */
33 private final List<ThemisAnalysisImport> theImports;
34
35 /**
36 * Constructor.
37 *
38 * @param pParser the parser
39 * @param pLine the initial import line
40 * @throws OceanusException on error
41 */
42 ThemisAnalysisImports(final ThemisAnalysisParser pParser,
43 final ThemisAnalysisLine pLine) throws OceanusException {
44 /* Create the list of comment lines */
45 theImports = new ArrayList<>();
46
47 /* Add import to list */
48 ThemisAnalysisImport myImport = new ThemisAnalysisImport(pLine.toString());
49 theImports.add(myImport);
50 final ThemisAnalysisDataMap myDataMap = pParser.getDataMap();
51 myDataMap.declareImport(myImport);
52
53 /* While there are further lines */
54 while (pParser.hasLines()) {
55 /* Access next line */
56 final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.peekNextLine();
57
58 /* It it is also an import */
59 if (isImport(myLine)) {
60 /* Add the import line and remove from input */
61 myImport = new ThemisAnalysisImport(myLine.toString());
62 theImports.add(myImport);
63 myDataMap.declareImport(myImport);
64 pParser.popNextLine();
65
66 /* else break loop */
67 } else {
68 break;
69 }
70 }
71 }
72
73 @Override
74 public int getNumLines() {
75 return theImports.size();
76 }
77
78 /**
79 * Is the line an import?
80 *
81 * @param pLine the line
82 * @return true/false
83 * @throws OceanusException on error
84 */
85 static boolean isImport(final ThemisAnalysisLine pLine) throws OceanusException {
86 /* If we are ended by a semi-colon and is an import line*/
87 if (pLine.endsWithChar(ThemisAnalysisChar.SEMICOLON)
88 && pLine.isStartedBy(ThemisAnalysisKeyWord.IMPORT.getKeyWord())) {
89 /* Strip the semi-colon and return true */
90 pLine.stripEndChar(ThemisAnalysisChar.SEMICOLON);
91 return true;
92 }
93
94 /* return false */
95 return false;
96 }
97
98 /**
99 * Import line.
100 */
101 static class ThemisAnalysisImport
102 implements ThemisAnalysisElement, ThemisAnalysisIntermediate {
103 /**
104 * The full name.
105 */
106 private final String theFullName;
107
108 /**
109 * The simple name.
110 */
111 private final String theSimpleName;
112
113 /**
114 * Constructor.
115 *
116 * @param pImport the import.
117 */
118 ThemisAnalysisImport(final String pImport) {
119 /* Store the full name */
120 theFullName = pImport;
121
122 /* Strip out the simple name */
123 final int myIndex = theFullName.lastIndexOf(ThemisAnalysisChar.PERIOD);
124 theSimpleName = myIndex == -1 ? theFullName : theFullName.substring(myIndex + 1);
125 }
126
127 /**
128 * Obtain the import.
129 *
130 * @return the import
131 */
132 public String getFullName() {
133 return theFullName;
134 }
135
136 /**
137 * Obtain the simple name.
138 *
139 * @return the simple name
140 */
141 public String getSimpleName() {
142 return theSimpleName;
143 }
144
145 @Override
146 public String toString() {
147 return getSimpleName();
148 }
149 }
150 }