LibreOffice Module xmloff (master) 1
xmlmetae.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
21#include <rtl/ustrbuf.hxx>
22#include <sal/log.hxx>
23
24#include <utility>
25#include <xmloff/xmlmetae.hxx>
26#include <xmloff/xmlexp.hxx>
29
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>
36
38
41
42using namespace com::sun::star;
43using namespace ::xmloff::token;
44
45static void lcl_AddTwoDigits( OUStringBuffer& rStr, sal_Int32 nVal )
46{
47 if ( nVal < 10 )
48 rStr.append( '0' );
49 rStr.append( nVal );
50}
51
52OUString
53SvXMLMetaExport::GetISODateTimeString( const util::DateTime& rDateTime )
54{
55 // return ISO date string "YYYY-MM-DDThh:mm:ss"
56
57 OUStringBuffer sTmp =
58 OUString::number( static_cast<sal_Int32>(rDateTime.Year) )
59 + "-";
60 lcl_AddTwoDigits( sTmp, rDateTime.Month );
61 sTmp.append( '-' );
62 lcl_AddTwoDigits( sTmp, rDateTime.Day );
63 sTmp.append( 'T' );
64 lcl_AddTwoDigits( sTmp, rDateTime.Hours );
65 sTmp.append( ':' );
66 lcl_AddTwoDigits( sTmp, rDateTime.Minutes );
67 sTmp.append( ':' );
68 lcl_AddTwoDigits( sTmp, rDateTime.Seconds );
69
70 return sTmp.makeStringAndClear();
71}
72
73void SvXMLMetaExport::SimpleStringElement( const OUString& rText,
74 sal_uInt16 nNamespace, enum XMLTokenEnum eElementName )
75{
76 if ( !rText.isEmpty() ) {
77 SvXMLElementExport aElem( mrExport, nNamespace, eElementName,
78 true, false );
79 mrExport.Characters( rText );
80 }
81}
82
83void SvXMLMetaExport::SimpleDateTimeElement( const util::DateTime & rDate,
84 sal_uInt16 nNamespace, enum XMLTokenEnum eElementName )
85{
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,
90 true, false );
91 mrExport.Characters( sValue );
92 }
93 }
94}
95
97{
98 // generator
99 {
101 true, true );
103 }
104
105 // document title
106 SimpleStringElement ( mxDocProps->getTitle(),
108
109 // description
110 SimpleStringElement ( mxDocProps->getDescription(),
112
113 // subject
114 SimpleStringElement ( mxDocProps->getSubject(),
116
117 // created...
118 SimpleStringElement ( mxDocProps->getAuthor(),
120 SimpleDateTimeElement( mxDocProps->getCreationDate(),
122
123 // modified...
124 SimpleStringElement ( mxDocProps->getModifiedBy(),
126 SimpleDateTimeElement( mxDocProps->getModificationDate(),
128
129 // printed...
130 SimpleStringElement ( mxDocProps->getPrintedBy(),
132 SimpleDateTimeElement( mxDocProps->getPrintDate(),
134
135 // keywords
136 const uno::Sequence< OUString > keywords = mxDocProps->getKeywords();
137 for (const auto& rKeyword : keywords) {
139 true, false );
140 mrExport.Characters( rKeyword );
141 }
142
143 // document language
144 {
145 OUString sValue = LanguageTag( mxDocProps->getLanguage()).getBcp47( false);
146 if (!sValue.isEmpty()) {
148 true, false );
149 mrExport.Characters( sValue );
150 }
151 }
152
153 // editing cycles
154 {
157 true, false );
158 mrExport.Characters( OUString::number(
159 mxDocProps->getEditingCycles() ) );
160 }
161
162 // editing duration
163 // property is a int32 (seconds)
164 {
165 sal_Int32 secs = mxDocProps->getEditingDuration();
168 true, false );
169 OUStringBuffer buf;
170 ::sax::Converter::convertDuration(buf, util::Duration(
171 false, 0, 0, 0, secs/3600, (secs%3600)/60, secs%60, 0));
172 mrExport.Characters(buf.makeStringAndClear());
173 }
174
175 // default target
176 const OUString sDefTarget = mxDocProps->getDefaultTarget();
177 if ( !sDefTarget.isEmpty() )
178 {
180 sDefTarget );
181
183 const XMLTokenEnum eShow = sDefTarget == "_blank" ? XML_NEW : XML_REPLACE;
185
188 true, false );
189 }
190
191 // auto-reload
192 const OUString sReloadURL = mxDocProps->getAutoloadURL();
193 const sal_Int32 sReloadDelay = mxDocProps->getAutoloadSecs();
194 if (sReloadDelay != 0 || !sReloadURL.isEmpty())
195 {
197 mrExport.GetRelativeReference( sReloadURL ) );
198
199 OUStringBuffer buf;
200 ::sax::Converter::convertDuration(buf, util::Duration(false, 0, 0, 0,
201 sReloadDelay/3600, (sReloadDelay%3600)/60, sReloadDelay%60, 0));
203 buf.makeStringAndClear());
204
206 true, false );
207 }
208
209 // template
210 const OUString sTplPath = mxDocProps->getTemplateURL();
211 if ( !sTplPath.isEmpty() )
212 {
215
216 // template URL
218 mrExport.GetRelativeReference(sTplPath) );
219
220 // template name
222 mxDocProps->getTemplateName() );
223
224 // template date
226 GetISODateTimeString( mxDocProps->getTemplateDate() ) );
227
229 true, false );
230 }
231
232 // user defined fields
233 uno::Reference< beans::XPropertyAccess > xUserDefined(
234 mxDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
235 const uno::Sequence< beans::PropertyValue > props =
236 xUserDefined->getPropertyValues();
237 for (const auto& rProp : props) {
238 OUStringBuffer sValueBuffer;
239 OUStringBuffer sType;
240 if (!::sax::Converter::convertAny(sValueBuffer, sType, rProp.Value))
241 {
242 continue;
243 }
246 sType.makeStringAndClear() );
248 XML_USER_DEFINED, true, false );
249 mrExport.Characters( sValueBuffer.makeStringAndClear() );
250 }
251
252 const uno::Sequence< beans::NamedValue > aDocStatistic =
253 mxDocProps->getDocumentStatistics();
254 // write document statistic if there is any provided
255 if ( !aDocStatistic.hasElements() )
256 return;
257
258 for ( const auto& rDocStat : aDocStatistic )
259 {
260 sal_Int32 nValue = 0;
261 if ( rDocStat.Value >>= nValue )
262 {
263 OUString aValue = OUString::number( nValue );
264 if ( rDocStat.Name == "TableCount" )
267 else if ( rDocStat.Name == "ObjectCount" )
270 else if ( rDocStat.Name == "ImageCount" )
273 else if ( rDocStat.Name == "PageCount" )
276 else if ( rDocStat.Name == "ParagraphCount" )
279 else if ( rDocStat.Name == "WordCount" )
282 else if ( rDocStat.Name == "CharacterCount" )
285 else if ( rDocStat.Name == "CellCount" )
288 else
289 {
290 SAL_WARN("xmloff", "Unknown statistic value!");
291 }
292 }
293 }
296}
297
298const char s_xmlns[] = "xmlns";
299const char s_xmlns2[] = "xmlns:";
300const char s_meta[] = "meta:";
301const char s_href[] = "xlink:href";
302
304 SvXMLExport& i_rExp,
305 uno::Reference<document::XDocumentProperties> i_rDocProps ) :
306 mrExport( i_rExp ),
307 mxDocProps(std::move( i_rDocProps )),
308 m_level( 0 )
309{
310 assert(mxDocProps.is());
311}
312
314{
315}
316
318{
319 uno::Reference< xml::sax::XSAXSerializable> xSAXable(mxDocProps,
320 uno::UNO_QUERY);
321 if (xSAXable.is()) {
322 ::std::vector< beans::StringPair > namespaces;
324 for (sal_uInt16 key = rNsMap.GetFirstKey();
325 key != USHRT_MAX; key = rNsMap.GetNextKey(key)) {
326 beans::StringPair ns;
327 const OUString attrname = rNsMap.GetAttrNameByKey(key);
328 if (!attrname.startsWith(s_xmlns2, &ns.First)
329 || attrname == s_xmlns) // default initialized empty string
330 {
331 assert(!"namespace attribute not starting with xmlns unexpected");
332 }
333 ns.Second = rNsMap.GetNameByKey(key);
334 namespaces.push_back(ns);
335 }
336 xSAXable->serialize(this, comphelper::containerToSequence(namespaces));
337 } else {
338 // office:meta
340 true, true );
341 // fall back to using public interface of XDocumentProperties
342 MExport_();
343 }
344}
345
346// css::xml::sax::XDocumentHandler:
347void SAL_CALL
349{
350 // ignore: has already been done by SvXMLExport::exportDoc
351 assert(m_level == 0 && "SvXMLMetaExport: level error");
352}
353
354void SAL_CALL
356{
357 // ignore: will be done by SvXMLExport::exportDoc
358 assert(m_level == 0 && "SvXMLMetaExport: level error");
359}
360
361// unfortunately, this method contains far too much ugly namespace mangling.
362void SAL_CALL
363SvXMLMetaExport::startElement(const OUString & i_rName,
364 const uno::Reference< xml::sax::XAttributeList > & i_xAttribs)
365{
366
367 if (m_level == 0) {
368 // namespace decls: default ones have been written at the root element
369 // non-default ones must be preserved here
370 const sal_Int16 nCount = i_xAttribs->getLength();
371 for (sal_Int16 i = 0; i < nCount; ++i) {
372 const OUString name(i_xAttribs->getNameByIndex(i));
373 if (name.startsWith(s_xmlns)) {
374 bool found(false);
376 for (sal_uInt16 key = rNsMap.GetFirstKey();
377 key != USHRT_MAX; key = rNsMap.GetNextKey(key)) {
378 if (name == rNsMap.GetAttrNameByKey(key)) {
379 found = true;
380 break;
381 }
382 }
383 if (!found) {
384 m_preservedNSs.emplace_back(name,
385 i_xAttribs->getValueByIndex(i));
386 }
387 }
388 }
389 // ignore the root: it has been written already
390 ++m_level;
391 return;
392 }
393
394 if (m_level == 1) {
395 // attach preserved namespace decls from root node here
396 for (const auto& rPreservedNS : m_preservedNSs) {
397 const OUString ns(rPreservedNS.First);
398 bool found(false);
399 // but only if it is not already there
400 const sal_Int16 nCount = i_xAttribs->getLength();
401 for (sal_Int16 i = 0; i < nCount; ++i) {
402 const OUString name(i_xAttribs->getNameByIndex(i));
403 if (ns == name) {
404 found = true;
405 break;
406 }
407 }
408 if (!found) {
409 mrExport.AddAttribute(ns, rPreservedNS.Second);
410 }
411 }
412 }
413
414 // attach the attributes
415 if (i_rName.startsWith(s_meta)) {
416 // special handling for all elements that may have
417 // xlink:href attributes; these must be made relative
418 const sal_Int16 nLength = i_xAttribs->getLength();
419 for (sal_Int16 i = 0; i < nLength; ++i) {
420 const OUString name (i_xAttribs->getNameByIndex (i));
421 OUString value(i_xAttribs->getValueByIndex(i));
422 if (name.startsWith(s_href)) {
424 }
426 }
427 } else {
428 const sal_Int16 nLength = i_xAttribs->getLength();
429 for (sal_Int16 i = 0; i < nLength; ++i) {
430 const OUString name (i_xAttribs->getNameByIndex(i));
431 const OUString value (i_xAttribs->getValueByIndex(i));
433 }
434 }
435
436 // finally, start the element
437 // #i107240# no whitespace here, because the DOM may already contain
438 // whitespace, which is not cleared when loading and thus accumulates.
439 mrExport.StartElement(i_rName, m_level <= 1);
440 ++m_level;
441}
442
443void SAL_CALL
444SvXMLMetaExport::endElement(const OUString & i_rName)
445{
446 --m_level;
447 if (m_level == 0) {
448 // ignore the root; see startElement
449 return;
450 }
451 assert(m_level >= 0 && "SvXMLMetaExport: level error");
452 mrExport.EndElement(i_rName, false);
453}
454
455void SAL_CALL
456SvXMLMetaExport::characters(const OUString & i_rChars)
457{
458 mrExport.Characters(i_rChars);
459}
460
461void SAL_CALL
462SvXMLMetaExport::ignorableWhitespace(const OUString & /*i_rWhitespaces*/)
463{
464 mrExport.IgnorableWhitespace(/*i_rWhitespaces*/);
465}
466
467void SAL_CALL
469 const OUString &)
470{
471 // ignore; the exporter cannot handle these
472}
473
474void SAL_CALL
475SvXMLMetaExport::setDocumentLocator(const uno::Reference<xml::sax::XLocator>&)
476{
477 // nothing to do here, move along...
478}
479
480/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sType
const OUString & getBcp47(bool bResolveSystem=true) const
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:385
OUString GetRelativeReference(const OUString &rValue)
Definition: xmlexp.cxx:2057
void StartElement(sal_uInt16 nPrefix, enum ::xmloff::token::XMLTokenEnum eName, bool bIgnWSOutside)
Definition: xmlexp.cxx:2091
void AddAttribute(sal_uInt16 nPrefix, const OUString &rName, const OUString &rValue)
Definition: xmlexp.cxx:907
void Characters(const OUString &rChars)
Definition: xmlexp.cxx:2126
void IgnorableWhitespace()
Definition: xmlexp.cxx:2187
void EndElement(sal_uInt16 nPrefix, enum ::xmloff::token::XMLTokenEnum eName, bool bIgnWSInside)
Definition: xmlexp.cxx:2148
void SimpleStringElement(const OUString &rText, sal_uInt16 nNamespace, enum ::xmloff::token::XMLTokenEnum eElementName)
Definition: xmlmetae.cxx:73
virtual void SAL_CALL startElement(const OUString &i_rName, const css::uno::Reference< css::xml::sax::XAttributeList > &i_xAttribs) override
Definition: xmlmetae.cxx:363
virtual void SAL_CALL endElement(const OUString &i_rName) override
Definition: xmlmetae.cxx:444
SvXMLExport & mrExport
Definition: xmlmetae.hxx:53
void Export()
export via XSAXWriter interface, with fallback to _MExport
Definition: xmlmetae.cxx:317
virtual void SAL_CALL processingInstruction(const OUString &i_rTarget, const OUString &i_rData) override
Definition: xmlmetae.cxx:468
virtual void SAL_CALL characters(const OUString &i_rChars) override
Definition: xmlmetae.cxx:456
virtual ~SvXMLMetaExport() override
Definition: xmlmetae.cxx:313
void MExport_()
currently unused; for exporting via the XDocumentProperties interface
Definition: xmlmetae.cxx:96
virtual void SAL_CALL endDocument() override
Definition: xmlmetae.cxx:355
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
Definition: xmlmetae.hxx:54
void SimpleDateTimeElement(const css::util::DateTime &rDate, sal_uInt16 nNamespace, enum ::xmloff::token::XMLTokenEnum eElementName)
Definition: xmlmetae.cxx:83
std::vector< css::beans::StringPair > m_preservedNSs
preserved namespaces. necessary because we do not write the root node.
Definition: xmlmetae.hxx:58
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &i_xLocator) override
Definition: xmlmetae.cxx:475
int m_level
counts levels of the xml document. necessary for special handling.
Definition: xmlmetae.hxx:56
static OUString GetISODateTimeString(const css::util::DateTime &rDateTime)
Definition: xmlmetae.cxx:53
virtual void SAL_CALL ignorableWhitespace(const OUString &i_rWhitespaces) override
Definition: xmlmetae.cxx:462
virtual void SAL_CALL startDocument() override
Definition: xmlmetae.cxx:348
SvXMLMetaExport(SvXMLExport &i_rExport, css::uno::Reference< css::document::XDocumentProperties > i_xDocProps)
Definition: xmlmetae.cxx:303
const OUString & GetNameByKey(sal_uInt16 nKey) const
sal_uInt16 GetFirstKey() const
OUString GetAttrNameByKey(sal_uInt16 nKey) const
sal_uInt16 GetNextKey(sal_uInt16 nOldKey) const
static void convertDuration(OUStringBuffer &rBuffer, const double fTime)
static bool convertAny(OUStringBuffer &rsValue, OUStringBuffer &rsType, const css::uno::Any &rValue)
static OUString GetGeneratorString()
Any value
int nCount
sal_Int16 nValue
const char * name
#define SAL_WARN(area, stream)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
ns
int i
dictionary props
Handling of tokens in XML:
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:50
@ XML_DOCUMENT_STATISTIC
Definition: xmltoken.hxx:705
@ XML_EDITING_DURATION
Definition: xmltoken.hxx:731
@ XML_HYPERLINK_BEHAVIOUR
Definition: xmltoken.hxx:1050
OReadStatusBarDocumentHandler::StatusBar_XML_Namespace nNamespace
const char s_meta[]
Definition: xmlmetae.cxx:300
const char s_xmlns[]
Definition: xmlmetae.cxx:298
static void lcl_AddTwoDigits(OUStringBuffer &rStr, sal_Int32 nVal)
Definition: xmlmetae.cxx:45
const char s_href[]
Definition: xmlmetae.cxx:301
const char s_xmlns2[]
Definition: xmlmetae.cxx:299
constexpr sal_uInt16 XML_NAMESPACE_META
constexpr sal_uInt16 XML_NAMESPACE_DC
constexpr sal_uInt16 XML_NAMESPACE_XLINK
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
sal_Int32 nLength
Definition: xmltoken.cxx:38