View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.utils.thresholds;
17  
18  /**
19   * The root of the parsing state machine.
20   *
21   * @author Massimiliano Ziccardi
22   */
23  class StartStage extends Stage {
24  
25      /**
26       *
27       */
28      protected StartStage() {
29          super("root");
30      }
31  
32      @Override
33      public boolean canParse(final String threshold) {
34          return false;
35      }
36  
37      @Override
38      public String parse(final String threshold, final RangeConfig tc)
39              throws RangeException {
40  
41          if (threshold == null) {
42              throw new RangeException("Range can't be null");
43          }
44  
45          Stage currentStage = this;
46  
47          String parsedThreshold = threshold;
48  
49          boolean stageParsed;
50  
51          while (parsedThreshold.length() != 0) {
52              stageParsed = false;
53              for (String transitionName : currentStage.getTransitionNames()) {
54                  Stage transition = currentStage.getTransition(transitionName);
55                  if (transition.canParse(parsedThreshold)) {
56                      parsedThreshold = transition.parse(parsedThreshold, tc);
57                      currentStage = transition;
58                      stageParsed = true;
59                      break;
60                  }
61              }
62  
63              if (!stageParsed) {
64                  throw new InvalidRangeSyntaxException(currentStage,
65                          parsedThreshold, threshold);
66              }
67          }
68  
69          if (!currentStage.isLeaf()) {
70              throw new PrematureEndOfRangeException(currentStage, threshold);
71          }
72  
73          return parsedThreshold;
74      }
75  
76      @Override
77      public String expects() {
78          return null;
79      }
80  }