LibreOffice Module sc (master) 1
connectionsbuffer.cxx
Go to the documentation of this file.
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 <connectionsbuffer.hxx>
21#include <biffhelper.hxx>
22
23#include <osl/diagnose.h>
25#include <oox/token/namespaces.hxx>
26#include <oox/token/tokens.hxx>
28
29namespace oox::xls {
30
31using namespace ::com::sun::star::uno;
32
33namespace {
34
35const sal_Int32 BIFF12_RECONNECT_AS_REQUIRED = 1;
36
37const sal_uInt8 BIFF12_CONNECTION_SAVEPASSWORD_ON = 1;
38
39const sal_uInt16 BIFF12_CONNECTION_KEEPALIVE = 0x0001;
40const sal_uInt16 BIFF12_CONNECTION_NEW = 0x0002;
41const sal_uInt16 BIFF12_CONNECTION_DELETED = 0x0004;
42const sal_uInt16 BIFF12_CONNECTION_ONLYUSECONNFILE = 0x0008;
43const sal_uInt16 BIFF12_CONNECTION_BACKGROUND = 0x0010;
44const sal_uInt16 BIFF12_CONNECTION_REFRESHONLOAD = 0x0020;
45const sal_uInt16 BIFF12_CONNECTION_SAVEDATA = 0x0040;
46
47const sal_uInt16 BIFF12_CONNECTION_HAS_SOURCEFILE = 0x0001;
48const sal_uInt16 BIFF12_CONNECTION_HAS_SOURCECONNFILE = 0x0002;
49const sal_uInt16 BIFF12_CONNECTION_HAS_DESCRIPTION = 0x0004;
50const sal_uInt16 BIFF12_CONNECTION_HAS_NAME = 0x0008;
51const sal_uInt16 BIFF12_CONNECTION_HAS_SSOID = 0x0010;
52
53const sal_uInt32 BIFF12_WEBPR_XML = 0x00000100;
54const sal_uInt32 BIFF12_WEBPR_SOURCEDATA = 0x00000200;
55const sal_uInt32 BIFF12_WEBPR_PARSEPRE = 0x00000400;
56const sal_uInt32 BIFF12_WEBPR_CONSECUTIVE = 0x00000800;
57const sal_uInt32 BIFF12_WEBPR_FIRSTROW = 0x00001000;
58const sal_uInt32 BIFF12_WEBPR_XL97CREATED = 0x00002000;
59const sal_uInt32 BIFF12_WEBPR_TEXTDATES = 0x00004000;
60const sal_uInt32 BIFF12_WEBPR_XL2000REFRESHED = 0x00008000;
61const sal_uInt32 BIFF12_WEBPR_HTMLTABLES = 0x00010000;
62
63const sal_uInt8 BIFF12_WEBPR_HAS_POSTMETHOD = 0x01;
64const sal_uInt8 BIFF12_WEBPR_HAS_EDITPAGE = 0x02;
65const sal_uInt8 BIFF12_WEBPR_HAS_URL = 0x04;
66
67} // namespace
68
70 mnHtmlFormat( XML_none ),
71 mbXml( false ),
72 mbSourceData( false ),
73 mbParsePre( false ),
74 mbConsecutive( false ),
75 mbFirstRow( false ),
76 mbXl97Created( false ),
77 mbTextDates( false ),
78 mbXl2000Refreshed( false ),
79 mbHtmlTables( false )
80{
81}
82
84 mnId( -1 ),
86 mnReconnectMethod( BIFF12_RECONNECT_AS_REQUIRED ),
87 mnCredentials( XML_integrated ),
88 mnInterval( 0 ),
89 mbKeepAlive( false ),
90 mbNew( false ),
91 mbDeleted( false ),
92 mbOnlyUseConnFile( false ),
93 mbBackground( false ),
94 mbRefreshOnLoad( false ),
95 mbSaveData( false ),
96 mbSavePassword( false )
97{
98}
99
101{
102 OSL_ENSURE( !mxWebPr, "ConnectionModel::createWebPr - multiple call" );
103 mxWebPr.reset( new WebPrModel );
104 return *mxWebPr;
105}
106
108 WorkbookHelper( rHelper )
109{
110 maModel.mnId = -1;
111}
112
114{
115 maModel.maName = rAttribs.getXString( XML_name, OUString() );
116 maModel.maDescription = rAttribs.getXString( XML_description, OUString() );
117 maModel.maSourceFile = rAttribs.getXString( XML_sourceFile, OUString() );
118 maModel.maSourceConnFile = rAttribs.getXString( XML_odcFile, OUString() );
119 maModel.maSsoId = rAttribs.getXString( XML_singleSignOnId, OUString() );
120 maModel.mnId = rAttribs.getInteger( XML_id, -1 );
121 // type and reconnectionMethod are using the BIFF12 constants instead of XML tokens
123 maModel.mnReconnectMethod = rAttribs.getInteger( XML_reconnectionMethod, BIFF12_RECONNECT_AS_REQUIRED );
124 maModel.mnCredentials = rAttribs.getToken( XML_credentials, XML_integrated );
125 maModel.mnInterval = rAttribs.getInteger( XML_interval, 0 );
126 maModel.mbKeepAlive = rAttribs.getBool( XML_keepAlive, false );
127 maModel.mbNew = rAttribs.getBool( XML_new, false );
128 maModel.mbDeleted = rAttribs.getBool( XML_deleted, false );
129 maModel.mbOnlyUseConnFile = rAttribs.getBool( XML_onlyUseConnectionFile, false );
130 maModel.mbBackground = rAttribs.getBool( XML_background, false );
131 maModel.mbRefreshOnLoad = rAttribs.getBool( XML_refreshOnLoad, false );
132 maModel.mbSaveData = rAttribs.getBool( XML_saveData, false );
133 maModel.mbSavePassword = rAttribs.getBool( XML_savePassword, false );
134}
135
137{
138 WebPrModel& rWebPr = maModel.createWebPr();
139
140 rWebPr.maUrl = rAttribs.getXString( XML_url, OUString() );
141 rWebPr.maPostMethod = rAttribs.getXString( XML_post, OUString() );
142 rWebPr.maEditPage = rAttribs.getXString( XML_editPage, OUString() );
143 rWebPr.mnHtmlFormat = rAttribs.getToken( XML_htmlFormat, XML_none );
144 rWebPr.mbXml = rAttribs.getBool( XML_xml, false );
145 rWebPr.mbSourceData = rAttribs.getBool( XML_sourceData, false );
146 rWebPr.mbParsePre = rAttribs.getBool( XML_parsePre, false );
147 rWebPr.mbConsecutive = rAttribs.getBool( XML_consecutive, false );
148 rWebPr.mbFirstRow = rAttribs.getBool( XML_firstRow, false );
149 rWebPr.mbXl97Created = rAttribs.getBool( XML_xl97, false );
150 rWebPr.mbTextDates = rAttribs.getBool( XML_textDates, false );
151 rWebPr.mbXl2000Refreshed = rAttribs.getBool( XML_xl2000, false );
152 rWebPr.mbHtmlTables = rAttribs.getBool( XML_htmlTables, false );
153}
154
156{
157 if( maModel.mxWebPr )
158 {
159 OSL_ENSURE( maModel.mxWebPr->maTables.empty(), "Connection::importTables - multiple calls" );
160 maModel.mxWebPr->maTables.clear();
161 }
162}
163
164void Connection::importTable( const AttributeList& rAttribs, sal_Int32 nElement )
165{
166 if( !maModel.mxWebPr )
167 return;
168
169 Any aTableAny;
170 switch( nElement )
171 {
172 case XLS_TOKEN( m ): break;
173 case XLS_TOKEN( s ): aTableAny <<= rAttribs.getXString( XML_v, OUString() ); break;
174 case XLS_TOKEN( x ): aTableAny <<= rAttribs.getInteger( XML_v, -1 ); break;
175 default:
176 OSL_ENSURE( false, "Connection::importTable - unexpected element" );
177 return;
178 }
179 maModel.mxWebPr->maTables.push_back( aTableAny );
180}
181
183{
184 sal_uInt16 nFlags, nStrFlags;
185 sal_uInt8 nSavePassword, nCredentials;
186 rStrm.skip( 2 );
187 nSavePassword = rStrm.readuChar();
188 rStrm.skip( 1 );
189 maModel.mnInterval = rStrm.readuInt16();
190 nFlags = rStrm.readuInt16();
191 nStrFlags = rStrm.readuInt16();
192 maModel.mnType = rStrm.readInt32();
193 maModel.mnReconnectMethod = rStrm.readInt32();
194 maModel.mnId = rStrm.readInt32();
195 nCredentials = rStrm.readuChar();
196
197 if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SOURCEFILE ) )
199 if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SOURCECONNFILE ) )
201 if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_DESCRIPTION ) )
203 if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_NAME ) )
205 if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SSOID ) )
207
208 static const sal_Int32 spnCredentials[] = { XML_integrated, XML_none, XML_stored, XML_prompt };
209 maModel.mnCredentials = STATIC_ARRAY_SELECT( spnCredentials, nCredentials, XML_integrated );
210
211 maModel.mbKeepAlive = getFlag( nFlags, BIFF12_CONNECTION_KEEPALIVE );
212 maModel.mbNew = getFlag( nFlags, BIFF12_CONNECTION_NEW );
213 maModel.mbDeleted = getFlag( nFlags, BIFF12_CONNECTION_DELETED );
214 maModel.mbOnlyUseConnFile = getFlag( nFlags, BIFF12_CONNECTION_ONLYUSECONNFILE );
215 maModel.mbBackground = getFlag( nFlags, BIFF12_CONNECTION_BACKGROUND );
216 maModel.mbRefreshOnLoad = getFlag( nFlags, BIFF12_CONNECTION_REFRESHONLOAD );
217 maModel.mbSaveData = getFlag( nFlags, BIFF12_CONNECTION_SAVEDATA );
218 maModel.mbSavePassword = nSavePassword == BIFF12_CONNECTION_SAVEPASSWORD_ON;
219}
220
222{
223 WebPrModel& rWebPr = maModel.createWebPr();
224
225 sal_uInt32 nFlags;
226 sal_uInt8 nStrFlags;
227 nFlags = rStrm.readuInt32();
228 nStrFlags = rStrm.readuChar();
229
230 if( getFlag( nStrFlags, BIFF12_WEBPR_HAS_URL ) )
231 rStrm >> rWebPr.maUrl;
232 if( getFlag( nStrFlags, BIFF12_WEBPR_HAS_POSTMETHOD ) )
233 rStrm >> rWebPr.maPostMethod;
234 if( getFlag( nStrFlags, BIFF12_WEBPR_HAS_EDITPAGE ) )
235 rStrm >> rWebPr.maEditPage;
236
237 static const sal_Int32 spnHmlFormats[] = { XML_none, XML_rtf, XML_all };
238 rWebPr.mnHtmlFormat = STATIC_ARRAY_SELECT( spnHmlFormats, extractValue< sal_uInt8 >( nFlags, 0, 8 ), XML_none );
239
240 rWebPr.mbXml = getFlag( nFlags, BIFF12_WEBPR_XML );
241 rWebPr.mbSourceData = getFlag( nFlags, BIFF12_WEBPR_SOURCEDATA );
242 rWebPr.mbParsePre = getFlag( nFlags, BIFF12_WEBPR_PARSEPRE );
243 rWebPr.mbConsecutive = getFlag( nFlags, BIFF12_WEBPR_CONSECUTIVE );
244 rWebPr.mbFirstRow = getFlag( nFlags, BIFF12_WEBPR_FIRSTROW );
245 rWebPr.mbXl97Created = getFlag( nFlags, BIFF12_WEBPR_XL97CREATED );
246 rWebPr.mbTextDates = getFlag( nFlags, BIFF12_WEBPR_TEXTDATES );
247 rWebPr.mbXl2000Refreshed = getFlag( nFlags, BIFF12_WEBPR_XL2000REFRESHED );
248 rWebPr.mbHtmlTables = getFlag( nFlags, BIFF12_WEBPR_HTMLTABLES );
249}
250
252{
253 if( maModel.mxWebPr )
254 {
255 OSL_ENSURE( maModel.mxWebPr->maTables.empty(), "Connection::importWebPrTables - multiple calls" );
256 maModel.mxWebPr->maTables.clear();
257 }
258}
259
260void Connection::importWebPrTable( SequenceInputStream& rStrm, sal_Int32 nRecId )
261{
262 if( !maModel.mxWebPr )
263 return;
264
265 Any aTableAny;
266 switch( nRecId )
267 {
268 case BIFF12_ID_PCITEM_MISSING: break;
269 case BIFF12_ID_PCITEM_STRING: aTableAny <<= BiffHelper::readString( rStrm ); break;
270 case BIFF12_ID_PCITEM_INDEX: aTableAny <<= rStrm.readInt32(); break;
271 default:
272 OSL_ENSURE( false, "Connection::importWebPrTable - unexpected record" );
273 return;
274 }
275 maModel.mxWebPr->maTables.push_back( aTableAny );
276}
277
279 WorkbookHelper( rHelper ),
280 mnUnusedId( 1 )
281{
282}
283
285{
286 ConnectionRef xConnection = std::make_shared<Connection>( *this );
287 maConnections.push_back( xConnection );
288 return *xConnection;
289}
290
292{
293 for( const auto& rxConnection : maConnections )
294 insertConnectionToMap( rxConnection );
295}
296
298{
299 return maConnectionsById.get( nConnId );
300}
301
303{
304 sal_Int32 nConnId = rxConnection->getConnectionId();
305 if( nConnId > 0 )
306 {
307 OSL_ENSURE( !maConnectionsById.has( nConnId ), "ConnectionsBuffer::insertConnectionToMap - multiple connection identifier" );
308 maConnectionsById[ nConnId ] = rxConnection;
309 mnUnusedId = ::std::max< sal_Int32 >( mnUnusedId, nConnId + 1 );
310 }
311}
312
313} // namespace oox::xls
314
315/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::optional< OUString > getXString(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
std::optional< bool > getBool(sal_Int32 nAttrToken) const
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
bool has(key_type nKey) const
mapped_type get(key_type nKey) const
static OUString readString(SequenceInputStream &rStrm, bool b32BitLen=true)
Reads a BIFF12 string with leading 16-bit or 32-bit length field.
Definition: biffhelper.cxx:79
An external data connection (database, web query, etc.).
void importTables()
Imports web query table settings from the tables element.
void importWebPr(const AttributeList &rAttribs)
Imports web query settings from the webPr element.
void importWebPrTable(SequenceInputStream &rStrm, sal_Int32 nRecId)
Imports a web query table identifier from the PCITEM_MISSING, PCITEM_STRING, or PCITEM_INDEX record.
void importTable(const AttributeList &rAttribs, sal_Int32 nElement)
Imports a web query table identifier from the m, s, or x element.
Connection(const WorkbookHelper &rHelper)
void importConnection(const AttributeList &rAttribs)
Imports connection settings from the connection element.
void importWebPrTables(SequenceInputStream &rStrm)
Imports web query table settings from the WEBPRTABLES record.
ConnectionRef getConnection(sal_Int32 nConnId) const
Returns a data connection by its unique identifier.
ConnectionsBuffer(const WorkbookHelper &rHelper)
void finalizeImport()
Maps all connections by their identifier.
Connection & createConnection()
Creates a new empty connection.
void insertConnectionToMap(const ConnectionRef &rxConnection)
Inserts the passed connection into the map according to its identifier.
Helper class to provide access to global workbook data.
sal_uInt16 mnId
float x
#define STATIC_ARRAY_SELECT(array, index, def)
void SvStream & rStrm
m
const sal_Int32 BIFF12_ID_PCITEM_INDEX
Definition: biffhelper.hxx:170
const sal_Int32 BIFF12_ID_PCITEM_MISSING
Definition: biffhelper.hxx:171
const sal_Int32 BIFF12_CONNECTION_UNKNOWN
std::shared_ptr< Connection > ConnectionRef
const sal_Int32 BIFF12_ID_PCITEM_STRING
Definition: biffhelper.hxx:172
bool getFlag(Type nBitField, Type nMask)
XML_none
XML_type
sal_Int32 mnType
sal_Int32 mnId
Single sign-on identifier.
sal_Int32 mnInterval
Credentials method.
sal_Int32 mnReconnectMethod
Data source type.
OUString maName
Special settings for web queries.
OUString maSsoId
URL of a source connection file.
bool mbSavePassword
True = save cached data with connection.
bool mbKeepAlive
Refresh interval in minutes.
bool mbSaveData
True = refresh connection on import.
bool mbBackground
True = use maSourceConnFile, ignore mnReconnectMethod.
bool mbNew
True = keep connection open after import.
ConnectionModel()
True = save password in connection string.
bool mbRefreshOnLoad
True = background refresh enabled.
OUString maDescription
Unique name of this connection.
sal_Int32 mnCredentials
Reconnection method.
OUString maSourceFile
User description of this connection.
bool mbDeleted
True = new connection, never updated.
sal_Int32 mnType
Unique connection identifier.
OUString maSourceConnFile
URL of a source data file.
bool mbOnlyUseConnFile
True = connection has been deleted.
Special properties for data connections representing web queries.
WebPrModel()
True = HTML tables, false = entire document.
OUString maPostMethod
Source URL to refresh the data.
OUString maEditPage
POST method to query data.
bool mbXl2000Refreshed
True = read date values as text, false = parse dates.
bool mbHtmlTables
True = refreshed with Excel 2000 or newer.
bool mbTextDates
True = web query created with Excel 97.
bool mbXl97Created
True = use column widths of first row for entire.
bool mbParsePre
True = import XML source data referred by HTML table.
bool mbConsecutive
True = parse preformatted sections (.
OUString maUrl
Names or indexes of the web query tables.
bool mbSourceData
True = XML query, false = HTML query.
bool mbFirstRow
True = join consecutive delimiters.
sal_Int32 mnHtmlFormat
Web page showing query data (for XML queries).
bool mbXml
Plain text, rich text, or HTML.
unsigned char sal_uInt8