Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Sa Dez 22 2007 01:35:21 CET
file stats: LOC: 243   Methods: 14
NCLOC: 112   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SourceFileException.java 70% 72,4% 64,3% 70,7%
coverage coverage
 1    /* $Id: SourceFileException.java,v 1.1 2007/12/21 23:33:47 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.1 $
 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  2403 public SourceFileException(final int errorCode, final String errorText,
 61    final Throwable exception, final SourceArea errorArea, final SourceArea referenceArea) {
 62  2403 super(errorCode, errorText, exception);
 63  2403 this.errorArea = errorArea;
 64  2403 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  2360 public SourceFileException(final QedeqException exception, final SourceArea errorArea,
 76    final SourceArea referenceArea) {
 77  2360 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.getMessage(), 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  15 public SourceFileException(final Throwable exception) {
 123  15 super(1000, exception.toString(), exception); // TODO mime 20071116: error code refac
 124  15 errorArea = null;
 125  15 referenceArea = null;
 126    }
 127   
 128    /**
 129    * Constructor.
 130    *
 131    * @param exception Exception to wrap.
 132    */
 133  0 public SourceFileException(final IOException exception) {
 134  0 super(9997, exception.toString(), exception); // TODO mime 20071116: error code refac
 135  0 errorArea = null;
 136  0 referenceArea = null;
 137    }
 138   
 139    /**
 140    * Constructor.
 141    *
 142    * @param exception Exception to wrap.
 143    */
 144  6 public SourceFileException(final SAXException exception) {
 145    // TODO mime 20071116: error code refac
 146  6 super(XmlSyntaxException.SAX_PARSER_EXCEPTION, exception.getMessage(), exception);
 147  6 errorArea = null;
 148  6 referenceArea = null;
 149    }
 150   
 151    /**
 152    * Get position information about error location.
 153    *
 154    * @return Error location position.
 155    */
 156  296 public final SourceArea getSourceArea() {
 157  296 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 URL for getting the line.
 173    * @return Referenced line.
 174    */
 175  162 public final String getLine(final URL localAddress) {
 176  162 if (line == null) {
 177  19 line = "";
 178  19 try {
 179  19 final TextInput input = new TextInput(localAddress);
 180  19 input.setRow(errorArea.getStartPosition().getLine());
 181  19 input.setColumn(errorArea.getStartPosition().getColumn());
 182  19 line = input.getLine();
 183    } catch (Exception e) {
 184    // ignore
 185    }
 186    }
 187  162 return line;
 188    }
 189   
 190  335 public final String getMessage() {
 191  335 if (getCause() != null) {
 192  335 if (getCause().getCause() != null) {
 193  0 return getCause().getCause().getMessage();
 194    }
 195  335 return getCause().getMessage();
 196    }
 197  0 return "";
 198    }
 199   
 200    /**
 201    * Get detailed error description.
 202    * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
 203    * The second line contains the local address, the line and column.
 204    * Third line is the result or {@link #getLine(URL)}.
 205    * In the fourth line the row position for the third line is marked.
 206    *
 207    * <p>TODO mime 20070219: rework description: add end (and perhaps reference) information
 208    * <p>TODO mime 20071128: move this method into another class!!!
 209    *
 210    * @param localAddress Lookup source here.
 211    *
 212    * @return Error description.
 213    */
 214  95 public final String getDescription(final URL localAddress) {
 215  95 final StringBuffer buffer = new StringBuffer();
 216  95 buffer.append(getErrorCode() + ": " + getMessage());
 217  95 if (errorArea != null && errorArea.getStartPosition() != null) {
 218  95 final SourcePosition start = errorArea.getStartPosition();
 219  95 buffer.append("\n");
 220  95 buffer.append(start.getAddress() + ":" + start.getLine() + ":"
 221    + start.getColumn());
 222  95 buffer.append("\n");
 223  95 buffer.append(ReplaceUtility.replace(getLine(localAddress), "\t", " "));
 224  95 buffer.append("\n");
 225  95 final StringBuffer whitespace = IoUtility.getSpaces(start.getColumn() - 1);
 226  95 buffer.append(whitespace);
 227  95 buffer.append("^");
 228    }
 229  95 return buffer.toString();
 230    }
 231   
 232  239 public final String toString() {
 233  239 final StringBuffer buffer = new StringBuffer();
 234  239 buffer.append(getErrorCode() + ": " + getMessage());
 235  239 if (errorArea != null && errorArea.getStartPosition() != null) {
 236  237 final SourcePosition start = errorArea.getStartPosition();
 237  237 buffer.append("\n");
 238  237 buffer.append(start.getAddress() + ":" + start.getLine() + ":"
 239    + start.getColumn());
 240    }
 241  239 return buffer.toString();
 242    }
 243    }