KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > ast > ErrorCounter


1 // $Id: ErrorCounter.java,v 1.9 2005/07/16 22:25:42 oneovthafew Exp $
2
package org.hibernate.hql.ast;
3
4 import antlr.RecognitionException;
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.hibernate.QueryException;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 /**
14  * An error handler that counts parsing errors and warnings.
15  */

16 public class ErrorCounter implements ParseErrorHandler {
17     private Log log = LogFactory.getLog( ErrorCounter.class );
18     private Log hqlLog = LogFactory.getLog( "org.hibernate.hql.PARSER" );
19
20     private List JavaDoc errorList = new ArrayList JavaDoc();
21     private List JavaDoc warningList = new ArrayList JavaDoc();
22     private List JavaDoc recognitionExceptions = new ArrayList JavaDoc();
23
24     public void reportError(RecognitionException e) {
25         reportError( e.toString() );
26         recognitionExceptions.add( e );
27         if ( log.isDebugEnabled() ) {
28             log.debug( e, e );
29         }
30     }
31
32     public void reportError(String JavaDoc message) {
33         hqlLog.error( message );
34         errorList.add( message );
35     }
36
37     public int getErrorCount() {
38         return errorList.size();
39     }
40
41     public void reportWarning(String JavaDoc message) {
42         hqlLog.debug( message );
43         warningList.add( message );
44     }
45
46     private String JavaDoc getErrorString() {
47         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
48         for ( Iterator JavaDoc iterator = errorList.iterator(); iterator.hasNext(); ) {
49             buf.append( ( String JavaDoc ) iterator.next() );
50             if ( iterator.hasNext() ) buf.append( "\n" );
51
52         }
53         return buf.toString();
54     }
55
56     public void throwQueryException() throws QueryException {
57         if ( getErrorCount() > 0 ) {
58             if ( recognitionExceptions.size() > 0 ) {
59                 throw new QuerySyntaxException( ( RecognitionException ) recognitionExceptions.get( 0 ) );
60             }
61             else {
62                 throw new QueryException( getErrorString() );
63             }
64         }
65         else {
66             // all clear
67
if ( log.isDebugEnabled() ) {
68                 log.debug( "throwQueryException() : no errors" );
69             }
70         }
71     }
72 }
73
Popular Tags