1 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 ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 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 errorList = new ArrayList (); 21 private List warningList = new ArrayList (); 22 private List recognitionExceptions = new ArrayList (); 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 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 message) { 42 hqlLog.debug( message ); 43 warningList.add( message ); 44 } 45 46 private String getErrorString() { 47 StringBuffer buf = new StringBuffer (); 48 for ( Iterator iterator = errorList.iterator(); iterator.hasNext(); ) { 49 buf.append( ( String ) 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 if ( log.isDebugEnabled() ) { 68 log.debug( "throwQueryException() : no errors" ); 69 } 70 } 71 } 72 } 73 | Popular Tags |