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