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 */
017package org.apache.camel.language.simple.types;
018
019import org.apache.camel.ExpressionIllegalSyntaxException;
020
021/**
022 * Syntax error in the simple language expression.
023 */
024public 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    /**
052     * Gets a short error message.
053     */
054    public String getShortMessage() {
055        if (message == null) {
056            return "[null]";
057        }
058        return message;
059    }
060
061    @Override
062    public String getMessage() {
063        if (message == null) {
064            return "[null]";
065        }
066
067        StringBuilder sb = new StringBuilder(message);
068        if (index > -1) {
069            sb.append(" at location ").append(index);
070            // create a nice looking message with indicator where the problem is
071            sb.append("\n").append(getExpression()).append("\n");
072            for (int i = 0; i < index; i++) {
073                sb.append(" ");
074            }
075            sb.append("*\n");
076        }
077        return sb.toString();
078    }
079
080}