001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.language.simple.types;
018    
019    import org.apache.camel.ExpressionIllegalSyntaxException;
020    
021    /**
022     * Syntax error in the simple language expression.
023     */
024    public class SimpleIllegalSyntaxException extends ExpressionIllegalSyntaxException {
025        private static final long serialVersionUID = 1L;
026        private final int index;
027        private final String message;
028    
029        public SimpleIllegalSyntaxException(String expression, int index, String message) {
030            super(expression);
031            this.index = index;
032            this.message = message;
033        }
034    
035        public SimpleIllegalSyntaxException(String expression, int index, String message, Throwable cause) {
036            super(expression, cause);
037            this.index = index;
038            this.message = message;
039        }
040    
041        /**
042         * Index where the parsing error occurred
043         *
044         * @return index of the parsing error in the input, returns <tt>-1</tt> if the cause of the problem
045         * is not applicable to specific index in the input
046         */
047        public int getIndex() {
048            return index;
049        }
050    
051        @Override
052        public String getMessage() {
053            if (message == null) {
054                return "[null]";
055            }
056    
057            StringBuilder sb = new StringBuilder(message);
058            if (index > -1) {
059                sb.append(" at location ").append(index);
060                // create a nice looking message with indicator where the problem is
061                sb.append("\n").append(getExpression()).append("\n");
062                for (int i = 0; i < index; i++) {
063                    sb.append(" ");
064                }
065                sb.append("*\n");
066            }
067            return sb.toString();
068        }
069    
070    }