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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
|
/* -*- 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/.
*/
#include "xmlimp.hxx"
#include <initializer_list>
#include <unordered_map>
#include <com/sun/star/svg/XSVGWriter.hpp>
#include <com/sun/star/uri/UriReferenceFactory.hpp>
#include <com/sun/star/xml/sax/InputSource.hpp>
#include <com/sun/star/xml/sax/Parser.hpp>
#include <com/sun/star/xml/sax/Writer.hpp>
#include <comphelper/propertyvalue.hxx>
#include <rtl/uri.hxx>
#include <tools/gen.hxx>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>
#include <unotools/streamwrap.hxx>
#include <tools/diagnose_ex.h>
#include "xmlfmt.hxx"
#include "xmlictxt.hxx"
#include "xmlmetai.hxx"
#include "xmltext.hxx"
using namespace com::sun::star;
namespace writerperfect
{
namespace exp
{
namespace
{
/// Looks up mime type for a given image extension.
OUString GetMimeType(const OUString &rExtension)
{
static const std::unordered_map<OUString, OUString> vMimeTypes =
{
{"gif", "image/gif"},
{"jpg", "image/jpeg"},
{"png", "image/png"},
{"svg", "image/svg+xml"},
};
auto it = vMimeTypes.find(rExtension);
return it == vMimeTypes.end() ? OUString() : it->second;
}
/// Determines the base directory for cover images, XMP metadata, popup images.
OUString FindMediaDir(const OUString &rDocumentBaseURL, const uno::Sequence<beans::PropertyValue> &rFilterData)
{
OUString aMediaDir;
// See if filter data contains a media directory explicitly.
for (sal_Int32 i = 0; i < rFilterData.getLength(); ++i)
{
if (rFilterData[i].Name == "RVNGMediaDir")
{
rFilterData[i].Value >>= aMediaDir;
break;
}
}
if (!aMediaDir.isEmpty())
return aMediaDir + "/";
// Not set explicitly, try to pick it up from the base directory.
INetURLObject aURL(rDocumentBaseURL);
try
{
aMediaDir = rtl::Uri::convertRelToAbs(rDocumentBaseURL, aURL.GetBase()) + "/";
}
catch (const rtl::MalformedUriException &)
{
DBG_UNHANDLED_EXCEPTION("writerperfect");
}
return aMediaDir;
}
/// Picks up a cover image from the base directory.
OUString FindCoverImage(const OUString &rDocumentBaseURL, OUString &rMimeType, const uno::Sequence<beans::PropertyValue> &rFilterData)
{
OUString aRet;
// See if filter data contains a cover image explicitly.
for (sal_Int32 i = 0; i < rFilterData.getLength(); ++i)
{
if (rFilterData[i].Name == "RVNGCoverImage")
{
rFilterData[i].Value >>= aRet;
break;
}
}
if (!aRet.isEmpty())
{
INetURLObject aRetURL(aRet);
rMimeType = GetMimeType(aRetURL.GetExtension());
return aRet;
}
// Not set explicitly, try to pick it up from the base directory.
if (rDocumentBaseURL.isEmpty())
return aRet;
static const std::initializer_list<OUStringLiteral> vExtensions =
{
"gif",
"jpg",
"png",
"svg"
};
OUString aMediaDir = FindMediaDir(rDocumentBaseURL, rFilterData);
for (const auto &rExtension : vExtensions)
{
aRet = aMediaDir + "cover." + rExtension;
if (!aRet.isEmpty())
{
SvFileStream aStream(aRet, StreamMode::READ);
if (aStream.IsOpen())
{
rMimeType = GetMimeType(rExtension);
// File exists.
return aRet;
}
aRet.clear();
}
}
return aRet;
}
/// Picks up XMP metadata from the base directory.
void FindXMPMetadata(const uno::Reference<uno::XComponentContext> &xContext, const OUString &rDocumentBaseURL, const uno::Sequence<beans::PropertyValue> &rFilterData, librevenge::RVNGPropertyList &rMetaData)
{
// See if filter data contains metadata explicitly.
OUString aValue;
for (sal_Int32 i = 0; i < rFilterData.getLength(); ++i)
{
if (rFilterData[i].Name == "RVNGIdentifier")
{
rFilterData[i].Value >>= aValue;
if (!aValue.isEmpty())
rMetaData.insert("dc:identifier", aValue.toUtf8().getStr());
}
else if (rFilterData[i].Name == "RVNGTitle")
{
rFilterData[i].Value >>= aValue;
if (!aValue.isEmpty())
rMetaData.insert("dc:title", aValue.toUtf8().getStr());
}
else if (rFilterData[i].Name == "RVNGInitialCreator")
{
rFilterData[i].Value >>= aValue;
if (!aValue.isEmpty())
rMetaData.insert("meta:initial-creator", aValue.toUtf8().getStr());
}
else if (rFilterData[i].Name == "RVNGLanguage")
{
rFilterData[i].Value >>= aValue;
if (!aValue.isEmpty())
rMetaData.insert("dc:language", aValue.toUtf8().getStr());
}
else if (rFilterData[i].Name == "RVNGDate")
{
rFilterData[i].Value >>= aValue;
if (!aValue.isEmpty())
rMetaData.insert("dc:date", aValue.toUtf8().getStr());
}
}
// If not set explicitly, try to pick it up from the base directory.
if (rDocumentBaseURL.isEmpty())
return;
OUString aMediaDir = FindMediaDir(rDocumentBaseURL, rFilterData);
OUString aURL = aMediaDir + "metadata.xmp";
SvFileStream aStream(aURL, StreamMode::READ);
if (!aStream.IsOpen())
return;
xml::sax::InputSource aInputSource;
uno::Reference<io::XInputStream> xStream(new utl::OStreamWrapper(aStream));
aInputSource.aInputStream = xStream;
uno::Reference<xml::sax::XParser> xParser = xml::sax::Parser::create(xContext);
rtl::Reference<XMPParser> xXMP(new XMPParser(rMetaData));
uno::Reference<xml::sax::XDocumentHandler> xDocumentHandler(xXMP.get());
xParser->setDocumentHandler(xDocumentHandler);
try
{
xParser->parseStream(aInputSource);
}
catch (const uno::Exception &)
{
DBG_UNHANDLED_EXCEPTION("writerperfect", "parseStream() failed");
return;
}
}
}
/// Handler for <office:body>.
class XMLBodyContext : public XMLImportContext
{
public:
XMLBodyContext(XMLImport &rImport);
rtl::Reference<XMLImportContext> CreateChildContext(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &/*xAttribs*/) override;
};
XMLBodyContext::XMLBodyContext(XMLImport &rImport)
: XMLImportContext(rImport)
{
}
rtl::Reference<XMLImportContext> XMLBodyContext::CreateChildContext(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &/*xAttribs*/)
{
if (rName == "office:text")
return new XMLBodyContentContext(mrImport);
return nullptr;
}
/// Handler for <office:document>.
class XMLOfficeDocContext : public XMLImportContext
{
public:
XMLOfficeDocContext(XMLImport &rImport);
rtl::Reference<XMLImportContext> CreateChildContext(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &/*xAttribs*/) override;
// Handles metafile for a single page.
void HandleFixedLayoutPage(const FixedLayoutPage &rPage, bool bFirst);
};
XMLOfficeDocContext::XMLOfficeDocContext(XMLImport &rImport)
: XMLImportContext(rImport)
{
}
rtl::Reference<XMLImportContext> XMLOfficeDocContext::CreateChildContext(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &/*xAttribs*/)
{
if (rName == "office:meta")
return new XMLMetaDocumentContext(mrImport);
if (rName == "office:automatic-styles")
return new XMLStylesContext(mrImport, XMLStylesContext::StyleType_AUTOMATIC);
if (rName == "office:styles")
return new XMLStylesContext(mrImport, XMLStylesContext::StyleType_NONE);
if (rName == "office:master-styles")
return new XMLStylesContext(mrImport, XMLStylesContext::StyleType_NONE);
if (rName == "office:font-face-decls")
return new XMLFontFaceDeclsContext(mrImport);
if (rName == "office:body")
{
if (mrImport.GetPageMetafiles().empty())
return new XMLBodyContext(mrImport);
// Ignore text from doc model in the fixed layout case, instead
// insert the page metafiles.
bool bFirst = true;
for (const auto &rPage : mrImport.GetPageMetafiles())
{
HandleFixedLayoutPage(rPage, bFirst);
if (bFirst)
bFirst = false;
}
}
return nullptr;
}
void XMLOfficeDocContext::HandleFixedLayoutPage(const FixedLayoutPage &rPage, bool bFirst)
{
uno::Reference<uno::XComponentContext> xCtx = mrImport.GetComponentContext();
uno::Reference<xml::sax::XWriter> xSaxWriter = xml::sax::Writer::create(xCtx);
if (!xSaxWriter.is())
return;
uno::Sequence<uno::Any> aArguments =
{
uno::makeAny<uno::Sequence<beans::PropertyValue>>({comphelper::makePropertyValue("DTDString", false)})
};
uno::Reference<svg::XSVGWriter> xSVGWriter(xCtx->getServiceManager()->createInstanceWithArgumentsAndContext("com.sun.star.svg.SVGWriter", aArguments, xCtx), uno::UNO_QUERY);
if (!xSVGWriter.is())
return;
SvMemoryStream aMemoryStream;
xSaxWriter->setOutputStream(new utl::OStreamWrapper(aMemoryStream));
xSVGWriter->write(xSaxWriter, rPage.aMetafile);
// Have all the info, invoke the generator.
librevenge::RVNGPropertyList aPageProperties;
// Pixel -> inch.
double fWidth = rPage.aCssPixels.getWidth();
fWidth /= 96;
aPageProperties.insert("fo:page-width", fWidth);
double fHeight = rPage.aCssPixels.getHeight();
fHeight /= 96;
aPageProperties.insert("fo:page-height", fHeight);
if (!rPage.aChapterNames.empty())
{
// Name of chapters starting on this page.
librevenge::RVNGPropertyListVector aChapterNames;
for (const auto &rName : rPage.aChapterNames)
{
librevenge::RVNGPropertyList aChapter;
aChapter.insert("librevenge:name", rName.toUtf8().getStr());
aChapterNames.append(aChapter);
}
aPageProperties.insert("librevenge:chapter-names", aChapterNames);
}
mrImport.GetGenerator().openPageSpan(aPageProperties);
librevenge::RVNGPropertyList aParagraphProperties;
if (!bFirst)
// All pages except the first one needs a page break before the page
// metafile.
aParagraphProperties.insert("fo:break-before", "page");
mrImport.GetGenerator().openParagraph(aParagraphProperties);
librevenge::RVNGPropertyList aImageProperties;
aImageProperties.insert("librevenge:mime-type", "image/svg+xml");
librevenge::RVNGBinaryData aBinaryData;
aBinaryData.append(static_cast<const unsigned char *>(aMemoryStream.GetBuffer()), aMemoryStream.GetSize());
aImageProperties.insert("office:binary-data", aBinaryData);
mrImport.GetGenerator().insertBinaryObject(aImageProperties);
mrImport.GetGenerator().closeParagraph();
mrImport.GetGenerator().closePageSpan();
}
XMLImport::XMLImport(const uno::Reference<uno::XComponentContext> &xContext, librevenge::RVNGTextInterface &rGenerator, const OUString &rURL, const uno::Sequence<beans::PropertyValue> &rDescriptor, const std::vector<FixedLayoutPage> &rPageMetafiles)
: mrGenerator(rGenerator),
mxContext(xContext),
mbIsInPageSpan(false),
mrPageMetafiles(rPageMetafiles)
{
uno::Sequence<beans::PropertyValue> aFilterData;
for (sal_Int32 i = 0; i < rDescriptor.getLength(); ++i)
{
if (rDescriptor[i].Name == "FilterData")
{
rDescriptor[i].Value >>= aFilterData;
break;
}
}
maMediaDir = FindMediaDir(rURL, aFilterData);
OUString aMimeType;
OUString aCoverImage = FindCoverImage(rURL, aMimeType, aFilterData);
if (!aCoverImage.isEmpty())
{
librevenge::RVNGBinaryData aBinaryData;
SvFileStream aStream(aCoverImage, StreamMode::READ);
SvMemoryStream aMemoryStream;
aMemoryStream.WriteStream(aStream);
aBinaryData.append(static_cast<const unsigned char *>(aMemoryStream.GetBuffer()), aMemoryStream.GetSize());
librevenge::RVNGPropertyList aCoverImageProperties;
aCoverImageProperties.insert("office:binary-data", aBinaryData);
aCoverImageProperties.insert("librevenge:mime-type", aMimeType.toUtf8().getStr());
maCoverImages.append(aCoverImageProperties);
}
FindXMPMetadata(mxContext, rURL, aFilterData, maMetaData);
mxUriReferenceFactory = uri::UriReferenceFactory::create(mxContext);
}
const librevenge::RVNGPropertyListVector &XMLImport::GetCoverImages()
{
return maCoverImages;
}
const librevenge::RVNGPropertyList &XMLImport::GetMetaData()
{
return maMetaData;
}
namespace
{
/// Finds out if a file URL exists.
bool FileURLExists(const OUString &rURL)
{
SvFileStream aStream(rURL, StreamMode::READ);
return aStream.IsOpen();
}
}
PopupState XMLImport::FillPopupData(const OUString &rURL, librevenge::RVNGPropertyList &rPropList)
{
uno::Reference<uri::XUriReference> xUriRef;
try
{
xUriRef = mxUriReferenceFactory->parse(rURL);
}
catch (const uno::Exception &)
{
DBG_UNHANDLED_EXCEPTION("writerperfect", "XUriReference::parse() failed");
}
bool bAbsolute = true;
if (xUriRef.is())
bAbsolute = xUriRef->isAbsolute();
if (bAbsolute)
return PopupState::NotConsumed;
// Default case: relative URL, popup data was in the same directory as the
// document at insertion time.
OUString aAbs = maMediaDir + rURL;
if (!FileURLExists(aAbs))
// Fallback case: relative URL, popup data was in the default media
// directory at insertion time.
aAbs = maMediaDir + "../" + rURL;
if (!FileURLExists(aAbs))
// Relative link, but points to non-existing file: don't emit that to
// librevenge, since it will be invalid anyway.
return PopupState::Ignore;
SvFileStream aStream(aAbs, StreamMode::READ);
librevenge::RVNGBinaryData aBinaryData;
SvMemoryStream aMemoryStream;
aMemoryStream.WriteStream(aStream);
aBinaryData.append(static_cast<const unsigned char *>(aMemoryStream.GetBuffer()), aMemoryStream.GetSize());
rPropList.insert("office:binary-data", aBinaryData);
INetURLObject aAbsURL(aAbs);
OUString aMimeType = GetMimeType(aAbsURL.GetExtension());
rPropList.insert("librevenge:mime-type", aMimeType.toUtf8().getStr());
return PopupState::Consumed;
}
const std::vector<FixedLayoutPage> &XMLImport::GetPageMetafiles() const
{
return mrPageMetafiles;
}
const uno::Reference<uno::XComponentContext> &XMLImport::GetComponentContext() const
{
return mxContext;
}
rtl::Reference<XMLImportContext> XMLImport::CreateContext(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &/*xAttribs*/)
{
if (rName == "office:document")
return new XMLOfficeDocContext(*this);
return nullptr;
}
librevenge::RVNGTextInterface &XMLImport::GetGenerator() const
{
return mrGenerator;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticTextStyles()
{
return maAutomaticTextStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticParagraphStyles()
{
return maAutomaticParagraphStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticCellStyles()
{
return maAutomaticCellStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticColumnStyles()
{
return maAutomaticColumnStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticRowStyles()
{
return maAutomaticRowStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticTableStyles()
{
return maAutomaticTableStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetAutomaticGraphicStyles()
{
return maAutomaticGraphicStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetTextStyles()
{
return maTextStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetParagraphStyles()
{
return maParagraphStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetCellStyles()
{
return maCellStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetColumnStyles()
{
return maColumnStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetRowStyles()
{
return maRowStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetTableStyles()
{
return maTableStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetGraphicStyles()
{
return maGraphicStyles;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetPageLayouts()
{
return maPageLayouts;
}
std::map<OUString, librevenge::RVNGPropertyList> &XMLImport::GetMasterStyles()
{
return maMasterStyles;
}
void XMLImport::startDocument()
{
mrGenerator.startDocument(librevenge::RVNGPropertyList());
}
void XMLImport::endDocument()
{
mrGenerator.endDocument();
}
void XMLImport::startElement(const OUString &rName, const uno::Reference<xml::sax::XAttributeList> &xAttribs)
{
rtl::Reference<XMLImportContext> xContext;
if (!maContexts.empty())
{
if (maContexts.top().is())
xContext = maContexts.top()->CreateChildContext(rName, xAttribs);
}
else
xContext = CreateContext(rName, xAttribs);
if (xContext.is())
xContext->startElement(rName, xAttribs);
maContexts.push(xContext);
}
void XMLImport::endElement(const OUString &rName)
{
if (maContexts.empty())
return;
if (maContexts.top().is())
maContexts.top()->endElement(rName);
maContexts.pop();
}
void XMLImport::characters(const OUString &rChars)
{
if (maContexts.top().is())
maContexts.top()->characters(rChars);
}
void XMLImport::ignorableWhitespace(const OUString &/*rWhitespaces*/)
{
}
void XMLImport::processingInstruction(const OUString &/*rTarget*/, const OUString &/*rData*/)
{
}
void XMLImport::setDocumentLocator(const uno::Reference<xml::sax::XLocator> &/*xLocator*/)
{
}
void XMLImport::HandlePageSpan(const librevenge::RVNGPropertyList &rPropertyList)
{
OUString sMasterPageName;
OUString sLayoutName;
if (rPropertyList["style:master-page-name"])
sMasterPageName = OStringToOUString(rPropertyList["style:master-page-name"]->getStr().cstr(), RTL_TEXTENCODING_UTF8);
else if (!GetIsInPageSpan())
sMasterPageName = "Standard";
if (sMasterPageName.getLength())
{
librevenge::RVNGPropertyList &rMasterPage = GetMasterStyles()[sMasterPageName];
if (rMasterPage["style:page-layout-name"])
{
sLayoutName = OStringToOUString(rMasterPage["style:page-layout-name"]->getStr().cstr(), RTL_TEXTENCODING_UTF8);
}
}
if (sLayoutName.getLength())
{
librevenge::RVNGPropertyList &rPageLayout = GetPageLayouts()[sLayoutName];
if (GetIsInPageSpan())
GetGenerator().closePageSpan();
GetGenerator().openPageSpan(rPageLayout);
SetIsInPageSpan(true);
}
}
} // namespace exp
} // namespace writerperfect
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|