db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myrn...@apache.org
Subject svn commit: r592500 - in /db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij: StatementFinder.java utilMain.java
Date Tue, 06 Nov 2007 17:52:04 GMT
Author: myrnavl
Date: Tue Nov  6 09:52:02 2007
New Revision: 592500

URL: http://svn.apache.org/viewvc?rev=592500&view=rev
Log:
DERBY-2255 - ij will now show a different prompt while in a multi-line interactive statement.
Patch contributed by Anders Morken.

Modified:
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java?rev=592500&r1=592499&r2=592500&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/StatementFinder.java Tue
Nov  6 09:52:02 2007
@@ -21,6 +21,9 @@
 
 package org.apache.derby.impl.tools.ij;
 
+import org.apache.derby.iapi.tools.i18n.LocalizedOutput;
+import org.apache.derby.iapi.tools.i18n.LocalizedInput;
+
 import java.io.IOException;
 import java.io.Reader;
 
@@ -54,6 +57,9 @@
 	private boolean peekEOF = false;
 	private char peekChar;
 	private boolean peeked = false;
+	private LocalizedOutput promptwriter;
+	private boolean doPrompt;
+	private boolean continuedStatement;
 
 	// state variables
 	private static final int IN_STATEMENT = 0;
@@ -77,19 +83,36 @@
 		The constructor does not assume the stream is data input
 		or buffered, so it will wrap it appropriately.
 
+		If the StatementFinder's input stream is connected to
+		System.in, a LocalizedOutput stream may be given to print
+		line continuation prompts when StatementFinder reads a newline.
+
 		@param s the input stream for reading statements from.
+		@param promptDest LocalizedOutput stream to write line
+						continuation prompts ("> ") to. If null,
+						no such prompts will be written.
 	 */
-	public StatementFinder(Reader s) { 
+	public StatementFinder(LocalizedInput s, LocalizedOutput promptDest) {
 		source = s;
+		if(promptDest != null && s.isStandardInput()) {
+			promptwriter = promptDest;
+			doPrompt = true;
+		} else {
+			doPrompt = false;
+		}
 	}
 
 	/**
 		Reinit is used to redirect the finder to another stream.
 		The previous stream should not have been in a PEEK state.
 
+		If an output stream was given when constructing this 
+		StatementFinder and the input is standard input, 
+		continuation prompting will be enabled.
+
 		@param s the input stream for reading statements from.
 	 */
-	public void ReInit(Reader s) { 
+	public void ReInit(LocalizedInput s) {
 	    try {
 			source.close();
 		} catch (IOException ioe) {
@@ -100,6 +123,11 @@
 		atEOF = false;
 		peekEOF = false;
 		peeked = false;
+		if(s.isStandardInput() && promptwriter != null) {
+			doPrompt = true;
+		} else {
+			doPrompt = false;
+		}
 	}
 
 	public void close() throws IOException {
@@ -145,6 +173,9 @@
 				state = END_OF_INPUT;
 				break;
 			}
+			
+			if (!(nextChar == MINUS))
+				continuedStatement=true;
 
 			switch(nextChar) {
 				case MINUS:
@@ -157,7 +188,19 @@
 				case SEMICOLON:
 					haveSemi = true;
 					state = END_OF_STATEMENT;
+					continuedStatement=false;
 					break;
+				case NEWLINE:
+				case RETURN:
+					if(doPrompt) {
+						utilMain.doPrompt(false, promptwriter, "");
+						/* If the next character is a newline as well,
+						   we swallow it to avoid double prompting on
+						   Windows. */
+						if(nextChar == RETURN && peekChar() == NEWLINE) {
+							readChar();
+						}
+					}
 				default:
 					// keep going, just a normal character
 					break;
@@ -204,7 +247,11 @@
 
 		// if nextChar is not a minus, it was just a normal minus,
 		// nothing special to do
-		if (nextChar != commentChar) return;
+		if (nextChar != commentChar)
+		{
+			continuedStatement=true;
+			return;
+		}
 
 		// we are really in a comment
 		readChar(); // grab the minus for real.
@@ -222,7 +269,23 @@
 				case RETURN:
 					readChar(); // okay to process the character
 					state = IN_STATEMENT;
-					return;
+					if (doPrompt){
+						// If we had previously already started a statement,
+						// add the prompt.
+						// Otherwise, consider this a single line comment,
+						// and the next line should not get a prompt
+						if (continuedStatement)
+							utilMain.doPrompt(false, promptwriter, "");
+                        else
+                            utilMain.doPrompt(true, promptwriter, "");
+					    
+						/* If the next character is a NEWLINE, we process
+						 *  it as well to account for Windows CRLFs. */
+						if(nextChar == RETURN && peekChar() == NEWLINE) {
+							readChar();
+						}
+					}
+				return;
 				default:
 					readChar(); // process the character, still in comment
 					break;

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java?rev=592500&r1=592499&r2=592500&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java Tue Nov  6
09:52:02 2007
@@ -140,7 +140,7 @@
 
 		for (int ictr = 0; ictr < numConnections; ictr++)
 		{
-		    commandGrabber[ictr] = new StatementFinder(langUtil.getNewInput(System.in));
+		    commandGrabber[ictr] = new StatementFinder(langUtil.getNewInput(System.in), out);
 			connEnv[ictr] = new ConnectionEnv(ictr, (numConnections > 1), (numConnections == 1));
 		}
 
@@ -609,7 +609,7 @@
 		// if the file was opened, move to use it for input.
 		oldGrabbers.push(commandGrabber[currCE]);
 	    commandGrabber[currCE] = 
-                new StatementFinder(langUtil.getNewInput(new BufferedInputStream(newFile,
BUFFEREDFILESIZE)));
+                new StatementFinder(langUtil.getNewInput(new BufferedInputStream(newFile,
BUFFEREDFILESIZE)), null);
 		fileInput = true;
 	}
 
@@ -618,7 +618,7 @@
 		if (is==null) throw ijException.resourceNotFound();
 		oldGrabbers.push(commandGrabber[currCE]);
 	    commandGrabber[currCE] = 
-                new StatementFinder(langUtil.getNewEncodedInput(new BufferedInputStream(is,
BUFFEREDFILESIZE), "UTF8"));
+                new StatementFinder(langUtil.getNewEncodedInput(new BufferedInputStream(is,
BUFFEREDFILESIZE), "UTF8"), null);
 		fileInput = true;
 	}
 



Mime
View raw message