1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <rtl/ustring.hxx>
21 #include <osl/diagnose.h>
22 #include <xmloff/families.hxx>
23 #include <xmloff/xmlaustp.hxx>
24 #include <xmloff/nmspmap.hxx>
26 #include "sheetdata.hxx"
28 ScSheetSaveData::ScSheetSaveData() :
31 maPreviousNote( OUString(), OUString(), ScAddress(ScAddress::INITIALIZE_INVALID
) ),
32 mbInSupportedSave( false )
36 ScSheetSaveData::~ScSheetSaveData()
40 void ScSheetSaveData::AddCellStyle( const OUString
& rName
, const ScAddress
& rCellPos
)
42 maCellStyles
.push_back( ScCellStyleEntry( rName
, rCellPos
) );
45 void ScSheetSaveData::AddColumnStyle( const OUString
& rName
, const ScAddress
& rCellPos
)
47 maColumnStyles
.push_back( ScCellStyleEntry( rName
, rCellPos
) );
50 void ScSheetSaveData::AddRowStyle( const OUString
& rName
, const ScAddress
& rCellPos
)
52 maRowStyles
.push_back( ScCellStyleEntry( rName
, rCellPos
) );
55 void ScSheetSaveData::AddTableStyle( const OUString
& rName
, const ScAddress
& rCellPos
)
57 maTableStyles
.push_back( ScCellStyleEntry( rName
, rCellPos
) );
60 void ScSheetSaveData::HandleNoteStyles( const OUString
& rStyleName
, const OUString
& rTextName
, const ScAddress
& rCellPos
)
62 // only consecutive duplicates (most common case) are filtered out here,
63 // the others are found when the styles are created
65 if ( rStyleName
== maPreviousNote
.maStyleName
&&
66 rTextName
== maPreviousNote
.maTextStyle
&&
67 rCellPos
.Tab() == maPreviousNote
.maCellPos
.Tab() )
69 // already stored for the same sheet - ignore
73 ScNoteStyleEntry
aNewEntry( rStyleName
, rTextName
, rCellPos
);
74 maPreviousNote
= aNewEntry
;
75 maNoteStyles
.push_back( aNewEntry
);
78 void ScSheetSaveData::AddNoteContentStyle( sal_uInt16 nFamily
, const OUString
& rName
, const ScAddress
& rCellPos
, const ESelection
& rSelection
)
80 if ( nFamily
== XML_STYLE_FAMILY_TEXT_PARAGRAPH
)
81 maNoteParaStyles
.push_back( ScTextStyleEntry( rName
, rCellPos
, rSelection
) );
83 maNoteTextStyles
.push_back( ScTextStyleEntry( rName
, rCellPos
, rSelection
) );
86 void ScSheetSaveData::AddTextStyle( const OUString
& rName
, const ScAddress
& rCellPos
, const ESelection
& rSelection
)
88 maTextStyles
.push_back( ScTextStyleEntry( rName
, rCellPos
, rSelection
) );
91 void ScSheetSaveData::BlockSheet( SCTAB nTab
)
93 if ( nTab
>= static_cast<SCTAB
>(maBlocked
.size()) )
94 maBlocked
.resize( nTab
+ 1, false ); // fill vector with "false" entries
96 maBlocked
[nTab
] = true;
99 bool ScSheetSaveData::IsSheetBlocked( SCTAB nTab
) const
101 if ( nTab
< static_cast<SCTAB
>(maBlocked
.size()) )
102 return maBlocked
[nTab
];
107 void ScSheetSaveData::AddStreamPos( SCTAB nTab
, sal_Int32 nStartOffset
, sal_Int32 nEndOffset
)
109 if ( nTab
>= static_cast<SCTAB
>(maStreamEntries
.size()) )
110 maStreamEntries
.resize( nTab
+ 1 );
112 maStreamEntries
[nTab
] = ScStreamEntry( nStartOffset
, nEndOffset
);
115 void ScSheetSaveData::StartStreamPos( SCTAB nTab
, sal_Int32 nStartOffset
)
117 OSL_ENSURE( mnStartTab
< 0, "StartStreamPos without EndStreamPos" );
120 mnStartOffset
= nStartOffset
;
123 void ScSheetSaveData::EndStreamPos( sal_Int32 nEndOffset
)
125 if ( mnStartTab
>= 0 )
127 AddStreamPos( mnStartTab
, mnStartOffset
, nEndOffset
);
133 void ScSheetSaveData::GetStreamPos( SCTAB nTab
, sal_Int32
& rStartOffset
, sal_Int32
& rEndOffset
) const
135 if ( nTab
< static_cast<SCTAB
>(maStreamEntries
.size()) )
137 const ScStreamEntry
& rEntry
= maStreamEntries
[nTab
];
138 rStartOffset
= rEntry
.mnStartOffset
;
139 rEndOffset
= rEntry
.mnEndOffset
;
142 rStartOffset
= rEndOffset
= -1;
145 bool ScSheetSaveData::HasStreamPos( SCTAB nTab
) const
147 sal_Int32 nStartOffset
= -1;
148 sal_Int32 nEndOffset
= -1;
149 GetStreamPos( nTab
, nStartOffset
, nEndOffset
);
150 return ( nStartOffset
>= 0 && nEndOffset
>= 0 );
153 void ScSheetSaveData::ResetSaveEntries()
155 maSaveEntries
.clear();
158 void ScSheetSaveData::AddSavePos( SCTAB nTab
, sal_Int32 nStartOffset
, sal_Int32 nEndOffset
)
160 if ( nTab
>= static_cast<SCTAB
>(maSaveEntries
.size()) )
161 maSaveEntries
.resize( nTab
+ 1 );
163 maSaveEntries
[nTab
] = ScStreamEntry( nStartOffset
, nEndOffset
);
166 void ScSheetSaveData::UseSaveEntries()
168 maStreamEntries
= maSaveEntries
;
171 void ScSheetSaveData::StoreInitialNamespaces( const SvXMLNamespaceMap
& rNamespaces
)
173 // the initial namespaces are just removed from the list of loaded namespaces,
174 // so only a unordered_map of the prefixes is needed.
176 const NameSpaceHash
& rNameHash
= rNamespaces
.GetAllEntries();
177 NameSpaceHash::const_iterator aIter
= rNameHash
.begin(), aEnd
= rNameHash
.end();
178 while (aIter
!= aEnd
)
180 maInitialPrefixes
.insert( aIter
->first
);
185 void ScSheetSaveData::StoreLoadedNamespaces( const SvXMLNamespaceMap
& rNamespaces
)
187 // store the loaded namespaces, so the prefixes in copied stream fragments remain valid
189 const NameSpaceHash
& rNameHash
= rNamespaces
.GetAllEntries();
190 NameSpaceHash::const_iterator aIter
= rNameHash
.begin(), aEnd
= rNameHash
.end();
191 while (aIter
!= aEnd
)
193 // ignore the initial namespaces
194 if ( maInitialPrefixes
.find( aIter
->first
) == maInitialPrefixes
.end() )
196 const NameSpaceEntry
& rEntry
= *(aIter
->second
);
197 maLoadedNamespaces
.push_back( ScLoadedNamespaceEntry( rEntry
.sPrefix
, rEntry
.sName
, rEntry
.nKey
) );
203 static bool lcl_NameInHash( const NameSpaceHash
& rNameHash
, const OUString
& rName
)
205 NameSpaceHash::const_iterator aIter
= rNameHash
.begin(), aEnd
= rNameHash
.end();
206 while (aIter
!= aEnd
)
208 if ( aIter
->second
->sName
== rName
)
213 return false; // not found
216 bool ScSheetSaveData::AddLoadedNamespaces( SvXMLNamespaceMap
& rNamespaces
) const
218 // Add the loaded namespaces to the name space map.
220 // first loop: only look for conflicts
221 // (if the loaded namespaces were added first, this might not be necessary)
222 const NameSpaceHash
& rNameHash
= rNamespaces
.GetAllEntries();
223 std::vector
<ScLoadedNamespaceEntry
>::const_iterator aIter
= maLoadedNamespaces
.begin();
224 std::vector
<ScLoadedNamespaceEntry
>::const_iterator aEnd
= maLoadedNamespaces
.end();
225 while (aIter
!= aEnd
)
227 NameSpaceHash::const_iterator aHashIter
= rNameHash
.find( aIter
->maPrefix
);
228 if ( aHashIter
== rNameHash
.end() )
230 if ( lcl_NameInHash( rNameHash
, aIter
->maName
) )
232 // a second prefix for the same name would confuse SvXMLNamespaceMap lookup,
233 // so this is also considered a conflict
237 else if ( aHashIter
->second
->sName
!= aIter
->maName
)
239 // same prefix, but different name: loaded namespaces can't be used
245 // only if there were no conflicts, add the entries that aren't in the map already
246 // (the key is needed if the same namespace is added later within an element)
247 aIter
= maLoadedNamespaces
.begin();
248 while (aIter
!= aEnd
)
250 NameSpaceHash::const_iterator aHashIter
= rNameHash
.find( aIter
->maPrefix
);
251 if ( aHashIter
== rNameHash
.end() )
252 rNamespaces
.Add( aIter
->maPrefix
, aIter
->maName
, aIter
->mnKey
);
256 return true; // success
259 void ScSheetSaveData::SetInSupportedSave( bool bSet
)
261 mbInSupportedSave
= bSet
;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */