1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlbodyi.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
35 // INCLUDE ---------------------------------------------------------------
38 #include "document.hxx"
40 #include "xmlbodyi.hxx"
41 #include "xmltabi.hxx"
42 #include "xmlnexpi.hxx"
43 #include "xmldrani.hxx"
44 #include "xmlimprt.hxx"
45 #include "xmldpimp.hxx"
46 #include "xmlcvali.hxx"
47 #include "xmlstyli.hxx"
48 #include "xmllabri.hxx"
49 #include "XMLConsolidationContext.hxx"
50 #include "XMLDDELinksContext.hxx"
51 #include "XMLCalculationSettingsContext.hxx"
52 #include "XMLTrackedChangesContext.hxx"
53 #include "XMLEmptyContext.hxx"
54 #include "scerrors.hxx"
55 #include "tabprotection.hxx"
57 #include <xmloff/xmltkmap.hxx>
58 #include <xmloff/xmltoken.hxx>
59 #include <xmloff/xmlnmspe.hxx>
60 #include <xmloff/nmspmap.hxx>
61 #include <xmloff/xmluconv.hxx>
62 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
63 #include <sal/types.h>
64 #include <tools/debug.hxx>
69 using namespace com::sun::star
;
70 using namespace xmloff::token
;
72 //------------------------------------------------------------------
74 ScXMLBodyContext::ScXMLBodyContext( ScXMLImport
& rImport
,
76 const ::rtl::OUString
& rLName
,
77 const uno::Reference
<xml::sax::XAttributeList
>& xAttrList
) :
78 SvXMLImportContext( rImport
, nPrfx
, rLName
),
80 meHash1(PASSHASH_UNSPECIFIED
),
81 meHash2(PASSHASH_UNSPECIFIED
),
82 bProtected(sal_False
),
83 bHadCalculationSettings(sal_False
),
84 pChangeTrackingImportHelper(NULL
)
86 ScDocument
* pDoc
= GetScImport().GetDocument();
89 // ODF 1.1 and earlier => GRAM_PODF; ODF 1.2 and later => GRAM_ODFF;
90 // no version => earlier than 1.2 => GRAM_PODF.
91 formula::FormulaGrammar::Grammar eGrammar
= formula::FormulaGrammar::GRAM_ODFF
;
92 OUString
aVer( rImport
.GetODFVersion());
93 sal_Int32 nLen
= aVer
.getLength();
94 #if OSL_DEBUG_LEVEL > 1
95 fprintf( stderr
, "\n ScXMLBodyContext ODFVersion: nLen: %d, str: %s\n",
96 (int)nLen
, OUStringToOString( aVer
, RTL_TEXTENCODING_UTF8
).getStr());
99 eGrammar
= formula::FormulaGrammar::GRAM_PODF
;
102 // In case there was a micro version, e.g. "1.2.3", this would
103 // still yield major.minor, but pParsedEnd (5th parameter, not
104 // passed here) would point before string end upon return.
105 double fVer
= ::rtl::math::stringToDouble( aVer
, '.', 0, NULL
, NULL
);
107 eGrammar
= formula::FormulaGrammar::GRAM_PODF
;
109 pDoc
->SetStorageGrammar( eGrammar
);
112 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
113 for( sal_Int16 i
=0; i
< nAttrCount
; ++i
)
115 const rtl::OUString
& sAttrName(xAttrList
->getNameByIndex( i
));
116 rtl::OUString aLocalName
;
117 USHORT nPrefix
= GetScImport().GetNamespaceMap().GetKeyByAttrName(
118 sAttrName
, &aLocalName
);
119 const rtl::OUString
& sValue(xAttrList
->getValueByIndex( i
));
121 if (nPrefix
== XML_NAMESPACE_TABLE
)
123 if (IsXMLToken(aLocalName
, XML_STRUCTURE_PROTECTED
))
124 bProtected
= IsXMLToken(sValue
, XML_TRUE
);
125 else if (IsXMLToken(aLocalName
, XML_PROTECTION_KEY
))
127 else if (IsXMLToken(aLocalName
, XML_PROTECTION_KEY_DIGEST_ALGORITHM
))
128 meHash1
= ScPassHashHelper::getHashTypeFromURI(sValue
);
129 else if (IsXMLToken(aLocalName
, XML_PROTECTION_KEY_DIGEST_ALGORITHM_2
))
130 meHash2
= ScPassHashHelper::getHashTypeFromURI(sValue
);
135 ScXMLBodyContext::~ScXMLBodyContext()
139 SvXMLImportContext
*ScXMLBodyContext::CreateChildContext( USHORT nPrefix
,
140 const ::rtl::OUString
& rLocalName
,
141 const ::com::sun::star::uno::Reference
<
142 ::com::sun::star::xml::sax::XAttributeList
>& xAttrList
)
144 SvXMLImportContext
*pContext
= 0;
146 const SvXMLTokenMap
& rTokenMap
= GetScImport().GetBodyElemTokenMap();
147 // sal_Bool bOrdered = sal_False;
148 // sal_Bool bHeading = sal_False;
149 switch( rTokenMap
.Get( nPrefix
, rLocalName
) )
151 // case XML_TOK_TEXT_H:
153 // case XML_TOK_TEXT_P:
154 // pContext = new SwXMLParaContext( GetSwImport(),nPrefix, rLocalName,
155 // xAttrList, bHeading );
157 // case XML_TOK_TEXT_ORDERED_LIST:
159 // case XML_TOK_TEXT_UNORDERED_LIST:
160 // pContext = new SwXMLListBlockContext( GetSwImport(),nPrefix, rLocalName,
161 // xAttrList, bOrdered );
163 case XML_TOK_BODY_TRACKED_CHANGES
:
165 pChangeTrackingImportHelper
= GetScImport().GetChangeTrackingImportHelper();
166 if (pChangeTrackingImportHelper
)
167 pContext
= new ScXMLTrackedChangesContext( GetScImport(), nPrefix
, rLocalName
, xAttrList
, pChangeTrackingImportHelper
);
170 case XML_TOK_BODY_CALCULATION_SETTINGS
:
171 pContext
= new ScXMLCalculationSettingsContext( GetScImport(), nPrefix
, rLocalName
, xAttrList
);
172 bHadCalculationSettings
= sal_True
;
174 case XML_TOK_BODY_CONTENT_VALIDATIONS
:
175 pContext
= new ScXMLContentValidationsContext( GetScImport(), nPrefix
, rLocalName
, xAttrList
);
177 case XML_TOK_BODY_LABEL_RANGES
:
178 pContext
= new ScXMLLabelRangesContext( GetScImport(), nPrefix
, rLocalName
, xAttrList
);
180 case XML_TOK_BODY_TABLE
:
182 if (GetScImport().GetTables().GetCurrentSheet() >= MAXTAB
)
184 GetScImport().SetRangeOverflowType(SCWARN_IMPORT_SHEET_OVERFLOW
);
185 pContext
= new ScXMLEmptyContext(GetScImport(), nPrefix
, rLocalName
);
189 pContext
= new ScXMLTableContext( GetScImport(),nPrefix
, rLocalName
,
194 case XML_TOK_BODY_NAMED_EXPRESSIONS
:
195 pContext
= new ScXMLNamedExpressionsContext ( GetScImport(), nPrefix
, rLocalName
,
198 case XML_TOK_BODY_DATABASE_RANGES
:
199 pContext
= new ScXMLDatabaseRangesContext ( GetScImport(), nPrefix
, rLocalName
,
202 case XML_TOK_BODY_DATABASE_RANGE
:
203 pContext
= new ScXMLDatabaseRangeContext ( GetScImport(), nPrefix
, rLocalName
,
206 case XML_TOK_BODY_DATA_PILOT_TABLES
:
207 pContext
= new ScXMLDataPilotTablesContext ( GetScImport(), nPrefix
, rLocalName
,
210 case XML_TOK_BODY_CONSOLIDATION
:
211 pContext
= new ScXMLConsolidationContext ( GetScImport(), nPrefix
, rLocalName
,
214 case XML_TOK_BODY_DDE_LINKS
:
215 pContext
= new ScXMLDDELinksContext ( GetScImport(), nPrefix
, rLocalName
,
221 pContext
= new SvXMLImportContext( GetImport(), nPrefix
, rLocalName
);
226 void ScXMLBodyContext::EndElement()
228 if (!bHadCalculationSettings
)
230 // #111055#; set calculation settings defaults if there is no calculation settings element
231 SvXMLImportContext
*pContext
= new ScXMLCalculationSettingsContext( GetScImport(), XML_NAMESPACE_TABLE
, GetXMLToken(XML_CALCULATION_SETTINGS
), NULL
);
232 pContext
->EndElement();
234 GetScImport().LockSolarMutex();
235 ScMyImpDetectiveOpArray
* pDetOpArray
= GetScImport().GetDetectiveOpArray();
236 ScDocument
* pDoc
= GetScImport().GetDocument();
237 ScMyImpDetectiveOp aDetOp
;
239 if (pDoc
&& GetScImport().GetModel().is())
244 while( pDetOpArray
->GetFirstOp( aDetOp
) )
246 ScDetOpData
aOpData( aDetOp
.aPosition
, aDetOp
.eOpType
);
247 pDoc
->AddDetectiveOperation( aOpData
);
251 if (pChangeTrackingImportHelper
)
252 pChangeTrackingImportHelper
->CreateChangeTrack(GetScImport().GetDocument());
255 // #i57869# table styles are applied before the contents now
257 std::vector
<rtl::OUString
> aTableStyleNames(GetScImport().GetTableStyle());
258 uno::Reference
<sheet::XSpreadsheetDocument
> xSpreadDoc( GetScImport().GetModel(), uno::UNO_QUERY
);
259 if ( xSpreadDoc
.is() && !aTableStyleNames
.empty())
261 uno::Reference
<container::XIndexAccess
> xIndex( xSpreadDoc
->getSheets(), uno::UNO_QUERY
);
264 sal_Int32 nTableCount
= xIndex
->getCount();
265 sal_Int32
nSize(aTableStyleNames
.size());
266 DBG_ASSERT(nTableCount
== nSize
, "every table should have a style name");
267 for(sal_uInt32 i
= 0; i
< nTableCount
; i
++)
271 uno::Reference
<beans::XPropertySet
> xProperties(xIndex
->getByIndex(i
), uno::UNO_QUERY
);
272 if (xProperties
.is())
274 rtl::OUString
sTableStyleName(aTableStyleNames
[i
]);
275 XMLTableStylesContext
*pStyles
= (XMLTableStylesContext
*)GetScImport().GetAutoStyles();
276 if ( pStyles
&& sTableStyleName
.getLength() )
278 XMLTableStyleContext
* pStyle
= (XMLTableStyleContext
*)pStyles
->FindStyleChildContext(
279 XML_STYLE_FAMILY_TABLE_TABLE
, sTableStyleName
, sal_True
);
281 pStyle
->FillPropertySet(xProperties
);
290 // #i37959# handle document protection after the sheet settings
293 ::std::auto_ptr
<ScDocProtection
> pProtection(new ScDocProtection
);
294 pProtection
->setProtected(true);
296 uno::Sequence
<sal_Int8
> aPass
;
297 if (sPassword
.getLength())
299 SvXMLUnitConverter::decodeBase64(aPass
, sPassword
);
300 pProtection
->setPasswordHash(aPass
, meHash1
, meHash2
);
303 pDoc
->SetDocProtection(pProtection
.get());
306 GetScImport().UnlockSolarMutex();