SourceFileException.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.se.common;
017 
018 import java.io.IOException;
019 
020 import org.qedeq.base.io.SourceArea;
021 import org.qedeq.base.io.SourcePosition;
022 import org.qedeq.base.utility.EqualsUtility;
023 
024 
025 /**
026  * Data validation error. Shows an error or warning within a source file.
027  *
028  @author  Michael Meyling
029  */
030 public class SourceFileException extends QedeqException {
031 
032     /** Serialization information. */
033     private static final long serialVersionUID = -4109767904038020052L;
034 
035     /** Error code of this Exception. */
036     private final Plugin plugin;
037 
038     /** Start of error location. */
039     private final SourceArea errorArea;
040 
041     /** End of error location. */
042     private final SourceArea referenceArea;
043 
044 
045     /**
046      * Constructor.
047      *
048      @param   plugin      This plugin generated the error.
049      @param   errorCode   Error code.
050      @param   errorText   Error text.
051      @param   exception   Exception to wrap.
052      @param   errorArea   Error location.
053      @param   referenceArea   Error reference location.
054      */
055     public SourceFileException(final Plugin plugin, final int errorCode, final String errorText,
056             final Throwable exception, final SourceArea errorArea, final SourceArea referenceArea) {
057         super(errorCode, errorText, exception);
058         this.plugin = plugin;
059         this.errorArea = errorArea;
060         this.referenceArea = referenceArea;
061     }
062 
063     /**
064      * Constructor.
065      *
066      @param   plugin      This plugin generated the error.
067      @param   exception   Exception to wrap.
068      @param   errorArea   Error location.
069      @param   referenceArea   Error reference location.
070      */
071     public SourceFileException(final Plugin plugin, final QedeqException exception,
072             final SourceArea errorArea, final SourceArea referenceArea) {
073         this(plugin, exception.getErrorCode(), exception.getMessage(), exception,
074             errorArea, referenceArea);
075     }
076 
077     /**
078      * Get plugin that found the error.
079      *
080      @return  Plugin.
081      */
082     public Plugin getPlugin() {
083         return plugin;
084     }
085 
086     /**
087      * Get position information about error location.
088      *
089      @return  Error location position.
090      */
091     public SourceArea getSourceArea() {
092         return errorArea;
093     }
094 
095     /**
096      * Get additional position information about another associated location.
097      *
098      @return  Additional error location position.
099      */
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             buffer.append("\n");
148         }
149         buffer.append("\t" + getErrorCode() ": " + getMessage());
150         return buffer.toString();
151     }
152 
153     public final int hashCode() {
154         return getErrorCode() (errorArea != null ? errorArea.hashCode() 13)
155             (getPlugin() != null ? getPlugin().hashCode() 131)
156             (getMessage() != null ? getMessage().hashCode() 499);
157     }
158 
159     public final boolean equals(final Object obj) {
160         if (!(obj instanceof SourceFileException)) {
161             return false;
162         }
163         final SourceFileException other = (SourceFileExceptionobj;
164         return  (getErrorCode() == other.getErrorCode())
165             &&  EqualsUtility.equals(getPlugin(), other.getPlugin())
166             &&  EqualsUtility.equals(getMessage(), other.getMessage())
167             &&  EqualsUtility.equals(errorArea, other.errorArea);
168     }
169 
170     public final String toString() {
171         return getDescription();
172     }
173 
174 }