LibreOffice Module xmloff (master)  1
xmlversion.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 
20 #include <com/sun/star/embed/ElementModes.hpp>
21 #include <xmlversion.hxx>
22 #include <xmloff/xmlnmspe.hxx>
23 #include <xmloff/xmlmetae.hxx>
24 #include <osl/diagnose.h>
25 #include <sal/log.hxx>
26 
27 #include <xmloff/xmltoken.hxx>
29 #include <com/sun/star/beans/XPropertySet.hpp>
30 #include <com/sun/star/io/IOException.hpp>
31 #include <com/sun/star/io/XOutputStream.hpp>
32 #include <com/sun/star/util/DateTime.hpp>
33 #include <com/sun/star/util/MeasureUnit.hpp>
34 #include <com/sun/star/xml/sax/InputSource.hpp>
35 #include <com/sun/star/xml/sax/Writer.hpp>
36 #include <com/sun/star/xml/sax/SAXParseException.hpp>
38 
39 using namespace ::com::sun::star::xml::sax;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star;
42 
43 const char XMLN_VERSIONSLIST[] = "VersionList.xml";
44 
46  const css::uno::Reference< css::uno::XComponentContext >& rContext,
47  const css::uno::Sequence < css::util::RevisionTag >& rVersions,
48  const OUString &rFileName,
49  Reference< XDocumentHandler > const &rHandler )
50 : SvXMLExport( rContext, "", rFileName, util::MeasureUnit::CM, rHandler ),
51  maVersions( rVersions )
52 {
57 }
58 
60 {
61  GetDocHandler()->startDocument();
62 
64 
66 
67  AddAttribute( XML_NAMESPACE_NONE, GetNamespaceMap_().GetAttrNameByIndex( nPos ),
68  GetNamespaceMap_().GetNameByIndex ( nPos ) );
69 
71  AddAttribute( XML_NAMESPACE_NONE, GetNamespaceMap_().GetAttrNameByIndex( nPos ),
72  GetNamespaceMap_().GetNameByIndex ( nPos ) );
73 
74  {
75  // the following object will write all collected attributes in its dtor
77 
78  for ( const util::RevisionTag& rInfo : maVersions )
79  {
82  rInfo.Identifier );
85  rInfo.Comment );
88  rInfo.Author );
89 
90  OUString aDateStr =
91  SvXMLMetaExport::GetISODateTimeString( rInfo.TimeStamp );
92 
94 
95  // the following object will write all collected attributes in its dtor
97  }
98  }
99  GetDocHandler()->endDocument();
100  return ERRCODE_NONE;
101 }
102 
104  const css::uno::Reference< css::uno::XComponentContext >& rContext,
105  css::uno::Sequence < css::util::RevisionTag >& rVersions )
106 : SvXMLImport(rContext, ""),
107  maVersions( rVersions )
108 {
109 }
110 
112 {}
113 
115  const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& /*xAttrList*/ )
116 {
117  SvXMLImportContext *pContext = nullptr;
118 
119  if ( nElement == XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_VERSION_LIST) )
120  {
121  pContext = new XMLVersionListContext( *this );
122  }
123 
124  return pContext;
125 }
126 
128  : SvXMLImportContext( rImport )
129 {
130 }
131 
133 {}
134 
135 css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL
137  const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList)
138 {
139  SvXMLImportContext *pContext = nullptr;
140 
141  if ( nElement == XML_ELEMENT(FRAMEWORK, xmloff::token::XML_VERSION_ENTRY)
142  || nElement == XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_VERSION_ENTRY) )
143  {
144  pContext = new XMLVersionContext( GetImport(), xAttrList );
145  }
146 
147  return pContext;
148 }
149 
151  const Reference< XFastAttributeList > & xAttrList )
152  : SvXMLImportContext( rImport )
153 {
154  sax_fastparser::FastAttributeList& rAttribList =
156  if ( rAttribList.getFastAttributeTokens().empty() )
157  return;
158  util::RevisionTag aInfo;
159  for (auto &aIter : rAttribList)
160  {
161  switch( aIter.getToken() )
162  {
163  case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_TITLE):
164  case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_TITLE):
165  {
166  aInfo.Identifier = aIter.toString();
167  break;
168  }
169  case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_COMMENT):
170  case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_COMMENT):
171  {
172  aInfo.Comment = aIter.toString();
173  break;
174  }
175  case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_CREATOR):
176  case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_CREATOR):
177  {
178  aInfo.Author = aIter.toString();
179  break;
180  }
182  {
183  util::DateTime aTime;
184  if ( ParseISODateTimeString( aIter.toString(), aTime ) )
185  aInfo.TimeStamp = aTime;
186  break;
187  }
188  default:
189  SAL_WARN("xmloff", "unknown attribute " << SvXMLImport::getPrefixAndNameFromToken(aIter.getToken()) << "=" << aIter.toString());
190  break;
191  }
192  }
193 
194  uno::Sequence < util::RevisionTag >& aList = rImport.GetList();
195  sal_Int32 nLength = aList.getLength();
196  aList.realloc( nLength+1 );
197  aList[nLength] = aInfo;
198 }
199 
201 {}
202 
204  const OUString& rString,
205  util::DateTime& rDateTime )
206 {
207  bool bSuccess = true;
208 
209  OUString aDateStr, aTimeStr;
210  sal_Int32 nPos = rString.indexOf( 'T' );
211  if ( nPos >= 0 )
212  {
213  aDateStr = rString.copy( 0, nPos );
214  aTimeStr = rString.copy( nPos + 1 );
215  }
216  else
217  aDateStr = rString; // no separator: only date part
218 
219  sal_Int32 nYear = 0;
220  sal_Int32 nMonth = 1;
221  sal_Int32 nDay = 1;
222  sal_Int32 nHour = 0;
223  sal_Int32 nMin = 0;
224  sal_Int32 nSec = 0;
225 
226  const sal_Unicode* pStr = aDateStr.getStr();
227  sal_Int32 nDateTokens = 1;
228  while ( *pStr )
229  {
230  if ( *pStr == '-' )
231  nDateTokens++;
232  pStr++;
233  }
234  if ( nDateTokens > 3 || aDateStr.isEmpty() )
235  bSuccess = false;
236  else
237  {
238  sal_Int32 n = 0;
239  nYear = aDateStr.getToken( 0, '-', n ).toInt32();
240  if ( nYear > 9999 )
241  bSuccess = false;
242  else if ( nDateTokens >= 2 )
243  {
244  nMonth = aDateStr.getToken( 0, '-', n ).toInt32();
245  if ( nMonth > 12 )
246  bSuccess = false;
247  else if ( nDateTokens >= 3 )
248  {
249  nDay = aDateStr.getToken( 0, '-', n ).toInt32();
250  if ( nDay > 31 )
251  bSuccess = false;
252  }
253  }
254  }
255 
256  if ( bSuccess && !aTimeStr.isEmpty() ) // time is optional
257  {
258  pStr = aTimeStr.getStr();
259  sal_Int32 nTimeTokens = 1;
260  while ( *pStr )
261  {
262  if ( *pStr == ':' )
263  nTimeTokens++;
264  pStr++;
265  }
266  if ( nTimeTokens > 3 )
267  bSuccess = false;
268  else
269  {
270  sal_Int32 n = 0;
271  nHour = aTimeStr.getToken( 0, ':', n ).toInt32();
272  if ( nHour > 23 )
273  bSuccess = false;
274  else if ( nTimeTokens >= 2 )
275  {
276  nMin = aTimeStr.getToken( 0, ':', n ).toInt32();
277  if ( nMin > 59 )
278  bSuccess = false;
279  else if ( nTimeTokens >= 3 )
280  {
281  nSec = aTimeStr.getToken( 0, ':', n ).toInt32();
282  if ( nSec > 59 )
283  bSuccess = false;
284  }
285  }
286  }
287  }
288 
289  if ( bSuccess )
290  {
291  rDateTime.Day = sal::static_int_cast< sal_uInt16 >(nDay);
292  rDateTime.Month = sal::static_int_cast< sal_uInt16 >(nMonth);
293  rDateTime.Year = sal::static_int_cast< sal_uInt16 >(nYear);
294  rDateTime.Hours = sal::static_int_cast< sal_uInt16 >(nHour);
295  rDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(nMin);
296  rDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(nSec);
297  }
298 
299  return bSuccess;
300 }
301 
302 void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions )
303 {
304  // no storage, no version list!
305  if ( xRoot.is() )
306  {
307  // get the services needed for writing the xml data
310 
311  Reference< XWriter > xWriter = Writer::create(xContext);
312 
313  // check whether there's already a sub storage with the version info
314  // and delete it
315  OUString sVerName( XMLN_VERSIONSLIST );
316 
317  try {
318  // open (create) the sub storage with the version info
319  uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement(
320  sVerName,
321  embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
322  if ( !xVerStream.is() )
323  throw uno::RuntimeException();
324 
325  Reference< io::XOutputStream > xOut = xVerStream->getOutputStream();
326  if ( !xOut.is() )
327  throw uno::RuntimeException(); // the stream was successfully opened for writing already
328 
329  xWriter->setOutputStream(xOut);
330 
331  rtl::Reference< XMLVersionListExport > xExp( new XMLVersionListExport( xContext, rVersions, sVerName, xWriter ) );
332 
333  xExp->exportDoc( ::xmloff::token::XML_VERSION );
334 
335  xVerStream.clear(); // use refcounting for now to dispose
336  }
337  catch( uno::Exception& )
338  {
339  // TODO: error handling
340  }
341  }
342 }
343 
344 uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot )
345 {
346  css::uno::Sequence < css::util::RevisionTag > aVersions;
347 
348  const OUString sDocName( XMLN_VERSIONSLIST );
349 
350  try {
351  if ( xRoot.is() && xRoot->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) )
352  {
354 
355  InputSource aParserInput;
356 
357  uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY );
358  OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!" );
359  if ( xProps.is() )
360  {
361  try {
362  xProps->getPropertyValue("URL") >>= aParserInput.sSystemId;
363  }
364  catch( uno::Exception& )
365  {}
366  }
367 
368  uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement(
369  sDocName,
370  embed::ElementModes::READ );
371  if ( !xDocStream.is() )
372  throw uno::RuntimeException();
373 
374  aParserInput.aInputStream = xDocStream->getInputStream();
375  OSL_ENSURE( aParserInput.aInputStream.is(),
376  "The stream was successfully opened for reading, the input part must be accessible!" );
377  if ( !aParserInput.aInputStream.is() )
378  throw uno::RuntimeException();
379 
380  // get filter
381  rtl::Reference< XMLVersionListImport > xImport = new XMLVersionListImport( xContext, aVersions );
382 
383  // parse
384  try
385  {
386  xImport->parseStream( aParserInput );
387  }
388  catch( SAXParseException& ) {}
389  catch( SAXException& ) {}
390  catch( io::IOException& ) {}
391  }
392  }
393  catch( uno::Exception& )
394  {
395  // TODO: error handling
396  }
397 
398  return aVersions;
399 }
400 
402 {
403  return "XMLVersionListPersistence";
404 }
405 
407  OUString const & ServiceName)
408 {
409  return cppu::supportsService(this, ServiceName);
410 }
411 
412 css::uno::Sequence<OUString>
414 {
415  return css::uno::Sequence<OUString>{
416  "com.sun.star.document.DocumentRevisionListPersistence"};
417 }
418 
419 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
421  css::uno::XComponentContext *,
422  css::uno::Sequence<css::uno::Any> const &)
423 {
424  return cppu::acquire(new XMLVersionListPersistence);
425 }
426 
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static sal_uInt16 GetIndexByKey(sal_uInt16 nKey)
Definition: nmspmap.cxx:374
XMLVersionListImport(const css::uno::Reference< css::uno::XComponentContext > &rContext, css::uno::Sequence< css::util::RevisionTag > &rVersions)
Definition: xmlversion.cxx:103
static bool ParseISODateTimeString(const OUString &rString, css::util::DateTime &rDateTime)
Definition: xmlversion.cxx:203
void addChaffWhenEncryptedStorage()
Definition: xmlexp.cxx:1222
sal_Int64 n
const sal_uInt16 XML_NAMESPACE_NONE
Definition: nmspmap.hxx:37
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
XMLVersionListExport(const css::uno::Reference< css::uno::XComponentContext > &rContext, const css::uno::Sequence< css::util::RevisionTag > &rVersions, const OUString &rFileName, css::uno::Reference< css::xml::sax::XDocumentHandler > const &rHandler)
Definition: xmlversion.cxx:45
constexpr sal_uInt16 XML_NAMESPACE_DC
Definition: xmlnmspe.hxx:36
const css::uno::Reference< css::xml::sax::XDocumentHandler > & GetDocHandler() const
Definition: xmlexp.hxx:386
XMLVersionContext(XMLVersionListImport &rImport, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Definition: xmlversion.cxx:150
sal_uInt16 sal_Unicode
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
void AddAttribute(sal_uInt16 nPrefix, const char *pName, const OUString &rValue)
Definition: xmlexp.cxx:926
OUString SAL_CALL getImplementationName() override
Definition: xmlversion.cxx:401
static OUString getPrefixAndNameFromToken(sal_Int32 nToken)
Definition: xmlimp.cxx:2020
XMLTokenEnum
The enumeration of all XML tokens.
Definition: xmltoken.hxx:47
ErrCode exportDoc(enum::xmloff::token::XMLTokenEnum eClass=::xmloff::token::XML_TOKEN_INVALID) override
Definition: xmlversion.cxx:59
css::uno::Sequence< css::util::RevisionTag > & GetList()
Definition: xmlversion.hxx:66
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttribs) override
Definition: xmlversion.cxx:136
virtual ~XMLVersionContext() override
Definition: xmlversion.cxx:200
unsigned char sal_Bool
XMLVersionListImport & GetImport()
Definition: xmlversion.hxx:72
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: xmlversion.cxx:413
virtual void SAL_CALL store(const css::uno::Reference< css::embed::XStorage > &Storage, const css::uno::Sequence< css::util::RevisionTag > &List) override
Definition: xmlversion.cxx:302
SvXMLNamespaceMap & GetNamespaceMap_()
Definition: xmlexp.hxx:181
#define ERRCODE_NONE
const css::uno::Sequence< css::util::RevisionTag > & maVersions
Definition: xmlversion.hxx:36
const OUString & GetXMLToken(enum XMLTokenEnum eToken)
return the OUString representation for eToken
Definition: xmltoken.cxx:3366
const char XMLN_VERSIONSLIST[]
Definition: xmlversion.cxx:43
sal_Bool SAL_CALL supportsService(OUString const &ServiceName) override
Definition: xmlversion.cxx:406
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:94
Reference< XComponentContext > getProcessComponentContext()
static OUString GetISODateTimeString(const css::util::DateTime &rDateTime)
Definition: xmlmetae.cxx:52
virtual SvXMLImportContext * CreateFastContext(sal_Int32 Element, const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList > &xAttrList) override
Definition: xmlversion.cxx:114
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * XMLVersionListPersistence_get_implementation(css::uno::XComponentContext *, css::uno::Sequence< css::uno::Any > const &)
Definition: xmlversion.cxx:420
#define SAL_WARN(area, stream)
void AddAtIndex(const OUString &rPrefix, const OUString &rName, sal_uInt16 nKey)
Definition: nmspmap.cxx:389
sal_Int32 nLength
Definition: xmltoken.cxx:36
const std::vector< sal_Int32 > & getFastAttributeTokens() const
virtual css::uno::Sequence< css::util::RevisionTag > SAL_CALL load(const css::uno::Reference< css::embed::XStorage > &Storage) override
Definition: xmlversion.cxx:344
virtual ~XMLVersionListImport() override
Definition: xmlversion.cxx:111
constexpr sal_uInt16 XML_NAMESPACE_FRAMEWORK
Definition: xmlnmspe.hxx:67
virtual ~XMLVersionListContext() override
Definition: xmlversion.cxx:132
sal_uInt16 nPos
XMLVersionListContext(XMLVersionListImport &rImport)
Definition: xmlversion.cxx:127