1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.kernel.se.common;
17
18 import java.io.IOException;
19
20 import org.qedeq.base.io.SourceArea;
21 import org.qedeq.base.io.SourcePosition;
22 import org.qedeq.base.utility.EqualsUtility;
23
24
25
26
27
28
29
30 public class SourceFileException extends QedeqException {
31
32
33 private static final long serialVersionUID = -4109767904038020052L;
34
35
36 private final Service service;
37
38
39 private final SourceArea errorArea;
40
41
42 private final SourceArea referenceArea;
43
44
45
46
47
48
49
50
51
52
53
54
55 public SourceFileException(final Service service, final int errorCode, final String errorText,
56 final Throwable exception, final SourceArea errorArea, final SourceArea referenceArea) {
57 super(errorCode, errorText, exception);
58 this.service = service;
59 this.errorArea = errorArea;
60 this.referenceArea = referenceArea;
61 }
62
63
64
65
66
67
68
69
70
71 public SourceFileException(final Service service, final QedeqException exception,
72 final SourceArea errorArea, final SourceArea referenceArea) {
73 this(service, exception.getErrorCode(), exception.getMessage(), exception,
74 errorArea, referenceArea);
75 }
76
77
78
79
80
81
82 public Service getService() {
83 return service;
84 }
85
86
87
88
89
90
91 public SourceArea getSourceArea() {
92 return errorArea;
93 }
94
95
96
97
98
99
100 public SourceArea getReferenceArea() {
101 return referenceArea;
102 }
103
104 public String getMessage() {
105 if (getCause() != null) {
106 if (getCause().getCause() != null) {
107 return getText(super.getMessage(), getCause().getCause());
108 }
109 return getText(super.getMessage(), getCause());
110 }
111 return super.getMessage();
112 }
113
114
115
116
117
118
119
120
121 private String getText(final String message, final Throwable cause) {
122 if (message != null) {
123 if (message.equals(cause.getMessage())) {
124 return message;
125 }
126 if (cause instanceof IOException || cause instanceof NullPointerException) {
127 return message + "; " + cause.toString();
128 }
129 return message + "; " + cause.getMessage();
130 }
131 return cause.toString();
132 }
133
134
135
136
137
138
139
140
141 public String getDescription() {
142 final StringBuffer buffer = new StringBuffer();
143 if (errorArea != null && errorArea.getStartPosition() != null) {
144 final SourcePosition start = errorArea.getStartPosition();
145 buffer.append(errorArea.getAddress() + ":" + start.getRow() + ":"
146 + start.getColumn());
147 final SourcePosition end = errorArea.getEndPosition();
148 if (end != null) {
149 buffer.append(":" + end.getRow() + ":" + end.getColumn());
150 }
151 buffer.append("\n");
152 }
153 buffer.append("\t" + getErrorCode() + ": " + getMessage());
154 return buffer.toString();
155 }
156
157 public final int hashCode() {
158 return getErrorCode() ^ (errorArea != null ? errorArea.hashCode() : 13)
159 ^ (getService() != null ? getService().hashCode() : 131)
160 ^ (getMessage() != null ? getMessage().hashCode() : 499);
161 }
162
163 public final boolean equals(final Object obj) {
164 if (!(obj instanceof SourceFileException)) {
165 return false;
166 }
167 final SourceFileException other = (SourceFileException) obj;
168 return (getErrorCode() == other.getErrorCode())
169 && EqualsUtility.equals(getService(), other.getService())
170 && EqualsUtility.equals(getMessage(), other.getMessage())
171 && EqualsUtility.equals(errorArea, other.errorArea);
172 }
173
174 public final String toString() {
175 return getDescription();
176 }
177
178 }