tdf#164793: fix misplaced rounding
[LibreOffice.git] / xmloff / source / meta / xmlversion.cxx
blob85ace1eaa4b27a084b5ce4c30ac8f8ed99c644c1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <com/sun/star/embed/ElementModes.hpp>
21 #include <xmlversion.hxx>
22 #include <xmloff/xmlnamespace.hxx>
23 #include <xmloff/xmlmetae.hxx>
24 #include <osl/diagnose.h>
25 #include <sal/log.hxx>
26 #include <o3tl/string_view.hxx>
28 #include <xmloff/xmltoken.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <com/sun/star/io/IOException.hpp>
32 #include <com/sun/star/io/XOutputStream.hpp>
33 #include <com/sun/star/util/DateTime.hpp>
34 #include <com/sun/star/util/MeasureUnit.hpp>
35 #include <com/sun/star/xml/sax/InputSource.hpp>
36 #include <com/sun/star/xml/sax/Writer.hpp>
37 #include <com/sun/star/xml/sax/SAXParseException.hpp>
38 #include <cppuhelper/supportsservice.hxx>
40 using namespace ::com::sun::star::xml::sax;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star;
44 constexpr OUString XMLN_VERSIONSLIST = u"VersionList.xml"_ustr;
46 XMLVersionListExport::XMLVersionListExport(
47 const css::uno::Reference< css::uno::XComponentContext >& rContext,
48 const css::uno::Sequence < css::util::RevisionTag >& rVersions,
49 const OUString &rFileName,
50 Reference< XDocumentHandler > const &rHandler )
51 : SvXMLExport( rContext, u""_ustr, rFileName, util::MeasureUnit::CM, rHandler ),
52 maVersions( rVersions )
54 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC),
55 xmloff::token::GetXMLToken(xmloff::token::XML_N_DC), XML_NAMESPACE_DC );
56 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
57 xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
60 ErrCode XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum )
62 GetDocHandler()->startDocument();
64 addChaffWhenEncryptedStorage();
66 sal_uInt16 nPos = SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_DC );
68 AddAttribute( XML_NAMESPACE_NONE, GetNamespaceMap_().GetAttrNameByIndex( nPos ),
69 GetNamespaceMap_().GetNameByIndex ( nPos ) );
71 nPos = SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_FRAMEWORK );
72 AddAttribute( XML_NAMESPACE_NONE, GetNamespaceMap_().GetAttrNameByIndex( nPos ),
73 GetNamespaceMap_().GetNameByIndex ( nPos ) );
76 // the following object will write all collected attributes in its dtor
77 SvXMLElementExport aRoot( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_LIST, true, true );
79 for ( const util::RevisionTag& rInfo : maVersions )
81 AddAttribute( XML_NAMESPACE_FRAMEWORK,
82 xmloff::token::XML_TITLE,
83 rInfo.Identifier );
84 AddAttribute( XML_NAMESPACE_FRAMEWORK,
85 xmloff::token::XML_COMMENT,
86 rInfo.Comment );
87 AddAttribute( XML_NAMESPACE_FRAMEWORK,
88 xmloff::token::XML_CREATOR,
89 rInfo.Author );
91 OUString aDateStr =
92 SvXMLMetaExport::GetISODateTimeString( rInfo.TimeStamp );
94 AddAttribute( XML_NAMESPACE_DC, xmloff::token::XML_DATE_TIME, aDateStr );
96 // the following object will write all collected attributes in its dtor
97 SvXMLElementExport aEntry( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_ENTRY, true, true );
100 GetDocHandler()->endDocument();
101 return ERRCODE_NONE;
104 XMLVersionListImport::XMLVersionListImport(
105 const css::uno::Reference< css::uno::XComponentContext >& rContext,
106 css::uno::Sequence < css::util::RevisionTag >& rVersions )
107 : SvXMLImport(rContext, u""_ustr),
108 maVersions( rVersions )
112 XMLVersionListImport::~XMLVersionListImport() noexcept
115 SvXMLImportContext *XMLVersionListImport::CreateFastContext( sal_Int32 nElement,
116 const ::css::uno::Reference< ::css::xml::sax::XFastAttributeList >& /*xAttrList*/ )
118 SvXMLImportContext *pContext = nullptr;
120 if ( nElement == XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_VERSION_LIST) )
122 pContext = new XMLVersionListContext( *this );
125 return pContext;
128 XMLVersionListContext::XMLVersionListContext( XMLVersionListImport& rImport)
129 : SvXMLImportContext( rImport )
133 XMLVersionListContext::~XMLVersionListContext()
136 css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL
137 XMLVersionListContext::createFastChildContext(sal_Int32 nElement,
138 const css::uno::Reference< css::xml::sax::XFastAttributeList > & xAttrList)
140 SvXMLImportContext *pContext = nullptr;
142 if ( nElement == XML_ELEMENT(FRAMEWORK, xmloff::token::XML_VERSION_ENTRY)
143 || nElement == XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_VERSION_ENTRY) )
145 pContext = new XMLVersionContext( GetImport(), xAttrList );
148 return pContext;
151 XMLVersionContext::XMLVersionContext( XMLVersionListImport& rImport,
152 const Reference< XFastAttributeList > & xAttrList )
153 : SvXMLImportContext( rImport )
155 sax_fastparser::FastAttributeList& rAttribList =
156 sax_fastparser::castToFastAttributeList( xAttrList );
157 if ( rAttribList.getFastAttributeTokens().empty() )
158 return;
159 util::RevisionTag aInfo;
160 for (auto &aIter : rAttribList)
162 switch( aIter.getToken() )
164 case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_TITLE):
165 case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_TITLE):
167 aInfo.Identifier = aIter.toString();
168 break;
170 case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_COMMENT):
171 case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_COMMENT):
173 aInfo.Comment = aIter.toString();
174 break;
176 case XML_ELEMENT(FRAMEWORK, xmloff::token::XML_CREATOR):
177 case XML_ELEMENT(VERSIONS_LIST, xmloff::token::XML_CREATOR):
179 aInfo.Author = aIter.toString();
180 break;
182 case XML_ELEMENT(DC, xmloff::token::XML_DATE_TIME):
184 util::DateTime aTime;
185 if ( ParseISODateTimeString( aIter.toString(), aTime ) )
186 aInfo.TimeStamp = aTime;
187 break;
189 default:
190 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
191 break;
195 uno::Sequence < util::RevisionTag >& aList = rImport.GetList();
196 sal_Int32 nLength = aList.getLength();
197 aList.realloc( nLength+1 );
198 aList.getArray()[nLength] = std::move(aInfo);
201 XMLVersionContext::~XMLVersionContext()
204 bool XMLVersionContext::ParseISODateTimeString(
205 std::u16string_view rString,
206 util::DateTime& rDateTime )
208 bool bSuccess = true;
210 std::u16string_view aDateStr, aTimeStr;
211 size_t nPos = rString.find( 'T' );
212 if ( nPos != std::u16string_view::npos )
214 aDateStr = rString.substr( 0, nPos );
215 aTimeStr = rString.substr( nPos + 1 );
217 else
218 aDateStr = rString; // no separator: only date part
220 sal_Int32 nYear = 0;
221 sal_Int32 nMonth = 1;
222 sal_Int32 nDay = 1;
223 sal_Int32 nHour = 0;
224 sal_Int32 nMin = 0;
225 sal_Int32 nSec = 0;
227 auto pStr = aDateStr.begin();
228 sal_Int32 nDateTokens = 1;
229 while ( pStr != aDateStr.end() )
231 if ( *pStr == '-' )
232 nDateTokens++;
233 pStr++;
235 if ( nDateTokens > 3 || aDateStr.empty() )
236 bSuccess = false;
237 else
239 sal_Int32 n = 0;
240 nYear = o3tl::toInt32(o3tl::getToken(aDateStr, 0, '-', n ));
241 if ( nYear > 9999 )
242 bSuccess = false;
243 else if ( nDateTokens >= 2 )
245 nMonth = o3tl::toInt32(o3tl::getToken(aDateStr, 0, '-', n ));
246 if ( nMonth > 12 )
247 bSuccess = false;
248 else if ( nDateTokens >= 3 )
250 nDay = o3tl::toInt32(o3tl::getToken(aDateStr, 0, '-', n ));
251 if ( nDay > 31 )
252 bSuccess = false;
257 if ( bSuccess && !aTimeStr.empty() ) // time is optional
259 pStr = aTimeStr.begin();
260 sal_Int32 nTimeTokens = 1;
261 while ( pStr != aTimeStr.end() )
263 if ( *pStr == ':' )
264 nTimeTokens++;
265 pStr++;
267 if ( nTimeTokens > 3 )
268 bSuccess = false;
269 else
271 sal_Int32 n = 0;
272 nHour = o3tl::toInt32(o3tl::getToken(aTimeStr, 0, ':', n ));
273 if ( nHour > 23 )
274 bSuccess = false;
275 else if ( nTimeTokens >= 2 )
277 nMin = o3tl::toInt32(o3tl::getToken(aTimeStr, 0, ':', n ));
278 if ( nMin > 59 )
279 bSuccess = false;
280 else if ( nTimeTokens >= 3 )
282 nSec = o3tl::toInt32(o3tl::getToken(aTimeStr, 0, ':', n ));
283 if ( nSec > 59 )
284 bSuccess = false;
290 if ( bSuccess )
292 rDateTime.Day = sal::static_int_cast< sal_uInt16 >(nDay);
293 rDateTime.Month = sal::static_int_cast< sal_uInt16 >(nMonth);
294 rDateTime.Year = sal::static_int_cast< sal_uInt16 >(nYear);
295 rDateTime.Hours = sal::static_int_cast< sal_uInt16 >(nHour);
296 rDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(nMin);
297 rDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(nSec);
300 return bSuccess;
303 void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions )
305 // no storage, no version list!
306 if ( !xRoot.is() )
307 return;
309 // get the services needed for writing the xml data
310 const Reference< uno::XComponentContext >& xContext =
311 comphelper::getProcessComponentContext();
313 Reference< XWriter > xWriter = Writer::create(xContext);
315 // check whether there's already a sub storage with the version info
316 // and delete it
317 OUString sVerName( XMLN_VERSIONSLIST );
319 try {
320 // open (create) the sub storage with the version info
321 uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement(
322 sVerName,
323 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
324 if ( !xVerStream.is() )
325 throw uno::RuntimeException();
327 Reference< io::XOutputStream > xOut = xVerStream->getOutputStream();
328 if ( !xOut.is() )
329 throw uno::RuntimeException(u"The stream was successfully opened for writing already!"_ustr);
331 xWriter->setOutputStream(xOut);
333 rtl::Reference< XMLVersionListExport > xExp( new XMLVersionListExport( xContext, rVersions, sVerName, xWriter ) );
335 xExp->exportDoc( ::xmloff::token::XML_VERSION );
337 xVerStream.clear(); // use refcounting for now to dispose
339 catch( uno::Exception& )
341 // TODO: error handling
345 uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot )
347 css::uno::Sequence < css::util::RevisionTag > aVersions;
349 const OUString sDocName( XMLN_VERSIONSLIST );
351 try {
352 if ( xRoot.is() && xRoot->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) )
354 const Reference< uno::XComponentContext >& xContext = comphelper::getProcessComponentContext();
356 InputSource aParserInput;
358 uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY );
359 OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!" );
360 if ( xProps.is() )
362 try {
363 xProps->getPropertyValue(u"URL"_ustr) >>= aParserInput.sSystemId;
365 catch( uno::Exception& )
369 uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement(
370 sDocName,
371 embed::ElementModes::READ );
372 if ( !xDocStream.is() )
373 throw uno::RuntimeException();
375 aParserInput.aInputStream = xDocStream->getInputStream();
376 OSL_ENSURE( aParserInput.aInputStream.is(),
377 "The stream was successfully opened for reading, the input part must be accessible!" );
378 if ( !aParserInput.aInputStream.is() )
379 throw uno::RuntimeException();
381 // get filter
382 rtl::Reference< XMLVersionListImport > xImport = new XMLVersionListImport( xContext, aVersions );
384 // parse
387 xImport->parseStream( aParserInput );
389 catch( SAXParseException& ) {}
390 catch( SAXException& ) {}
391 catch( io::IOException& ) {}
394 catch( uno::Exception& )
396 // TODO: error handling
399 return aVersions;
402 OUString XMLVersionListPersistence::getImplementationName()
404 return u"XMLVersionListPersistence"_ustr;
407 sal_Bool XMLVersionListPersistence::supportsService(
408 OUString const & ServiceName)
410 return cppu::supportsService(this, ServiceName);
413 css::uno::Sequence<OUString>
414 XMLVersionListPersistence::getSupportedServiceNames()
416 return css::uno::Sequence<OUString>{
417 u"com.sun.star.document.DocumentRevisionListPersistence"_ustr};
420 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
421 XMLVersionListPersistence_get_implementation(
422 css::uno::XComponentContext *,
423 css::uno::Sequence<css::uno::Any> const &)
425 return cppu::acquire(new XMLVersionListPersistence);
428 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */