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 import it.jnrpe.utils.BadThresholdException;
19
20 /**
21 * Base class for the range parsing exceptions.
22 *
23 * @author Massimiliano Ziccardi
24 */
25 public class RangeException extends BadThresholdException {
26
27 /**
28 *
29 */
30 private static final long serialVersionUID = 8370789724508683948L;
31
32 /**
33 * The whole range string.
34 */
35 private String wholeRangeString = null;
36
37 /**
38 * The stage that raised the exception.
39 */
40 private Stage failedStage;
41
42 /**
43 * The erroneous tokens.
44 */
45 private final String badString;
46
47 /**
48 * Generic exception message.
49 */
50 private String message = null;
51
52 /**
53 * Builds and initialize the exception with the error message.
54 *
55 * @param errorMessage
56 * The message
57 */
58 public RangeException(final String errorMessage) {
59 super();
60 failedStage = null;
61 badString = null;
62 wholeRangeString = null;
63 message = errorMessage;
64 }
65
66 /**
67 * Builds the exception specifying the stage, the bad tokens and the whole
68 * string.
69 *
70 * @param stage
71 * The stage that caused the error.
72 * @param found
73 * The string that caused the error.
74 * @param wholeString
75 * The whole range string.
76 */
77 public RangeException(final Stage stage, final String found,
78 final String wholeString) {
79 super();
80 failedStage = stage;
81 badString = found;
82 wholeRangeString = wholeString;
83 }
84
85 /**
86 * @return The whole range string
87 */
88 protected final String getWholeRangeString() {
89 return wholeRangeString;
90 }
91
92 /**
93 * Sets the whole range string.
94 *
95 * @param rangeString
96 * The whole range string
97 */
98 final void setWholeRangeString(final String rangeString) {
99 wholeRangeString = rangeString;
100 }
101
102 /**
103 * @return The parser stage that failed
104 */
105 protected final Stage getFailedStage() {
106 return failedStage;
107 }
108
109 /**
110 * @return the erroneous tokens
111 */
112 protected final String getBadString() {
113 return badString;
114 }
115
116 /**
117 * Utility method for error messages.
118 *
119 * @param stage
120 * the stage to ask for expected tokens.
121 * @return The list of expected tokens
122 */
123 private static String parseExpecting(final Stage stage) {
124 StringBuffer expected = new StringBuffer();
125
126 for (String key : stage.getTransitionNames()) {
127 expected.append(",").append(stage.getTransition(key).expects());
128 }
129
130 return expected.substring(1);
131 }
132
133 /**
134 * Returns the expected token for the failed stage.
135 *
136 * @return the expected token for the failed stage.
137 */
138 protected final String getExpectedTokens() {
139 return parseExpecting(failedStage);
140 }
141
142 /**
143 * Sets the stage that failed.
144 *
145 * @param stage
146 * The failed stage
147 */
148 final void setFailedStage(final Stage stage) {
149 failedStage = stage;
150 }
151
152 /**
153 * @return the error message.
154 */
155 @Override
156 public String getMessage() {
157 return message;
158 }
159 }