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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
/* -*- 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 "OOXMLPropertySet.hxx"
#include <stdio.h>
#include <iostream>
#include <ooxml/QNameToString.hxx>
#include <com/sun/star/drawing/XShape.hpp>
#include <sax/tools/converter.hxx>
#include <tools/color.hxx>
#include <o3tl/string_view.hxx>
#include <utility>
namespace writerfilter::ooxml
{
using namespace com::sun::star;
OOXMLProperty::OOXMLProperty(Id id, OOXMLValue::Pointer_t pValue,
OOXMLProperty::Type_t eType)
: mId(id), mpValue(std::move(pValue)), meType(eType)
{
}
OOXMLProperty::~OOXMLProperty()
{
}
sal_uInt32 OOXMLProperty::getId() const
{
return mId;
}
Value::Pointer_t OOXMLProperty::getValue()
{
Value::Pointer_t pResult;
if (mpValue)
pResult = Value::Pointer_t(mpValue->clone());
else
pResult = Value::Pointer_t(new OOXMLValue());
return pResult;
}
writerfilter::Reference<Properties>::Pointer_t OOXMLProperty::getProps()
{
writerfilter::Reference<Properties>::Pointer_t pResult;
if (mpValue)
pResult = mpValue->getProperties();
return pResult;
}
#ifdef DBG_UTIL
std::string OOXMLProperty::getName() const
{
std::string sResult(QNameToString(mId));
if (sResult.length() == 0)
sResult = fastTokenToId(mId);
if (sResult.length() == 0)
{
static char sBuffer[256];
snprintf(sBuffer, sizeof(sBuffer), "%" SAL_PRIxUINT32, mId);
sResult = sBuffer;
}
return sResult;
}
#endif
#ifdef DBG_UTIL
std::string OOXMLProperty::toString() const
{
std::string sResult = "(";
sResult += getName();
sResult += ", ";
if (mpValue)
sResult += mpValue->toString();
else
sResult +="(null)";
sResult +=")";
return sResult;
}
#endif
void OOXMLProperty::resolve(writerfilter::Properties & rProperties)
{
switch (meType)
{
case SPRM:
if (mId != 0x0)
rProperties.sprm(*this);
break;
case ATTRIBUTE:
rProperties.attribute(mId, *getValue());
break;
}
}
/*
class OOXMLValue
*/
OOXMLValue::OOXMLValue()
{
}
OOXMLValue::~OOXMLValue()
{
}
int OOXMLValue::getInt() const
{
return 0;
}
OUString OOXMLValue::getString() const
{
return OUString();
}
uno::Any OOXMLValue::getAny() const
{
return uno::Any();
}
writerfilter::Reference<Properties>::Pointer_t OOXMLValue::getProperties()
{
return writerfilter::Reference<Properties>::Pointer_t();
}
writerfilter::Reference<BinaryObj>::Pointer_t OOXMLValue::getBinary()
{
return writerfilter::Reference<BinaryObj>::Pointer_t();
}
#ifdef DBG_UTIL
std::string OOXMLValue::toString() const
{
return "OOXMLValue";
}
#endif
OOXMLValue * OOXMLValue::clone() const
{
return new OOXMLValue(*this);
}
/*
class OOXMLBinaryValue
*/
OOXMLBinaryValue::OOXMLBinaryValue(OOXMLBinaryObjectReference::Pointer_t pBinaryObj)
: mpBinaryObj(std::move(pBinaryObj))
{
}
OOXMLBinaryValue::~OOXMLBinaryValue()
{
}
writerfilter::Reference<BinaryObj>::Pointer_t OOXMLBinaryValue::getBinary()
{
return mpBinaryObj;
}
#ifdef DBG_UTIL
std::string OOXMLBinaryValue::toString() const
{
return "BinaryObj";
}
#endif
OOXMLValue * OOXMLBinaryValue::clone() const
{
return new OOXMLBinaryValue(mpBinaryObj);
}
/*
class OOXMLBooleanValue
*/
static bool GetBooleanValue(std::string_view pValue)
{
return pValue == "true"
|| pValue == "True"
|| pValue == "1"
|| pValue == "on"
|| pValue == "On";
}
OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(bool bValue)
{
static OOXMLValue::Pointer_t False(new OOXMLBooleanValue (false));
static OOXMLValue::Pointer_t True(new OOXMLBooleanValue (true));
return bValue ? True : False;
}
OOXMLValue::Pointer_t const & OOXMLBooleanValue::Create(std::string_view pValue)
{
return Create (GetBooleanValue(pValue));
}
OOXMLBooleanValue::OOXMLBooleanValue(bool bValue)
: mbValue(bValue)
{
}
OOXMLBooleanValue::~OOXMLBooleanValue()
{
}
int OOXMLBooleanValue::getInt() const
{
return mbValue ? 1 : 0;
}
uno::Any OOXMLBooleanValue::getAny() const
{
return uno::Any(mbValue);
}
#ifdef DBG_UTIL
std::string OOXMLBooleanValue::toString() const
{
return mbValue ? "true" : "false";
}
#endif
OOXMLValue * OOXMLBooleanValue::clone() const
{
return new OOXMLBooleanValue(*this);
}
/*
class OOXMLStringValue
*/
OOXMLStringValue::OOXMLStringValue(OUString sStr)
: mStr(std::move(sStr))
{
}
OOXMLStringValue::~OOXMLStringValue()
{
}
uno::Any OOXMLStringValue::getAny() const
{
return uno::Any(mStr);
}
OUString OOXMLStringValue::getString() const
{
return mStr;
}
#ifdef DBG_UTIL
std::string OOXMLStringValue::toString() const
{
return std::string(OUStringToOString(mStr, RTL_TEXTENCODING_ASCII_US));
}
#endif
OOXMLValue * OOXMLStringValue::clone() const
{
return new OOXMLStringValue(*this);
}
/*
class OOXMLInputStreamValue
*/
OOXMLInputStreamValue::OOXMLInputStreamValue(uno::Reference<io::XInputStream> xInputStream)
: mxInputStream(std::move(xInputStream))
{
}
OOXMLInputStreamValue::~OOXMLInputStreamValue()
{
}
uno::Any OOXMLInputStreamValue::getAny() const
{
return uno::Any(mxInputStream);
}
#ifdef DBG_UTIL
std::string OOXMLInputStreamValue::toString() const
{
return "InputStream";
}
#endif
OOXMLValue * OOXMLInputStreamValue::clone() const
{
return new OOXMLInputStreamValue(mxInputStream);
}
/**
class OOXMLPropertySet
*/
OOXMLPropertySet::OOXMLPropertySet()
{
}
OOXMLPropertySet::~OOXMLPropertySet()
{
}
void OOXMLPropertySet::resolve(Properties & rHandler)
{
// The pProp->resolve(rHandler) call below can cause elements to
// be appended to mProperties. I don't think it can cause elements
// to be deleted. But let's check with < here just to be safe that
// the indexing below works.
for (size_t nIt = 0; nIt < mProperties.size(); ++nIt)
{
OOXMLProperty::Pointer_t pProp = mProperties[nIt];
if (pProp)
pProp->resolve(rHandler);
}
}
OOXMLPropertySet::OOXMLProperties_t::iterator OOXMLPropertySet::begin()
{
return mProperties.begin();
}
OOXMLPropertySet::OOXMLProperties_t::iterator OOXMLPropertySet::end()
{
return mProperties.end();
}
OOXMLPropertySet::OOXMLProperties_t::const_iterator
OOXMLPropertySet::begin() const
{
return mProperties.begin();
}
OOXMLPropertySet::OOXMLProperties_t::const_iterator
OOXMLPropertySet::end() const
{
return mProperties.end();
}
void OOXMLPropertySet::add(const OOXMLProperty::Pointer_t& pProperty)
{
if (pProperty && pProperty->getId() != 0x0)
{
mProperties.push_back(pProperty);
}
}
void OOXMLPropertySet::add(Id id, const OOXMLValue::Pointer_t& pValue, OOXMLProperty::Type_t eType)
{
OOXMLProperty::Pointer_t pProperty(new OOXMLProperty(id, pValue, eType));
add(pProperty);
}
void OOXMLPropertySet::add(const OOXMLPropertySet::Pointer_t& pPropertySet)
{
const OOXMLPropertySet * pSet = pPropertySet.get();
if (pSet != nullptr)
{
mProperties.insert( mProperties.end(), pSet->mProperties.begin(), pSet->mProperties.end() );
}
}
OOXMLPropertySet * OOXMLPropertySet::clone() const
{
return new OOXMLPropertySet(*this);
}
#ifdef DBG_UTIL
std::string OOXMLPropertySet::toString()
{
std::string sResult = "[";
char sBuffer[256];
snprintf(sBuffer, sizeof(sBuffer), "%p", this);
sResult += sBuffer;
sResult += ":";
OOXMLProperties_t::iterator aItBegin = begin();
OOXMLProperties_t::iterator aItEnd = end();
for (OOXMLProperties_t::iterator aIt = aItBegin; aIt != aItEnd; ++aIt)
{
if (aIt != aItBegin)
sResult += ", ";
if (*aIt)
sResult += (*aIt)->toString();
else
sResult += "0x0";
}
sResult += "]";
return sResult;
}
#endif
/*
class OOXMLPropertySetValue
*/
OOXMLPropertySetValue::OOXMLPropertySetValue(OOXMLPropertySet::Pointer_t pPropertySet)
: mpPropertySet(std::move(pPropertySet))
{
}
OOXMLPropertySetValue::~OOXMLPropertySetValue()
{
}
writerfilter::Reference<Properties>::Pointer_t OOXMLPropertySetValue::getProperties()
{
return writerfilter::Reference<Properties>::Pointer_t
(mpPropertySet->clone());
}
#ifdef DBG_UTIL
std::string OOXMLPropertySetValue::toString() const
{
char sBuffer[256];
snprintf(sBuffer, sizeof(sBuffer), "t:%p, m:%p", this, mpPropertySet.get());
return "OOXMLPropertySetValue(" + std::string(sBuffer) + ")";
}
#endif
OOXMLValue * OOXMLPropertySetValue::clone() const
{
return new OOXMLPropertySetValue(*this);
}
/*
class OOXMLIntegerValue
*/
OOXMLValue::Pointer_t OOXMLIntegerValue::Create(sal_Int32 nValue)
{
static OOXMLValue::Pointer_t Zero(new OOXMLIntegerValue (0));
static OOXMLValue::Pointer_t One(new OOXMLIntegerValue (1));
static OOXMLValue::Pointer_t Two(new OOXMLIntegerValue (2));
static OOXMLValue::Pointer_t Three(new OOXMLIntegerValue (3));
static OOXMLValue::Pointer_t Four(new OOXMLIntegerValue (4));
static OOXMLValue::Pointer_t Five(new OOXMLIntegerValue (5));
static OOXMLValue::Pointer_t Six(new OOXMLIntegerValue (6));
static OOXMLValue::Pointer_t Seven(new OOXMLIntegerValue (7));
static OOXMLValue::Pointer_t Eight(new OOXMLIntegerValue (8));
static OOXMLValue::Pointer_t Nine(new OOXMLIntegerValue (9));
switch (nValue) {
case 0: return Zero;
case 1: return One;
case 2: return Two;
case 3: return Three;
case 4: return Four;
case 5: return Five;
case 6: return Six;
case 7: return Seven;
case 8: return Eight;
case 9: return Nine;
default: break;
}
OOXMLValue::Pointer_t value(new OOXMLIntegerValue(nValue));
return value;
}
OOXMLIntegerValue::OOXMLIntegerValue(sal_Int32 nValue)
: mnValue(nValue)
{
}
OOXMLIntegerValue::~OOXMLIntegerValue()
{
}
int OOXMLIntegerValue::getInt() const
{
return mnValue;
}
uno::Any OOXMLIntegerValue::getAny() const
{
return uno::Any(mnValue);
}
OOXMLValue * OOXMLIntegerValue::clone() const
{
return new OOXMLIntegerValue(*this);
}
#ifdef DBG_UTIL
std::string OOXMLIntegerValue::toString() const
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "%" SAL_PRIdINT32, mnValue);
return buffer;
}
#endif
/*
class OOXMLHexValue
*/
OOXMLHexValue::OOXMLHexValue(sal_uInt32 nValue)
: mnValue(nValue)
{
}
OOXMLHexValue::OOXMLHexValue(std::string_view pValue)
: mnValue(o3tl::toUInt32(pValue, 16))
{
}
OOXMLHexValue::~OOXMLHexValue()
{
}
int OOXMLHexValue::getInt() const
{
return mnValue;
}
OOXMLValue * OOXMLHexValue::clone() const
{
return new OOXMLHexValue(*this);
}
#ifdef DBG_UTIL
std::string OOXMLHexValue::toString() const
{
char buffer[256];
snprintf(buffer, sizeof(buffer), "0x%" SAL_PRIxUINT32, mnValue);
return buffer;
}
#endif
/*
class OOXMLHexColorValue
*/
OOXMLHexColorValue::OOXMLHexColorValue(std::string_view pValue)
: OOXMLHexValue(sal_uInt32(COL_AUTO))
{
if (pValue == "auto")
return;
mnValue = o3tl::toUInt32(pValue, 16);
// Convert hash-encoded values (like #FF0080)
const sal_Int32 nLen = pValue.size();
if ( !mnValue && nLen > 1 && pValue[0] == '#' )
{
sal_Int32 nColor(COL_AUTO);
// Word appears to require strict 6 digit length, else it ignores it
if ( nLen == 7 )
{
const OUString sHashColor(pValue.data(), nLen, RTL_TEXTENCODING_ASCII_US);
sax::Converter::convertColor( nColor, sHashColor );
}
mnValue = nColor;
}
}
// OOXMLUniversalMeasureValue
// ECMA-376 5th ed. Part 1 , 22.9.2.15
OOXMLUniversalMeasureValue::OOXMLUniversalMeasureValue(std::string_view pValue, sal_uInt32 npPt)
{
double val = o3tl::toDouble(pValue); // will ignore the trailing unit
int nLen = pValue.size();
if (nLen > 2 &&
pValue[nLen-2] == 'p' &&
pValue[nLen-1] == 't')
{
mnValue = static_cast<int>(val * npPt);
}
else if (nLen > 2 &&
pValue[nLen - 2] == 'c' &&
pValue[nLen - 1] == 'm')
{
mnValue = static_cast<int>(val * npPt * 72 / 2.54);
}
else if (nLen > 2 &&
pValue[nLen - 2] == 'm' &&
pValue[nLen - 1] == 'm')
{
mnValue = static_cast<int>(val * npPt * 72 / 25.4);
}
else if (nLen > 2 &&
pValue[nLen - 2] == 'i' &&
pValue[nLen - 1] == 'n')
{
mnValue = static_cast<int>(val * npPt * 72);
}
else if (nLen > 2 &&
pValue[nLen - 2] == 'p' &&
( pValue[nLen - 1] == 'c' || pValue[nLen - 1] == 'i' ))
{
mnValue = static_cast<int>(val * npPt * 12);
}
else
{
mnValue = static_cast<int>(val);
}
}
OOXMLUniversalMeasureValue::~OOXMLUniversalMeasureValue()
{
}
int OOXMLUniversalMeasureValue::getInt() const
{
return mnValue;
}
#ifdef DBG_UTIL
std::string OOXMLUniversalMeasureValue::toString() const
{
return std::string(OString::number(mnValue));
}
#endif
// OOXMLMeasurementOrPercentValue
// ECMA-376 5th ed. Part 1 , 17.18.107; 17.18.11
OOXMLMeasurementOrPercentValue::OOXMLMeasurementOrPercentValue(std::string_view pValue)
{
double val = o3tl::toDouble(pValue); // will ignore the trailing unit
int nLen = pValue.size();
if (nLen > 1 &&
pValue[nLen - 1] == '%')
{
mnValue = static_cast<int>(val * 50);
}
else
{
mnValue = OOXMLTwipsMeasureValue(pValue).getInt();
}
}
int OOXMLMeasurementOrPercentValue::getInt() const
{
return mnValue;
}
#ifdef DBG_UTIL
std::string OOXMLMeasurementOrPercentValue::toString() const
{
return std::string(OString::number(mnValue));
}
#endif
/*
class OOXMLShapeValue
*/
OOXMLShapeValue::OOXMLShapeValue(uno::Reference<drawing::XShape> xShape)
: mrShape(std::move(xShape))
{
}
OOXMLShapeValue::~OOXMLShapeValue()
{
}
uno::Any OOXMLShapeValue::getAny() const
{
return uno::Any(mrShape);
}
#ifdef DBG_UTIL
std::string OOXMLShapeValue::toString() const
{
return "Shape";
}
#endif
OOXMLValue * OOXMLShapeValue::clone() const
{
return new OOXMLShapeValue(mrShape);
}
/*
class OOXMLStarMathValue
*/
OOXMLStarMathValue::OOXMLStarMathValue( uno::Reference< embed::XEmbeddedObject > c )
: m_component(std::move(c))
{
}
OOXMLStarMathValue::~OOXMLStarMathValue()
{
}
uno::Any OOXMLStarMathValue::getAny() const
{
return uno::Any(m_component);
}
#ifdef DBG_UTIL
std::string OOXMLStarMathValue::toString() const
{
return "StarMath";
}
#endif
OOXMLValue * OOXMLStarMathValue::clone() const
{
return new OOXMLStarMathValue( m_component );
}
/*
class OOXMLTableImpl
*/
OOXMLTable::OOXMLTable()
{
}
OOXMLTable::~OOXMLTable()
{
}
void OOXMLTable::resolve(Table & rTable)
{
Table * pTable = &rTable;
int nPos = 0;
for (const auto& rPropSet : mPropertySets)
{
writerfilter::Reference<Properties>::Pointer_t pProperties
(rPropSet->getProperties());
if (pProperties)
pTable->entry(nPos, pProperties);
++nPos;
}
}
void OOXMLTable::add(const ValuePointer_t& pPropertySet)
{
if (pPropertySet)
mPropertySets.push_back(pPropertySet);
}
OOXMLTable * OOXMLTable::clone() const
{
return new OOXMLTable(*this);
}
/*
class: OOXMLPropertySetEntryToString
*/
OOXMLPropertySetEntryToString::OOXMLPropertySetEntryToString(Id nId)
: mnId(nId)
{
}
OOXMLPropertySetEntryToString::~OOXMLPropertySetEntryToString()
{
}
void OOXMLPropertySetEntryToString::sprm(Sprm & /*rSprm*/)
{
}
void OOXMLPropertySetEntryToString::attribute(Id nId, Value & rValue)
{
if (nId == mnId)
mStr = rValue.getString();
}
/*
class: OOXMLPropertySetEntryToInteger
*/
OOXMLPropertySetEntryToInteger::OOXMLPropertySetEntryToInteger(Id nId)
: mnId(nId), mnValue(0)
{
}
OOXMLPropertySetEntryToInteger::~OOXMLPropertySetEntryToInteger()
{
}
void OOXMLPropertySetEntryToInteger::sprm(Sprm & /*rSprm*/)
{
}
void OOXMLPropertySetEntryToInteger::attribute(Id nId, Value & rValue)
{
if (nId == mnId)
mnValue = rValue.getInt();
}
/*
class: OOXMLPropertySetEntryToBool
*/
OOXMLPropertySetEntryToBool::OOXMLPropertySetEntryToBool(Id nId)
: mnId(nId), mValue(false)
{}
OOXMLPropertySetEntryToBool::~OOXMLPropertySetEntryToBool() {}
void OOXMLPropertySetEntryToBool::sprm(Sprm & /*rSprm*/) {}
void OOXMLPropertySetEntryToBool::attribute(Id nId, Value & rValue)
{
if (nId == mnId)
mValue = (rValue.getInt() != 0);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|