summaryrefslogtreecommitdiff
path: root/xmloff/source/table/XMLTableImport.cxx
diff options
context:
space:
mode:
authorJakub Trzebiatowski <ubap.dev@gmail.com>2016-06-21 00:31:01 +0200
committerMiklos Vajna <vmiklos@collabora.co.uk>2016-07-07 09:02:15 +0000
commit40fba0f4418084d50cc5c388cb0b6e1abe395d61 (patch)
tree4b1e97aa7755c80d9a8749fb94dc3fe26a2f35fb /xmloff/source/table/XMLTableImport.cxx
parent046244bcfe1c5c1cd2325fe74b933c05e43cf190 (diff)
GSoC Writer Table Styles Import
This patch is implementing import of table styles (table-template). Modified shared code: Added "background" to the cell styles export. To make cell export properties map accessible by both export and import code, moved from xmloff/source/table/XMLTableExport.cxx to xmloff/txtprmap.hxx. To avoid export of default valued properties implemented XPropertyState for SwXTextCellStyle Change-Id: I8b4f12e4b51f478f8ce8fde1203cd4611d7ae852 Reviewed-on: https://gerrit.libreoffice.org/26721 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Miklos Vajna <vmiklos@collabora.co.uk>
Diffstat (limited to 'xmloff/source/table/XMLTableImport.cxx')
-rw-r--r--xmloff/source/table/XMLTableImport.cxx129
1 files changed, 126 insertions, 3 deletions
diff --git a/xmloff/source/table/XMLTableImport.cxx b/xmloff/source/table/XMLTableImport.cxx
index d4b505d8bb9e..6ac15996f9d0 100644
--- a/xmloff/source/table/XMLTableImport.cxx
+++ b/xmloff/source/table/XMLTableImport.cxx
@@ -24,6 +24,7 @@
#include <com/sun/star/table/XTable.hpp>
#include <com/sun/star/text/XText.hpp>
#include <com/sun/star/container/XNameContainer.hpp>
+#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/lang/XSingleServiceFactory.hpp>
#include <xmloff/table/XMLTableImport.hxx>
@@ -37,6 +38,7 @@
#include <xmloff/prstylei.hxx>
#include <xmloff/xmlnmspe.hxx>
+#include <xmloff/xmluconv.hxx>
#include "table.hxx"
#include <osl/diagnose.h>
@@ -160,6 +162,7 @@ public:
virtual void EndElement() override;
+ virtual void CreateAndInsert( bool bOverwrite ) override;
private:
XMLTableTemplate maTableTemplate;
OUString msTemplateStyleName;
@@ -186,8 +189,37 @@ SvXMLImportContext * XMLProxyContext::CreateChildContext( sal_uInt16 nPrefix, co
XMLTableImport::XMLTableImport( SvXMLImport& rImport, const rtl::Reference< XMLPropertySetMapper >& xCellPropertySetMapper, const rtl::Reference< XMLPropertyHandlerFactory >& xFactoryRef )
: mrImport( rImport )
{
- mxCellImportPropertySetMapper = new SvXMLImportPropertyMapper( xCellPropertySetMapper.get(), rImport );
- mxCellImportPropertySetMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(rImport));
+ bool bWriter = false;
+ // check if called by Writer
+ Reference<XMultiServiceFactory> xFac(rImport.GetModel(), UNO_QUERY);
+ if (xFac.is()) try
+ {
+ Sequence<OUString> sSNS = xFac->getAvailableServiceNames();
+ const sal_Int32 nLength = sSNS.getLength();
+ const OUString* pSNS = sSNS.getConstArray();
+ for (sal_Int32 i=0; i < nLength; ++i, ++pSNS)
+ {
+ if (*pSNS == "com.sun.star.style.TableStyle")
+ {
+ bWriter = true;
+ break;
+ }
+ }
+ }
+ catch(const Exception&)
+ {
+ SAL_WARN("xmloff.table", "Error while checking avaiable service names");
+ }
+
+ if (bWriter)
+ {
+ mxCellImportPropertySetMapper = XMLTextImportHelper::CreateTableCellExtPropMapper(rImport);
+ }
+ else
+ {
+ mxCellImportPropertySetMapper = new SvXMLImportPropertyMapper( xCellPropertySetMapper.get(), rImport );
+ mxCellImportPropertySetMapper->ChainImportMapper(XMLTextImportHelper::CreateParaExtPropMapper(rImport));
+ }
rtl::Reference < XMLPropertySetMapper > xRowMapper( new XMLPropertySetMapper( getRowPropertiesMap(), xFactoryRef.get(), false ) );
mxRowImportPropertySetMapper = new SvXMLImportPropertyMapper( xRowMapper, rImport );
@@ -218,6 +250,67 @@ void XMLTableImport::addTableTemplate( const OUString& rsStyleName, XMLTableTemp
maTableTemplates[rsStyleName] = xPtr;
}
+void XMLTableImport::insertTabletemplate(const OUString& rsStyleName, bool bOverwrite)
+{
+ XMLTableTemplateMap::iterator it = maTableTemplates.find(rsStyleName);
+ if (it == maTableTemplates.end())
+ return;
+
+ try
+ {
+ Reference<XStyleFamiliesSupplier> xFamiliesSupp(mrImport.GetModel(), UNO_QUERY_THROW);
+ Reference<XNameAccess> xFamilies(xFamiliesSupp->getStyleFamilies());
+ const OUString sFamilyName("TableStyles");
+ const OUString sCellFamilyName("CellStyles");
+
+ Reference<XNameContainer> xTableFamily(xFamilies->getByName(sFamilyName), UNO_QUERY_THROW);
+ Reference<XIndexAccess> xCellFamily(xFamilies->getByName(sCellFamilyName), UNO_QUERY_THROW);
+
+ const OUString sTemplateName(it->first);
+ Reference<XMultiServiceFactory> xFactory(mrImport.GetModel(), UNO_QUERY_THROW);
+ Reference<XNameReplace> xTemplate(xFactory->createInstance("com.sun.star.style.TableStyle"), UNO_QUERY_THROW);
+
+ std::shared_ptr<XMLTableTemplate> xT(it->second);
+
+ for (auto aStyleIter=xT->begin(); aStyleIter != xT->end(); ++aStyleIter) try
+ {
+ const OUString sPropName((*aStyleIter).first);
+ const OUString sStyleName((*aStyleIter).second);
+ // Internally unassigned cell styles are stored by display name.
+ // However table-template elements reference cell styles by its encoded name.
+ // This loop is looking for cell style by their encoded names.
+ sal_Int32 nCount = xCellFamily->getCount();
+ for (sal_Int32 i=0; i < nCount; ++i)
+ {
+ Any xCellStyle = xCellFamily->getByIndex(i);
+ OUString sEncodedStyleName = mrImport.GetMM100UnitConverter().encodeStyleName(
+ xCellStyle.get<Reference<XStyle>>()->getName());
+ if (sEncodedStyleName == sStyleName)
+ {
+ xTemplate->replaceByName(sPropName, xCellStyle);
+ break;
+ }
+ }
+ }
+ catch (Exception&)
+ {
+ SAL_WARN("xmloff.table", "XMLTableImport::insertTabletemplate(), exception caught!");
+ }
+
+ if (xTemplate.is())
+ {
+ if (xTableFamily->hasByName(sTemplateName) && bOverwrite)
+ xTableFamily->replaceByName(sTemplateName, Any(xTemplate));
+ else
+ xTableFamily->insertByName(sTemplateName, Any(xTemplate));
+ }
+ }
+ catch (Exception&)
+ {
+ SAL_WARN("xmloff.table", "XMLTableImport::insertTabletemplate(), exception caught!");
+ }
+}
+
void XMLTableImport::finishStyles()
{
if( !maTableTemplates.empty() ) try
@@ -692,7 +785,9 @@ void XMLTableTemplateContext::StartElement( const Reference< XAttributeList >& x
{
OUString sAttrName;
sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName( xAttrList->getNameByIndex( i ), &sAttrName );
- if( (nAttrPrefix == XML_NAMESPACE_TEXT ) && IsXMLToken( sAttrName, XML_STYLE_NAME ) )
+ if( (nAttrPrefix == XML_NAMESPACE_TEXT && IsXMLToken( sAttrName, XML_STYLE_NAME ))
+ // Writer specific: according to oasis odf 1.2 prefix should be "table" and element name should be "name"
+ || (nAttrPrefix == XML_NAMESPACE_TABLE && IsXMLToken( sAttrName, XML_NAME )))
{
msTemplateStyleName = xAttrList->getValueByIndex( i );
break;
@@ -707,6 +802,13 @@ void XMLTableTemplateContext::EndElement()
xTableImport->addTableTemplate( msTemplateStyleName, maTableTemplate );
}
+void XMLTableTemplateContext::CreateAndInsert(bool bOverwrite)
+{
+ rtl::Reference<XMLTableImport> xTableImport(GetImport().GetShapeImport()->GetShapeTableImport());
+ if(xTableImport.is())
+ xTableImport->insertTabletemplate(msTemplateStyleName, bOverwrite);
+}
+
SvXMLImportContext * XMLTableTemplateContext::CreateChildContext( sal_uInt16 nPrefix, const OUString& rLocalName, const Reference< XAttributeList >& xAttrList )
{
if( nPrefix == XML_NAMESPACE_TABLE )
@@ -730,6 +832,27 @@ SvXMLImportContext * XMLTableTemplateContext::CreateChildContext( sal_uInt16 nPr
}
}
}
+ } else if (nPrefix == XML_NAMESPACE_LO_EXT) // Writer specific cell styles
+ {
+ const TableStyleElement* pElements = getWriterSpecificTableStyleMap();
+ while ((pElements->meElement != XML_TOKEN_END) && !IsXMLToken(rLocalName, pElements->meElement ))
+ pElements++;
+
+ if (pElements->meElement != XML_TOKEN_END)
+ {
+ sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
+ for (sal_Int16 i=0; i < nAttrCount; i++)
+ {
+ OUString sAttrName;
+ sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(xAttrList->getNameByIndex( i ), &sAttrName);
+ if( (nAttrPrefix == XML_NAMESPACE_TEXT || nAttrPrefix == XML_NAMESPACE_TABLE) &&
+ IsXMLToken( sAttrName, XML_STYLE_NAME ) )
+ {
+ maTableTemplate[pElements->msStyleName] = xAttrList->getValueByIndex(i);
+ break;
+ }
+ }
+ }
}
return SvXMLImportContext::CreateChildContext( nPrefix, rLocalName, xAttrList );