1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <i18nlangtag/languagetag.hxx>
21 #include <rtl/ustrbuf.hxx>
22 #include <sal/log.hxx>
25 #include <xmloff/xmlmetae.hxx>
26 #include <xmloff/xmlexp.hxx>
27 #include <xmloff/namespacemap.hxx>
28 #include <xmloff/xmlnamespace.hxx>
30 #include <com/sun/star/beans/XPropertyAccess.hpp>
31 #include <com/sun/star/beans/StringPair.hpp>
32 #include <com/sun/star/document/XDocumentProperties.hpp>
33 #include <com/sun/star/util/DateTime.hpp>
34 #include <com/sun/star/util/Duration.hpp>
35 #include <com/sun/star/xml/sax/XSAXSerializable.hpp>
37 #include <sax/tools/converter.hxx>
39 #include <comphelper/sequence.hxx>
40 #include <unotools/docinfohelper.hxx>
42 using namespace com::sun::star
;
43 using namespace ::xmloff::token
;
45 static void lcl_AddTwoDigits( OUStringBuffer
& rStr
, sal_Int32 nVal
)
53 SvXMLMetaExport::GetISODateTimeString( const util::DateTime
& rDateTime
)
55 // return ISO date string "YYYY-MM-DDThh:mm:ss"
58 OUString::number( static_cast<sal_Int32
>(rDateTime
.Year
) )
60 lcl_AddTwoDigits( sTmp
, rDateTime
.Month
);
62 lcl_AddTwoDigits( sTmp
, rDateTime
.Day
);
64 lcl_AddTwoDigits( sTmp
, rDateTime
.Hours
);
66 lcl_AddTwoDigits( sTmp
, rDateTime
.Minutes
);
68 lcl_AddTwoDigits( sTmp
, rDateTime
.Seconds
);
70 return sTmp
.makeStringAndClear();
73 void SvXMLMetaExport::SimpleStringElement( const OUString
& rText
,
74 sal_uInt16 nNamespace
, enum XMLTokenEnum eElementName
)
76 if ( !rText
.isEmpty() ) {
77 SvXMLElementExport
aElem( mrExport
, nNamespace
, eElementName
,
79 mrExport
.Characters( rText
);
83 void SvXMLMetaExport::SimpleDateTimeElement( const util::DateTime
& rDate
,
84 sal_uInt16 nNamespace
, enum XMLTokenEnum eElementName
)
86 if (rDate
.Month
!= 0) { // invalid dates are 0-0-0
87 OUString sValue
= GetISODateTimeString( rDate
);
88 if ( !sValue
.isEmpty() ) {
89 SvXMLElementExport
aElem( mrExport
, nNamespace
, eElementName
,
91 mrExport
.Characters( sValue
);
96 void SvXMLMetaExport::MExport_()
98 bool bRemovePersonalInfo
= SvtSecurityOptions::IsOptionSet(SvtSecurityOptions::EOption::DocWarnRemovePersonalInfo
);
99 bool bRemoveUserInfo
= bRemovePersonalInfo
&& !SvtSecurityOptions::IsOptionSet(SvtSecurityOptions::EOption::DocWarnKeepDocUserInfo
);
103 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_META
, XML_GENERATOR
,
105 mrExport
.Characters( ::utl::DocInfoHelper::GetGeneratorString() );
109 SimpleStringElement ( mxDocProps
->getTitle(),
110 XML_NAMESPACE_DC
, XML_TITLE
);
113 SimpleStringElement ( mxDocProps
->getDescription(),
114 XML_NAMESPACE_DC
, XML_DESCRIPTION
);
117 SimpleStringElement ( mxDocProps
->getSubject(),
118 XML_NAMESPACE_DC
, XML_SUBJECT
);
121 if (!bRemoveUserInfo
)
123 SimpleStringElement(mxDocProps
->getAuthor(), XML_NAMESPACE_META
, XML_INITIAL_CREATOR
);
124 SimpleDateTimeElement(mxDocProps
->getCreationDate(), XML_NAMESPACE_META
, XML_CREATION_DATE
);
127 SimpleStringElement(mxDocProps
->getModifiedBy(), XML_NAMESPACE_DC
, XML_CREATOR
);
128 SimpleDateTimeElement(mxDocProps
->getModificationDate(), XML_NAMESPACE_DC
, XML_DATE
);
131 SimpleStringElement(mxDocProps
->getPrintedBy(), XML_NAMESPACE_META
, XML_PRINTED_BY
);
132 SimpleDateTimeElement(mxDocProps
->getPrintDate(), XML_NAMESPACE_META
, XML_PRINT_DATE
);
136 const uno::Sequence
< OUString
> keywords
= mxDocProps
->getKeywords();
137 for (const auto& rKeyword
: keywords
) {
138 SvXMLElementExport
aKwElem( mrExport
, XML_NAMESPACE_META
, XML_KEYWORD
,
140 mrExport
.Characters( rKeyword
);
145 OUString sValue
= LanguageTag( mxDocProps
->getLanguage()).getBcp47( false);
146 if (!sValue
.isEmpty()) {
147 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_DC
, XML_LANGUAGE
,
149 mrExport
.Characters( sValue
);
154 if (!bRemovePersonalInfo
)
156 SvXMLElementExport
aElem( mrExport
,
157 XML_NAMESPACE_META
, XML_EDITING_CYCLES
,
159 mrExport
.Characters( OUString::number(
160 mxDocProps
->getEditingCycles() ) );
164 // property is a int32 (seconds)
165 if (!bRemovePersonalInfo
)
167 sal_Int32 secs
= mxDocProps
->getEditingDuration();
168 SvXMLElementExport
aElem( mrExport
,
169 XML_NAMESPACE_META
, XML_EDITING_DURATION
,
172 ::sax::Converter::convertDuration(buf
, util::Duration(
173 false, 0, 0, 0, secs
/3600, (secs
%3600)/60, secs
%60, 0));
174 mrExport
.Characters(buf
.makeStringAndClear());
178 const OUString sDefTarget
= mxDocProps
->getDefaultTarget();
179 if ( !sDefTarget
.isEmpty() )
181 mrExport
.AddAttribute( XML_NAMESPACE_OFFICE
, XML_TARGET_FRAME_NAME
,
184 //! define strings for xlink:show values
185 const XMLTokenEnum eShow
= sDefTarget
== "_blank" ? XML_NEW
: XML_REPLACE
;
186 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_SHOW
, eShow
);
188 SvXMLElementExport
aElem( mrExport
,
189 XML_NAMESPACE_META
,XML_HYPERLINK_BEHAVIOUR
,
194 const OUString sReloadURL
= mxDocProps
->getAutoloadURL();
195 const sal_Int32 sReloadDelay
= mxDocProps
->getAutoloadSecs();
196 if (sReloadDelay
!= 0 || !sReloadURL
.isEmpty())
198 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_HREF
,
199 mrExport
.GetRelativeReference( sReloadURL
) );
202 ::sax::Converter::convertDuration(buf
, util::Duration(false, 0, 0, 0,
203 sReloadDelay
/3600, (sReloadDelay
%3600)/60, sReloadDelay
%60, 0));
204 mrExport
.AddAttribute( XML_NAMESPACE_META
, XML_DELAY
,
205 buf
.makeStringAndClear());
207 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_META
, XML_AUTO_RELOAD
,
212 const OUString sTplPath
= mxDocProps
->getTemplateURL();
213 if ( !bRemovePersonalInfo
&& !sTplPath
.isEmpty() )
215 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_TYPE
, XML_SIMPLE
);
216 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_ACTUATE
, XML_ONREQUEST
);
219 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_HREF
,
220 mrExport
.GetRelativeReference(sTplPath
) );
223 mrExport
.AddAttribute( XML_NAMESPACE_XLINK
, XML_TITLE
,
224 mxDocProps
->getTemplateName() );
227 mrExport
.AddAttribute( XML_NAMESPACE_META
, XML_DATE
,
228 GetISODateTimeString( mxDocProps
->getTemplateDate() ) );
230 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_META
, XML_TEMPLATE
,
234 // user defined fields
235 uno::Reference
< beans::XPropertyAccess
> xUserDefined(
236 mxDocProps
->getUserDefinedProperties(), uno::UNO_QUERY_THROW
);
237 const uno::Sequence
< beans::PropertyValue
> props
=
238 xUserDefined
->getPropertyValues();
239 for (const auto& rProp
: props
) {
240 OUStringBuffer sValueBuffer
;
241 OUStringBuffer sType
;
242 if (!::sax::Converter::convertAny(sValueBuffer
, sType
, rProp
.Value
))
246 mrExport
.AddAttribute( XML_NAMESPACE_META
, XML_NAME
, rProp
.Name
);
247 mrExport
.AddAttribute( XML_NAMESPACE_META
, XML_VALUE_TYPE
,
248 sType
.makeStringAndClear() );
249 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_META
,
250 XML_USER_DEFINED
, true, false );
251 mrExport
.Characters( sValueBuffer
.makeStringAndClear() );
254 const uno::Sequence
< beans::NamedValue
> aDocStatistic
=
255 mxDocProps
->getDocumentStatistics();
256 // write document statistic if there is any provided
257 if ( !aDocStatistic
.hasElements() )
260 for ( const auto& rDocStat
: aDocStatistic
)
262 sal_Int32 nValue
= 0;
263 if ( rDocStat
.Value
>>= nValue
)
265 OUString aValue
= OUString::number( nValue
);
266 if ( rDocStat
.Name
== "TableCount" )
267 mrExport
.AddAttribute(
268 XML_NAMESPACE_META
, XML_TABLE_COUNT
, aValue
);
269 else if ( rDocStat
.Name
== "ObjectCount" )
270 mrExport
.AddAttribute(
271 XML_NAMESPACE_META
, XML_OBJECT_COUNT
, aValue
);
272 else if ( rDocStat
.Name
== "ImageCount" )
273 mrExport
.AddAttribute(
274 XML_NAMESPACE_META
, XML_IMAGE_COUNT
, aValue
);
275 else if ( rDocStat
.Name
== "PageCount" )
276 mrExport
.AddAttribute(
277 XML_NAMESPACE_META
, XML_PAGE_COUNT
, aValue
);
278 else if ( rDocStat
.Name
== "ParagraphCount" )
279 mrExport
.AddAttribute(
280 XML_NAMESPACE_META
, XML_PARAGRAPH_COUNT
, aValue
);
281 else if ( rDocStat
.Name
== "WordCount" )
282 mrExport
.AddAttribute(
283 XML_NAMESPACE_META
, XML_WORD_COUNT
, aValue
);
284 else if ( rDocStat
.Name
== "CharacterCount" )
285 mrExport
.AddAttribute(
286 XML_NAMESPACE_META
, XML_CHARACTER_COUNT
, aValue
);
287 else if ( rDocStat
.Name
== "CellCount" )
288 mrExport
.AddAttribute(
289 XML_NAMESPACE_META
, XML_CELL_COUNT
, aValue
);
292 SAL_WARN("xmloff", "Unknown statistic value!");
296 SvXMLElementExport
aElem( mrExport
,
297 XML_NAMESPACE_META
, XML_DOCUMENT_STATISTIC
, true, true );
300 const char s_xmlns
[] = "xmlns";
301 const char s_xmlns2
[] = "xmlns:";
302 const char s_meta
[] = "meta:";
303 const char s_href
[] = "xlink:href";
305 SvXMLMetaExport::SvXMLMetaExport(
307 uno::Reference
<document::XDocumentProperties
> i_rDocProps
) :
309 mxDocProps(std::move( i_rDocProps
)),
312 assert(mxDocProps
.is());
315 SvXMLMetaExport::~SvXMLMetaExport()
319 void SvXMLMetaExport::Export()
321 uno::Reference
< xml::sax::XSAXSerializable
> xSAXable(mxDocProps
,
323 bool bRemovePersonalInfo
324 = SvtSecurityOptions::IsOptionSet(SvtSecurityOptions::EOption::DocWarnRemovePersonalInfo
);
325 if (xSAXable
.is() && !bRemovePersonalInfo
) {
326 ::std::vector
< beans::StringPair
> namespaces
;
327 const SvXMLNamespaceMap
& rNsMap(mrExport
.GetNamespaceMap());
328 for (sal_uInt16 key
= rNsMap
.GetFirstKey();
329 key
!= USHRT_MAX
; key
= rNsMap
.GetNextKey(key
)) {
330 beans::StringPair ns
;
331 const OUString attrname
= rNsMap
.GetAttrNameByKey(key
);
332 if (!attrname
.startsWith(s_xmlns2
, &ns
.First
)
333 || attrname
== s_xmlns
) // default initialized empty string
335 assert(!"namespace attribute not starting with xmlns unexpected");
337 ns
.Second
= rNsMap
.GetNameByKey(key
);
338 namespaces
.push_back(ns
);
340 xSAXable
->serialize(this, comphelper::containerToSequence(namespaces
));
343 SvXMLElementExport
aElem( mrExport
, XML_NAMESPACE_OFFICE
, XML_META
,
345 // fall back to using public interface of XDocumentProperties
350 // css::xml::sax::XDocumentHandler:
352 SvXMLMetaExport::startDocument()
354 // ignore: has already been done by SvXMLExport::exportDoc
355 assert(m_level
== 0 && "SvXMLMetaExport: level error");
359 SvXMLMetaExport::endDocument()
361 // ignore: will be done by SvXMLExport::exportDoc
362 assert(m_level
== 0 && "SvXMLMetaExport: level error");
365 // unfortunately, this method contains far too much ugly namespace mangling.
367 SvXMLMetaExport::startElement(const OUString
& i_rName
,
368 const uno::Reference
< xml::sax::XAttributeList
> & i_xAttribs
)
372 // namespace decls: default ones have been written at the root element
373 // non-default ones must be preserved here
374 const sal_Int16 nCount
= i_xAttribs
->getLength();
375 for (sal_Int16 i
= 0; i
< nCount
; ++i
) {
376 const OUString
name(i_xAttribs
->getNameByIndex(i
));
377 if (name
.startsWith(s_xmlns
)) {
379 const SvXMLNamespaceMap
& rNsMap(mrExport
.GetNamespaceMap());
380 for (sal_uInt16 key
= rNsMap
.GetFirstKey();
381 key
!= USHRT_MAX
; key
= rNsMap
.GetNextKey(key
)) {
382 if (name
== rNsMap
.GetAttrNameByKey(key
)) {
388 m_preservedNSs
.emplace_back(name
,
389 i_xAttribs
->getValueByIndex(i
));
393 // ignore the root: it has been written already
399 // attach preserved namespace decls from root node here
400 for (const auto& rPreservedNS
: m_preservedNSs
) {
401 const OUString
ns(rPreservedNS
.First
);
403 // but only if it is not already there
404 const sal_Int16 nCount
= i_xAttribs
->getLength();
405 for (sal_Int16 i
= 0; i
< nCount
; ++i
) {
406 const OUString
name(i_xAttribs
->getNameByIndex(i
));
413 mrExport
.AddAttribute(ns
, rPreservedNS
.Second
);
418 // attach the attributes
419 if (i_rName
.startsWith(s_meta
)) {
420 // special handling for all elements that may have
421 // xlink:href attributes; these must be made relative
422 const sal_Int16 nLength
= i_xAttribs
->getLength();
423 for (sal_Int16 i
= 0; i
< nLength
; ++i
) {
424 const OUString
name (i_xAttribs
->getNameByIndex (i
));
425 OUString
value(i_xAttribs
->getValueByIndex(i
));
426 if (name
.startsWith(s_href
)) {
427 value
= mrExport
.GetRelativeReference(value
);
429 mrExport
.AddAttribute(name
, value
);
432 const sal_Int16 nLength
= i_xAttribs
->getLength();
433 for (sal_Int16 i
= 0; i
< nLength
; ++i
) {
434 const OUString
name (i_xAttribs
->getNameByIndex(i
));
435 const OUString
value (i_xAttribs
->getValueByIndex(i
));
436 mrExport
.AddAttribute(name
, value
);
440 // finally, start the element
441 // #i107240# no whitespace here, because the DOM may already contain
442 // whitespace, which is not cleared when loading and thus accumulates.
443 mrExport
.StartElement(i_rName
, m_level
<= 1);
448 SvXMLMetaExport::endElement(const OUString
& i_rName
)
452 // ignore the root; see startElement
455 assert(m_level
>= 0 && "SvXMLMetaExport: level error");
456 mrExport
.EndElement(i_rName
, false);
460 SvXMLMetaExport::characters(const OUString
& i_rChars
)
462 mrExport
.Characters(i_rChars
);
466 SvXMLMetaExport::ignorableWhitespace(const OUString
& /*i_rWhitespaces*/)
468 mrExport
.IgnorableWhitespace(/*i_rWhitespaces*/);
472 SvXMLMetaExport::processingInstruction(const OUString
&,
475 // ignore; the exporter cannot handle these
479 SvXMLMetaExport::setDocumentLocator(const uno::Reference
<xml::sax::XLocator
>&)
481 // nothing to do here, move along...
484 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */