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