1/*
2 * Copyright (C) 2010 Google Inc.
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
17package com.google.clearsilver.jsilver.exceptions;
18
19/**
20 * Thrown when resource (e.g. template or HDF) contains bad syntax.
21 */
22public class JSilverBadSyntaxException extends JSilverException {
23
24  private final String resourceName;
25
26  private final int line;
27
28  private final int column;
29
30  /**
31   * Signifies line or column is not known.
32   */
33  public static final int UNKNOWN_POSITION = -1;
34
35  /**
36   * Constructor of JSilverBadSyntaxException.
37   *
38   * @param message text of an error message
39   * @param lineContent content of a line where error occurred (can be null)
40   * @param resourceName name of a file where error occurred (can be null)
41   * @param line number of a line in {@code resourceName} where error occurred (ignored if set to
42   *        {@link #UNKNOWN_POSITION})
43   * @param column number of a column in {@code resourceName} where error occurred (ignored if set
44   *        to {@link #UNKNOWN_POSITION})
45   * @param cause an original exception of an error. Null value is permitted and indicates that the
46   *        cause is nonexistent or unknown.
47   */
48  public JSilverBadSyntaxException(String message, String lineContent, String resourceName,
49      int line, int column, Throwable cause) {
50    super(makeMessage(message, lineContent, resourceName, line, column), cause);
51    this.resourceName = resourceName;
52    this.line = line;
53    this.column = column;
54  }
55
56  private static String makeMessage(String message, String lineContent, String resourceName,
57      int line, int column) {
58    StringBuilder result = new StringBuilder(message);
59    if (resourceName != null) {
60      result.append(" resource=").append(resourceName);
61    }
62    if (lineContent != null) {
63      result.append(" content=").append(lineContent);
64    }
65    if (line != UNKNOWN_POSITION) {
66      result.append(" line=").append(line);
67    }
68    if (column != UNKNOWN_POSITION) {
69      result.append(" column=").append(column);
70    }
71    return result.toString();
72  }
73
74  /**
75   * Name of resource that had syntax error (typically a file name).
76   */
77  public String getResourceName() {
78    return resourceName;
79  }
80
81  /**
82   * Line number this syntax error occured, or {@link #UNKNOWN_POSITION}.
83   */
84  public int getLine() {
85    return line;
86  }
87
88  /**
89   * Column number this syntax error occured, or {@link #UNKNOWN_POSITION}.
90   */
91  public int getColumn() {
92    return column;
93  }
94
95}
96