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 
29 namespace oox::xls {
30 
31 using namespace ::com::sun::star::uno;
32 
33 namespace {
34 
35 const sal_Int32 BIFF12_RECONNECT_AS_REQUIRED = 1;
36 
37 const sal_uInt8 BIFF12_CONNECTION_SAVEPASSWORD_ON = 1;
38 
39 const sal_uInt16 BIFF12_CONNECTION_KEEPALIVE = 0x0001;
40 const sal_uInt16 BIFF12_CONNECTION_NEW = 0x0002;
41 const sal_uInt16 BIFF12_CONNECTION_DELETED = 0x0004;
42 const sal_uInt16 BIFF12_CONNECTION_ONLYUSECONNFILE = 0x0008;
43 const sal_uInt16 BIFF12_CONNECTION_BACKGROUND = 0x0010;
44 const sal_uInt16 BIFF12_CONNECTION_REFRESHONLOAD = 0x0020;
45 const sal_uInt16 BIFF12_CONNECTION_SAVEDATA = 0x0040;
46 
47 const sal_uInt16 BIFF12_CONNECTION_HAS_SOURCEFILE = 0x0001;
48 const sal_uInt16 BIFF12_CONNECTION_HAS_SOURCECONNFILE = 0x0002;
49 const sal_uInt16 BIFF12_CONNECTION_HAS_DESCRIPTION = 0x0004;
50 const sal_uInt16 BIFF12_CONNECTION_HAS_NAME = 0x0008;
51 const sal_uInt16 BIFF12_CONNECTION_HAS_SSOID = 0x0010;
52 
53 const sal_uInt32 BIFF12_WEBPR_XML = 0x00000100;
54 const sal_uInt32 BIFF12_WEBPR_SOURCEDATA = 0x00000200;
55 const sal_uInt32 BIFF12_WEBPR_PARSEPRE = 0x00000400;
56 const sal_uInt32 BIFF12_WEBPR_CONSECUTIVE = 0x00000800;
57 const sal_uInt32 BIFF12_WEBPR_FIRSTROW = 0x00001000;
58 const sal_uInt32 BIFF12_WEBPR_XL97CREATED = 0x00002000;
59 const sal_uInt32 BIFF12_WEBPR_TEXTDATES = 0x00004000;
60 const sal_uInt32 BIFF12_WEBPR_XL2000REFRESHED = 0x00008000;
61 const sal_uInt32 BIFF12_WEBPR_HTMLTABLES = 0x00010000;
62 
63 const sal_uInt8 BIFF12_WEBPR_HAS_POSTMETHOD = 0x01;
64 const sal_uInt8 BIFF12_WEBPR_HAS_EDITPAGE = 0x02;
65 const 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
122  maModel.mnType = rAttribs.getInteger( XML_type, BIFF12_CONNECTION_UNKNOWN );
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 
136 void Connection::importWebPr( const AttributeList& rAttribs )
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 
164 void 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();
194  maModel.mnId = rStrm.readInt32();
195  nCredentials = rStrm.readuChar();
196 
197  if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SOURCEFILE ) )
198  rStrm >> maModel.maSourceFile;
199  if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SOURCECONNFILE ) )
200  rStrm >> maModel.maSourceConnFile;
201  if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_DESCRIPTION ) )
202  rStrm >> maModel.maDescription;
203  if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_NAME ) )
204  rStrm >> maModel.maName;
205  if( getFlag( nStrFlags, BIFF12_CONNECTION_HAS_SSOID ) )
206  rStrm >> maModel.maSsoId;
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 
260 void 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: */
Helper class to provide access to global workbook data.
bool mbSaveData
True = refresh connection on import.
bool mbSavePassword
True = save cached data with connection.
sal_Int32 mnCredentials
Reconnection method.
sal_Int32 mnType
Unique connection identifier.
Connection(const WorkbookHelper &rHelper)
virtual void skip(sal_Int32 nBytes, size_t nAtomSize=1) override
OptValue< bool > getBool(sal_Int32 nAttrToken) const
bool mbDeleted
True = new connection, never updated.
OptValue< OUString > getXString(sal_Int32 nAttrToken) const
bool mbTextDates
True = web query created with Excel 97.
OptValue< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
bool mbSourceData
True = XML query, false = HTML query.
const sal_Int32 BIFF12_ID_PCITEM_STRING
Definition: biffhelper.hxx:173
OUString maSourceConnFile
URL of a source data file.
sal_uInt32 readuInt32()
#define STATIC_ARRAY_SELECT(array, index, def)
unsigned char readuChar()
sal_uInt16 mnId
float x
void importWebPrTables(SequenceInputStream &rStrm)
Imports web query table settings from the WEBPRTABLES record.
OUString maSsoId
URL of a source connection file.
bool mbNew
True = keep connection open after import.
OUString maEditPage
POST method to query data.
OUString maDescription
Unique name of this connection.
sal_uInt16 readuInt16()
sal_Int32 mnReconnectMethod
Data source type.
ConnectionModel()
True = save password in connection string.
bool has(key_type nKey) const
bool mbHtmlTables
True = refreshed with Excel 2000 or newer.
bool getFlag(Type nBitField, Type nMask)
bool mbXml
Plain text, rich text, or HTML.
WebPrModel()
True = HTML tables, false = entire document.
Connection & createConnection()
Creates a new empty connection.
std::shared_ptr< Connection > ConnectionRef
sal_Int32 mnType
An external data connection (database, web query, etc.).
bool mbXl97Created
True = use column widths of first row for entire.
static OUString readString(SequenceInputStream &rStrm, bool b32BitLen=true)
Reads a BIFF12 string with leading 16-bit or 32-bit length field.
Definition: biffhelper.cxx:78
void finalizeImport()
Maps all connections by their identifier.
void importWebPr(const AttributeList &rAttribs)
Imports web query settings from the webPr element.
OUString maSourceFile
User description of this connection.
OUString maPostMethod
Source URL to refresh the data.
void importConnection(const AttributeList &rAttribs)
Imports connection settings from the connection element.
bool mbFirstRow
True = join consecutive delimiters.
void importWebPrTable(SequenceInputStream &rStrm, sal_Int32 nRecId)
Imports a web query table identifier from the PCITEM_MISSING, PCITEM_STRING, or PCITEM_INDEX record...
void importTables()
Imports web query table settings from the tables element.
Special properties for data connections representing web queries.
sal_Int32 mnHtmlFormat
Web page showing query data (for XML queries).
ConnectionsBuffer(const WorkbookHelper &rHelper)
bool mbConsecutive
True = parse preformatted sections (.
bool mbOnlyUseConnFile
True = connection has been deleted.
bool mbXl2000Refreshed
True = read date values as text, false = parse dates.
OUString maUrl
Names or indexes of the web query tables.
mapped_type get(key_type nKey) const
bool mbRefreshOnLoad
True = background refresh enabled.
void importTable(const AttributeList &rAttribs, sal_Int32 nElement)
Imports a web query table identifier from the m, s, or x element.
unsigned char sal_uInt8
const sal_Int32 BIFF12_CONNECTION_UNKNOWN
void insertConnectionToMap(const ConnectionRef &rxConnection)
Inserts the passed connection into the map according to its identifier.
const sal_Int32 BIFF12_ID_PCITEM_MISSING
Definition: biffhelper.hxx:172
const sal_Int32 BIFF12_ID_PCITEM_INDEX
Definition: biffhelper.hxx:171
OUString maName
Special settings for web queries.
bool mbParsePre
True = import XML source data referred by HTML table.
bool mbKeepAlive
Refresh interval in minutes.
tuple m
sal_Int32 mnInterval
Credentials method.
bool mbBackground
True = use maSourceConnFile, ignore mnReconnectMethod.
ConnectionRef getConnection(sal_Int32 nConnId) const
Returns a data connection by its unique identifier.
sal_Int32 mnId
Single sign-on identifier.
OptValue< sal_Int32 > getToken(sal_Int32 nAttrToken) const