Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Jan 26 2008 14:11:34 CET
file stats: LOC: 246   Methods: 14
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SourceFileException.java 75% 73,3% 64,3% 72,1%
coverage coverage
 1    /* $Id: SourceFileException.java,v 1.2 2008/01/26 12:39:11 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.common;
 19   
 20    import java.io.File;
 21    import java.io.IOException;
 22    import java.net.URL;
 23   
 24    import org.qedeq.kernel.utility.IoUtility;
 25    import org.qedeq.kernel.utility.ReplaceUtility;
 26    import org.qedeq.kernel.utility.TextInput;
 27    import org.qedeq.kernel.xml.common.XmlSyntaxException;
 28    import org.xml.sax.SAXException;
 29   
 30   
 31    /**
 32    * Data validation error. Shows an error within a source file.
 33    *
 34    * @version $Revision: 1.2 $
 35    * @author Michael Meyling
 36    */
 37    public class SourceFileException extends QedeqException {
 38   
 39    /** Serialization information. */
 40    private static final long serialVersionUID = -4109767904038020052L;
 41   
 42    /** Start of error location. */
 43    private final SourceArea errorArea;
 44   
 45    /** End of error location. */
 46    private final SourceArea referenceArea;
 47   
 48    /** Referenced line with marker. */
 49    private String line;
 50   
 51    /**
 52    * Constructor.
 53    *
 54    * @param errorCode Error code.
 55    * @param errorText Error text.
 56    * @param exception Exception to wrap.
 57    * @param errorArea Error location.
 58    * @param referenceArea Error reference location.
 59    */
 60  55 public SourceFileException(final int errorCode, final String errorText,
 61    final Throwable exception, final SourceArea errorArea, final SourceArea referenceArea) {
 62  55 super(errorCode, errorText, exception);
 63  55 this.errorArea = errorArea;
 64  55 this.referenceArea = referenceArea;
 65    }
 66   
 67   
 68    /**
 69    * Constructor.
 70    *
 71    * @param exception Exception to wrap.
 72    * @param errorArea Error location.
 73    * @param referenceArea Error reference location.
 74    */
 75  25 public SourceFileException(final QedeqException exception, final SourceArea errorArea,
 76    final SourceArea referenceArea) {
 77  25 this(exception.getErrorCode(), exception.getMessage(), exception, errorArea, referenceArea);
 78    }
 79   
 80   
 81    /**
 82    * Constructor.
 83    *
 84    * @param url Parsed file.
 85    * @param exception Exception to wrap.
 86    */
 87  0 public SourceFileException(final URL url, final Exception exception) {
 88  0 super(9997, exception.toString(), exception); // TODO mime 20071116: error code refac
 89  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 90  0 referenceArea = null;
 91    }
 92   
 93    /**
 94    * Constructor.
 95    *
 96    * @param file Parsed file.
 97    * @param exception Exception to wrap.
 98    */
 99  0 public SourceFileException(final File file, final Exception exception) {
 100  0 super(9998, exception.getMessage(), exception); // TODO mime 20071116: error code refac
 101  0 final URL url = IoUtility.toUrl(file);
 102  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 103  0 referenceArea = null;
 104    }
 105   
 106    /**
 107    * Constructor.
 108    *
 109    * @param exception Exception to wrap.
 110    */
 111  0 public SourceFileException(final Exception exception) {
 112  0 super(9999, exception.getMessage(), exception); // TODO mime 20071116: error code refac
 113  0 errorArea = null;
 114  0 referenceArea = null;
 115    }
 116   
 117    /**
 118    * Constructor.
 119    *
 120    * @param exception Exception to wrap.
 121    */
 122  0 public SourceFileException(final Throwable exception) {
 123  0 super(1000, exception.toString(), exception); // TODO mime 20071116: error code refac
 124  0 errorArea = null;
 125  0 referenceArea = null;
 126    }
 127   
 128    /**
 129    * Constructor.
 130    *
 131    * @param exception Exception to wrap.
 132    */
 133  10 public SourceFileException(final IOException exception) {
 134  10 super(9997, exception.toString(), exception); // TODO mime 20071116: error code refac
 135  10 errorArea = null;
 136  10 referenceArea = null;
 137    }
 138   
 139    /**
 140    * Constructor.
 141    *
 142    * @param exception Exception to wrap.
 143    */
 144  4 public SourceFileException(final SAXException exception) {
 145    // TODO mime 20071116: error code refac
 146  4 super(XmlSyntaxException.SAX_PARSER_EXCEPTION, exception.getMessage(), exception);
 147  4 errorArea = null;
 148  4 referenceArea = null;
 149    }
 150   
 151    /**
 152    * Get position information about error location.
 153    *
 154    * @return Error location position.
 155    */
 156  270 public final SourceArea getSourceArea() {
 157  270 return errorArea;
 158    }
 159   
 160    /**
 161    * Get additional position information about another associated location.
 162    *
 163    * @return Additional error location position.
 164    */
 165  0 public final SourceArea getReferenceArea() {
 166  0 return referenceArea;
 167    }
 168   
 169    /**
 170    * Get line that is referenced by {@link #getSourceArea()}.
 171    *
 172    * @param localAddress Source file for getting the line.
 173    * @return Referenced line.
 174    */
 175  4 public final String getLine(final File localAddress) {
 176  4 if (line == null) {
 177  2 line = "";
 178  2 try {
 179  2 final TextInput input = new TextInput(localAddress);
 180  2 input.setRow(errorArea.getStartPosition().getLine());
 181  2 input.setColumn(errorArea.getStartPosition().getColumn());
 182  2 line = input.getLine();
 183    } catch (Exception e) {
 184    // ignore
 185    }
 186    }
 187  4 return line;
 188    }
 189   
 190  273 public final String getMessage() {
 191  273 if (getCause() != null) {
 192  273 if (getCause() instanceof IOException) {
 193  10 return getCause().toString();
 194    }
 195  263 if (getCause().getCause() != null) {
 196  0 return getCause().getCause().getMessage();
 197    }
 198  263 return getCause().getMessage();
 199    }
 200  0 return "";
 201    }
 202   
 203    /**
 204    * Get detailed error description.
 205    * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
 206    * The second line contains the local address, the line and column.
 207    * Third line is the result or {@link #getLine(File)}.
 208    * In the fourth line the row position for the third line is marked.
 209    *
 210    * <p>TODO mime 20070219: rework description: add end (and perhaps reference) information
 211    * <p>TODO mime 20071128: move this method into another class!!!
 212    *
 213    * @param localAddress Lookup source here.
 214    *
 215    * @return Error description.
 216    */
 217  4 public final String getDescription(final File localAddress) {
 218  4 final StringBuffer buffer = new StringBuffer();
 219  4 buffer.append(getErrorCode() + ": " + getMessage());
 220  4 if (errorArea != null && errorArea.getStartPosition() != null) {
 221  4 final SourcePosition start = errorArea.getStartPosition();
 222  4 buffer.append("\n");
 223  4 buffer.append(start.getAddress() + ":" + start.getLine() + ":"
 224    + start.getColumn());
 225  4 buffer.append("\n");
 226  4 buffer.append(ReplaceUtility.replace(getLine(localAddress), "\t", " "));
 227  4 buffer.append("\n");
 228  4 final StringBuffer whitespace = IoUtility.getSpaces(start.getColumn() - 1);
 229  4 buffer.append(whitespace);
 230  4 buffer.append("^");
 231    }
 232  4 return buffer.toString();
 233    }
 234   
 235  269 public final String toString() {
 236  269 final StringBuffer buffer = new StringBuffer();
 237  269 buffer.append(getErrorCode() + ": " + getMessage());
 238  269 if (errorArea != null && errorArea.getStartPosition() != null) {
 239  257 final SourcePosition start = errorArea.getStartPosition();
 240  257 buffer.append("\n");
 241  257 buffer.append(start.getAddress() + ":" + start.getLine() + ":"
 242    + start.getColumn());
 243    }
 244  269 return buffer.toString();
 245    }
 246    }