1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package it.jnrpe.utils.thresholds;
17
18 import java.text.MessageFormat;
19
20
21
22
23
24
25 public class InvalidRangeSyntaxException extends RangeException {
26
27
28
29 private static final long serialVersionUID = 2036144202685590610L;
30
31
32
33
34 private static final String INVALID_RANGE = "Invalid range";
35
36
37
38
39 private static final String INVALID_RANGE_STRING = "Invalid range ''{0}''";
40
41
42
43
44 private static final String MESSAGE_PATTERN =
45 "{0} - Stage {1} expected one of ''{2}'' but found ''{3}'' instead.";
46
47
48
49
50
51
52
53
54
55 public InvalidRangeSyntaxException(final Stage stage, final String found) {
56 super(stage, found, null);
57 }
58
59
60
61
62
63
64
65
66
67
68
69
70 public InvalidRangeSyntaxException(final Stage stage, final String found,
71 final String wholeString) {
72 super(stage, found, wholeString);
73 }
74
75
76
77
78
79
80
81
82 private static String parseExpecting(final Stage stage) {
83 StringBuffer expected = new StringBuffer();
84
85 for (String key : stage.getTransitionNames()) {
86 expected.append(",").append(
87 stage.getTransition(key).expects());
88 }
89
90 if (expected.length() > 1) {
91 return expected.substring(1);
92 }
93 return "{END OF RANGE}";
94 }
95
96
97
98
99 @Override
100 public final String getMessage() {
101 String invalidRange = null;
102 if (getWholeRangeString() != null) {
103 invalidRange =
104 MessageFormat
105 .format(INVALID_RANGE_STRING, getWholeRangeString());
106 } else {
107 invalidRange = INVALID_RANGE;
108 }
109
110 return MessageFormat.format(MESSAGE_PATTERN,
111 getFailedStage().getName(), invalidRange,
112 parseExpecting(getFailedStage()), getBadString());
113 }
114 }