[commit] r2398 - trunk/Tools/TableEditor

GMESRC Repository Notifications gme-commit at list.isis.vanderbilt.edu
Thu Nov 14 13:33:11 CST 2013


Author: ksmyth
Date: Thu Nov 14 13:33:11 2013
New Revision: 2398

Log:
TableEditor: bring back Import/Export, per request. Fix double sprintf format. Fix Importer (virtual func params were wrong).

Added:
   trunk/Tools/TableEditor/TableEditorDumper.cpp
   trunk/Tools/TableEditor/TableEditorDumper.h
   trunk/Tools/TableEditor/TableEditorParser.cpp
   trunk/Tools/TableEditor/TableEditorParser.h
Modified:
   trunk/Tools/TableEditor/Component.rc
   trunk/Tools/TableEditor/GridDlg.cpp
   trunk/Tools/TableEditor/GridDlg.h
   trunk/Tools/TableEditor/Resource.h
   trunk/Tools/TableEditor/TableEditor.vcxproj
   trunk/Tools/TableEditor/TableEditor.vcxproj.filters

Modified: trunk/Tools/TableEditor/Component.rc
==============================================================================
--- trunk/Tools/TableEditor/Component.rc	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/Component.rc	Thu Nov 14 13:33:11 2013	(r2398)
@@ -106,6 +106,7 @@
     DEFPUSHBUTTON   "OK",IDOK,14,324,50,14
     PUSHBUTTON      "Cancel",IDCANCEL,69,324,50,14
     CONTROL         "Custom1",IDC_GRID,"MFCGridCtrl",WS_TABSTOP,7,7,420,265
+    PUSHBUTTON      "Export...",IDC_BTNEXPORT,125,324,48,14
     CONTROL         "Display All Types",IDC_CHKALLTYPES,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,64,295,69,10
     CONTROL         "Models",IDC_CHKMODEL,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,18,308,39,10
     CONTROL         "Atoms",IDC_CHKATOM,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,60,308,35,10
@@ -120,6 +121,7 @@
     LTEXT           "Type Filters:",IDC_STATIC_TYPE,13,295,46,10
     LTEXT           "Invoking the table editor with selected objects will display only those objects",IDC_STATIC_SELECTION,7,276,257,8
     LTEXT           "Filters:",IDC_STATIC_FILTERS,12,286,54,8
+    PUSHBUTTON      "Import...",IDC_BTNIMPORT,179,324,48,14
 END
 
 
@@ -149,6 +151,11 @@
 
 IDR_GRIDDLG_MENU MENU
 BEGIN
+    POPUP "&File"
+    BEGIN
+        MENUITEM "&Import ...",                 ID_FILE_IMPORT
+        MENUITEM "&Export ...",                 ID_FILE_EXPORT
+    END
     POPUP "&View"
     BEGIN
         MENUITEM "&Recursively Show Items",     ID_VIEW_RECURSIVELYSHOWITEMS

Modified: trunk/Tools/TableEditor/GridDlg.cpp
==============================================================================
--- trunk/Tools/TableEditor/GridDlg.cpp	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/GridDlg.cpp	Thu Nov 14 13:33:11 2013	(r2398)
@@ -10,8 +10,8 @@
 #include "NewCellTypes/GridCellFCO.h"
 #include "NewCellTypes/GridCellMultiLine.h"
 #include "NewCellTypes/GridCellAttrName.h"
-//#include "TableEditorParser.h"
-//#include "TableEditorDumper.h"
+#include "TableEditorParser.h"
+#include "TableEditorDumper.h"
 #include <queue>
 #include "Console.h"
 
@@ -386,7 +386,7 @@
 						if (!m_Grid.SetCellType(RowNum,ColNum, RUNTIME_CLASS(CGridCellDouble)))
 							break;
 						char chNum[ 100 ];
-						sprintf( chNum, "%f", actval_dbl );
+						sprintf_s(chNum, "%.17g", actval_dbl);
 						actval_str = chNum;
 						m_Grid.SetItemText(RowNum,ColNum,actval_str);
 					}
@@ -603,6 +603,7 @@
 {
 	CDialog::DoDataExchange(pDX);
 	//{{AFX_DATA_MAP(CGridDlg)
+	DDX_Control(pDX, IDC_BTNIMPORT, m_btnImport);
 	DDX_Control(pDX, IDC_STATIC_FILTERS, m_stcFilters);
 	DDX_Control(pDX, IDC_STATIC_SELECTION, m_stcSelect);
 	DDX_Control(pDX, IDC_CHKALLTYPES, m_btnAllTypes);
@@ -617,6 +618,7 @@
 	DDX_Control(pDX, IDC_CHKCON, m_btnCon);
 	DDX_Control(pDX, IDC_CHKATOM, m_btnAtom);
 	DDX_Control(pDX, IDC_LISTKIND, m_lstKind);
+	DDX_Control(pDX, IDC_BTNEXPORT, m_btnExport);
 	DDX_Control(pDX, IDCANCEL, m_btnCANCEL);
 	DDX_Control(pDX, IDOK, m_btnOK);
 	DDX_Check(pDX, IDC_CHECKALLKINDS, m_chkAllKinds);
@@ -633,8 +635,11 @@
 
 BEGIN_MESSAGE_MAP(CGridDlg, CDialog)
 	//{{AFX_MSG_MAP(CGridDlg)
+	ON_COMMAND(ID_FILE_IMPORT, OnBtnImport)
+	ON_COMMAND(ID_FILE_EXPORT, OnBtnExport)
 	ON_COMMAND(ID_VIEW_RECURSIVELYSHOWITEMS, OnRecursivelyShowItems)
 	ON_WM_SIZE()
+	ON_BN_CLICKED(IDC_BTNEXPORT, OnBtnExport)
 	ON_BN_CLICKED(IDC_BTNDISP, OnButtonDisplay)
 	ON_BN_CLICKED(IDC_CHECKALLKINDS, OnCheckAllKinds)
 	ON_BN_CLICKED(IDC_CHKALLTYPES, OnChkAllTypes)
@@ -643,6 +648,7 @@
 	ON_BN_CLICKED(IDC_CHKMODEL, OnChkModel)
 	ON_BN_CLICKED(IDC_CHKREF, OnChkRef)
 	ON_BN_CLICKED(IDC_CHKSET, OnChkSet)
+	ON_BN_CLICKED(IDC_BTNIMPORT, OnBtnImport)
 	//}}AFX_MSG_MAP
     ON_NOTIFY(NM_DBLCLK, IDC_GRID, OnGridDblClick)
     ON_NOTIFY(NM_CLICK, IDC_GRID, OnGridClick)
@@ -952,6 +958,8 @@
 
 	MoveWndDown(&m_btnCANCEL, Translate.cy);
 
+	MoveWndDown(&m_btnExport, Translate.cy);
+
 	MoveWndDown(&m_btnDisp, Translate.cy);
 
 	MoveWndDown(&m_btnAllKinds, Translate.cy);
@@ -980,6 +988,8 @@
 
 	MoveWndDown(&m_stcFilters, Translate.cy);
 
+	MoveWndDown(&m_btnImport, Translate.cy);
+
 	Invalidate();
 
 	m_OldSize = CSize(cx,cy);
@@ -996,11 +1006,97 @@
 	wnd->MoveWindow(rect.left, rect.top+offset, rect.Width(), rect.Height(), FALSE);
 }
 
+
+
+void CGridDlg::OnBtnExport() 
+{
+	//Export to .xml file for use in MS Excel
+	CFileDialog FileSelector(FALSE,"xml",NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"Excel formatted XML (*.xml)|*.xml||");
+	if(FileSelector.DoModal() == IDOK)
+	{
+		int MaxRows = m_Grid.GetRowCount();
+		int MaxCols = m_Grid.GetColumnCount();
+		CString cell_type;
+
+		TableEditorDumper dumper;  // creates dumper 
+		dumper.InitDump(FileSelector.GetPathName(),MaxCols,MaxRows); // adds in proper excel header
+		for( int i=0; i<MaxRows; i++ )
+		{
+			dumper.StartElem("Row");
+			for( int j=0; j<MaxCols; j++ )
+			{
+				CString ClassName = m_Grid.GetCell(i,j)->GetRuntimeClass()->m_lpszClassName; 
+				CString cell_entry = m_Grid.GetItemText(i,j);
+				CString cell_type = "String"; //default to String.
+				BOOL multi = FALSE;
+				BOOL boolean = FALSE;
+				
+				if( ClassName == "CGridCellComboBool" ) 
+				{
+					cell_type = "Boolean";
+					boolean = TRUE;
+				}
+				
+				if( ClassName == "CGridCellNumeric" || ClassName == "CGridCellDouble" ) 
+				{
+					cell_type = "Number";
+				}
+				
+				if( ClassName == "CGridCell" ) 
+				{
+					cell_type = "String";
+				}
+				
+				if( ClassName == "CGridCellMultiLine" ) 
+				{
+					cell_type = "String";
+					multi = TRUE;
+				}
+				
+				dumper.DumpCell(cell_type, cell_entry, multi, boolean);
+			}
+			
+			dumper.EndElem("Row");
+		}
+		
+		dumper.DoneDump();
+
+	}
+}
+
 void CGridDlg::OnRecursivelyShowItems()
 {
 	ShowItemsRecursively();
 }
 
+void CGridDlg::OnBtnImport() 
+{
+	CFileDialog FileSelector(TRUE,"xml",NULL, NULL,"EXCEL Exported XML (*.xml)|*.xml||");
+	if(FileSelector.DoModal() == IDOK)
+	{
+		try
+		{
+			CTableEditorParser parser(m_Project);
+			m_FCOs =  parser.ParseExcelXML(m_Project,FileSelector.GetPathName());
+			InitGrid();
+		}
+		catch(HRESULT)
+		{
+			//need better error handling
+			AfxMessageBox("A GME COM error occurred on import. Does this file match the current project?");
+			CDialog::OnCancel();
+			throw; //rethrow, for the user to get a more detailed message from GME
+		}
+		catch(const XMLException &)
+		{
+			CDialog::OnCancel();
+		}
+	
+	}
+	
+}
+
+
 	
 BOOL CGridDlg::GetMultiLine(CComPtr<IMgaMetaAttribute> p_Meta) 
 {

Modified: trunk/Tools/TableEditor/GridDlg.h
==============================================================================
--- trunk/Tools/TableEditor/GridDlg.h	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/GridDlg.h	Thu Nov 14 13:33:11 2013	(r2398)
@@ -32,6 +32,7 @@
 // Dialog Data
 	//{{AFX_DATA(CGridDlg)
 	enum { IDD = IDD_GRIDDLG };
+	CButton	m_btnImport;
 	CStatic	m_stcFilters;
 	CStatic	m_stcSelect;
 	CButton	m_btnAllTypes;
@@ -46,6 +47,7 @@
 	CButton	m_btnCon;
 	CButton	m_btnAtom;
 	CListBox	m_lstKind;
+	CButton	m_btnExport;
 	CButton	m_btnCANCEL;
 	CButton	m_btnOK;
 	BOOL	m_chkAllKinds;
@@ -94,6 +96,7 @@
 	//{{AFX_MSG(CGridDlg)
 	virtual BOOL OnInitDialog();
 	afx_msg void OnSize(UINT nType, int cx, int cy);
+	afx_msg void OnBtnExport();
 	afx_msg void OnRecursivelyShowItems();
 	afx_msg void OnButtonDisplay();
 	afx_msg void OnCheckAllKinds();
@@ -103,6 +106,7 @@
 	afx_msg void OnChkModel();
 	afx_msg void OnChkRef();
 	afx_msg void OnChkSet();
+	afx_msg void OnBtnImport();
 	//}}AFX_MSG
 	afx_msg void OnGridDblClick(NMHDR *pNotifyStruct, LRESULT* pResult);
     afx_msg void OnGridClick(NMHDR *pNotifyStruct, LRESULT* pResult);

Modified: trunk/Tools/TableEditor/Resource.h
==============================================================================
--- trunk/Tools/TableEditor/Resource.h	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/Resource.h	Thu Nov 14 13:33:11 2013	(r2398)
@@ -16,11 +16,16 @@
 #define IDC_CHKREF                      217
 #define IDC_CHKSET                      218
 #define IDC_STATICHELP                  219
+#define IDC_BTNEXPORT                   220
 #define IDC_CHKCON                      221
 #define IDC_BTNDISP                     222
 #define IDC_STATIC_TYPE                 223
 #define IDC_STATIC_SELECTION            224
 #define IDC_STATIC_FILTERS              225
+#define IDC_BTNIMPORT                   226
+#define ID_FILE_EXPORTAS                32768
+#define ID_FILE_IMPORT                  32769
+#define ID_FILE_EXPORT                  32770
 #define ID_VIEW_RECURSIVELYSHOWITEMS    32771
 
 // Next default values for new objects

Modified: trunk/Tools/TableEditor/TableEditor.vcxproj
==============================================================================
--- trunk/Tools/TableEditor/TableEditor.vcxproj	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/TableEditor.vcxproj	Thu Nov 14 13:33:11 2013	(r2398)
@@ -80,6 +80,7 @@
       <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ResourceCompile>
     <Link>
+      <AdditionalDependencies>xerces-c_3.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(GME_ROOT)/GME/Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <ModuleDefinitionFile>Component.def</ModuleDefinitionFile>
       <GenerateDebugInformation>true</GenerateDebugInformation>
@@ -123,6 +124,7 @@
       <AdditionalIncludeDirectories>$(IntDir);%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ResourceCompile>
     <Link>
+      <AdditionalDependencies>xerces-c_3.lib;%(AdditionalDependencies)</AdditionalDependencies>
       <AdditionalLibraryDirectories>$(GME_ROOT)/GME/Lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
       <ModuleDefinitionFile>Component.def</ModuleDefinitionFile>
       <GenerateDebugInformation>true</GenerateDebugInformation>
@@ -144,6 +146,8 @@
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
       <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
     </ClCompile>
+    <ClCompile Include="TableEditorDumper.cpp" />
+    <ClCompile Include="TableEditorParser.cpp" />
     <ClCompile Include="GridCtrl_src\GridCell.cpp" />
     <ClCompile Include="GridCtrl_src\GridCellBase.cpp" />
     <ClCompile Include="GridCtrl_src\GridCtrl.cpp" />

Modified: trunk/Tools/TableEditor/TableEditor.vcxproj.filters
==============================================================================
--- trunk/Tools/TableEditor/TableEditor.vcxproj.filters	Tue Nov  5 14:55:18 2013	(r2397)
+++ trunk/Tools/TableEditor/TableEditor.vcxproj.filters	Thu Nov 14 13:33:11 2013	(r2398)
@@ -51,6 +51,12 @@
     <ClCompile Include="StdAfx.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
+    <ClCompile Include="TableEditorDumper.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="TableEditorParser.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
     <ClCompile Include="GridCtrl_src\GridCell.cpp">
       <Filter>GridCtrl</Filter>
     </ClCompile>

Added: trunk/Tools/TableEditor/TableEditorDumper.cpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/Tools/TableEditor/TableEditorDumper.cpp	Thu Nov 14 13:33:11 2013	(r2398)
@@ -0,0 +1,131 @@
+// TableEditorXMLDumper.cpp: implementation of the TableEditorXMLDumper class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "stdafx.h"
+#include "TableEditorDumper.h"
+
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+
+TableEditorDumper::TableEditorDumper()
+{
+	
+}
+
+TableEditorDumper::~TableEditorDumper()
+{
+	
+}
+
+// --------------------------- TableEditorXMLDumper
+
+void TableEditorDumper::InitDump(CString filename, int numCols, int numRows) 
+{
+	ASSERT( !ofs.is_open() );
+	ofs.open(filename, ios::out | ios::trunc);
+
+// Manually write out header necessary for Excel to open spreadsheet properly
+	ofs << "<?xml version=\"1.0\"?>\n";
+	ofs << "<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\n";
+	ofs << " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\n";
+	ofs << " xmlns:x=\"urn:schemas-microsoft-com:office:excel\"\n";
+	ofs << " xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\">\n";
+	StartElem("Styles");
+	ofs << "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\n";
+	EndElem("Style");
+	ofs << "<Style ss:ID=\"s21\">\n";
+	ofs << "<Alignment ss:Vertical=\"Bottom\" ss:WrapText=\"1\"/>\n";
+	EndElem("Style");
+	EndElem("Styles");	
+	StartElemAttr("Worksheet");
+	Attr("ss:Name", "temp", TRUE);	
+	ofs << "\n";
+	StartElemAttr("Table");
+
+	char numBuf[256];
+	_itoa(numCols,numBuf,10);
+	Attr("ss:ExpandedColumnCount",numBuf, FALSE);
+	_itoa(numRows,numBuf,10);
+	Attr("ss:ExpandedRowCount",numBuf, FALSE);
+	Attr("x:FullColumns","1",FALSE);
+	Attr("x:FullRows","1",TRUE);
+}
+
+void TableEditorDumper::DoneDump()
+{
+
+	EndElem("Table"); // </Table>
+	
+	EndElem("Worksheet"); // </Worksheet>
+	
+	EndElem("Workbook"); // </Workbook>
+	
+	ofs.close();
+}
+
+
+void TableEditorDumper::DumpCell(CString cell_type, CString cell_entry, BOOL multi, BOOL boolean)
+{
+
+	//xml special chars
+	cell_entry.Replace("&", "&amp;");
+	cell_entry.Replace("<","&lt;");
+	cell_entry.Replace(">","&gt;");
+	
+	
+	// First checks if cell entry is multiline. w/o specifying styleID, little boxes show up 
+	if( multi ) 
+	{
+		StartElemAttr("Cell");
+		Attr("ss:StyleID", "s21", TRUE);
+		cell_entry.Replace("\n", "&#10;"); // to preserve multiline
+	}
+	else {
+		StartElem("Cell");
+	}
+
+// Checks if cell entry is boolean. w/o converting to 1 / 0, gets Table strict parse error
+	if( boolean )
+	{
+		cell_entry.Replace("true", "1");
+		cell_entry.Replace("false", "0");
+	}
+	
+	StartElemAttr("Data");
+	Attr("ss:Type", cell_type, TRUE);
+	ofs << cell_entry;
+	EndElem("Data"); // </Data>
+	EndElem("Cell"); // </Cell>
+}
+
+
+// ------- Low level stuff
+
+inline void TableEditorDumper::StartElem(const char *name)
+{
+	ASSERT( name != NULL );
+	ofs << '<' << name << '>';
+}
+
+inline void TableEditorDumper::StartElemAttr(const char *name)
+{
+	ASSERT( name != NULL );
+	ofs << '<' << name;
+}
+
+inline void TableEditorDumper::Attr(const char *name, const char *value, BOOL last)
+{
+	ASSERT( name != NULL );
+	ASSERT( value != NULL );
+
+  	ofs << ' ' << name << "=\"" << value << "\"";
+	if(last) ofs << ">";
+}
+
+inline void TableEditorDumper::EndElem(const char *name)
+{
+	ofs << "</" << name << ">\n";
+}
\ No newline at end of file

Added: trunk/Tools/TableEditor/TableEditorDumper.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/Tools/TableEditor/TableEditorDumper.h	Thu Nov 14 13:33:11 2013	(r2398)
@@ -0,0 +1,42 @@
+// TableEditorXMLDumper.h: interface for the TableEditorXMLDumper class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_)
+#define AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_
+
+
+#include <fstream>
+using namespace std;
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+
+
+
+class TableEditorDumper  
+{
+public:
+	TableEditorDumper();
+	virtual ~TableEditorDumper();
+
+// ------- Attributes
+	
+public:
+	ofstream ofs;
+
+public:
+	void InitDump(CString filename, int numCols, int numRows);
+	void DoneDump();
+	void DumpCell(CString cell_type, CString cell_entry, BOOL multi, BOOL boolean);
+
+// ------- Low level stuff
+	void StartElem(const char *name);
+	void StartElemAttr(const char *name);
+	void Attr(const char *name, const char *value, BOOL last);
+	void EndElem(const char *name);
+
+};
+
+#endif // !defined(AFX_TABLEEDITORDUMPER_H__8E6DCC47_5A1A_46D0_9CD7_CA0ADE359A49__INCLUDED_)

Added: trunk/Tools/TableEditor/TableEditorParser.cpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/Tools/TableEditor/TableEditorParser.cpp	Thu Nov 14 13:33:11 2013	(r2398)
@@ -0,0 +1,274 @@
+// TableEditorParser.cpp: implementation of the CTableEditorParser class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#include "stdafx.h"
+#include "TableEditorParser.h"
+#include <xercesc/parsers/SAXParser.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+XERCES_CPP_NAMESPACE_USE
+
+//////////////////////////////////////////////////////////////////////
+// Construction/Destruction
+//////////////////////////////////////////////////////////////////////
+
+CTableEditorParser::CTableEditorParser(IMgaProject* theProject):HandlerBase()
+{
+	currentProject = theProject;
+	importedFCOs = NULL;
+	cdata = "";
+
+}
+
+CTableEditorParser::~CTableEditorParser()
+{
+
+}
+
+//initiate parsing
+IMgaFCOs* CTableEditorParser::ParseExcelXML(IMgaProject *p, CString filename)
+{
+	try //catch moved up into GridDlg.cpp
+	{
+		XMLPlatformUtils::Initialize();
+
+		SAXParser parser;
+		//parser.setDoValidation(true);
+		parser.setDocumentHandler(this);
+		parser.setErrorHandler(this);
+		parser.setEntityResolver(this);
+
+		parser.parse(filename);
+
+		//XMLPlatformUtils::Terminate(); 
+	}
+	catch(const XMLException &e)
+	{
+		CString desc(e.getMessage());
+		AfxMessageBox(desc);
+		throw;
+	}
+
+	return importedFCOs;
+
+}
+
+//XML Doc Handler stuff
+
+void CTableEditorParser::startDocument()
+{
+	currentRowNum = 0;
+	currentColNum = 0;
+	firstAttrColNum = -1;
+	objIDColNum = -1;
+	objNameColNum = -1;
+	inData = FALSE;
+	cdata = "";
+
+	COMTHROW(importedFCOs.CoCreateInstance(L"Mga.MgaFCOs"));
+}
+
+void CTableEditorParser::endDocument()
+{
+	//AfxMessageBox("EndDoc");
+}
+
+void CTableEditorParser::startElement(const XMLCh* const name, AttributeList& attributes)
+{
+	CString elementName(name);
+
+	//if(elementName.Compare("Table")==0)
+	if(elementName.Compare("Row")==0)
+	{
+		currentColNum = 0;
+	}
+	if(elementName.Compare("Data")==0)
+	{
+		inData = TRUE;
+		cdata = "";
+	}
+	if(elementName.Compare("Cell")==0)
+	{
+		int len = attributes.getLength();
+		for(int index = 0; index < len; index++)
+		{
+			CString name = attributes.getName(index);
+			CString value = attributes.getValue(index);
+			if(name.Compare("ss:Index")==0)//excel will not make <Cell> tags for empty cells
+			{                              //including ss:Index means that some cells have been skipped over
+				if(currentColNum>firstAttrColNum)//skipped some columns, so there was an empty attribute.
+				{	
+					SetAttr(currentAttrDName,"");
+				}
+				
+				currentColNum=atoi(value)-1;//this code is 0-based, the ss:Index is 1-based
+			}
+		}
+	}
+
+}
+
+void CTableEditorParser::endElement(const XMLCh* const name)
+{
+	CString elementName(name);
+
+	if(elementName.Compare("Row")==0)//end of row, advance to next row, set col to 0
+	{
+		++currentRowNum;
+		currentColNum = 0;
+	}
+	if(elementName.Compare("Cell")==0)
+	{
+		++currentColNum;
+	}
+	if(elementName.Compare("Data")==0)
+	{
+		inData = FALSE;
+		cdata = "";
+	}
+}
+
+void CTableEditorParser::characters(const XMLCh* const chars, const XMLSize_t length)
+{
+
+	CString newdata(chars);
+//	newdata.Remove('\n'); //it appeared as if '\n' was added at the front and back of every string
+
+//	if(cdata != "") //if multiline
+//		cdata = cdata+"\n";
+
+	cdata = cdata + newdata; //if it's multiline, this func gets called multiple times
+
+
+	if(currentColNum == objNameColNum && inData)
+	{
+		currentFCOname = cdata;
+	}
+
+	if(currentColNum == objIDColNum && inData)
+	{
+		CComBSTR ID(cdata);
+		currentFCO = 0;
+		COMTHROW(currentProject->GetFCOByID(ID,&currentFCO));
+		CComBSTR name(currentFCOname);
+		COMTHROW(currentFCO->put_Name(name));
+
+		COMTHROW(importedFCOs->Append(currentFCO));
+	}
+
+
+	if(currentRowNum==0) //special stuff for first row, need to find what col ID, Name and First attr are in
+	{
+		if(cdata.Compare("Object Name")==0)
+			objNameColNum = currentColNum;
+		if(cdata.Compare("Object ID")==0)
+			objIDColNum = currentColNum;
+		if(cdata.Compare("Attribute")==0 && firstAttrColNum == -1)
+			firstAttrColNum = currentColNum;
+	}
+
+	//finally, the attributes
+	if(currentRowNum > 0 && currentColNum >= firstAttrColNum && inData)
+	{
+
+		if((currentColNum-firstAttrColNum)%2==0)//Attribute Column
+		{
+			currentAttrDName = cdata;
+		}
+		else //Value Column
+		{
+			CString tmp = cdata;
+			tmp.Replace("\n\n","\n");//somehow the newlines are doubling
+			SetAttr(currentAttrDName,tmp);
+		}
+	}
+
+}
+
+void CTableEditorParser::error(const SAXParseException& exception)
+{
+	CString desc(exception.getMessage());
+	AfxMessageBox(desc);
+}
+
+void CTableEditorParser::fatalError(const SAXParseException& exception)
+{
+	CString desc(exception.getMessage());
+	AfxMessageBox(desc);
+}
+
+void CTableEditorParser::setDocumentLocator(const Locator *const locator)
+{
+}
+
+BOOL CTableEditorParser::SetAttr(CString DisplayedName, CString Value)
+{
+
+	CComPtr<IMgaAttributes> attrs; //we only know the displayed name, so have to check through the meta on each attr
+	COMTHROW(currentFCO->get_Attributes(&attrs));
+	// TODO: memoize
+
+	MGACOLL_ITERATE(IMgaAttribute,attrs)
+	{
+
+		CComPtr<IMgaAttribute> attr = MGACOLL_ITER;
+		CComPtr<IMgaMetaAttribute> metaAttr;
+		COMTHROW(attr->get_Meta(&metaAttr));
+
+		CComBSTR metaDisplayedName;
+		COMTHROW(metaAttr->get_DisplayedName(&metaDisplayedName));
+
+		if(DisplayedName.Compare(CString(metaDisplayedName))==0)
+		{
+			attval_enum AttrType;
+			COMTHROW(metaAttr->get_ValueType(&AttrType));
+
+			CComBSTR StringVal = Value;
+			CComBSTR prevStringVal;
+			long IntVal = atoi(Value);
+			long prevIntVal;
+			double DoubleVal = atof(Value);
+			double prevDoubleVal;
+			VARIANT_BOOL BoolVal = (atoi(Value)==1);
+			VARIANT_BOOL prevBoolVal;
+			switch(AttrType)
+			{
+				case ATTVAL_NULL:
+					break;
+				case ATTVAL_STRING:
+					COMTHROW(attr->get_StringValue(&prevStringVal));
+					if(CString(prevStringVal).Compare(CString(StringVal))!=0)//only put if there's a change
+						COMTHROW(attr->put_StringValue(StringVal));
+					break;
+				case ATTVAL_INTEGER:
+					COMTHROW(attr->get_IntValue(&prevIntVal));
+					if(prevIntVal != IntVal)
+						COMTHROW(attr->put_IntValue(IntVal));
+					break;
+				case ATTVAL_DOUBLE:
+					COMTHROW(attr->get_FloatValue(&prevDoubleVal));
+					if(prevDoubleVal != DoubleVal)
+						COMTHROW(attr->put_FloatValue(DoubleVal));
+					break;
+				case ATTVAL_BOOLEAN:
+					COMTHROW(attr->get_BoolValue(&prevBoolVal));
+					if(((prevBoolVal == 0) != (BoolVal == 0))) //Variant_bools are 0 for false, nonzero for true, 
+						COMTHROW(attr->put_BoolValue(BoolVal)); //I want this to evaluate if prevBoolVal and BoolVal are different (true and false)
+					break;
+				case ATTVAL_REFERENCE:
+					break;
+				case ATTVAL_ENUM:
+					COMTHROW(attr->get_StringValue(&prevStringVal));
+					if(CString(prevStringVal).Compare(CString(StringVal))!=0)
+						COMTHROW(attr->put_StringValue(StringVal));
+					break;
+				case ATTVAL_DYNAMIC:
+					break;
+			}
+			return TRUE;
+		}
+
+	}MGACOLL_ITERATE_END
+
+	return FALSE;
+}
\ No newline at end of file

Added: trunk/Tools/TableEditor/TableEditorParser.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/Tools/TableEditor/TableEditorParser.h	Thu Nov 14 13:33:11 2013	(r2398)
@@ -0,0 +1,60 @@
+// TableEditorParser.h: interface for the CTableEditorParser class.
+//
+//////////////////////////////////////////////////////////////////////
+
+#if !defined(AFX_TABLEEDITORPARSER_H__1CB08981_9ED5_4AFF_862A_71AD347CCEF9__INCLUDED_)
+#define AFX_TABLEEDITORPARSER_H__1CB08981_9ED5_4AFF_862A_71AD347CCEF9__INCLUDED_
+
+#if _MSC_VER > 1000
+#pragma once
+#endif // _MSC_VER > 1000
+
+#include "GMECOM.h"
+#include "Mga.h"
+#include <xercesc/sax/HandlerBase.hpp>
+XERCES_CPP_NAMESPACE_USE
+
+class CTableEditorParser : public HandlerBase  
+{
+public:
+	CTableEditorParser(IMgaProject* theProject);
+	virtual ~CTableEditorParser();
+
+	//Handler Base
+public:
+	//virtual InputSource *resolveEntity (const XMLCh* const publicId, const XMLCh* const systemId);
+
+    virtual void startDocument();
+    virtual void endDocument();
+
+    virtual void startElement(const XMLCh* const name, AttributeList& attributes);
+    virtual void endElement(const XMLCh* const name);
+
+	virtual void characters(const XMLCh* const chars, const XMLSize_t length);
+
+    virtual void error(const SAXParseException& exception);
+    virtual void fatalError(const SAXParseException& exception);
+
+	virtual void setDocumentLocator(const Locator *const locator);
+ 
+	IMgaFCOs* ParseExcelXML(IMgaProject *p, CString filename); //must be for current project open, and all FCOs must previously exist
+
+private:
+	CComPtr<IMgaFCO> currentFCO;
+	CComPtr<IMgaProject> currentProject;
+	CComPtr<IMgaFCOs> importedFCOs;
+	int currentColNum; //state of parser info
+	int currentRowNum; //state of parser info
+	int firstAttrColNum; //the start of Attribute/Value pairs of columns
+	int objIDColNum;
+	int objNameColNum;
+	CString currentFCOname;
+	CString currentAttrDName;
+	BOOL inData; //only interested in CDATA in the <Data> tag
+	CString cdata;
+
+	BOOL SetAttr(CString DisplayedName, CString Value);
+
+};
+
+#endif // !defined(AFX_TABLEEDITORPARSER_H__1CB08981_9ED5_4AFF_862A_71AD347CCEF9__INCLUDED_)


More information about the gme-commit mailing list