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>
26 #include <xmloff/nmspmap.hxx>
27 #include <xmloff/xmlnmspe.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 
36 #include <sax/tools/converter.hxx>
37 
38 #include <comphelper/sequence.hxx>
40 
41 using namespace com::sun::star;
42 using namespace ::xmloff::token;
43 
44 static void lcl_AddTwoDigits( OUStringBuffer& rStr, sal_Int32 nVal )
45 {
46  if ( nVal < 10 )
47  rStr.append( '0' );
48  rStr.append( nVal );
49 }
50 
51 OUString
52 SvXMLMetaExport::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 
72 void 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 
82 void 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 );
101  mrExport.Characters( ::utl::DocInfoHelper::GetGeneratorString() );
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  {
154  SvXMLElementExport aElem( mrExport,
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();
165  SvXMLElementExport aElem( mrExport,
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  {
178  mrExport.AddAttribute( XML_NAMESPACE_OFFICE, XML_TARGET_FRAME_NAME,
179  sDefTarget );
180 
182  const XMLTokenEnum eShow = sDefTarget == "_blank" ? XML_NEW : XML_REPLACE;
183  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_SHOW, eShow );
184 
185  SvXMLElementExport aElem( mrExport,
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  {
195  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF,
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));
201  mrExport.AddAttribute( XML_NAMESPACE_META, XML_DELAY,
202  buf.makeStringAndClear());
203 
205  true, false );
206  }
207 
208  // template
209  const OUString sTplPath = mxDocProps->getTemplateURL();
210  if ( !sTplPath.isEmpty() )
211  {
212  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TYPE, XML_SIMPLE );
213  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_ACTUATE, XML_ONREQUEST );
214 
215  // template URL
216  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_HREF,
217  mrExport.GetRelativeReference(sTplPath) );
218 
219  // template name
220  mrExport.AddAttribute( XML_NAMESPACE_XLINK, XML_TITLE,
221  mxDocProps->getTemplateName() );
222 
223  // template date
224  mrExport.AddAttribute( XML_NAMESPACE_META, XML_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  }
243  mrExport.AddAttribute( XML_NAMESPACE_META, XML_NAME, rProp.Name );
244  mrExport.AddAttribute( XML_NAMESPACE_META, XML_VALUE_TYPE,
245  sType.makeStringAndClear() );
246  SvXMLElementExport aElem( mrExport, XML_NAMESPACE_META,
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  {
256  for ( const auto& rDocStat : aDocStatistic )
257  {
258  sal_Int32 nValue = 0;
259  if ( rDocStat.Value >>= nValue )
260  {
261  OUString aValue = OUString::number( nValue );
262  if ( rDocStat.Name == "TableCount" )
263  mrExport.AddAttribute(
265  else if ( rDocStat.Name == "ObjectCount" )
266  mrExport.AddAttribute(
268  else if ( rDocStat.Name == "ImageCount" )
269  mrExport.AddAttribute(
271  else if ( rDocStat.Name == "PageCount" )
272  mrExport.AddAttribute(
274  else if ( rDocStat.Name == "ParagraphCount" )
275  mrExport.AddAttribute(
277  else if ( rDocStat.Name == "WordCount" )
278  mrExport.AddAttribute(
280  else if ( rDocStat.Name == "CharacterCount" )
281  mrExport.AddAttribute(
283  else if ( rDocStat.Name == "CellCount" )
284  mrExport.AddAttribute(
286  else
287  {
288  SAL_WARN("xmloff", "Unknown statistic value!");
289  }
290  }
291  }
292  SvXMLElementExport aElem( mrExport,
294  }
295 }
296 
297 static const char s_xmlns[] = "xmlns";
298 static const char s_xmlns2[] = "xmlns:";
299 static const char s_meta[] = "meta:";
300 static const 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  m_preservedNSs()
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;
323  const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap());
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:
347 void SAL_CALL
349 {
350  // ignore: has already been done by SvXMLExport::exportDoc
351  assert(m_level == 0 && "SvXMLMetaExport: level error");
352 }
353 
354 void 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.
362 void SAL_CALL
363 SvXMLMetaExport::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);
375  const SvXMLNamespaceMap & rNsMap(mrExport.GetNamespaceMap());
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 
443 void SAL_CALL
444 SvXMLMetaExport::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 
455 void SAL_CALL
456 SvXMLMetaExport::characters(const OUString & i_rChars)
457 {
458  mrExport.Characters(i_rChars);
459 }
460 
461 void SAL_CALL
462 SvXMLMetaExport::ignorableWhitespace(const OUString & /*i_rWhitespaces*/)
463 {
464  mrExport.IgnorableWhitespace(/*i_rWhitespaces*/);
465 }
466 
467 void SAL_CALL
469  const OUString &)
470 {
471  // ignore; the exporter cannot handle these
472 }
473 
474 void SAL_CALL
475 SvXMLMetaExport::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: */
virtual void SAL_CALL startDocument() override
Definition: xmlmetae.cxx:348
static const char s_xmlns2[]
Definition: xmlmetae.cxx:298
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &i_xLocator) override
Definition: xmlmetae.cxx:475
void MExport_()
currently unused; for exporting via the XDocumentProperties interface
Definition: xmlmetae.cxx:95
tuple ns
void Export()
export via XSAXWriter interface, with fallback to _MExport
Definition: xmlmetae.cxx:317
static const char s_xmlns[]
Definition: xmlmetae.cxx:297
static void lcl_AddTwoDigits(OUStringBuffer &rStr, sal_Int32 nVal)
Definition: xmlmetae.cxx:44
static const char s_href[]
Definition: xmlmetae.cxx:300
static bool convertAny(OUStringBuffer &rsValue, OUStringBuffer &rsType, const css::uno::Any &rValue)
constexpr sal_uInt16 XML_NAMESPACE_DC
Definition: xmlnmspe.hxx:36
void SimpleStringElement(const OUString &rText, sal_uInt16 nNamespace, enum::xmloff::token::XMLTokenEnum eElementName)
Definition: xmlmetae.cxx:72
virtual void SAL_CALL characters(const OUString &i_rChars) override
Definition: xmlmetae.cxx:456
int nCount
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:926
void StartElement(sal_uInt16 nPrefix, enum::xmloff::token::XMLTokenEnum eName, bool bIgnWSOutside)
Definition: xmlexp.cxx:2128
static OUString GetGeneratorString()
OUString GetRelativeReference(const OUString &rValue)
Definition: xmlexp.cxx:2094
void SimpleDateTimeElement(const css::util::DateTime &rDate, sal_uInt16 nNamespace, enum::xmloff::token::XMLTokenEnum eElementName)
Definition: xmlmetae.cxx:82
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:47
SvXMLExport & mrExport
Definition: xmlmetae.hxx:54
SvXMLMetaExport(SvXMLExport &i_rExport, const css::uno::Reference< css::document::XDocumentProperties > &i_rDocProps)
Definition: xmlmetae.cxx:302
OptionalString sType
int i
virtual void SAL_CALL processingInstruction(const OUString &i_rTarget, const OUString &i_rData) override
Definition: xmlmetae.cxx:468
virtual ~SvXMLMetaExport() override
Definition: xmlmetae.cxx:313
virtual void SAL_CALL ignorableWhitespace(const OUString &i_rWhitespaces) override
Definition: xmlmetae.cxx:462
dictionary props
static void convertDuration(OUStringBuffer &rBuffer, const double fTime)
static const char s_meta[]
Definition: xmlmetae.cxx:299
const SvXMLNamespaceMap & GetNamespaceMap() const
Definition: xmlexp.hxx:392
int m_level
counts levels of the xml document. necessary for special handling.
Definition: xmlmetae.hxx:57
constexpr sal_uInt16 XML_NAMESPACE_XLINK
Definition: xmlnmspe.hxx:35
void IgnorableWhitespace()
Definition: xmlexp.cxx:2224
std::vector< css::beans::StringPair > m_preservedNSs
preserved namespaces. necessary because we do not write the root node.
Definition: xmlmetae.hxx:59
void EndElement(sal_uInt16 nPrefix, enum::xmloff::token::XMLTokenEnum eName, bool bIgnWSInside)
Definition: xmlexp.cxx:2185
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Handling of tokens in XML:
css::uno::Reference< css::document::XDocumentProperties > mxDocProps
Definition: xmlmetae.hxx:55
void Characters(const OUString &rChars)
Definition: xmlexp.cxx:2163
virtual void SAL_CALL endElement(const OUString &i_rName) override
Definition: xmlmetae.cxx:444
const char * name
Any value
constexpr sal_uInt16 XML_NAMESPACE_META
Definition: xmlnmspe.hxx:37
static OUString GetISODateTimeString(const css::util::DateTime &rDateTime)
Definition: xmlmetae.cxx:52
#define SAL_WARN(area, stream)
sal_Int32 nLength
Definition: xmltoken.cxx:36
virtual void SAL_CALL endDocument() override
Definition: xmlmetae.cxx:355
virtual void SAL_CALL startElement(const OUString &i_rName, const css::uno::Reference< css::xml::sax::XAttributeList > &i_xAttribs) override
Definition: xmlmetae.cxx:363
sal_Int16 nValue
constexpr sal_uInt16 XML_NAMESPACE_OFFICE
Definition: xmlnmspe.hxx:29