Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: So Feb 25 2007 22:22:30 CET
file stats: LOC: 280   Methods: 14
NCLOC: 148   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
XmlFilePositionException.java 16,7% 19,7% 21,4% 19,6%
coverage coverage
 1    /* $Id: XmlFilePositionException.java,v 1.2 2007/02/25 20:05:38 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.xml.mapper;
 19   
 20    import java.io.File;
 21    import java.io.IOException;
 22    import java.net.URL;
 23   
 24    import javax.xml.parsers.ParserConfigurationException;
 25   
 26    import org.qedeq.kernel.base.module.Qedeq;
 27    import org.qedeq.kernel.bo.module.ModuleDataException;
 28    import org.qedeq.kernel.context.ModuleContext;
 29    import org.qedeq.kernel.log.Trace;
 30    import org.qedeq.kernel.utility.IoUtility;
 31    import org.qedeq.kernel.utility.ReplaceUtility;
 32    import org.qedeq.kernel.utility.TextInput;
 33    import org.qedeq.kernel.xml.parser.SourceArea;
 34    import org.qedeq.kernel.xml.parser.SourcePosition;
 35    import org.qedeq.kernel.xml.parser.SyntaxException;
 36    import org.qedeq.kernel.xml.tracker.SimpleXPath;
 37    import org.qedeq.kernel.xml.tracker.XPathLocationFinder;
 38    import org.xml.sax.SAXException;
 39   
 40   
 41    /**
 42    * Data validation error. Occurs if a set or add method leads to wrong or inconsistent data.
 43    *
 44    * @version $Revision: 1.2 $
 45    * @author Michael Meyling
 46    */
 47    public final class XmlFilePositionException extends Exception {
 48   
 49    /** Serialization information. */
 50    private static final long serialVersionUID = -4109767904038020052L;
 51   
 52    /** Error code of this Exception. */
 53    private final int errorCode;
 54   
 55    /** Start of error location. */
 56    private final SourceArea errorArea;
 57   
 58    /** End of error location. */
 59    private final SourceArea referenceArea;
 60   
 61    /** Referenced line with marker. */
 62    private String line;
 63   
 64    /**
 65    * Constructor.
 66    *
 67    * @param qedeq Exception occurred in this QEDEQ module.
 68    * @param exception Exception to wrap.
 69    */
 70  1 public XmlFilePositionException(final Qedeq qedeq, final ModuleDataException exception) {
 71  1 super(exception);
 72  1 this.errorCode = exception.getErrorCode();
 73  1 this.errorArea = createSourceArea(qedeq, exception.getContext());
 74  1 this.referenceArea = createSourceArea(qedeq, exception.getReferenceContext());
 75    }
 76   
 77   
 78    /**
 79    * Get area in XML source file for QEDEQ module context.
 80    *
 81    * @param qedeq Look at this QEDEQ module.
 82    * @param context Search for this context.
 83    * @return Created file area. Maybe <code>null</code>.
 84    */
 85  2 public static SourceArea createSourceArea(final Qedeq qedeq, final ModuleContext context) {
 86  2 final String method = "createSourceArea(Qedeq, ModuleContext)";
 87  2 if (qedeq == null || context == null) {
 88  0 return null;
 89    }
 90  2 final String xpath;
 91  2 try {
 92  2 xpath = Context2SimpleXPath.getXPath(context, qedeq).toString();
 93    } catch (ModuleDataException e) {
 94  0 Trace.trace(XmlFilePositionException.class, method, e);
 95  0 return null;
 96    };
 97   
 98  2 SimpleXPath find = null;
 99  2 try {
 100  2 find = XPathLocationFinder.getXPathLocation(
 101    context.getModuleLocation(), xpath);
 102  2 if (find.getStartLocation() == null) {
 103  0 return null;
 104    }
 105  2 return new SourceArea(find.getStartLocation().getAddress(),
 106    find.getStartLocation().getLocalAddress(), find.getStartLocation(),
 107    find.getEndLocation());
 108    } catch (ParserConfigurationException e) {
 109  0 Trace.trace(XmlFilePositionException.class, method, e);
 110    } catch (SAXException e) {
 111  0 Trace.trace(XmlFilePositionException.class, method, e);
 112    } catch (IOException e) {
 113  0 Trace.trace(XmlFilePositionException.class, method, e);
 114    }
 115  0 return null;
 116    }
 117   
 118    /**
 119    * Constructor.
 120    *
 121    * @param exception Exception to wrap.
 122    */
 123  0 public XmlFilePositionException(final SyntaxException exception) {
 124  0 super(exception);
 125  0 this.errorCode = exception.getErrorCode();
 126  0 errorArea = new SourceArea(exception.getErrorPosition().getAddress(),
 127    exception.getErrorPosition().getLocalAddress(), exception.getErrorPosition(), null);
 128  0 referenceArea = null;
 129    }
 130   
 131    /**
 132    * Constructor.
 133    *
 134    * @param url Parsed file.
 135    * @param exception Exception to wrap.
 136    */
 137  0 public XmlFilePositionException(final URL url, final Exception exception) {
 138  0 super(exception);
 139  0 this.errorCode = 9997;
 140  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 141  0 referenceArea = null;
 142    }
 143   
 144    /**
 145    * Constructor.
 146    *
 147    * @param file Parsed file.
 148    * @param exception Exception to wrap.
 149    */
 150  0 public XmlFilePositionException(final File file, final Exception exception) {
 151  0 super(exception);
 152  0 this.errorCode = 9998;
 153  0 final URL url = IoUtility.toUrl(file);
 154  0 errorArea = new SourceArea(url, new SourcePosition(url, 1, 1), null);
 155  0 referenceArea = null;
 156    }
 157   
 158    /**
 159    * Constructor.
 160    *
 161    * @param exception Exception to wrap.
 162    */
 163  0 public XmlFilePositionException(final Exception exception) {
 164  0 super(exception);
 165  0 this.errorCode = 9999;
 166  0 errorArea = null;
 167  0 referenceArea = null;
 168    }
 169   
 170    /**
 171    * Constructor.
 172    *
 173    * @param exception Exception to wrap.
 174    */
 175  0 public XmlFilePositionException(final IOException exception) {
 176  0 super(exception);
 177  0 this.errorCode = 9997;
 178  0 errorArea = null;
 179  0 referenceArea = null;
 180    }
 181   
 182    /**
 183    * Constructor.
 184    *
 185    * @param exception Exception to wrap.
 186    */
 187  0 public XmlFilePositionException(final SAXException exception) {
 188  0 super(exception);
 189  0 this.errorCode = SyntaxException.SAX_PARSER_EXCEPTION;
 190  0 errorArea = null;
 191  0 referenceArea = null;
 192    }
 193   
 194    /**
 195    * Get position information about error location.
 196    *
 197    * @return Error location position.
 198    */
 199  2 public final SourceArea getSourceArea() {
 200  2 return errorArea;
 201    }
 202   
 203    /**
 204    * Get additional position information about another associated location.
 205    *
 206    * @return Additional error location position.
 207    */
 208  0 public final SourceArea getReferenceArea() {
 209  0 return referenceArea;
 210    }
 211   
 212    /**
 213    * Get error code.
 214    *
 215    * @return Error code.
 216    */
 217  0 public final int getErrorCode() {
 218  0 return errorCode;
 219    }
 220   
 221    /**
 222    * Get line that is referenced by {@link #getSourceArea()}.
 223    *
 224    * @return Referenced line.
 225    */
 226  0 public final String getLine() {
 227  0 if (line == null) {
 228  0 line = "";
 229  0 try {
 230  0 final TextInput input = new TextInput(errorArea.getLocalAddress());
 231  0 input.setRow(errorArea.getStartPosition().getLine());
 232  0 input.setColumn(errorArea.getStartPosition().getColumn());
 233  0 line = input.getLine();
 234    } catch (Exception e) {
 235    // ignore
 236    }
 237    }
 238  0 return line;
 239    }
 240   
 241  0 public final String getMessage() {
 242  0 if (getCause() != null) {
 243  0 if (getCause().getCause() != null) {
 244  0 return getCause().getCause().getMessage();
 245    }
 246  0 return getCause().getMessage();
 247    }
 248  0 return "";
 249    }
 250   
 251    /**
 252    * Get detailed error description.
 253    * The first line contains {@link #getErrorCode()} and {@link #getMessage()}.
 254    * The second line contains the local address, the line and column.
 255    * Third line is the result or {@link #getLine()}.
 256    * In the fourth line the row position for the third line is marked.
 257    *
 258    * <p>TODO mime 20070219: rework description: add end (and perhaps reference) information
 259    *
 260    * @return Error description.
 261    */
 262  0 public final String getDescription() {
 263  0 final StringBuffer buffer = new StringBuffer();
 264  0 buffer.append(getErrorCode() + ": " + getMessage());
 265  0 if (errorArea != null && errorArea.getStartPosition() != null) {
 266  0 final SourcePosition start = errorArea.getStartPosition();
 267  0 buffer.append("\n");
 268  0 buffer.append(start.getLocalAddress() + ":" + start.getLine() + ":"
 269    + start.getColumn());
 270  0 buffer.append("\n");
 271  0 buffer.append(ReplaceUtility.replace(getLine(), "\t", " "));
 272  0 buffer.append("\n");
 273  0 final StringBuffer whitespace = IoUtility.getSpaces(start.getColumn() - 1);
 274  0 buffer.append(whitespace);
 275  0 buffer.append("^");
 276    }
 277  0 return buffer.toString();
 278    }
 279   
 280    }