Преглед на файлове

#223 Trim spaces on CSV import was not handling well the last line

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@760 a333f486-631f-4898-b8df-5754b55c2be0
romainq преди 14 години
родител
ревизия
63c798289e
променени са 3 файла, в които са добавени 50 реда и са изтрити 54 реда
  1. 36 27
      core/csvparser.class.inc.php
  2. 12 12
      core/test.class.inc.php
  3. 2 15
      pages/testlist.inc.php

+ 36 - 27
core/csvparser.class.inc.php

@@ -38,6 +38,7 @@ define('evSEPARATOR', 1);
 define('evNEWLINE', 2);
 define('evTEXTQUAL', 3); // used for escaping as well
 define('evOTHERCHAR', 4);
+define('evEND', 5);
 
 
 /**
@@ -141,24 +142,28 @@ class CSVParser
 		$aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
 		$aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
 		$aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
+		$aTransitions[stSTARTING][evEND] = array('__AddRow', stSTARTING);
 
 		$aTransitions[stRAW][evBLANK] = array('__AddChar', stRAW);
 		$aTransitions[stRAW][evSEPARATOR] = array('__AddCellTrimmed', stSTARTING);
 		$aTransitions[stRAW][evNEWLINE] = array('__AddRowTrimmed', stSTARTING);
 		$aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
 		$aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
+		$aTransitions[stRAW][evEND] = array('__AddRowTrimmed', stSTARTING);
 
 		$aTransitions[stQUALIFIED][evBLANK] = array('__AddChar', stQUALIFIED);
 		$aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
 		$aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
 		$aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
 		$aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
+		$aTransitions[stQUALIFIED][evEND] = array('__AddRow', stSTARTING);
 
 		$aTransitions[stESCAPED][evBLANK] = array('', stESCAPED);
 		$aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
 		$aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
 		$aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
 		$aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
+		$aTransitions[stESCAPED][evEND] = array('__AddRow', stSTARTING);
 
 		// Reset parser variables
 		$this->m_sCurrCell = '';
@@ -166,37 +171,43 @@ class CSVParser
 		$this->m_iToSkip = $iToSkip;
 		$this->m_aDataSet = array();
 
+		$iDataLength = strlen($this->m_sCSVData);
+
 		$iState = stSTARTING;
-		for($i = 0; $i < strlen($this->m_sCSVData) ; $i++)
+		for($i = 0; $i <= $iDataLength ; $i++)
 		{
-			$c = $this->m_sCSVData[$i];
-
-//			// Note: I did that because the unit test was not working fine (file edited with notepad: \n chars padded :-(
-//			if (ord($c) == 0) continue;
-
-			if ($c == $this->m_sSep)
-			{
-				$iEvent = evSEPARATOR;
-			}
-			elseif ($c == ' ')
-			{
-				$iEvent = evBLANK;
-			}
-			elseif ($c == "\t")
+			if ($i == $iDataLength)
 			{
-				$iEvent = evBLANK;
-			}
-			elseif ($c == "\n")
-			{
-				$iEvent = evNEWLINE;
-			}
-			elseif ($c == $this->m_sTextQualifier)
-			{
-				$iEvent = evTEXTQUAL;
+				$iEvent = evEND;
 			}
 			else
 			{
-				$iEvent = evOTHERCHAR;
+				$c = $this->m_sCSVData[$i];
+
+				if ($c == $this->m_sSep)
+				{
+					$iEvent = evSEPARATOR;
+				}
+				elseif ($c == ' ')
+				{
+					$iEvent = evBLANK;
+				}
+				elseif ($c == "\t")
+				{
+					$iEvent = evBLANK;
+				}
+				elseif ($c == "\n")
+				{
+					$iEvent = evNEWLINE;
+				}
+				elseif ($c == $this->m_sTextQualifier)
+				{
+					$iEvent = evTEXTQUAL;
+				}
+				else
+				{
+					$iEvent = evOTHERCHAR;
+				}
 			}
 
 			$sAction = $aTransitions[$iState][$iEvent][0];
@@ -218,8 +229,6 @@ class CSVParser
 			$iLineCount = count($this->m_aDataSet);
 			if (($iMax > 0) && ($iLineCount >= $iMax)) break;
 		}
-		// Close the final line
-		$this->__AddRow(null, $aFieldMap);
 		return $this->m_aDataSet;
 	}
 

+ 12 - 12
core/test.class.inc.php

@@ -88,8 +88,8 @@ abstract class TestHandler
 		$this->m_aErrors = array();
 	}
 
-	abstract static public function GetName();
-	abstract static public function GetDescription();
+	static public function GetName() {return "fooname";}
+	static public function GetDescription(){return "foodesc";}
 
 	protected function DoPrepare() {return true;}
 	abstract protected function DoExecute();
@@ -278,8 +278,8 @@ abstract class TestSoapWebService extends TestHandler
  */
 abstract class TestFunctionInOut extends TestFunction
 {
-	abstract static public function GetCallSpec(); // parameters to call_user_func
-	abstract static public function GetInOut(); // array of input => output
+//	abstract static public function GetCallSpec(); // parameters to call_user_func
+//	abstract static public function GetInOut(); // array of input => output
 
 	protected function DoExecute()
 	{
@@ -316,9 +316,9 @@ abstract class TestFunctionInOut extends TestFunction
  */
 abstract class TestUrl extends TestHandler
 {
-	abstract static public function GetUrl();
-	abstract static public function GetErrorKeywords();
-	abstract static public function GetWarningKeywords();
+//	abstract static public function GetUrl();
+//	abstract static public function GetErrorKeywords();
+//	abstract static public function GetWarningKeywords();
 
 	protected function DoExecute()
 	{
@@ -348,10 +348,10 @@ abstract class TestUserRights extends TestHandler
  */
 abstract class TestScenarioOnDB extends TestHandler
 {
-	abstract static public function GetDBHost();
-	abstract static public function GetDBUser();
-	abstract static public function GetDBPwd();
-	abstract static public function GetDBName();
+//	abstract static public function GetDBHost();
+//	abstract static public function GetDBUser();
+//	abstract static public function GetDBPwd();
+//	abstract static public function GetDBName();
 
 	protected function DoPrepare()
 	{
@@ -385,7 +385,7 @@ abstract class TestBizModel extends TestHandler
 {
 //	abstract static public function GetDBSubName();
 //	abstract static public function GetBusinessModelFile();
-	abstract static public function GetConfigFile();
+//	abstract static public function GetConfigFile();
 
 	protected function DoPrepare()
 	{

+ 2 - 15
pages/testlist.inc.php

@@ -220,20 +220,6 @@ class TestCSVParser extends TestFunction
 
 	public function DoExecute()
 	{
-		$sDataFile = '"field1","field2","field3"
-"a","b","c"
-a,b,c
-"","",""
-,,
-"a""","b","c"
-"a1
-a2","b","c"
-"a1,a2","b","c"
-"a","b","c1,"",c2
-,c3"
-"a","b","ouf !"
-';
-
 		$sDataFile = '?field1?;?field2?;?field3?
 ?a?;?b?;?c?
 a;b;c
@@ -248,7 +234,7 @@ a2?;?b?;?c?
 ?a?;?b?;?c1,",c2
 ,c3?
 ?a?;?b?;?ouf !?
-';
+    Espace sur la fin ; 1234; e@taloc.com ';
 
 		echo "<pre style=\"font-style:italic;\">\n";
 		print_r($sDataFile);
@@ -267,6 +253,7 @@ a2?;?b?;?c?
 			array('a1,a2', 'b', 'c'),
 			array('a', 'b', "c1,\",c2\n,c3"),
 			array('a', 'b', 'ouf !'),
+			array('Espace sur la fin', '1234', 'e@taloc.com'),
 		);
 	
 		$oCSVParser = new CSVParser($sDataFile, ';', '?');