Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mai 10 2007 03:16:40 CEST
file stats: LOC: 234   Methods: 14
NCLOC: 111   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XmlFileException.java 75% 67,8% 64,3% 67,9%
coverage coverage
 1    /* $Id: XmlFileException.java,v 1.1 2007/04/12 23:50:10 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.xml.sax.SAXException;
 28   
 29   
 30    /**
 31    * Data validation error. Occurs if a set or add method leads to wrong or inconsistent data.
 32    *
 33    * @version $Revision: 1.1 $
 34    * @author Michael Meyling
 35    */
 36    public final class XmlFileException extends Exception {
 37   
 38    /** Serialization information. */
 39    private static final long serialVersionUID = -4109767904038020052L;
 40   
 41    /** Error code of this Exception. */
 42    private final int errorCode;
 43   
 44    /** Start of error location. */
 45    private final SourceArea errorArea;
 46   
 47    /** End of error location. */
 48    private final SourceArea referenceArea;
 49   
 50    /** Referenced line with marker. */
 51    private String line;
 52   
 53    /**
 54    * Constructor.
 55    *
 56    * @param exception Exception to wrap.
 57    * @param errorArea Error location.
 58    * @param referenceArea Error reference location.
 59    */
 60  5 public XmlFileException(final QedeqException exception, final SourceArea errorArea,
 61    final SourceArea referenceArea) {
 62  5 super(exception);
 63  5 this.errorCode = exception.getErrorCode();
 64  5 this.errorArea = errorArea;
 65  5 this.referenceArea = referenceArea;
 66    }
 67   
 68   
 69    /**
 70    * Constructor.
 71    *
 72    * @param exception Exception to wrap.
 73    */
 74  10 public XmlFileException(final SyntaxException exception) {
 75  10 super(exception);
 76  10 errorCode = exception.getErrorCode();
 77  10 errorArea = new SourceArea(exception.getErrorPosition().getAddress(),
 78    exception.getErrorPosition().getLocalAddress(), exception.getErrorPosition(), null);
 79  10 referenceArea = null;
 80    }
 81   
 82    /**
 83    * Constructor.
 84    *
 85    * @param url Parsed file.
 86    * @param exception Exception to wrap.
 87    */
 88  0 public XmlFileException(final URL url, final Exception exception) {
 89  0 super(exception);
 90  0 this.errorCode = 9997;
 91  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 92  0 referenceArea = null;
 93    }
 94   
 95    /**
 96    * Constructor.
 97    *
 98    * @param file Parsed file.
 99    * @param exception Exception to wrap.
 100    */
 101  0 public XmlFileException(final File file, final Exception exception) {
 102  0 super(exception);
 103  0 this.errorCode = 9998;
 104  0 final URL url = IoUtility.toUrl(file);
 105  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 106  0 referenceArea = null;
 107    }
 108   
 109    /**
 110    * Constructor.
 111    *
 112    * @param exception Exception to wrap.
 113    */
 114  1 public XmlFileException(final Exception exception) {
 115  1 super(exception);
 116  1 this.errorCode = 9999;
 117  1 errorArea = null;
 118  1 referenceArea = null;
 119    }
 120   
 121    /**
 122    * Constructor.
 123    *
 124    * @param exception Exception to wrap.
 125    */
 126  0 public XmlFileException(final IOException exception) {
 127  0 super(exception);
 128  0 this.errorCode = 9997;
 129  0 errorArea = null;
 130  0 referenceArea = null;
 131    }
 132   
 133    /**
 134    * Constructor.
 135    *
 136    * @param exception Exception to wrap.
 137    */
 138  0 public XmlFileException(final SAXException exception) {
 139  0 super(exception);
 140  0 this.errorCode = SyntaxException.SAX_PARSER_EXCEPTION;
 141  0 errorArea = null;
 142  0 referenceArea = null;
 143    }
 144   
 145    /**
 146    * Get position information about error location.
 147    *
 148    * @return Error location position.
 149    */
 150  26 public final SourceArea getSourceArea() {
 151  26 return errorArea;
 152    }
 153   
 154    /**
 155    * Get additional position information about another associated location.
 156    *
 157    * @return Additional error location position.
 158    */
 159  0 public final SourceArea getReferenceArea() {
 160  0 return referenceArea;
 161    }
 162   
 163    /**
 164    * Get error code.
 165    *
 166    * @return Error code.
 167    */
 168  18 public final int getErrorCode() {
 169  18 return errorCode;
 170    }
 171   
 172    /**
 173    * Get line that is referenced by {@link #getSourceArea()}.
 174    *
 175    * @return Referenced line.
 176    */
 177  4 public final String getLine() {
 178  4 if (line == null) {
 179  4 line = "";
 180  4 try {
 181  4 final TextInput input = new TextInput(errorArea.getLocalAddress());
 182  4 input.setRow(errorArea.getStartPosition().getLine());
 183  4 input.setColumn(errorArea.getStartPosition().getColumn());
 184  4 line = input.getLine();
 185    } catch (Exception e) {
 186    // ignore
 187    }
 188    }
 189  4 return line;
 190    }
 191   
 192  5 public final String getMessage() {
 193  5 if (getCause() != null) {
 194  5 if (getCause().getCause() != null) {
 195  3 return getCause().getCause().getMessage();
 196    }
 197  2 return getCause().getMessage();
 198    }
 199  0 return "";
 200    }
 201   
 202    /**
 203    * Get detailed error description.
 204    * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
 205    * The second line contains the local address, the line and column.
 206    * Third line is the result or {@link #getLine()}.
 207    * In the fourth line the row position for the third line is marked.
 208    *
 209    * <p>TODO mime 20070219: rework description: add end (and perhaps reference) information
 210    *
 211    * @return Error description.
 212    */
 213  5 public final String getDescription() {
 214  5 final StringBuffer buffer = new StringBuffer();
 215  5 buffer.append(getErrorCode() + ": " + getMessage());
 216  5 if (errorArea != null && errorArea.getStartPosition() != null) {
 217  4 final SourcePosition start = errorArea.getStartPosition();
 218  4 buffer.append("\n");
 219  4 buffer.append(start.getLocalAddress() + ":" + start.getLine() + ":"
 220    + start.getColumn());
 221  4 buffer.append("\n");
 222  4 buffer.append(ReplaceUtility.replace(getLine(), "\t", " "));
 223  4 buffer.append("\n");
 224  4 final StringBuffer whitespace = IoUtility.getSpaces(start.getColumn() - 1);
 225  4 buffer.append(whitespace);
 226  4 buffer.append("^");
 227    }
 228  5 return buffer.toString();
 229    }
 230   
 231  4 public final String toString() {
 232  4 return getDescription();
 233    }
 234    }