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
21 import java.util.ArrayDeque;
22 import java.util.ArrayList;
23 import java.util.Deque;
24 import java.util.List;
25
26 /**
27 * Case construct.
28 */
29 public class ThemisAnalysisCase
30 implements ThemisAnalysisContainer {
31 /**
32 * The parent.
33 */
34 private final ThemisAnalysisContainer theParent;
35
36 /**
37 * The contents.
38 */
39 private final Deque<ThemisAnalysisElement> theContents;
40
41 /**
42 * The case values.
43 */
44 private final List<Object> theCases;
45
46 /**
47 * The number of lines.
48 */
49 private final int theNumLines;
50
51 /**
52 * Constructor.
53 *
54 * @param pParser the parser
55 * @param pOwner the owning switch
56 * @param pCase the case
57 * @throws OceanusException on error
58 */
59 ThemisAnalysisCase(final ThemisAnalysisParser pParser,
60 final ThemisAnalysisContainer pOwner,
61 final Object pCase) throws OceanusException {
62 /* Record the parent */
63 theParent = pOwner;
64
65 /* Initialise the case value */
66 theCases = new ArrayList<>();
67 theCases.add(pCase);
68
69 /* Create a parser */
70 theContents = new ArrayDeque<>();
71 final ThemisAnalysisParser myParser = new ThemisAnalysisParser(pParser, theContents);
72 processLines(myParser);
73
74 /* Calculate the number of lines */
75 theNumLines = theCases.size();
76 }
77
78 /**
79 * process the lines.
80 *
81 * @param pParser the parser
82 * @throws OceanusException on error
83 */
84 void processLines(final ThemisAnalysisParser pParser) throws OceanusException {
85 /* we are still processing Cases */
86 boolean look4Case = true;
87
88 /* Loop through the lines */
89 while (pParser.hasLines()) {
90 /* Access next line */
91 final ThemisAnalysisLine myLine = (ThemisAnalysisLine) pParser.popNextLine();
92
93 /* Process comments/blanks/languageConstructs */
94 boolean processed = pParser.processCommentsAndBlanks(myLine)
95 || pParser.processLanguage(myLine)
96 || pParser.processBlocks(myLine);
97
98 /* If we have not processed */
99 if (!processed) {
100 /* Look for new case */
101 myLine.mark();
102 final Object myCase = ThemisAnalysisParser.parseCase(myLine);
103
104 /* If we have a new case */
105 if (myCase != null) {
106 /* If we are still looking for further cases */
107 if (look4Case) {
108 /* Process additional caseValue */
109 theCases.add(myCase);
110 processed = true;
111
112 /* else we have finished */
113 } else {
114 /* reset and restore line and break loop */
115 myLine.reset();
116 pParser.pushLine(myLine);
117 return;
118 }
119 }
120 }
121
122 /* If we have not processed */
123 if (!processed) {
124 /* Have finished looking for cases */
125 look4Case = false;
126
127 /* Just add the line to contents at present */
128 theContents.add(myLine);
129 }
130 }
131 }
132
133 @Override
134 public Deque<ThemisAnalysisElement> getContents() {
135 return theContents;
136 }
137
138 @Override
139 public ThemisAnalysisContainer getParent() {
140 return theParent;
141 }
142
143 @Override
144 public int getNumLines() {
145 return theNumLines;
146 }
147
148 @Override
149 public String toString() {
150 /* Start parameters */
151 final StringBuilder myBuilder = new StringBuilder();
152
153 /* Build parameters */
154 boolean bFirst = true;
155 for (Object myCase : theCases) {
156 /* Handle separators */
157 if (!bFirst) {
158 myBuilder.append(ThemisAnalysisChar.COMMA);
159 myBuilder.append(ThemisAnalysisChar.BLANK);
160 } else {
161 bFirst = false;
162 }
163
164 /* Add case */
165 myBuilder.append(myCase);
166 }
167
168 /* Return the string */
169 return myBuilder.toString();
170 }
171 }