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  import java.text.MessageFormat;
19  
20  /**
21   * Exception thrown when a range syntax error is found.
22   *
23   * @author Massimiliano Ziccardi
24   */
25  public class InvalidRangeSyntaxException extends RangeException {
26      /**
27      *
28      */
29      private static final long serialVersionUID = 2036144202685590610L;
30  
31      /**
32       * Message header if the whold string is unknown.
33       */
34      private static final String INVALID_RANGE = "Invalid range";
35  
36      /**
37       * Message header if the whold string is known.
38       */
39      private static final String INVALID_RANGE_STRING = "Invalid range ''{0}''";
40  
41      /**
42       * The error message pattern.
43       */
44      private static final String MESSAGE_PATTERN =
45           "{0} - Stage {1} expected one of ''{2}'' but found ''{3}'' instead.";
46  
47      /**
48       * Builds the exception specifying the stage and the range string.
49       *
50       * @param stage
51       *            The stage that caused the error.
52       * @param found
53       *            The string that caused the error.
54       */
55      public InvalidRangeSyntaxException(final Stage stage, final String found) {
56          super(stage, found, null);
57      }
58  
59      /**
60       * Builds the exception specifying the stage, the bad tokens and the whole
61       * string.
62       *
63       * @param stage
64       *            The stage that caused the error.
65       * @param found
66       *            The string that caused the error.
67       * @param wholeString
68       *            The whole range string.
69       */
70      public InvalidRangeSyntaxException(final Stage stage, final String found,
71              final String wholeString) {
72          super(stage, found, wholeString);
73      }
74  
75      /**
76       * Utility method for error messages.
77       *
78       * @param stage
79       *            the stage to ask for expected tokens.
80       * @return The list of expected tokens
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       * @return the exception message
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 }