001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.rio;
007:
008: import org.openrdf.OpenRDFException;
009:
010: /**
011: * A parse exception that can be thrown by a parser when it encounters an error
012: * from which it cannot or doesn't want to recover.
013: */
014: public class RDFParseException extends OpenRDFException {
015:
016: /*-----------*
017: * Constants *
018: *-----------*/
019:
020: private static final long serialVersionUID = -4686126837948873012L;
021:
022: private final int lineNo;
023:
024: private final int columnNo;
025:
026: /*--------------*
027: * Constructors *
028: *--------------*/
029:
030: /**
031: * Creates a new ParseException.
032: *
033: * @param msg
034: * An error message.
035: */
036: public RDFParseException(String msg) {
037: this (msg, -1, -1);
038: }
039:
040: /**
041: * Creates a new ParseException.
042: *
043: * @param msg
044: * An error message.
045: * @param lineNo
046: * A line number associated with the message.
047: * @param columnNo
048: * A column number associated with the message.
049: */
050: public RDFParseException(String msg, int lineNo, int columnNo) {
051: super (msg + getLocationString(lineNo, columnNo));
052: this .lineNo = lineNo;
053: this .columnNo = columnNo;
054: }
055:
056: /**
057: * Creates a new ParseException wrapping another exception. The
058: * ParseException will inherit its message from the supplied source
059: * exception.
060: *
061: * @param t
062: * The source exception.
063: */
064: public RDFParseException(Throwable t) {
065: this (t, -1, -1);
066: }
067:
068: /**
069: * Creates a new ParseException wrapping another exception. The
070: * ParseException will inherit its message from the supplied source
071: * exception.
072: *
073: * @param t
074: * The source exception.
075: * @param lineNo
076: * A line number associated with the message.
077: * @param columnNo
078: * A column number associated with the message.
079: */
080: public RDFParseException(Throwable t, int lineNo, int columnNo) {
081: super (t.getMessage() + getLocationString(lineNo, columnNo), t);
082: this .lineNo = lineNo;
083: this .columnNo = columnNo;
084: }
085:
086: /*---------*
087: * Methods *
088: *---------*/
089:
090: /**
091: * Gets the line number associated with this parse exception.
092: *
093: * @return A line number, or -1 if no line number is available or applicable.
094: */
095: public int getLineNumber() {
096: return lineNo;
097: }
098:
099: /**
100: * Gets the column number associated with this parse exception.
101: *
102: * @return A column number, or -1 if no column number is available or
103: * applicable.
104: */
105: public int getColumnNumber() {
106: return columnNo;
107: }
108:
109: /**
110: * Creates a string to that shows the specified line and column number.
111: * Negative line numbers are interpreted as unknowns. Example output: "[line
112: * 12, column 34]". If the specified line number is negative, this method
113: * returns an empty string.
114: */
115: public static String getLocationString(int lineNo, int columnNo) {
116: if (lineNo < 0) {
117: return "";
118: }
119:
120: StringBuilder sb = new StringBuilder(16);
121: sb.append(" [line ");
122: sb.append(lineNo);
123:
124: if (columnNo >= 0) {
125: sb.append(", column ");
126: sb.append(columnNo);
127: }
128:
129: sb.append("]");
130: return sb.toString();
131: }
132: }
|