/************************************************************************
 *
 * Licensed Materials - Property of IBM.
 * (C) Copyright IBM Corporation 2003, 2012.  All Rights Reserved.
 * U.S. Government Users Restricted Rights:
 * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 ************************************************************************/




#ifndef SDTABLE_IMPORTCONTEXT_HXX
#include "sdtableimpcontext.hxx"
#endif

#include <xmloff/xmltkmap.hxx>
#ifndef _XMLOFF_XMLTOKEN_HXX
#include <xmloff/xmltoken.hxx>
#endif
#ifndef _XMLOFF_XMLNMSPE_HXX
#include "xmloff/xmlnmspe.hxx"
#endif
#ifndef _XMLOFF_NMSPMAP_HXX
#include <xmloff/nmspmap.hxx>
#endif
#ifndef _XMLOFF_XMLUCONV_HXX
#include <xmloff/xmluconv.hxx>
#endif

#ifndef _SAL_TYPES_H_
#include <sal/types.h>
#endif
#ifndef _RTTI_HXX_
#include <tools/rtti.hxx>
#endif

#ifndef SDTABLE_XML_STYLE_HXX
#include "sdtablexmlstyle.hxx"
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSET_HPP_
#include <com/sun/star/beans/XPropertySet.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XSERVICEINFO_HPP_ 
#include <com/sun/star/lang/XServiceInfo.hpp>
#endif
#ifndef _COM_SUN_STAR_DRAWING_XSHAPES_HPP_
#include <com/sun/star/drawing/XShapes.hpp>
#endif
#ifndef _COM_SUN_STAR_BEANS_XPROPERTYSETINFO_HPP_ 
#include <com/sun/star/beans/XPropertySetInfo.hpp>
#endif
//added , 2009-1-12
#include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
using namespace ::com::sun::star::style;
//end of add
using namespace std;
using namespace com::sun::star;
using namespace xmloff::token;
using namespace com::sun::star::uno;

SdTableXMLBodyContext::SdTableXMLBodyContext( SdTableXMLImport& rImport,
											  USHORT nPrfx,
				   	  						  const ::rtl::OUString& rLName,
											  const uno::Reference<xml::sax::XAttributeList>& xAttrList ) :
	SvXMLImportContext( rImport, nPrfx, rLName )
{
}

SdTableXMLBodyContext::~SdTableXMLBodyContext()
{
}

SvXMLImportContext *SdTableXMLBodyContext::CreateChildContext( USHORT nPrefix,
									 const ::rtl::OUString& rLocalName,
									 const ::com::sun::star::uno::Reference<
									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
{
	SvXMLImportContext *pContext = 0;

	const SvXMLTokenMap& rTokenMap = GetSdTableImport().GetBodyElemTokenMap();

	switch( rTokenMap.Get( nPrefix, rLocalName ) )
	{
	case XML_TOK_BODY_SPREADSHEET:
		pContext = new SdTableXMLSpreadsheetContext ( GetSdTableImport(), nPrefix, rLocalName,
				xAttrList);
	break;
	}
	return pContext;
}

void SdTableXMLBodyContext::EndElement()
{
	//to be done...
}

//--------------------------------------------------------------------------
SdTableXMLSpreadsheetContext::SdTableXMLSpreadsheetContext( SdTableXMLImport& rImport, USHORT nPrfx, const ::rtl::OUString& rLName,
						const ::com::sun::star::uno::Reference<com::sun::star::xml::sax::XAttributeList>& xAttrList ) : 
	bHasTable(sal_False),
	SvXMLImportContext( rImport, nPrfx, rLName )
{
	
}
SdTableXMLSpreadsheetContext::~SdTableXMLSpreadsheetContext()
{
	
}

SvXMLImportContext* SdTableXMLSpreadsheetContext::CreateChildContext( USHORT nPrefix,
													const ::rtl::OUString& rLocalName,
													const ::com::sun::star::uno::Reference<
									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
{
	SvXMLImportContext *pContext = 0;
	if (bHasTable)
		return pContext;

	const SvXMLTokenMap& rTokenMap = GetSdTableImport().GetBodyElemTokenMap();

	switch( rTokenMap.Get( nPrefix, rLocalName ) )
	{
	case XML_TOK_BODY_TABLE:{
		pContext = new SdTableXMLTableContext( GetSdTableImport(),nPrefix, rLocalName,
											      xAttrList );
		bHasTable = sal_True;

							}
	break;	
	}	
	return pContext;
}

//--------------------------------------------------------------------------
SdTableXMLTableContext::SdTableXMLTableContext( SdTableXMLImport& rImport, USHORT nPrfx,
						const ::rtl::OUString& rLName,
						const ::com::sun::star::uno::Reference<
										::com::sun::star::xml::sax::XAttributeList>& xAttrList) :
	 SvXMLImportContext( rImport, nPrfx, rLName )
{	
		sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
		const SvXMLTokenMap& rAttrTokenMap = GetSdTableImport().GetTableAttrTokenMap();
		for( sal_Int16 i=0; i < nAttrCount; i++ )
		{
			rtl::OUString sAttrName = xAttrList->getNameByIndex( i );
			rtl::OUString aLocalName;
			USHORT nPrefix = GetSdTableImport().GetNamespaceMap().GetKeyByAttrName(
												sAttrName, &aLocalName );
			rtl::OUString sValue = xAttrList->getValueByIndex( i );

			switch( rAttrTokenMap.Get( nPrefix, aLocalName ) )
			{
				case XML_TOK_TABLE_NAME:
						m_sName = sValue;
					break;
				case XML_TOK_TABLE_STYLE_NAME:
						m_sStyleName = sValue;
					break;
			}
		}
}

SdTableXMLTableContext::~SdTableXMLTableContext()
{
}

//add  for SD_Embedded_Table, 20090904
void SdTableXMLTableContext::StartElement( const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList >& xAttrList ){
	com::sun::star::uno::Reference< com::sun::star::drawing::XShape > xTableShape
			= GetSdTableImport().GetSdTableShape();
		com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps(xTableShape, uno::UNO_QUERY);
		const rtl::Reference< XMLTableImport >& xTableImport( GetImport().GetShapeImport()->GetShapeTableImport() );
		//added , 2009-1-12
		Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetImport().GetParentModel(), UNO_QUERY_THROW );
		Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
		const rtl::OUString sFamilyName( RTL_CONSTASCII_USTRINGPARAM("table" ) );
		//Modify 
		const rtl::OUString msTemplateStyleName( RTL_CONSTASCII_USTRINGPARAM("sym13table" ) );//default&sym13table
		//End modify 
		Reference< XNameAccess > xTableFamily( xFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
		Reference< XStyle > xTableStyle( xTableFamily->getByName( msTemplateStyleName ), UNO_QUERY_THROW );
		xProps->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "TableTemplate" ) ), Any( xTableStyle ) );
		//end of add
		if( xTableImport.is() && xProps.is() )
		{
			uno::Reference< table::XColumnRowRange > xColumnRowRange(
				xProps->getPropertyValue( ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Model") ) ), uno::UNO_QUERY );

			if( xColumnRowRange.is() )
				//  changed
				mxTableImportContext = xTableImport->CreateTableContext( GetPrefix(), GetLocalName(), xColumnRowRange, xProps );

			if( mxTableImportContext.Is() )
				mxTableImportContext->StartElement( xAttrList );
		}
}
SvXMLImportContext * SdTableXMLTableContext::CreateChildContext( USHORT nPrefix,
									 const ::rtl::OUString& rLocalName,
									 const ::com::sun::star::uno::Reference<
									  	::com::sun::star::xml::sax::XAttributeList>& xAttrList )
{
	//modified  for SD_Embedded_Table, 20090914
	if( mxTableImportContext.Is() )
		return mxTableImportContext->CreateChildContext( nPrefix, rLocalName, xAttrList );
	else
		return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );
		
}

void SdTableXMLTableContext::EndElement()
{
	//to be done...
	//add  for SD_Embedded_Table, 20090914
	if( mxTableImportContext.Is() )
		mxTableImportContext->EndElement();
	else
		SvXMLImportContext::EndElement();
	//end add
}

