1 /*-------------------------------------------------------------------------
2 Copyright 2006 Olivier Berlanger
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 net.sf.xolite;
17
18
19 /**
20 * Exception for XML parsing. <br>
21 * This exception allow to define the location of the bad tokens that caused the exception in the input XML. This location can be
22 * expressed as two int (line, row) or as a plain string.
23 */
24 public class XMLParseException extends Exception {
25
26 private static final long serialVersionUID = -5693785937821376660L;
27
28 private String source;
29 private int line = -1;
30 private int column = -1;
31
32
33 public XMLParseException(String message) {
34 super(message);
35 }
36
37
38 public XMLParseException(String message, Throwable cause) {
39 super(message, cause);
40 }
41
42
43 @Override
44 public String getMessage() {
45 StringBuilder sb = new StringBuilder();
46 // add exception message
47 sb.append(super.getMessage());
48 // add source description
49 if (source != null) {
50 sb.append("\n\t\tin ");
51 sb.append(source);
52 }
53 // add location in source
54 if (line >= 0) {
55 sb.append("\n\t\tat line ");
56 sb.append(line);
57 if (column >= 0) {
58 sb.append(", column ");
59 sb.append(column);
60 }
61 }
62 return sb.toString();
63 }
64
65
66 public void setSource(String sourceDescription) {
67 source = sourceDescription;
68 }
69
70
71 public void setLocation(int newLine, int newColumn) {
72 line = newLine;
73 column = newColumn;
74 }
75
76
77 public String getSource() {
78 return source;
79 }
80
81
82 public int getLine() {
83 return line;
84 }
85
86
87 public int getColumn() {
88 return column;
89 }
90
91 }