1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "OOXMLStreamImpl.hxx"
#include <oox/core/fasttokenhandler.hxx>
#include <com/sun/star/embed/XHierarchicalStorageAccess.hpp>
#include <com/sun/star/uri/UriReferenceFactory.hpp>
#include <comphelper/storagehelper.hxx>
namespace writerfilter::ooxml
{
using namespace com::sun::star;
OOXMLStreamImpl::OOXMLStreamImpl
(uno::Reference<uno::XComponentContext> const & xContext,
uno::Reference<io::XInputStream> const & xStorageStream,
StreamType_t nType, bool bRepairStorage)
: mxContext(xContext), mxStorageStream(xStorageStream), mnStreamType(nType)
{
mxStorage.set
(comphelper::OStorageHelper::GetStorageOfFormatFromInputStream
(OFOPXML_STORAGE_FORMAT_STRING, mxStorageStream, xContext, bRepairStorage));
mxRelationshipAccess.set(mxStorage, uno::UNO_QUERY_THROW);
init();
}
OOXMLStreamImpl::OOXMLStreamImpl
(OOXMLStreamImpl const & rOOXMLStream, StreamType_t nStreamType)
: mxContext(rOOXMLStream.mxContext),
mxStorageStream(rOOXMLStream.mxStorageStream),
mxStorage(rOOXMLStream.mxStorage),
mnStreamType(nStreamType),
msPath(rOOXMLStream.msPath)
{
mxRelationshipAccess.set(rOOXMLStream.mxDocumentStream, uno::UNO_QUERY_THROW);
init();
}
OOXMLStreamImpl::OOXMLStreamImpl
(OOXMLStreamImpl const & rOOXMLStream, const OUString & rId)
: mxContext(rOOXMLStream.mxContext),
mxStorageStream(rOOXMLStream.mxStorageStream),
mxStorage(rOOXMLStream.mxStorage),
mnStreamType(UNKNOWN),
msId(rId),
msPath(rOOXMLStream.msPath)
{
mxRelationshipAccess.set(rOOXMLStream.mxDocumentStream, uno::UNO_QUERY_THROW);
init();
}
OOXMLStreamImpl::~OOXMLStreamImpl()
{
}
const OUString & OOXMLStreamImpl::getTarget() const
{
return msTarget;
}
bool OOXMLStreamImpl::lcl_getTarget(const uno::Reference<embed::XRelationshipAccess>&
xRelationshipAccess,
StreamType_t nStreamType,
const OUString & rId,
OUString & rDocumentTarget)
{
static const char sId[] = "Id";
static const char sTarget[] = "Target";
static const char sTargetMode[] = "TargetMode";
static const char sExternal[] = "External";
if (maIdCache.empty())
{
// Cache is empty? Then let's build it!
const uno::Sequence< uno::Sequence<beans::StringPair> >aSeqs = xRelationshipAccess->getAllRelationships();
for (const uno::Sequence<beans::StringPair>& rSeq : aSeqs)
{
OUString aId;
OUString aTarget;
bool bExternal = false;
for (const beans::StringPair& rPair : rSeq)
{
if (rPair.First == sId)
aId = rPair.Second;
else if (rPair.First == sTarget)
aTarget = rPair.Second;
else if (rPair.First == sTargetMode && rPair.Second == sExternal)
bExternal = true;
}
// Only cache external targets, internal ones are more complex (see below)
if (bExternal || aTarget.startsWith("#"))
maIdCache[aId] = aTarget;
}
}
if (maIdCache.find(rId) != maIdCache.end())
{
rDocumentTarget = maIdCache[rId];
return true;
}
bool bFound = false;
static uno::Reference<uri::XUriReferenceFactory> xFac = uri::UriReferenceFactory::create(mxContext);
// use '/' to representent the root of the zip package ( and provide a 'file' scheme to
// keep the XUriReference implementation happy )
// add mspath to represent the 'source' of this stream
uno::Reference<uri::XUriReference> xBase = xFac->parse("file:///" + msPath);
static const char sType[] = "Type";
static const char sDocumentType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
static const char sStylesType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles";
static const char sNumberingType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering";
static const char sFonttableType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable";
static const char sFootnotesType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes";
static const char sEndnotesType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes";
static const char sCommentsType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
static const char sThemeType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
static const char sCustomType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml";
static const char sCustomPropsType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps";
static const char sGlossaryType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument";
static const char sWebSettings[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings";
static const char sSettingsType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings";
static const char sChartType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
static const char sEmbeddingsType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/package";
static const char sFooterType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer";
static const char sHeaderType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header";
static const char sOleObjectType[] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
// OOXML strict
static const char sDocumentTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/officeDocument";
static const char sStylesTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/styles";
static const char sNumberingTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/numbering";
static const char sFonttableTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/fontTable";
static const char sFootnotesTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/footnotes";
static const char sEndnotesTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/endnotes";
static const char sCommentsTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/comments";
static const char sThemeTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/theme";
static const char sCustomTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/customXml";
static const char sCustomPropsTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/customXmlProps";
static const char sGlossaryTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/glossaryDocument";
static const char sWebSettingsStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/webSettings";
static const char sSettingsTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/settings";
static const char sChartTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/chart";
static const char sEmbeddingsTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/package";
static const char sFootersTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/footer";
static const char sHeaderTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/header";
static const char sOleObjectTypeStrict[] = "http://purl.oclc.org/ooxml/officeDocument/relationships/oleObject";
static const char sVBAProjectType[] = "http://schemas.microsoft.com/office/2006/relationships/vbaProject";
static const char sVBADataType[] = "http://schemas.microsoft.com/office/2006/relationships/wordVbaData";
OUString sStreamType;
OUString sStreamTypeStrict;
switch (nStreamType)
{
case VBAPROJECT:
sStreamType = sVBAProjectType;
sStreamTypeStrict = sVBAProjectType;
break;
case VBADATA:
sStreamType = sVBADataType;
sStreamTypeStrict = sVBADataType;
break;
case DOCUMENT:
sStreamType = sDocumentType;
sStreamTypeStrict = sDocumentTypeStrict;
break;
case STYLES:
sStreamType = sStylesType;
sStreamTypeStrict = sStylesTypeStrict;
break;
case NUMBERING:
sStreamType = sNumberingType;
sStreamTypeStrict = sNumberingTypeStrict;
break;
case FONTTABLE:
sStreamType = sFonttableType;
sStreamTypeStrict = sFonttableTypeStrict;
break;
case FOOTNOTES:
sStreamType = sFootnotesType;
sStreamTypeStrict = sFootnotesTypeStrict;
break;
case ENDNOTES:
sStreamType = sEndnotesType;
sStreamTypeStrict = sEndnotesTypeStrict;
break;
case COMMENTS:
sStreamType = sCommentsType;
sStreamTypeStrict = sCommentsTypeStrict;
break;
case THEME:
sStreamType = sThemeType;
sStreamTypeStrict = sThemeTypeStrict;
break;
case CUSTOMXML:
sStreamType = sCustomType;
sStreamTypeStrict = sCustomTypeStrict;
break;
case CUSTOMXMLPROPS:
sStreamType = sCustomPropsType;
sStreamTypeStrict = sCustomPropsTypeStrict;
break;
case SETTINGS:
sStreamType = sSettingsType;
sStreamTypeStrict = sSettingsTypeStrict;
break;
case GLOSSARY:
sStreamType = sGlossaryType;
sStreamTypeStrict = sGlossaryTypeStrict;
break;
case WEBSETTINGS:
sStreamType = sWebSettings;
sStreamTypeStrict = sWebSettingsStrict;
break;
case CHARTS:
sStreamType = sChartType;
sStreamTypeStrict = sChartTypeStrict;
break;
case EMBEDDINGS:
sStreamType = sEmbeddingsType;
sStreamTypeStrict = sEmbeddingsTypeStrict;
break;
case FOOTER:
sStreamType = sFooterType;
sStreamTypeStrict = sFootersTypeStrict;
break;
case HEADER:
sStreamType = sHeaderType;
sStreamTypeStrict = sHeaderTypeStrict;
break;
default:
break;
}
if (xRelationshipAccess.is())
{
const uno::Sequence< uno::Sequence< beans::StringPair > >aSeqs =
xRelationshipAccess->getAllRelationships();
for (const uno::Sequence< beans::StringPair > &rSeq : aSeqs)
{
bool bExternalTarget = false;
OUString sMyTarget;
for (const beans::StringPair &rPair : rSeq)
{
if (rPair.First == sType &&
( rPair.Second == sStreamType ||
rPair.Second == sStreamTypeStrict ))
bFound = true;
else if(rPair.First == sType &&
((rPair.Second == sOleObjectType ||
rPair.Second == sOleObjectTypeStrict) &&
nStreamType == EMBEDDINGS))
{
bFound = true;
}
else if (rPair.First == sId &&
rPair.Second == rId)
bFound = true;
else if (rPair.First == sTarget)
{
// checking item[n].xml is not visited already.
if(customTarget != rPair.Second && (sStreamType == sCustomType || sStreamType == sChartType || sStreamType == sFooterType || sStreamType == sHeaderType))
{
bFound = false;
}
else
{
sMyTarget = rPair.Second;
}
}
else if (rPair.First == sTargetMode &&
rPair.Second == sExternal)
bExternalTarget = true;
}
if (bFound)
{
if (bExternalTarget)
rDocumentTarget = sMyTarget;
else
{
// 'Target' is a relative Uri, so a 'Target=/path'
// with a base Uri of file://base/foo will resolve to
// file://base/word. We need something more than some
// simple string concatenation here to handle that.
uno::Reference<uri::XUriReference> xPart = xFac->parse(sMyTarget);
uno::Reference<uri::XUriReference> xAbs = xFac->makeAbsolute(xBase, xPart, true, uri::RelativeUriExcessParentSegments_RETAIN);
if (!xAbs)
{
//it was invalid gibberish
bFound = false;
}
else
{
rDocumentTarget = xAbs->getPath();
// path will start with the fragment separator. need to
// remove that
rDocumentTarget = rDocumentTarget.copy( 1 );
if(sStreamType == sEmbeddingsType)
embeddingsTarget = rDocumentTarget;
}
}
break;
}
}
}
return bFound;
}
OUString OOXMLStreamImpl::getTargetForId(const OUString & rId)
{
OUString sTarget;
uno::Reference<embed::XRelationshipAccess> xRelationshipAccess
(mxDocumentStream, uno::UNO_QUERY_THROW);
if (lcl_getTarget(xRelationshipAccess, UNKNOWN, rId, sTarget))
return sTarget;
return OUString();
}
void OOXMLStreamImpl::init()
{
bool bFound = lcl_getTarget(mxRelationshipAccess,
mnStreamType, msId, msTarget);
if (bFound)
{
sal_Int32 nLastIndex = msTarget.lastIndexOf('/');
if (nLastIndex >= 0)
msPath = msTarget.copy(0, nLastIndex + 1);
uno::Reference<embed::XHierarchicalStorageAccess>
xHierarchicalStorageAccess(mxStorage, uno::UNO_QUERY);
if (xHierarchicalStorageAccess.is())
{
uno::Any aAny(xHierarchicalStorageAccess->
openStreamElementByHierarchicalName
(msTarget, embed::ElementModes::SEEKABLEREAD));
aAny >>= mxDocumentStream;<--- Variable 'aAny' is assigned a value that is never used.
// Non-cached ID lookup works by accessing mxDocumentStream as an embed::XRelationshipAccess.
// So when it changes, we should empty the cache.
maIdCache.clear();
}
}
}
uno::Reference<io::XInputStream> OOXMLStreamImpl::getDocumentStream()
{
uno::Reference<io::XInputStream> xResult;
if (mxDocumentStream.is())
xResult = mxDocumentStream->getInputStream();
return xResult;
}
uno::Reference<uno::XComponentContext> OOXMLStreamImpl::getContext()
{
return mxContext;
}
uno::Reference <xml::sax::XFastTokenHandler> OOXMLStreamImpl::getFastTokenHandler()
{
if (! mxFastTokenHandler.is())
mxFastTokenHandler.set(new oox::core::FastTokenHandler());
return mxFastTokenHandler;
}
OOXMLStream::Pointer_t
OOXMLDocumentFactory::createStream
(const uno::Reference<uno::XComponentContext>& xContext,
const uno::Reference<io::XInputStream>& rStream,
bool bRepairStorage)
{
OOXMLStreamImpl * pStream = new OOXMLStreamImpl(xContext, rStream,
OOXMLStream::DOCUMENT, bRepairStorage);
return OOXMLStream::Pointer_t(pStream);
}
OOXMLStream::Pointer_t
OOXMLDocumentFactory::createStream
(const OOXMLStream::Pointer_t& pStream, OOXMLStream::StreamType_t nStreamType)
{
OOXMLStream::Pointer_t pRet;
if (nStreamType != OOXMLStream::VBADATA)
{
if (OOXMLStreamImpl* pImpl = dynamic_cast<OOXMLStreamImpl *>(pStream.get()))
pRet = new OOXMLStreamImpl(*pImpl, nStreamType);
}
else
{
// VBADATA is not a relation of the document, but of the VBAPROJECT stream.
if (OOXMLStreamImpl* pImpl = dynamic_cast<OOXMLStreamImpl *>(pStream.get()))
{
std::unique_ptr<OOXMLStreamImpl> pProject(new OOXMLStreamImpl(*pImpl, OOXMLStream::VBAPROJECT));
pRet = new OOXMLStreamImpl(*pProject, OOXMLStream::VBADATA);
}
}
return pRet;
}
OOXMLStream::Pointer_t
OOXMLDocumentFactory::createStream
(const OOXMLStream::Pointer_t& pStream, const OUString & rId)
{
OOXMLStream::Pointer_t pRet;
if (OOXMLStreamImpl* pImpl = dynamic_cast<OOXMLStreamImpl *>(pStream.get()))
pRet = new OOXMLStreamImpl(*pImpl, rId);
return pRet;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|