1 /*
2 * Hibernate, Relational Persistence for Idiomatic Java
3 *
4 * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
5 * indicated by the @author tags or express copyright attribution
6 * statements applied by the authors. All third-party contributions are
7 * distributed under license by Red Hat Middleware LLC.
8 *
9 * This copyrighted material is made available to anyone wishing to use, modify,
10 * copy, or redistribute it subject to the terms and conditions of the GNU
11 * Lesser General Public License, as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this distribution; if not, write to:
20 * Free Software Foundation, Inc.
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301 USA
23 *
24 */
25 package org.hibernate.hql.ast;
26
27 import antlr.RecognitionException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.hibernate.QueryException;
31
32 import java.util.ArrayList;
33 import java.util.Iterator;
34 import java.util.List;
35
36 /**
37 * An error handler that counts parsing errors and warnings.
38 */
39 public class ErrorCounter implements ParseErrorHandler {
40 private Logger log = LoggerFactory.getLogger( ErrorCounter.class );
41 private Logger hqlLog = LoggerFactory.getLogger( "org.hibernate.hql.PARSER" );
42
43 private List errorList = new ArrayList();
44 private List warningList = new ArrayList();
45 private List recognitionExceptions = new ArrayList();
46
47 public void reportError(RecognitionException e) {
48 reportError( e.toString() );
49 recognitionExceptions.add( e );
50 if ( log.isDebugEnabled() ) {
51 log.debug( e.toString(), e );
52 }
53 }
54
55 public void reportError(String message) {
56 hqlLog.error( message );
57 errorList.add( message );
58 }
59
60 public int getErrorCount() {
61 return errorList.size();
62 }
63
64 public void reportWarning(String message) {
65 hqlLog.debug( message );
66 warningList.add( message );
67 }
68
69 private String getErrorString() {
70 StringBuffer buf = new StringBuffer();
71 for ( Iterator iterator = errorList.iterator(); iterator.hasNext(); ) {
72 buf.append( ( String ) iterator.next() );
73 if ( iterator.hasNext() ) buf.append( "\n" );
74
75 }
76 return buf.toString();
77 }
78
79 public void throwQueryException() throws QueryException {
80 if ( getErrorCount() > 0 ) {
81 if ( recognitionExceptions.size() > 0 ) {
82 throw QuerySyntaxException.convert( ( RecognitionException ) recognitionExceptions.get( 0 ) );
83 }
84 else {
85 throw new QueryException( getErrorString() );
86 }
87 }
88 else {
89 // all clear
90 if ( log.isDebugEnabled() ) {
91 log.debug( "throwQueryException() : no errors" );
92 }
93 }
94 }
95 }