1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 11 * Licensed to the Apache Software Foundation (ASF) under one or more 12 * contributor license agreements. See the NOTICE file distributed 13 * with this work for additional information regarding copyright 14 * ownership. The ASF licenses this file to you under the Apache 15 * License, Version 2.0 (the "License"); you may not use this file 16 * except in compliance with the License. You may obtain a copy of 17 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 18 */ 19 20 #ifndef INCLUDED_SC_SOURCE_FILTER_XML_XMLIMPRT_HXX 21 #define INCLUDED_SC_SOURCE_FILTER_XML_XMLIMPRT_HXX 22 23 #include <xmloff/xmlimp.hxx> 24 #include <xmloff/xmlprmap.hxx> 25 #include "xmlsubti.hxx" 26 #include <formula/grammar.hxx> 27 #include <dociter.hxx> 28 29 #include <com/sun/star/sheet/ValidationAlertStyle.hpp> 30 #include <com/sun/star/sheet/ValidationType.hpp> 31 #include <com/sun/star/sheet/ConditionOperator.hpp> 32 33 #include <memory> 34 #include <map> 35 #include <vector> 36 #include <list> 37 38 namespace com { namespace sun { namespace star { namespace beans { class XPropertySet; } } } } 39 namespace com { namespace sun { namespace star { namespace sheet { class XSheetCellRangeContainer; } } } } 40 namespace com { namespace sun { namespace star { namespace table { struct CellRangeAddress; } } } } 41 namespace com { namespace sun { namespace star { namespace util { class XNumberFormatTypes; } } } } 42 namespace com { namespace sun { namespace star { namespace util { class XNumberFormats; } } } } 43 namespace sax_fastparser { class FastAttributeList; } 44 45 class ScCompiler; 46 class ErrCode; 47 class ScMyStyleNumberFormats; 48 class XMLNumberFormatAttributesExportHelper; 49 class ScEditEngineDefaulter; 50 class ScDocumentImport; 51 class ScMyImpDetectiveOpArray; 52 53 namespace sc { 54 55 struct ImportPostProcessData; 56 struct PivotTableSources; 57 58 } 59 60 enum ScXMLDocTokens 61 { 62 XML_TOK_DOC_FONTDECLS, 63 XML_TOK_DOC_STYLES, 64 XML_TOK_DOC_AUTOSTYLES, 65 XML_TOK_DOC_MASTERSTYLES, 66 XML_TOK_DOC_META, 67 XML_TOK_DOC_SCRIPTS, 68 XML_TOK_DOC_BODY, 69 XML_TOK_DOC_SETTINGS 70 }; 71 72 enum ScXMLContentValidationElemTokens 73 { 74 XML_TOK_CONTENT_VALIDATION_ELEM_HELP_MESSAGE, 75 XML_TOK_CONTENT_VALIDATION_ELEM_ERROR_MESSAGE, 76 XML_TOK_CONTENT_VALIDATION_ELEM_ERROR_MACRO, 77 XML_TOK_CONTENT_VALIDATION_ELEM_EVENT_LISTENERS 78 }; 79 80 enum ScXMLContentValidationMessageElemTokens 81 { 82 XML_TOK_P 83 }; 84 85 enum ScXMLTableTokens 86 { 87 XML_TOK_TABLE_NAMED_EXPRESSIONS, 88 XML_TOK_TABLE_COL_GROUP, 89 XML_TOK_TABLE_HEADER_COLS, 90 XML_TOK_TABLE_COLS, 91 XML_TOK_TABLE_COL, 92 XML_TOK_TABLE_ROW_GROUP, 93 XML_TOK_TABLE_HEADER_ROWS, 94 XML_TOK_TABLE_PROTECTION, 95 XML_TOK_TABLE_PROTECTION_EXT, 96 XML_TOK_TABLE_ROWS, 97 XML_TOK_TABLE_ROW, 98 XML_TOK_TABLE_SOURCE, 99 XML_TOK_TABLE_SCENARIO, 100 XML_TOK_TABLE_SHAPES, 101 XML_TOK_TABLE_FORMS, 102 XML_TOK_TABLE_EVENT_LISTENERS, 103 XML_TOK_TABLE_EVENT_LISTENERS_EXT, 104 XML_TOK_TABLE_CONDFORMATS 105 }; 106 107 enum ScXMLTableRowsTokens 108 { 109 XML_TOK_TABLE_ROWS_ROW_GROUP, 110 XML_TOK_TABLE_ROWS_HEADER_ROWS, 111 XML_TOK_TABLE_ROWS_ROWS, 112 XML_TOK_TABLE_ROWS_ROW 113 }; 114 115 enum ScXMLTableRowTokens 116 { 117 XML_TOK_TABLE_ROW_CELL, 118 XML_TOK_TABLE_ROW_COVERED_CELL 119 }; 120 121 enum ScXMLTableRowAttrTokens 122 { 123 XML_TOK_TABLE_ROW_ATTR_STYLE_NAME, 124 XML_TOK_TABLE_ROW_ATTR_VISIBILITY, 125 XML_TOK_TABLE_ROW_ATTR_REPEATED, 126 XML_TOK_TABLE_ROW_ATTR_DEFAULT_CELL_STYLE_NAME 127 // XML_TOK_TABLE_ROW_ATTR_USE_OPTIMAL_HEIGHT 128 }; 129 130 enum ScXMLTableRowCellTokens 131 { 132 XML_TOK_TABLE_ROW_CELL_P, 133 XML_TOK_TABLE_ROW_CELL_TABLE, 134 XML_TOK_TABLE_ROW_CELL_ANNOTATION, 135 XML_TOK_TABLE_ROW_CELL_DETECTIVE, 136 XML_TOK_TABLE_ROW_CELL_CELL_RANGE_SOURCE 137 }; 138 139 enum ScXMLTableRowCellAttrTokens 140 { 141 XML_TOK_TABLE_ROW_CELL_ATTR_STYLE_NAME, 142 XML_TOK_TABLE_ROW_CELL_ATTR_CONTENT_VALIDATION_NAME, 143 XML_TOK_TABLE_ROW_CELL_ATTR_SPANNED_ROWS, 144 XML_TOK_TABLE_ROW_CELL_ATTR_SPANNED_COLS, 145 XML_TOK_TABLE_ROW_CELL_ATTR_SPANNED_MATRIX_COLS, 146 XML_TOK_TABLE_ROW_CELL_ATTR_SPANNED_MATRIX_ROWS, 147 XML_TOK_TABLE_ROW_CELL_ATTR_REPEATED, 148 XML_TOK_TABLE_ROW_CELL_ATTR_VALUE_TYPE, 149 XML_TOK_TABLE_ROW_CELL_ATTR_NEW_VALUE_TYPE, 150 XML_TOK_TABLE_ROW_CELL_ATTR_VALUE, 151 XML_TOK_TABLE_ROW_CELL_ATTR_DATE_VALUE, 152 XML_TOK_TABLE_ROW_CELL_ATTR_TIME_VALUE, 153 XML_TOK_TABLE_ROW_CELL_ATTR_STRING_VALUE, 154 XML_TOK_TABLE_ROW_CELL_ATTR_BOOLEAN_VALUE, 155 XML_TOK_TABLE_ROW_CELL_ATTR_FORMULA, 156 XML_TOK_TABLE_ROW_CELL_ATTR_CURRENCY 157 }; 158 159 enum ScXMLAnnotationAttrTokens 160 { 161 XML_TOK_TABLE_ANNOTATION_ATTR_AUTHOR, 162 XML_TOK_TABLE_ANNOTATION_ATTR_CREATE_DATE, 163 XML_TOK_TABLE_ANNOTATION_ATTR_CREATE_DATE_STRING, 164 XML_TOK_TABLE_ANNOTATION_ATTR_DISPLAY, 165 XML_TOK_TABLE_ANNOTATION_ATTR_X, 166 XML_TOK_TABLE_ANNOTATION_ATTR_Y 167 }; 168 169 class SvXMLTokenMap; 170 class XMLShapeImportHelper; 171 class ScXMLChangeTrackingImportHelper; 172 class SolarMutexGuard; 173 174 struct ScMyNamedExpression 175 { 176 OUString sName; 177 OUString sContent; 178 OUString sContentNmsp; 179 OUString sBaseCellAddress; 180 OUString sRangeType; 181 formula::FormulaGrammar::Grammar eGrammar; 182 bool bIsExpression; 183 }; 184 185 typedef ::std::list<std::unique_ptr<ScMyNamedExpression>> ScMyNamedExpressions; 186 187 struct ScMyLabelRange 188 { 189 OUString const sLabelRangeStr; 190 OUString const sDataRangeStr; 191 bool const bColumnOrientation; 192 }; 193 194 typedef std::list< std::unique_ptr<const ScMyLabelRange> > ScMyLabelRanges; 195 196 struct ScMyImportValidation 197 { 198 OUString sName; 199 OUString sImputTitle; 200 OUString sImputMessage; 201 OUString sErrorTitle; 202 OUString sErrorMessage; 203 OUString sFormula1; 204 OUString sFormula2; 205 OUString sFormulaNmsp1; 206 OUString sFormulaNmsp2; 207 OUString sBaseCellAddress; // string is used directly 208 css::sheet::ValidationAlertStyle aAlertStyle; 209 css::sheet::ValidationType aValidationType; 210 css::sheet::ConditionOperator aOperator; 211 formula::FormulaGrammar::Grammar eGrammar1; 212 formula::FormulaGrammar::Grammar eGrammar2; 213 sal_Int16 nShowList; 214 bool bShowErrorMessage; 215 bool bShowImputMessage; 216 bool bIgnoreBlanks; 217 }; 218 219 typedef std::vector<ScMyImportValidation> ScMyImportValidations; 220 class ScMyStylesImportHelper; 221 class ScXMLEditAttributeMap; 222 223 class ScXMLImport: public SvXMLImport 224 { 225 ScXMLImport(const ScXMLImport&) = delete; 226 const ScXMLImport& operator=(const ScXMLImport&) = delete; 227 228 typedef ::std::map<SCTAB, std::unique_ptr<ScMyNamedExpressions>> SheetNamedExpMap; 229 230 ScDocument* pDoc; 231 std::unique_ptr<ScDocumentImport> mpDocImport; 232 std::unique_ptr<ScCompiler> mpComp; // For error-checking of cached string cell values. 233 std::unique_ptr<ScEditEngineDefaulter> mpEditEngine; 234 std::unique_ptr<sc::PivotTableSources> mpPivotSources; 235 236 mutable std::unique_ptr<ScXMLEditAttributeMap> mpEditAttrMap; 237 std::unique_ptr<ScXMLChangeTrackingImportHelper> pChangeTrackingImportHelper; 238 std::unique_ptr<ScMyStylesImportHelper> pStylesImportHelper; 239 240 rtl::Reference < XMLPropertyHandlerFactory > xScPropHdlFactory; 241 rtl::Reference < XMLPropertySetMapper > xCellStylesPropertySetMapper; 242 rtl::Reference < XMLPropertySetMapper > xColumnStylesPropertySetMapper; 243 rtl::Reference < XMLPropertySetMapper > xRowStylesPropertySetMapper; 244 rtl::Reference < XMLPropertySetMapper > xTableStylesPropertySetMapper; 245 246 std::unique_ptr<SvXMLTokenMap> pDocElemTokenMap; 247 std::unique_ptr<SvXMLTokenMap> pContentValidationElemTokenMap; 248 std::unique_ptr<SvXMLTokenMap> pContentValidationMessageElemTokenMap; 249 std::unique_ptr<SvXMLTokenMap> pTableElemTokenMap; 250 std::unique_ptr<SvXMLTokenMap> pTableRowsElemTokenMap; 251 std::unique_ptr<SvXMLTokenMap> pTableRowElemTokenMap; 252 std::unique_ptr<SvXMLTokenMap> pTableRowAttrTokenMap; 253 std::unique_ptr<SvXMLTokenMap> pTableRowCellElemTokenMap; 254 std::unique_ptr<SvXMLTokenMap> pTableRowCellAttrTokenMap; 255 std::unique_ptr<SvXMLTokenMap> pTableAnnotationAttrTokenMap; 256 257 sc::ImportPostProcessData* mpPostProcessData; /// Lift cycle managed elsewhere, no need to delete. 258 259 ScMyTables aTables; 260 261 std::vector<ScDocRowHeightUpdater::TabRanges> maRecalcRowRanges; 262 263 std::unique_ptr<ScMyNamedExpressions> m_pMyNamedExpressions; 264 SheetNamedExpMap m_SheetNamedExpressions; 265 266 std::unique_ptr<ScMyLabelRanges> pMyLabelRanges; 267 std::unique_ptr<ScMyImportValidations> pValidations; 268 std::unique_ptr<ScMyImpDetectiveOpArray> pDetectiveOpArray; 269 std::unique_ptr<SolarMutexGuard> pSolarMutexGuard; 270 271 std::unique_ptr<XMLNumberFormatAttributesExportHelper> pNumberFormatAttributesExportHelper; 272 std::unique_ptr<ScMyStyleNumberFormats> pStyleNumberFormats; 273 css::uno::Reference <css::util::XNumberFormats> xNumberFormats; 274 css::uno::Reference <css::util::XNumberFormatTypes> xNumberFormatTypes; 275 276 css::uno::Reference <css::sheet::XSheetCellRangeContainer> xSheetCellRanges; 277 278 OUString sPrevStyleName; 279 OUString sPrevCurrency; 280 sal_uInt32 nSolarMutexLocked; 281 sal_Int32 nProgressCount; 282 sal_Int16 nPrevCellType; 283 bool bLoadDoc; // Load doc or styles only 284 bool bNullDateSetted; 285 bool bSelfImportingXMLSet; 286 bool mbLockSolarMutex; 287 bool mbImportStyles; 288 bool mbHasNewCondFormatData; 289 290 protected: 291 292 // This method is called after the namespace map has been updated, but 293 // before a context for the current element has been pushed. 294 virtual SvXMLImportContext *CreateFastContext( sal_Int32 nElement, 295 const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& xAttrList ) override; 296 297 virtual XMLShapeImportHelper* CreateShapeImport() override; 298 299 public: 300 ScXMLImport( 301 const css::uno::Reference< css::uno::XComponentContext >& rContext, 302 OUString const & implementationName, SvXMLImportFlags nImportFlag); 303 304 virtual ~ScXMLImport() throw() override; 305 306 // XInitialization 307 virtual void SAL_CALL initialize( const css::uno::Sequence<css::uno::Any>& aArguments ) override; 308 309 // namespace office 310 // NB: in contrast to other CreateFooContexts, this particular one handles 311 // the root element (i.e. office:document-meta) 312 SvXMLImportContext *CreateMetaContext( 313 const sal_Int32 nElement ); 314 SvXMLImportContext *CreateFontDeclsContext(const sal_uInt16 nPrefix, const OUString& rLocalName, 315 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttrList); 316 SvXMLImportContext *CreateScriptContext( 317 const OUString& rLocalName ); 318 SvXMLImportContext *CreateStylesContext(const OUString& rLocalName, 319 const css::uno::Reference<css::xml::sax::XAttributeList>& xAttrList, 320 bool bAutoStyles ); 321 322 SvXMLImportContext *CreateBodyContext( 323 const rtl::Reference<sax_fastparser::FastAttributeList>& rAttrList ); 324 325 virtual void SetStatistics( const css::uno::Sequence< css::beans::NamedValue> & i_rStats) override; 326 327 ScDocumentImport& GetDoc(); 328 GetDocument()329 ScDocument* GetDocument() { return pDoc; } GetDocument() const330 const ScDocument* GetDocument() const { return pDoc; } 331 GetTables()332 ScMyTables& GetTables() { return aTables; } 333 GetRecalcRowRanges()334 std::vector<ScDocRowHeightUpdater::TabRanges>& GetRecalcRowRanges() { return maRecalcRowRanges; } 335 IsStylesOnlyMode() const336 bool IsStylesOnlyMode() const { return !bLoadDoc; } 337 338 static sal_Int16 GetCellType(const char* rStrValue, const sal_Int32 nStrLength); 339 GetCellStylesPropertySetMapper() const340 const rtl::Reference < XMLPropertySetMapper >& GetCellStylesPropertySetMapper() const { return xCellStylesPropertySetMapper; } GetColumnStylesPropertySetMapper() const341 const rtl::Reference < XMLPropertySetMapper >& GetColumnStylesPropertySetMapper() const { return xColumnStylesPropertySetMapper; } GetRowStylesPropertySetMapper() const342 const rtl::Reference < XMLPropertySetMapper >& GetRowStylesPropertySetMapper() const { return xRowStylesPropertySetMapper; } GetTableStylesPropertySetMapper() const343 const rtl::Reference < XMLPropertySetMapper >& GetTableStylesPropertySetMapper() const { return xTableStylesPropertySetMapper; } 344 345 const SvXMLTokenMap& GetDocElemTokenMap(); 346 const SvXMLTokenMap& GetContentValidationElemTokenMap(); 347 const SvXMLTokenMap& GetContentValidationMessageElemTokenMap(); 348 const SvXMLTokenMap& GetTableElemTokenMap(); 349 const SvXMLTokenMap& GetTableRowsElemTokenMap(); 350 const SvXMLTokenMap& GetTableRowElemTokenMap(); 351 const SvXMLTokenMap& GetTableRowAttrTokenMap(); 352 const SvXMLTokenMap& GetTableRowCellElemTokenMap(); 353 const SvXMLTokenMap& GetTableRowCellAttrTokenMap(); 354 const SvXMLTokenMap& GetTableAnnotationAttrTokenMap(); 355 356 void SetPostProcessData( sc::ImportPostProcessData* p ); GetPostProcessData()357 sc::ImportPostProcessData* GetPostProcessData() { return mpPostProcessData;} 358 359 sc::PivotTableSources& GetPivotTableSources(); 360 AddNamedExpression(ScMyNamedExpression * pMyNamedExpression)361 void AddNamedExpression(ScMyNamedExpression* pMyNamedExpression) 362 { 363 if (!m_pMyNamedExpressions) 364 m_pMyNamedExpressions.reset(new ScMyNamedExpressions); 365 m_pMyNamedExpressions->push_back(std::unique_ptr<ScMyNamedExpression>(pMyNamedExpression)); 366 } 367 368 void AddNamedExpression(SCTAB nTab, ScMyNamedExpression* pNamedExp); 369 AddLabelRange(std::unique_ptr<const ScMyLabelRange> pMyLabelRange)370 void AddLabelRange(std::unique_ptr<const ScMyLabelRange> pMyLabelRange) { 371 if (!pMyLabelRanges) 372 pMyLabelRanges.reset(new ScMyLabelRanges); 373 pMyLabelRanges->push_back(std::move(pMyLabelRange)); } 374 AddValidation(const ScMyImportValidation & rValidation)375 void AddValidation(const ScMyImportValidation& rValidation) { 376 if (!pValidations) 377 pValidations.reset(new ScMyImportValidations); 378 pValidations->push_back(rValidation); } 379 bool GetValidation(const OUString& sName, ScMyImportValidation& aValidation); 380 381 ScMyImpDetectiveOpArray* GetDetectiveOpArray(); 382 383 ScXMLChangeTrackingImportHelper* GetChangeTrackingImportHelper(); 384 void InsertStyles(); 385 386 void SetChangeTrackingViewSettings(const css::uno::Sequence<css::beans::PropertyValue>& rChangeProps); 387 virtual void SetViewSettings(const css::uno::Sequence<css::beans::PropertyValue>& aViewProps) override; 388 virtual void SetConfigurationSettings(const css::uno::Sequence<css::beans::PropertyValue>& aConfigProps) override; 389 GetStylesImportHelper()390 ScMyStylesImportHelper* GetStylesImportHelper() { return pStylesImportHelper.get(); } 391 sal_Int32 SetCurrencySymbol(const sal_Int32 nKey, const OUString& rCurrency); 392 bool IsCurrencySymbol(const sal_Int32 nNumberFormat, const OUString& sCurrencySymbol, const OUString& sBankSymbol); 393 void SetType(const css::uno::Reference <css::beans::XPropertySet>& rProperties, 394 sal_Int32& rNumberFormat, 395 const sal_Int16 nCellType, 396 const OUString& rCurrency); 397 398 void ProgressBarIncrement(); 399 SetNewCondFormatData()400 void SetNewCondFormatData() { mbHasNewCondFormatData = true; } HasNewCondFormatData() const401 bool HasNewCondFormatData() const { return mbHasNewCondFormatData; } 402 403 private: 404 void AddStyleRange(const css::table::CellRangeAddress& rCellRange); 405 void SetStyleToRanges(); 406 407 void ExamineDefaultStyle(); 408 public: 409 void SetStyleToRange(const ScRange& rRange, const OUString* pStyleName, 410 const sal_Int16 nCellType, const OUString* pCurrency); 411 bool SetNullDateOnUnitConverter(); 412 XMLNumberFormatAttributesExportHelper* GetNumberFormatAttributesExportHelper(); 413 ScMyStyleNumberFormats* GetStyleNumberFormats(); 414 415 void SetStylesToRangesFinished(); 416 417 // XImporter 418 virtual void SAL_CALL setTargetDocument( const css::uno::Reference< css::lang::XComponent >& xDoc ) override; 419 420 virtual void SAL_CALL startDocument() override; 421 virtual void SAL_CALL endDocument() override; 422 423 virtual void DisposingModel() override; 424 425 /** 426 * Use this class to manage solar mutex locking instead of calling 427 * LockSolarMutex() and UnlockSolarMutex() directly. 428 */ 429 class MutexGuard 430 { 431 public: 432 explicit MutexGuard(ScXMLImport& rImport); 433 ~MutexGuard(); 434 private: 435 ScXMLImport& mrImport; 436 }; 437 void LockSolarMutex(); 438 void UnlockSolarMutex(); 439 440 sal_Int32 GetByteOffset() const; 441 442 void SetRangeOverflowType(ErrCode nType); 443 444 static sal_Int32 GetRangeType(const OUString& sRangeType); 445 void SetNamedRanges(); 446 void SetSheetNamedRanges(); 447 void SetLabelRanges(); 448 void SetStringRefSyntaxIfMissing(); 449 450 /** Extracts the formula string, the formula grammar namespace URL, and a 451 grammar enum value from the passed formula attribute value. 452 453 @param rFormula 454 (out-parameter) Returns the plain formula string with the leading 455 equality sign if existing. 456 457 @param rFormulaNmsp 458 (out-parameter) Returns the URL of the formula grammar namespace if 459 the attribute value contains the prefix of an unknown namespace. 460 461 @param reGrammar 462 (out-parameter) Returns the exact formula grammar if the formula 463 is in a supported ODF format (e.g. FormulaGrammar::GRAM_PODF for 464 ODF 1.0/1.1 formulas, or FormulaGrammar::GRAM_ODFF for ODF 1.2 465 formulas a.k.a. OpenFormula). Returns the default storage grammar, 466 if the attribute value does not contain a namespace prefix. Returns 467 the special value FormulaGrammar::GRAM_EXTERNAL, if an unknown 468 namespace could be extracted from the formula which will be 469 contained in the parameter rFormulaNmsp then. 470 471 @param rAttrValue 472 The value of the processed formula attribute. 473 474 @param bRestrictToExternalNmsp 475 If set to true, only namespaces of external formula grammars will 476 be recognized. Internal namespace prefixes (e.g. 'oooc:' or 'of:' 477 will be considered to be part of the formula, e.g. an expression 478 with range operator. 479 */ 480 void ExtractFormulaNamespaceGrammar( 481 OUString& rFormula, 482 OUString& rFormulaNmsp, 483 ::formula::FormulaGrammar::Grammar& reGrammar, 484 const OUString& rAttrValue, 485 bool bRestrictToExternalNmsp = false ) const; 486 487 FormulaError GetFormulaErrorConstant( const OUString& rStr ) const; 488 489 ScEditEngineDefaulter* GetEditEngine(); 490 const ScXMLEditAttributeMap& GetEditAttributeMap() const; 491 virtual void NotifyEmbeddedFontRead() override; 492 }; 493 494 #endif 495 496 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 497