Line data Source code
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 : #include <rtl/ustring.hxx>
21 : #include <xmloff/families.hxx>
22 : #include <xmloff/xmlaustp.hxx>
23 : #include <xmloff/nmspmap.hxx>
24 :
25 : #include "sheetdata.hxx"
26 :
27 : // -----------------------------------------------------------------------
28 :
29 78 : ScSheetSaveData::ScSheetSaveData() :
30 : mnStartTab( -1 ),
31 : mnStartOffset( -1 ),
32 : maPreviousNote( OUString(), OUString(), ScAddress(ScAddress::INITIALIZE_INVALID) ),
33 78 : mbInSupportedSave( false )
34 : {
35 78 : }
36 :
37 71 : ScSheetSaveData::~ScSheetSaveData()
38 : {
39 71 : }
40 :
41 213 : void ScSheetSaveData::AddCellStyle( const OUString& rName, const ScAddress& rCellPos )
42 : {
43 213 : maCellStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
44 213 : }
45 :
46 207 : void ScSheetSaveData::AddColumnStyle( const OUString& rName, const ScAddress& rCellPos )
47 : {
48 207 : maColumnStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
49 207 : }
50 :
51 226 : void ScSheetSaveData::AddRowStyle( const OUString& rName, const ScAddress& rCellPos )
52 : {
53 226 : maRowStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
54 226 : }
55 :
56 168 : void ScSheetSaveData::AddTableStyle( const OUString& rName, const ScAddress& rCellPos )
57 : {
58 168 : maTableStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
59 168 : }
60 :
61 8 : void ScSheetSaveData::HandleNoteStyles( const OUString& rStyleName, const OUString& rTextName, const ScAddress& rCellPos )
62 : {
63 : // only consecutive duplicates (most common case) are filtered out here,
64 : // the others are found when the styles are created
65 :
66 21 : if ( rStyleName == maPreviousNote.maStyleName &&
67 13 : rTextName == maPreviousNote.maTextStyle &&
68 5 : rCellPos.Tab() == maPreviousNote.maCellPos.Tab() )
69 : {
70 : // already stored for the same sheet - ignore
71 8 : return;
72 : }
73 :
74 8 : ScNoteStyleEntry aNewEntry( rStyleName, rTextName, rCellPos );
75 8 : maPreviousNote = aNewEntry;
76 8 : maNoteStyles.push_back( aNewEntry );
77 : }
78 :
79 7 : void ScSheetSaveData::AddNoteContentStyle( sal_uInt16 nFamily, const OUString& rName, const ScAddress& rCellPos, const ESelection& rSelection )
80 : {
81 7 : if ( nFamily == XML_STYLE_FAMILY_TEXT_PARAGRAPH )
82 3 : maNoteParaStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
83 : else
84 4 : maNoteTextStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
85 7 : }
86 :
87 0 : void ScSheetSaveData::AddTextStyle( const OUString& rName, const ScAddress& rCellPos, const ESelection& rSelection )
88 : {
89 0 : maTextStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
90 0 : }
91 :
92 95 : void ScSheetSaveData::BlockSheet( SCTAB nTab )
93 : {
94 95 : if ( nTab >= static_cast<SCTAB>(maBlocked.size()) )
95 32 : maBlocked.resize( nTab + 1, false ); // fill vector with "false" entries
96 :
97 95 : maBlocked[nTab] = true;
98 95 : }
99 :
100 168 : bool ScSheetSaveData::IsSheetBlocked( SCTAB nTab ) const
101 : {
102 168 : if ( nTab < static_cast<SCTAB>(maBlocked.size()) )
103 33 : return maBlocked[nTab];
104 : else
105 135 : return false;
106 : }
107 :
108 152 : void ScSheetSaveData::AddStreamPos( SCTAB nTab, sal_Int32 nStartOffset, sal_Int32 nEndOffset )
109 : {
110 152 : if ( nTab >= static_cast<SCTAB>(maStreamEntries.size()) )
111 152 : maStreamEntries.resize( nTab + 1 );
112 :
113 152 : maStreamEntries[nTab] = ScStreamEntry( nStartOffset, nEndOffset );
114 152 : }
115 :
116 152 : void ScSheetSaveData::StartStreamPos( SCTAB nTab, sal_Int32 nStartOffset )
117 : {
118 : OSL_ENSURE( mnStartTab < 0, "StartStreamPos without EndStreamPos" );
119 :
120 152 : mnStartTab = nTab;
121 152 : mnStartOffset = nStartOffset;
122 152 : }
123 :
124 152 : void ScSheetSaveData::EndStreamPos( sal_Int32 nEndOffset )
125 : {
126 152 : if ( mnStartTab >= 0 )
127 : {
128 152 : AddStreamPos( mnStartTab, mnStartOffset, nEndOffset );
129 152 : mnStartTab = -1;
130 152 : mnStartOffset = -1;
131 : }
132 152 : }
133 :
134 2 : void ScSheetSaveData::GetStreamPos( SCTAB nTab, sal_Int32& rStartOffset, sal_Int32& rEndOffset ) const
135 : {
136 2 : if ( nTab < static_cast<SCTAB>(maStreamEntries.size()) )
137 : {
138 0 : const ScStreamEntry& rEntry = maStreamEntries[nTab];
139 0 : rStartOffset = rEntry.mnStartOffset;
140 0 : rEndOffset = rEntry.mnEndOffset;
141 : }
142 : else
143 2 : rStartOffset = rEndOffset = -1;
144 2 : }
145 :
146 2 : bool ScSheetSaveData::HasStreamPos( SCTAB nTab ) const
147 : {
148 2 : sal_Int32 nStartOffset = -1;
149 2 : sal_Int32 nEndOffset = -1;
150 2 : GetStreamPos( nTab, nStartOffset, nEndOffset );
151 2 : return ( nStartOffset >= 0 && nEndOffset >= 0 );
152 : }
153 :
154 8 : void ScSheetSaveData::ResetSaveEntries()
155 : {
156 8 : maSaveEntries.clear();
157 8 : }
158 :
159 0 : void ScSheetSaveData::AddSavePos( SCTAB nTab, sal_Int32 nStartOffset, sal_Int32 nEndOffset )
160 : {
161 0 : if ( nTab >= static_cast<SCTAB>(maSaveEntries.size()) )
162 0 : maSaveEntries.resize( nTab + 1 );
163 :
164 0 : maSaveEntries[nTab] = ScStreamEntry( nStartOffset, nEndOffset );
165 0 : }
166 :
167 2 : void ScSheetSaveData::UseSaveEntries()
168 : {
169 2 : maStreamEntries = maSaveEntries;
170 2 : }
171 :
172 75 : void ScSheetSaveData::StoreInitialNamespaces( const SvXMLNamespaceMap& rNamespaces )
173 : {
174 : // the initial namespaces are just removed from the list of loaded namespaces,
175 : // so only a boost::unordered_map of the prefixes is needed.
176 :
177 75 : const NameSpaceHash& rNameHash = rNamespaces.GetAllEntries();
178 75 : NameSpaceHash::const_iterator aIter = rNameHash.begin(), aEnd = rNameHash.end();
179 2775 : while (aIter != aEnd)
180 : {
181 2625 : maInitialPrefixes.insert( aIter->first );
182 2625 : ++aIter;
183 : }
184 75 : }
185 :
186 75 : void ScSheetSaveData::StoreLoadedNamespaces( const SvXMLNamespaceMap& rNamespaces )
187 : {
188 : // store the loaded namespaces, so the prefixes in copied stream fragments remain valid
189 :
190 75 : const NameSpaceHash& rNameHash = rNamespaces.GetAllEntries();
191 75 : NameSpaceHash::const_iterator aIter = rNameHash.begin(), aEnd = rNameHash.end();
192 5116 : while (aIter != aEnd)
193 : {
194 : // ignore the initial namespaces
195 4966 : if ( maInitialPrefixes.find( aIter->first ) == maInitialPrefixes.end() )
196 : {
197 2341 : const NameSpaceEntry& rEntry = *(aIter->second);
198 2341 : maLoadedNamespaces.push_back( ScLoadedNamespaceEntry( rEntry.sPrefix, rEntry.sName, rEntry.nKey ) );
199 : }
200 4966 : ++aIter;
201 : }
202 75 : }
203 :
204 0 : static bool lcl_NameInHash( const NameSpaceHash& rNameHash, const OUString& rName )
205 : {
206 0 : NameSpaceHash::const_iterator aIter = rNameHash.begin(), aEnd = rNameHash.end();
207 0 : while (aIter != aEnd)
208 : {
209 0 : if ( aIter->second->sName == rName )
210 0 : return true;
211 :
212 0 : ++aIter;
213 : }
214 0 : return false; // not found
215 : }
216 :
217 0 : bool ScSheetSaveData::AddLoadedNamespaces( SvXMLNamespaceMap& rNamespaces ) const
218 : {
219 : // Add the loaded namespaces to the name space map.
220 :
221 : // first loop: only look for conflicts
222 : // (if the loaded namespaces were added first, this might not be necessary)
223 0 : const NameSpaceHash& rNameHash = rNamespaces.GetAllEntries();
224 0 : std::vector<ScLoadedNamespaceEntry>::const_iterator aIter = maLoadedNamespaces.begin();
225 0 : std::vector<ScLoadedNamespaceEntry>::const_iterator aEnd = maLoadedNamespaces.end();
226 0 : while (aIter != aEnd)
227 : {
228 0 : NameSpaceHash::const_iterator aHashIter = rNameHash.find( aIter->maPrefix );
229 0 : if ( aHashIter == rNameHash.end() )
230 : {
231 0 : if ( lcl_NameInHash( rNameHash, aIter->maName ) )
232 : {
233 : // a second prefix for the same name would confuse SvXMLNamespaceMap lookup,
234 : // so this is also considered a conflict
235 0 : return false;
236 : }
237 : }
238 0 : else if ( aHashIter->second->sName != aIter->maName )
239 : {
240 : // same prefix, but different name: loaded namespaces can't be used
241 0 : return false;
242 : }
243 0 : ++aIter;
244 : }
245 :
246 : // only if there were no conflicts, add the entries that aren't in the map already
247 : // (the key is needed if the same namespace is added later within an element)
248 0 : aIter = maLoadedNamespaces.begin();
249 0 : while (aIter != aEnd)
250 : {
251 0 : NameSpaceHash::const_iterator aHashIter = rNameHash.find( aIter->maPrefix );
252 0 : if ( aHashIter == rNameHash.end() )
253 0 : rNamespaces.Add( aIter->maPrefix, aIter->maName, aIter->mnKey );
254 0 : ++aIter;
255 : }
256 :
257 0 : return true; // success
258 : }
259 :
260 0 : bool ScSheetSaveData::IsInSupportedSave() const
261 : {
262 0 : return mbInSupportedSave;
263 : }
264 :
265 7 : void ScSheetSaveData::SetInSupportedSave( bool bSet )
266 : {
267 7 : mbInSupportedSave = bSet;
268 7 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|