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 24 : ScSheetSaveData::ScSheetSaveData() :
30 : mnStartTab( -1 ),
31 : mnStartOffset( -1 ),
32 : maPreviousNote( rtl::OUString(), rtl::OUString(), ScAddress(ScAddress::INITIALIZE_INVALID) ),
33 24 : mbInSupportedSave( false )
34 : {
35 24 : }
36 :
37 21 : ScSheetSaveData::~ScSheetSaveData()
38 : {
39 21 : }
40 :
41 93 : void ScSheetSaveData::AddCellStyle( const rtl::OUString& rName, const ScAddress& rCellPos )
42 : {
43 93 : maCellStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
44 93 : }
45 :
46 73 : void ScSheetSaveData::AddColumnStyle( const rtl::OUString& rName, const ScAddress& rCellPos )
47 : {
48 73 : maColumnStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
49 73 : }
50 :
51 63 : void ScSheetSaveData::AddRowStyle( const rtl::OUString& rName, const ScAddress& rCellPos )
52 : {
53 63 : maRowStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
54 63 : }
55 :
56 50 : void ScSheetSaveData::AddTableStyle( const rtl::OUString& rName, const ScAddress& rCellPos )
57 : {
58 50 : maTableStyles.push_back( ScCellStyleEntry( rName, rCellPos ) );
59 50 : }
60 :
61 1 : void ScSheetSaveData::HandleNoteStyles( const rtl::OUString& rStyleName, const rtl::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 1 : if ( rStyleName == maPreviousNote.maStyleName &&
67 0 : rTextName == maPreviousNote.maTextStyle &&
68 0 : rCellPos.Tab() == maPreviousNote.maCellPos.Tab() )
69 : {
70 : // already stored for the same sheet - ignore
71 1 : return;
72 : }
73 :
74 1 : ScNoteStyleEntry aNewEntry( rStyleName, rTextName, rCellPos );
75 1 : maPreviousNote = aNewEntry;
76 1 : maNoteStyles.push_back( aNewEntry );
77 : }
78 :
79 1 : void ScSheetSaveData::AddNoteContentStyle( sal_uInt16 nFamily, const rtl::OUString& rName, const ScAddress& rCellPos, const ESelection& rSelection )
80 : {
81 1 : if ( nFamily == XML_STYLE_FAMILY_TEXT_PARAGRAPH )
82 1 : maNoteParaStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
83 : else
84 0 : maNoteTextStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
85 1 : }
86 :
87 1 : void ScSheetSaveData::AddTextStyle( const rtl::OUString& rName, const ScAddress& rCellPos, const ESelection& rSelection )
88 : {
89 1 : maTextStyles.push_back( ScTextStyleEntry( rName, rCellPos, rSelection ) );
90 1 : }
91 :
92 16 : void ScSheetSaveData::BlockSheet( SCTAB nTab )
93 : {
94 16 : if ( nTab >= static_cast<SCTAB>(maBlocked.size()) )
95 3 : maBlocked.resize( nTab + 1, false ); // fill vector with "false" entries
96 :
97 16 : maBlocked[nTab] = true;
98 16 : }
99 :
100 50 : bool ScSheetSaveData::IsSheetBlocked( SCTAB nTab ) const
101 : {
102 50 : if ( nTab < static_cast<SCTAB>(maBlocked.size()) )
103 3 : return maBlocked[nTab];
104 : else
105 47 : return false;
106 : }
107 :
108 50 : void ScSheetSaveData::AddStreamPos( SCTAB nTab, sal_Int32 nStartOffset, sal_Int32 nEndOffset )
109 : {
110 50 : if ( nTab >= static_cast<SCTAB>(maStreamEntries.size()) )
111 50 : maStreamEntries.resize( nTab + 1 );
112 :
113 50 : maStreamEntries[nTab] = ScStreamEntry( nStartOffset, nEndOffset );
114 50 : }
115 :
116 50 : void ScSheetSaveData::StartStreamPos( SCTAB nTab, sal_Int32 nStartOffset )
117 : {
118 : OSL_ENSURE( mnStartTab < 0, "StartStreamPos without EndStreamPos" );
119 :
120 50 : mnStartTab = nTab;
121 50 : mnStartOffset = nStartOffset;
122 50 : }
123 :
124 50 : void ScSheetSaveData::EndStreamPos( sal_Int32 nEndOffset )
125 : {
126 50 : if ( mnStartTab >= 0 )
127 : {
128 50 : AddStreamPos( mnStartTab, mnStartOffset, nEndOffset );
129 50 : mnStartTab = -1;
130 50 : mnStartOffset = -1;
131 : }
132 50 : }
133 :
134 0 : void ScSheetSaveData::GetStreamPos( SCTAB nTab, sal_Int32& rStartOffset, sal_Int32& rEndOffset ) const
135 : {
136 0 : 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 0 : rStartOffset = rEndOffset = -1;
144 0 : }
145 :
146 0 : bool ScSheetSaveData::HasStreamPos( SCTAB nTab ) const
147 : {
148 0 : sal_Int32 nStartOffset = -1;
149 0 : sal_Int32 nEndOffset = -1;
150 0 : GetStreamPos( nTab, nStartOffset, nEndOffset );
151 0 : return ( nStartOffset >= 0 && nEndOffset >= 0 );
152 : }
153 :
154 2 : void ScSheetSaveData::ResetSaveEntries()
155 : {
156 2 : maSaveEntries.clear();
157 2 : }
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 0 : void ScSheetSaveData::UseSaveEntries()
168 : {
169 0 : maStreamEntries = maSaveEntries;
170 0 : }
171 :
172 22 : 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 22 : const NameSpaceHash& rNameHash = rNamespaces.GetAllEntries();
178 22 : NameSpaceHash::const_iterator aIter = rNameHash.begin(), aEnd = rNameHash.end();
179 814 : while (aIter != aEnd)
180 : {
181 770 : maInitialPrefixes.insert( aIter->first );
182 770 : ++aIter;
183 : }
184 22 : }
185 :
186 22 : void ScSheetSaveData::StoreLoadedNamespaces( const SvXMLNamespaceMap& rNamespaces )
187 : {
188 : // store the loaded namespaces, so the prefixes in copied stream fragments remain valid
189 :
190 22 : const NameSpaceHash& rNameHash = rNamespaces.GetAllEntries();
191 22 : NameSpaceHash::const_iterator aIter = rNameHash.begin(), aEnd = rNameHash.end();
192 1504 : while (aIter != aEnd)
193 : {
194 : // ignore the initial namespaces
195 1460 : if ( maInitialPrefixes.find( aIter->first ) == maInitialPrefixes.end() )
196 : {
197 690 : const NameSpaceEntry& rEntry = *(aIter->second);
198 690 : maLoadedNamespaces.push_back( ScLoadedNamespaceEntry( rEntry.sPrefix, rEntry.sName, rEntry.nKey ) );
199 : }
200 1460 : ++aIter;
201 : }
202 22 : }
203 :
204 0 : static bool lcl_NameInHash( const NameSpaceHash& rNameHash, const rtl::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 0 : void ScSheetSaveData::SetInSupportedSave( bool bSet )
266 : {
267 0 : mbInSupportedSave = bSet;
268 0 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|