View Javadoc

1   /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">http://www.qedeq.org
2    *
3    * Copyright 2000-2014,  Michael Meyling <mime@qedeq.org>.
4    *
5    * "Hilbert II" is free software; you can redistribute
6    * it and/or modify it under the terms of the GNU General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU General Public License for more details.
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   * Data validation error. Shows an error or warning within a source file.
27   *
28   * @author  Michael Meyling
29   */
30  public class SourceFileException extends QedeqException {
31  
32      /** Serialization information. */
33      private static final long serialVersionUID = -4109767904038020052L;
34  
35      /** Error code of this Exception. */
36      private final Service service;
37  
38      /** Start of error location. */
39      private final SourceArea errorArea;
40  
41      /** End of error location. */
42      private final SourceArea referenceArea;
43  
44  
45      /**
46       * Constructor.
47       *
48       * @param   service     This service generated the error.
49       * @param   errorCode   Error code.
50       * @param   errorText   Error text.
51       * @param   exception   Exception to wrap.
52       * @param   errorArea   Error location.
53       * @param   referenceArea   Error reference location.
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       * Constructor.
65       *
66       * @param   service      This service generated the error.
67       * @param   exception   Exception to wrap.
68       * @param   errorArea   Error location.
69       * @param   referenceArea   Error reference location.
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       * Get service that found the error.
79       *
80       * @return  Service.
81       */
82      public Service getService() {
83          return service;
84      }
85  
86      /**
87       * Get position information about error location.
88       *
89       * @return  Error location position.
90       */
91      public SourceArea getSourceArea() {
92          return errorArea;
93      }
94  
95      /**
96       * Get additional position information about another associated location.
97       *
98       * @return  Additional error location position.
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      * Format error message.
116      *
117      * @param   message Normal error message.
118      * @param   cause   This caused the exception. Must not be <code>null</code>.
119      * @return  Error message.
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      * Get detailed error description.
136      * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
137      * The second line contains the local address, the line and column.
138      *
139      * @return  Error description.
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 }