001: /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
002: /*
003: * (c) Copyright 2001-2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP All rights
004: * reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met: 1.
008: * Redistributions of source code must retain the above copyright notice, this
009: * list of conditions and the following disclaimer. 2. Redistributions in
010: * binary form must reproduce the above copyright notice, this list of
011: * conditions and the following disclaimer in the documentation and/or other
012: * materials provided with the distribution. 3. The name of the author may not
013: * be used to endorse or promote products derived from this software without
014: * specific prior written permission.
015: *
016: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
017: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
019: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
020: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
021: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
022: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
023: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
024: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
025: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
026: * * $Id: ParseException.java,v 1.17 2008/01/02 12:06:45 andy_seaborne Exp $
027: *
028: * AUTHOR: Jeremy J. Carroll
029: */
030: package com.hp.hpl.jena.rdf.arp;
031:
032: import org.xml.sax.SAXParseException;
033:
034: import com.hp.hpl.jena.rdf.arp.impl.Location;
035:
036: /**
037: * An exception during the RDF processing of ARP. Note: it is distinguished from
038: * an XML related exception from Xerces because while both are
039: * SAXParseException's, the latter are not com.hp.hpl.jena.arp.ParseException's.
040: *
041: */
042: public class ParseException extends SAXParseException implements
043: ARPErrorNumbers {
044:
045: /**
046: *
047: */
048: private static final long serialVersionUID = -5986976549492477885L;
049: final int id;
050:
051: protected ParseException(int id, Location where, String msg) {
052: super (msg, where.inputName, null, where.endLine,
053: where.endColumn);
054: this .id = id;
055:
056: }
057:
058: public ParseException(int id, Location where, Exception e) {
059: super (e.getMessage(), where.inputName, null, where.endLine,
060: where.endColumn, e);
061: if (getCause() == null)
062: initCause(e);
063: this .id = id;
064: }
065:
066: /**
067: * The error number (from {@link ARPErrorNumbers}) related to this
068: * exception.
069: *
070: * @return The error number.
071: */
072: public int getErrorNumber() {
073: return id;
074: }
075:
076: /**
077: * Is this error an RDF syntax error.
078: * A syntax error indicates that well-formed XML,
079: * uses RDF properties and attributes, and whitespace
080: * and XML elements, in a way that does not conform with
081: * the RDF/XML Syntax (Revised) specification.
082: * (Currently most such errors have code
083: * {@link ARPErrorNumbers#ERR_SYNTAX_ERROR},
084: * but this may change in the future).
085: * @return True if this is a syntax error
086: */
087: public boolean isSyntaxError() {
088: switch (id) {
089: case ERR_SYNTAX_ERROR:
090: case ERR_BAD_RDF_ELEMENT:
091: case ERR_BAD_RDF_ATTRIBUTE:
092: case ERR_LI_AS_TYPE:
093: case ERR_NOT_WHITESPACE:
094: return true;
095: }
096: return false;
097: }
098:
099: SAXParseException rootCause() {
100: Exception e = getException();
101: return e == null ? this : (SAXParseException) e;
102: }
103:
104: private boolean promoteMe;
105:
106: /**
107: * Intended for use within an RDFErrorHandler. This method is untested.
108: * Marks the exception to be promoted to be thrown from the parser's entry
109: * method.
110: */
111: public void promote() {
112: promoteMe = true;
113: }
114:
115: /**
116: * The message without location information. Use either the formatMessage
117: * method, or the SAXParseException interface, to access the location
118: * information.
119: *
120: * @return The exception message.
121: */
122: public String getMessage() {
123: // turn 1 to W001
124: // turn 204 to E204
125: String idStr = id != 0 ? "{" + (id < 200 ? "W" : "E")
126: + ("" + (1000 + id)).substring(1) + "} " : "";
127:
128: return idStr + super .getMessage();
129: }
130:
131: /**
132: * Calls e.getMessage() and also accesses line and column information for
133: * SAXParseException's.
134: *
135: * @return e.getMessage() possibly prepended by error location information.
136: * @param e
137: * The exception to describe.
138: */
139: static public String formatMessage(Exception e) {
140: String msg = e.getMessage();
141: if (msg == null)
142: msg = e.toString();
143: if (!(e instanceof SAXParseException))
144: return msg;
145: SAXParseException sax = (SAXParseException) e;
146: String file = sax.getSystemId();
147: if (file == null)
148: file = sax.getPublicId();
149: String rslt = file == null ? "" : file;
150: if (sax.getLineNumber() == -1)
151: return (file != null ? (file + ": ") : "") + msg;
152:
153: if (sax.getColumnNumber() == -1) {
154: return rslt + "(line " + sax.getLineNumber() + "): " + msg;
155: }
156: return rslt + "(line " + sax.getLineNumber() + " column "
157: + sax.getColumnNumber() + "): " + msg;
158:
159: }
160:
161: public boolean isPromoted() {
162: return promoteMe;
163: }
164:
165: /**
166: * The string from
167: * {@link ARPErrorNumbers} associated with an integer error code
168: * @param errNo An error code from {@link ARPErrorNumbers}.
169: * @return The field name from {@link ARPErrorNumbers} with this error number, or null
170: */
171: static public String errorCodeName(int errNo) {
172: Class c = ARPErrorNumbers.class;
173: java.lang.reflect.Field flds[] = c.getDeclaredFields();
174: for (int i = 0; i < flds.length; i++) {
175: try {
176: if (flds[i].getInt(null) == errNo)
177: return flds[i].getName();
178: } catch (Exception e) {
179: // ignore exceptions
180: }
181: }
182: return null;
183: }
184:
185: /**
186: * The integer code associated with a string from
187: * {@link ARPErrorNumbers}.
188: * @param upper A field name from {@link ARPErrorNumbers}, (in upper case).
189: * @return The integer value or -1, if none.
190: */
191: static public int errorCode(String upper) {
192: Class c = ARPErrorNumbers.class;
193: try {
194: java.lang.reflect.Field fld = c.getField(upper);
195: return fld.getInt(null);
196: } catch (Exception e) {
197: return -1;
198: }
199: }
200:
201: }
|