KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > ParseException


1 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
2 /*
3  * (c) Copyright 2001-2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met: 1.
8  * Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer. 2. Redistributions in
10  * binary form must reproduce the above copyright notice, this list of
11  * conditions and the following disclaimer in the documentation and/or other
12  * materials provided with the distribution. 3. The name of the author may not
13  * be used to endorse or promote products derived from this software without
14  * specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * * $Id: ParseException.java,v 1.8 2005/02/21 12:09:15 andy_seaborne Exp $
27  *
28  * AUTHOR: Jeremy J. Carroll
29  */

30 package com.hp.hpl.jena.rdf.arp;
31
32 import org.xml.sax.SAXParseException JavaDoc;
33 import java.util.*;
34
35 /**
36  * An exception during the RDF processing of ARP. Note: it is distinguished
37  * from an XML related exception from Xerces because while both are
38  * SAXParseException's, the latter are not
39  * com.hp.hpl.jena.arp.ParseException's.
40  *
41  */

42 public class ParseException
43     extends SAXParseException JavaDoc
44     implements ARPErrorNumbers, RDFParserConstants {
45
46     /**
47      * @param cTok
48      * @param expectedTokenSequencesVal
49      * @param tokenImageVal
50      */

51     ParseException(
52         Token cTok,
53         int[][] expectedTokenSequencesVal,
54         String JavaDoc[] tokenImageVal) {
55         super(null, publicId(cTok), systemId(cTok), line(cTok), col(cTok));
56         specialConstructor = true;
57         where = cTok.location;
58         currentToken = cTok;
59         expectedTokenSequences = expectedTokenSequencesVal;
60         id = ERR_SYNTAX_ERROR;
61         tokenImage = tokenImageVal;
62     }
63
64     private static String JavaDoc systemId(Token c) {
65         return c.location != null ? c.location.inputName : null;
66     }
67     private static String JavaDoc publicId(Token c) {
68         return null;
69     }
70     private static int line(Token c) {
71         return c.location != null ? c.location.endLine : -1;
72     }
73     private static int col(Token c) {
74         return c.location != null ? c.location.endColumn : -1;
75     }
76
77     ParseException(int id, Location where, String JavaDoc msg, SAXParseException JavaDoc e) {
78         super(msg, where.inputName, null, where.endLine, where.endColumn, e);
79         this.where = where;
80         this.id = id;
81         specialConstructor = false;
82     }
83
84     ParseException(int id, Location where, String JavaDoc msg) {
85         super(msg, where.inputName, null, where.endLine, where.endColumn);
86         this.where = where;
87         this.id = id;
88         specialConstructor = false;
89     }
90
91     ParseException(int id, String JavaDoc message) {
92         super(message, null);
93         this.id = id;
94         specialConstructor = false;
95     }
96
97     ParseException() {
98         super(null, null);
99         specialConstructor = false;
100     }
101     SAXParseException JavaDoc rootCause() {
102         Exception JavaDoc e = getException();
103         return e == null ? this : (SAXParseException JavaDoc) e;
104     }
105
106     private int id;
107     /**
108      * The error number (from {@link ARPErrorNumbers}) related to this
109      * exception.
110      *
111      * @return The error number.
112      */

113     public int getErrorNumber() {
114         return id;
115     }
116     private Location where;
117
118     boolean specialConstructor;
119
120     Token currentToken;
121
122     private boolean isFatal;
123     void setFatal(boolean v) {
124         isFatal = v;
125     }
126     boolean getFatal() {
127         return isFatal;
128     }
129
130     int[][] expectedTokenSequences;
131     private String JavaDoc tokenImage[];
132
133     boolean promoteMe;
134
135     /**
136      * Intended for use within an RDFErrorHandler. This method is untested.
137      * Marks the exception to be promoted to be thrown from the
138      * parser's entry method.
139      */

140     public void promote() {
141         promoteMe = true;
142     }
143
144     private Token startAttributes;
145
146     void setStartAttribute(Token t) {
147         startAttributes = t;
148     }
149     private void startAttrSkip(Token upto) {
150         Token t;
151         for (t = startAttributes; t != upto.next; t = t.next) {
152             switch (t.kind) {
153                 case A_XMLBASE :
154                 case A_XMLLANG :
155                 case AV_STRING :
156                 case X_WARNING :
157                 case A_XMLNS :
158                     continue;
159                 default :
160                     startAttributes = t;
161                     return;
162             }
163         }
164     }
165
166     private String JavaDoc getAttributes(Token upto) {
167         Token t;
168         String JavaDoc rslt = "";
169         for (t = startAttributes; t != upto.next; t = t.next) {
170             switch (t.kind) {
171                 case A_TYPE :
172                     rslt += ", rdf:type";
173                     break;
174                 case A_ABOUT :
175                     rslt += ", rdf:about";
176                     break;
177                 case A_DATATYPE :
178                     rslt += ", rdf:datatype";
179                     break;
180                 case A_NODEID :
181                     rslt += ", rdf:nodeID";
182                     break;
183                 case A_ID :
184                     rslt += ", rdf:ID";
185                     break;
186                 case A_PARSETYPE :
187                     rslt += ", rdf:parseType";
188                     break;
189                 case A_RESOURCE :
190                     rslt += ", rdf:resource";
191                     break;
192                 case A_RDF_N :
193                     rslt += ", rdf:_NNN";
194                     break;
195
196                 case A_OTHER :
197                     rslt += ", a property attribute";
198                     break;
199
200                 case AV_LITERAL :
201                     rslt += "='Literal'";
202                     break;
203                 case AV_RESOURCE :
204                     rslt += "='Resource'";
205                     break;
206                 case AV_DAMLCOLLECTION :
207                     rslt += "='daml:collection'";
208                     break;
209
210                 case AV_STRING :
211                 case X_WARNING :
212                     continue;
213                 case A_XMLNS :
214                 case A_XMLBASE :
215                 case A_XMLLANG :
216                 default :
217                     String JavaDoc msg =
218                         "Internal mishap in ParseException.getAttributes()";
219                     System.err.println(msg);
220                     return msg;
221             }
222         }
223         return rslt.length() > 2 ? rslt.substring(2) : rslt;
224     }
225
226     /**
227      * The message without location information. Use either the formatMessage
228      * method, or the SAXParseException interface, to access the location
229      * information.
230      *
231      * @return The exception message.
232      */

233     public String JavaDoc getMessage() {
234         // turn 1 to W001
235
// turn 204 to E204
236
String JavaDoc idStr =
237             id != 0
238                 ? "{"
239                     + (id < 200 ? "W" : "E")
240                     + ("" + (1000 + id)).substring(1)
241                     + "} "
242                 : "";
243         if (!specialConstructor) {
244             return idStr + super.getMessage();
245         }
246         Token tok = currentToken.next;
247         startAttrSkip(tok);
248         String JavaDoc retval =
249             "Syntax error when processing " + tok.toString() + "." + eol;
250         // First check for the following case:
251
// <property>
252
// <obj1 about="ffoo"/>
253
// *HERE*<obj2 about="error"/>
254
// </property>
255
if (isElementStripingProblem(tok)) {
256             return idStr
257                 + retval
258                 + "Cannot have another XML element here."
259                 + eol
260                 + "(Maybe one object has already been given as the value of the enclosing property).";
261         }
262         switch (tok.kind) {
263             case CD_STRING :
264                 break;
265             case EOF :
266                 retval
267                     += "Input to RDF parser ended prematurely. This is often related to an XML parser abort."
268                     + eol;
269                 break;
270
271             case E_DESCRIPTION :
272                 return idStr
273                     + retval
274                     + "rdf:Description elements generally may"
275                     + eol
276                     + "only occur to describe an object.";
277             case E_RDF :
278                 return idStr
279                     + retval
280                     + "rdf:RDF element tags generally may not occur inside RDF content.";
281             case A_OTHER :
282             case A_RDF_N :
283                 if (startAttributes != null) {
284                     if (startAttributes == tok) {
285                         return idStr
286                             + retval
287                             + "Cannot have property attributes in this context.";
288                     } else {
289                         return idStr
290                             + retval
291                             + "Cannot have property attributes with the following other attributes:"
292                             + eol
293                             + " "
294                             + getAttributes(currentToken);
295                     }
296                 }
297             case A_TYPE :
298                 if (startAttributes != null) {
299                     if (startAttributes == tok) {
300                         return idStr
301                             + retval
302                             + "Cannot have rdf:type attribute in this context.";
303                     } else {
304                         return idStr
305                             + retval
306                             + "Cannot have rdf:type attribute with the following other attributes:"
307                             + eol
308                             + " "
309                             + getAttributes(currentToken);
310                     }
311                 }
312             case A_ABOUT :
313             case A_ID :
314             case A_PARSETYPE :
315             case A_RESOURCE :
316             case A_NODEID :
317             case A_DATATYPE :
318                 if (startAttributes != null) {
319                     if (startAttributes == tok) {
320                         return idStr
321                             + retval
322                             + "Cannot have "
323                             + tokenImage[tok.kind]
324                             + " in this context.";
325                     } else {
326                         return idStr
327                             + retval
328                             + "In this context, the following attributes are not allowed together:"
329                             + eol
330                             + " "
331                             + getAttributes(tok);
332                     }
333                 }
334
335             case A_XMLBASE :
336             case A_XMLLANG :
337             case A_XMLNS :
338             default :
339                 retval = "Unusual " + retval;
340
341         }
342         String JavaDoc expected = "";
343         int maxSize = 0;
344         BitSet suppress = new BitSet();
345         suppress.set(X_WARNING);
346         for (int i = 0; i < expectedTokenSequences.length; i++) {
347             switch (expectedTokenSequences[i][0]) {
348                 case E_OTHER :
349                     suppress.set(E_LI);
350                     suppress.set(E_RDF_N);
351                     break;
352                 case A_OTHER :
353                     suppress.set(A_RDF_N);
354                     break;
355                 case CD_STRING :
356                     suppress.set(COMMENT);
357                     suppress.set(PROCESSING_INSTRUCTION);
358                     break;
359             }
360         }
361         for (int i = 0; i < expectedTokenSequences.length; i++) {
362             if (suppress.get(expectedTokenSequences[i][0]))
363                 continue;
364             if (maxSize < expectedTokenSequences[i].length) {
365                 maxSize = expectedTokenSequences[i].length;
366             }
367             for (int j = 0; j < expectedTokenSequences[i].length; j++) {
368                 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
369             }
370             expected += eol + " ";
371         }
372         retval += "Encountered ";
373         for (int i = 0; i < maxSize; i++) {
374             if (i != 0)
375                 retval += " ";
376             retval += tok.toString();
377             if (tok.kind == 0) {
378                 break;
379             }
380             tok = tok.next;
381         }
382         if (expectedTokenSequences.length == 1) {
383             retval += " Was expecting:" + eol + " ";
384         } else {
385             retval += " Was expecting one of:" + eol + " ";
386         }
387         retval += expected;
388         return idStr + retval;
389
390     }
391     static final private int elementStriping[] =
392         new int[] {
393             CD_STRING,
394             PROCESSING_INSTRUCTION,
395             COMMENT,
396             X_SAX_EX,
397             E_END };
398     static {
399         Arrays.sort(elementStriping);
400     }
401     private boolean isElementStripingProblem(Token tok) {
402         if ( expectedTokenSequences.length != elementStriping.length )
403             return false;
404         int e[] = new int[elementStriping.length];
405         for (int i=0;i<e.length; i++)
406             e[i] = expectedTokenSequences[i][0];
407         Arrays.sort(e);
408         for (int i=0;i<e.length;i++)
409             if (e[i]!=elementStriping[i])
410                 return false;
411        return true;
412     }
413
414     /**
415      * The end of line string for this machine.
416      */

417     static String JavaDoc eol;
418
419     static {
420         try {
421             eol = System.getProperty("line.separator", "\n");
422         } catch (SecurityException JavaDoc e) {
423             eol = "\n";
424         }
425     }
426
427     /**
428      * Calls e.getMessage() and also accesses line and column information for
429      * SAXParseException's.
430      *
431      * @return e.getMessage() possibly prepended by error location information.
432      * @param e
433      * The exception to describe.
434      */

435     static public String JavaDoc formatMessage(Exception JavaDoc e) {
436         String JavaDoc msg = e.getMessage();
437         if (msg == null)
438             msg = e.toString();
439         if (e instanceof SAXParseException JavaDoc) {
440             SAXParseException JavaDoc sax = (SAXParseException JavaDoc) e;
441             String JavaDoc file = sax.getSystemId();
442             if (file == null)
443                 file = sax.getPublicId();
444             String JavaDoc rslt = file == null ? "" : file;
445             if (sax.getLineNumber() != -1) {
446                 if (sax.getColumnNumber() == -1) {
447                     return rslt + "[" + sax.getLineNumber() + "]: " + msg;
448                 } else {
449                     return rslt
450                         + "["
451                         + sax.getLineNumber()
452                         + ":"
453                         + sax.getColumnNumber()
454                         + "]: "
455                         + msg;
456                 }
457             } else {
458                 return (file != null ? (file + ": ") : "") + msg;
459             }
460         } else {
461             return msg;
462         }
463     }
464
465 }
466
Popular Tags