[hibernate-commits] Hibernate SVN: r15856 - core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Feb 2 12:15:35 EST 2009


Author: steve.ebersole at jboss.com
Date: 2009-02-02 12:15:35 -0500 (Mon, 02 Feb 2009)
New Revision: 15856

Added:
   core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/CopyableNode.java
   core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandler.java
   core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegate.java
   core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegateImpl.java
Modified:
   core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/NodeDeepCopier.java
Log:
HHH-2407 : HQL translation rework ->
HHH-3687 : parse (phase1)
HHH-3688 : normalize (phase2)

Added: core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/CopyableNode.java
===================================================================
--- core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/CopyableNode.java	                        (rev 0)
+++ core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/CopyableNode.java	2009-02-02 17:15:35 UTC (rev 15856)
@@ -0,0 +1,38 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.util;
+
+import antlr.collections.AST;
+
+/**
+ * Contract for nodes wishing to detail exactly how they should be shallow copied.
+ * <p/>
+ * Intended for use from {@link NodeDeepCopier#createShallowCopy} instead of the default shallow copy
+ * algorithm.
+ *
+ * @author Steve Ebersole
+ */
+public interface CopyableNode {
+	public AST createCopy();
+}

Copied: core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandler.java (from rev 15693, core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorReporter.java)
===================================================================
--- core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandler.java	                        (rev 0)
+++ core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandler.java	2009-02-02 17:15:35 UTC (rev 15856)
@@ -0,0 +1,56 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.util;
+
+import antlr.RecognitionException;
+
+/**
+ * Contract for handling error originating from the Antlr {@link antlr.TreeParser base} parser.  In fact,
+ * these methods are all redefinitions of the error handling methods defined there.
+ *
+ * @author Joshua Davis
+ * @author Steve Ebersole
+ */
+public interface ErrorHandler {
+	/**
+	 * Handle delegation of {@link antlr.TreeParser#reportError(antlr.RecognitionException)}
+	 *
+	 * @param recognitionException The Antlr recognition exception
+	 */
+	public void reportError(RecognitionException recognitionException);
+
+	/**
+	 * Handle delegation of {@link antlr.TreeParser#reportError(java.lang.String)}
+	 *
+	 * @param message The error message
+	 */
+	public void reportError(String message);
+
+	/**
+	 * Handle delegation of {@link antlr.TreeParser#reportWarning(String)}
+	 *
+	 * @param message The warning message
+	 */
+	public void reportWarning(String message);
+}
\ No newline at end of file

Copied: core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegate.java (from rev 15693, core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ParseErrorHandler.java)
===================================================================
--- core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegate.java	                        (rev 0)
+++ core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegate.java	2009-02-02 17:15:35 UTC (rev 15856)
@@ -0,0 +1,48 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.util;
+
+import org.hibernate.QueryException;
+
+/**
+ * Additional contract for providing delegation of {@link ErrorHandler} functionality.
+ *
+ * @author Joshua Davis
+ * @author Steve Ebersole
+ */
+public interface ErrorHandlerDelegate extends ErrorHandler {
+	/**
+	 * Get the number of errors current being tracked inside this delegate.
+	 *
+	 * @return The number of errors.
+	 */
+	public int getErrorCount();
+
+	/**
+	 * Construct and throw an appropriate query exception if required based on internal state.
+	 *
+	 * @throws QueryException If our internal states deems it necessary to generate a query exception
+	 */
+	public void throwQueryException() throws QueryException;
+}
\ No newline at end of file

Copied: core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegateImpl.java (from rev 15693, core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorCounter.java)
===================================================================
--- core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegateImpl.java	                        (rev 0)
+++ core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/ErrorHandlerDelegateImpl.java	2009-02-02 17:15:35 UTC (rev 15856)
@@ -0,0 +1,110 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.sql.ast.util;
+
+import antlr.RecognitionException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hibernate.QueryException;
+import org.hibernate.sql.ast.QuerySyntaxException;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * An implementation of the {@link ErrorHandlerDelegate} contract.
+ *
+ * @author Joshua Davis
+ * @author Steve Ebersole
+ */
+public class ErrorHandlerDelegateImpl implements ErrorHandlerDelegate {
+	private static final Log log = LogFactory.getLog( ErrorHandlerDelegateImpl.class );
+
+	private List errorList = new ArrayList();
+	private List recognitionExceptions = new ArrayList();
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void reportError(RecognitionException e) {
+		final String msg = e.toString();
+		reportError( msg );
+		recognitionExceptions.add( e );
+		if ( log.isTraceEnabled() ) {
+			log.trace( msg, e );
+		}
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void reportError(String message) {
+		log.error( message );
+		errorList.add( message );
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void reportWarning(String message) {
+		log.debug( message );
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public int getErrorCount() {
+		return errorList.size();
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	public void throwQueryException() throws QueryException {
+		if ( getErrorCount() > 0 ) {
+			if ( recognitionExceptions.size() > 0 ) {
+				throw new QuerySyntaxException( ( RecognitionException ) recognitionExceptions.get( 0 ) );
+			}
+			else {
+				throw new QueryException( generateErrorString() );
+			}
+		}
+		else {
+			// all clear
+			if ( log.isDebugEnabled() ) {
+				log.debug( "throwQueryException() : no errors" );
+			}
+		}
+	}
+	private String generateErrorString() {
+		StringBuffer buf = new StringBuffer();
+		for ( Iterator iterator = errorList.iterator(); iterator.hasNext(); ) {
+			buf.append( ( String ) iterator.next() );
+			if ( iterator.hasNext() ) buf.append( "\n" );
+
+		}
+		return buf.toString();
+	}
+}
\ No newline at end of file

Modified: core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/NodeDeepCopier.java
===================================================================
--- core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/NodeDeepCopier.java	2009-02-02 15:50:32 UTC (rev 15855)
+++ core/branches/SQL_GEN_REDESIGN/src/main/java/org/hibernate/sql/ast/util/NodeDeepCopier.java	2009-02-02 17:15:35 UTC (rev 15856)
@@ -27,7 +27,7 @@
 import antlr.ASTFactory;
 
 /**
- * TODO : javadoc
+ * Strategy for creating copies of AST trees.
  *
  * @author Steve Ebersole
  */
@@ -39,18 +39,34 @@
 	}
 
 	public AST copy(AST node) {
+		// copy the root (incoming) node
 		AST newNode = createShallowCopy( node );
+		// track a running reference to the child node we are currently processing.
 		AST child = node.getFirstChild();
 		while ( child != null ) {
+			// create a deep copy of the current child and add it as a child to the copied root
 			newNode.addChild( copy( child ) );
+			// spin forward to the next child
 			child = child.getNextSibling();
 		}
 		return newNode;
 	}
 
+	/**
+	 * Creates a shallow (non-linked) copy of an AST node.
+	 *
+	 * @param node The AST node to shallow copy.
+	 *
+	 * @return The shallow copy.
+	 */
 	public AST createShallowCopy(AST node) {
-		AST newNode = astFactory.create( node.getType(), node.getText() );
-		newNode.initialize( node );
-		return newNode;
+		if ( node instanceof CopyableNode ) {
+			return ( ( CopyableNode ) node ).createCopy();
+		}
+		else {
+			AST newNode = astFactory.create( node.getType(), node.getText() );
+			newNode.initialize( node );
+			return newNode;
+		}
 	}
 }




More information about the hibernate-commits mailing list