package org.nybatis.core.executor.parser;

import java.util.ArrayList;
import java.util.List;

/**
 * OS 명령어(Command Line)를 배열로 변환하는 유틸리티 클래스
 *
 * @author nayasis
 *
 */
public class CommandParser {
	
	List<String>  commandList = null;
	StringBuilder command     = null;

	public List<String> parse( String commandLine ) {

		if( commandLine == null || commandLine.length() == 0 ) return new ArrayList<String>();
		
		commandList = new ArrayList<>();
		command     = new StringBuilder();
		
		boolean quotationMode = false;
		
		for( int i = 0, iCnt = commandLine.length(); i < iCnt; i++ ) {
			
			char c = commandLine.charAt( i );
			
			if( quotationMode == true ) {

				if( c == '"' ) {

					quotationMode = false;
					
					addCommandList( command );
					
					continue;
					
				}
					
			} else {
				
				if( c == '"' ) {
					
					quotationMode = true;
					
					continue;
					
				} else if ( c == ' ' || c == '\t' || c == '\r' || c == '\n'  ) {
					
					addCommandList( command );
					
					continue;
					
				}
				
			}
			
			command.append( c );
			
		}
		
		addCommandList( command );
		
		return commandList;
		
	}
	
	private void addCommandList( StringBuilder command ) {
		
		if( command == null ) return;
		
		String appendCommand = command.toString().trim();
		
		if( command.length() == 0 ) return;
		
		this.commandList.add( appendCommand );
		
		this.command = new StringBuilder();
		
	}
	
}

@@ -715,21 +715,42 @@
  false;%0A
+%0A
 %09%09%09%09%09
+command.append( c );
 %0A%09%09%09%09%09ad
@@ -766,38 +766,32 @@
 ist( command );%0A
-%09%09%09%09%09%0A
 %09%09%09%09%09continue;%0A%09
@@ -874,29 +874,50 @@
 ode = true;%0A
+%0A
 %09%09%09%09%09
+command.append( c );
 %0A%09%09%09%09%09contin
@@ -991,30 +991,24 @@
 = '%5Cn'  ) %7B%0A
-%09%09%09%09%09%0A
 %09%09%09%09%09addComm
@@ -1027,22 +1027,16 @@
 mand );%0A
-%09%09%09%09%09%0A
 %09%09%09%09%09con
@@ -1034,38 +1034,32 @@
 %0A%09%09%09%09%09continue;%0A
-%09%09%09%09%09%0A
 %09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%7D%0A

fix to remove quotation in parameter.