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.lethe.analysis;
18  
19  import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20  import io.github.tonywasher.joceanus.themis.lethe.analysis.ThemisAnalysisContainer.ThemisAnalysisAdoptable;
21  
22  import java.util.ArrayDeque;
23  import java.util.Collections;
24  import java.util.Deque;
25  import java.util.Iterator;
26  
27  /**
28   * Catch construct.
29   */
30  public class ThemisAnalysisCatch
31          implements ThemisAnalysisContainer, ThemisAnalysisAdoptable {
32      /**
33       * The parent.
34       */
35      private ThemisAnalysisContainer theParent;
36  
37      /**
38       * The headers.
39       */
40      private final Deque<ThemisAnalysisElement> theHeaders;
41  
42      /**
43       * The contents.
44       */
45      private final Deque<ThemisAnalysisElement> theContents;
46  
47      /**
48       * The catch clause(s).
49       */
50      private final ThemisAnalysisCatch theCatch;
51  
52      /**
53       * The number of lines.
54       */
55      private final int theNumLines;
56  
57      /**
58       * The dataMap.
59       */
60      private final ThemisAnalysisDataMap theDataMap;
61  
62      /**
63       * Constructor.
64       *
65       * @param pParser the parser
66       * @param pOwner  the owning try
67       * @param pLine   the initial catch line
68       * @throws OceanusException on error
69       */
70      ThemisAnalysisCatch(final ThemisAnalysisParser pParser,
71                          final ThemisAnalysisContainer pOwner,
72                          final ThemisAnalysisLine pLine) throws OceanusException {
73          /* Record the parent */
74          theParent = pOwner;
75          theDataMap = new ThemisAnalysisDataMap(theParent.getDataMap());
76  
77          /* Create the arrays */
78          theHeaders = ThemisAnalysisBuilder.parseHeaders(pParser, pLine);
79          final Deque<ThemisAnalysisElement> myLines = ThemisAnalysisBuilder.processBody(pParser);
80          theNumLines = theHeaders.size();
81  
82          /* Look for catch clauses */
83          theCatch = (ThemisAnalysisCatch) pParser.processExtra(pOwner, ThemisAnalysisKeyWord.CATCH);
84  
85          /* Create a parser */
86          theContents = new ArrayDeque<>();
87          final ThemisAnalysisParser myParser = new ThemisAnalysisParser(myLines, theContents, this);
88          myParser.processLines();
89      }
90  
91      @Override
92      public Deque<ThemisAnalysisElement> getContents() {
93          return theContents;
94      }
95  
96      @Override
97      public ThemisAnalysisContainer getParent() {
98          return theParent;
99      }
100 
101     @Override
102     public void setParent(final ThemisAnalysisContainer pParent) {
103         theParent = pParent;
104         theDataMap.setParent(pParent.getDataMap());
105         if (theCatch != null) {
106             theCatch.setParent(pParent);
107         }
108     }
109 
110     @Override
111     public ThemisAnalysisDataMap getDataMap() {
112         return theDataMap;
113     }
114 
115     @Override
116     public void postProcessExtras() throws OceanusException {
117         /* Process the catch clause if required */
118         if (theCatch != null) {
119             theCatch.postProcessLines();
120         }
121     }
122 
123     /**
124      * Obtain the additional catch clause (if any).
125      *
126      * @return the catch clause
127      */
128     public ThemisAnalysisCatch getCatch() {
129         return theCatch;
130     }
131 
132     @Override
133     public Iterator<ThemisAnalysisContainer> containerIterator() {
134         return theCatch == null
135                 ? Collections.emptyIterator()
136                 : Collections.singleton((ThemisAnalysisContainer) theCatch).iterator();
137     }
138 
139     @Override
140     public int getNumLines() {
141         return theNumLines;
142     }
143 
144     @Override
145     public String toString() {
146         return ThemisAnalysisBuilder.formatLines(theHeaders);
147     }
148 }