1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <comphelper/string.hxx>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/io/IOException.hpp>
33 #include <com/sun/star/io/XOutputStream.hpp>
34 #include <com/sun/star/util/DateTime.hpp>
35 #include <com/sun/star/util/MeasureUnit.hpp>
36 #include <com/sun/star/xml/sax/InputSource.hpp>
37 #include <com/sun/star/xml/sax/Writer.hpp>
38 #include <com/sun/star/xml/sax/SAXParseException.hpp>
39 #include <cppuhelper/supportsservice.hxx>
41 using namespace ::com::sun::star::xml::sax
;
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star
;
45 constexpr OUStringLiteral XMLN_VERSIONSLIST
= u
"VersionList.xml";
47 XMLVersionListExport::XMLVersionListExport(
48 const css::uno::Reference
< css::uno::XComponentContext
>& rContext
,
49 const css::uno::Sequence
< css::util::RevisionTag
>& rVersions
,
50 const OUString
&rFileName
,
51 Reference
< XDocumentHandler
> const &rHandler
)
52 : SvXMLExport( rContext
, "", rFileName
, util::MeasureUnit::CM
, rHandler
),
53 maVersions( rVersions
)
55 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC
),
56 xmloff::token::GetXMLToken(xmloff::token::XML_N_DC
), XML_NAMESPACE_DC
);
57 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST
),
58 xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST
), XML_NAMESPACE_FRAMEWORK
);
61 ErrCode
XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum
)
63 GetDocHandler()->startDocument();
65 addChaffWhenEncryptedStorage();
67 sal_uInt16 nPos
= SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_DC
);
69 AddAttribute( XML_NAMESPACE_NONE
, GetNamespaceMap_().GetAttrNameByIndex( nPos
),
70 GetNamespaceMap_().GetNameByIndex ( nPos
) );
72 nPos
= SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_FRAMEWORK
);
73 AddAttribute( XML_NAMESPACE_NONE
, GetNamespaceMap_().GetAttrNameByIndex( nPos
),
74 GetNamespaceMap_().GetNameByIndex ( nPos
) );
77 // the following object will write all collected attributes in its dtor
78 SvXMLElementExport
aRoot( *this, XML_NAMESPACE_FRAMEWORK
, xmloff::token::XML_VERSION_LIST
, true, true );
80 for ( const util::RevisionTag
& rInfo
: maVersions
)
82 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
83 xmloff::token::XML_TITLE
,
85 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
86 xmloff::token::XML_COMMENT
,
88 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
89 xmloff::token::XML_CREATOR
,
93 SvXMLMetaExport::GetISODateTimeString( rInfo
.TimeStamp
);
95 AddAttribute( XML_NAMESPACE_DC
, xmloff::token::XML_DATE_TIME
, aDateStr
);
97 // the following object will write all collected attributes in its dtor
98 SvXMLElementExport
aEntry( *this, XML_NAMESPACE_FRAMEWORK
, xmloff::token::XML_VERSION_ENTRY
, true, true );
101 GetDocHandler()->endDocument();
105 XMLVersionListImport::XMLVersionListImport(
106 const css::uno::Reference
< css::uno::XComponentContext
>& rContext
,
107 css::uno::Sequence
< css::util::RevisionTag
>& rVersions
)
108 : SvXMLImport(rContext
, ""),
109 maVersions( rVersions
)
113 XMLVersionListImport::~XMLVersionListImport() noexcept
116 SvXMLImportContext
*XMLVersionListImport::CreateFastContext( sal_Int32 nElement
,
117 const ::css::uno::Reference
< ::css::xml::sax::XFastAttributeList
>& /*xAttrList*/ )
119 SvXMLImportContext
*pContext
= nullptr;
121 if ( nElement
== XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_VERSION_LIST
) )
123 pContext
= new XMLVersionListContext( *this );
129 XMLVersionListContext::XMLVersionListContext( XMLVersionListImport
& rImport
)
130 : SvXMLImportContext( rImport
)
134 XMLVersionListContext::~XMLVersionListContext()
137 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
138 XMLVersionListContext::createFastChildContext(sal_Int32 nElement
,
139 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
)
141 SvXMLImportContext
*pContext
= nullptr;
143 if ( nElement
== XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_VERSION_ENTRY
)
144 || nElement
== XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_VERSION_ENTRY
) )
146 pContext
= new XMLVersionContext( GetImport(), xAttrList
);
152 XMLVersionContext::XMLVersionContext( XMLVersionListImport
& rImport
,
153 const Reference
< XFastAttributeList
> & xAttrList
)
154 : SvXMLImportContext( rImport
)
156 sax_fastparser::FastAttributeList
& rAttribList
=
157 sax_fastparser::castToFastAttributeList( xAttrList
);
158 if ( rAttribList
.getFastAttributeTokens().empty() )
160 util::RevisionTag aInfo
;
161 for (auto &aIter
: rAttribList
)
163 switch( aIter
.getToken() )
165 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_TITLE
):
166 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_TITLE
):
168 aInfo
.Identifier
= aIter
.toString();
171 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_COMMENT
):
172 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_COMMENT
):
174 aInfo
.Comment
= aIter
.toString();
177 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_CREATOR
):
178 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_CREATOR
):
180 aInfo
.Author
= aIter
.toString();
183 case XML_ELEMENT(DC
, xmloff::token::XML_DATE_TIME
):
185 util::DateTime aTime
;
186 if ( ParseISODateTimeString( aIter
.toString(), aTime
) )
187 aInfo
.TimeStamp
= aTime
;
191 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
196 uno::Sequence
< util::RevisionTag
>& aList
= rImport
.GetList();
197 sal_Int32 nLength
= aList
.getLength();
198 aList
.realloc( nLength
+1 );
199 aList
.getArray()[nLength
] = aInfo
;
202 XMLVersionContext::~XMLVersionContext()
205 bool XMLVersionContext::ParseISODateTimeString(
206 std::u16string_view rString
,
207 util::DateTime
& rDateTime
)
209 bool bSuccess
= true;
211 std::u16string_view aDateStr
, aTimeStr
;
212 size_t nPos
= rString
.find( 'T' );
213 if ( nPos
!= std::u16string_view::npos
)
215 aDateStr
= rString
.substr( 0, nPos
);
216 aTimeStr
= rString
.substr( nPos
+ 1 );
219 aDateStr
= rString
; // no separator: only date part
222 sal_Int32 nMonth
= 1;
228 auto pStr
= aDateStr
.begin();
229 sal_Int32 nDateTokens
= 1;
230 while ( pStr
!= aDateStr
.end() )
236 if ( nDateTokens
> 3 || aDateStr
.empty() )
241 nYear
= o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '-', n
));
244 else if ( nDateTokens
>= 2 )
246 nMonth
= o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '-', n
));
249 else if ( nDateTokens
>= 3 )
251 nDay
= o3tl::toInt32(o3tl::getToken(aDateStr
, 0, '-', n
));
258 if ( bSuccess
&& !aTimeStr
.empty() ) // time is optional
260 pStr
= aTimeStr
.begin();
261 sal_Int32 nTimeTokens
= 1;
262 while ( pStr
!= aTimeStr
.end() )
268 if ( nTimeTokens
> 3 )
273 nHour
= o3tl::toInt32(o3tl::getToken(aTimeStr
, 0, ':', n
));
276 else if ( nTimeTokens
>= 2 )
278 nMin
= o3tl::toInt32(o3tl::getToken(aTimeStr
, 0, ':', n
));
281 else if ( nTimeTokens
>= 3 )
283 nSec
= o3tl::toInt32(o3tl::getToken(aTimeStr
, 0, ':', n
));
293 rDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(nDay
);
294 rDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(nMonth
);
295 rDateTime
.Year
= sal::static_int_cast
< sal_uInt16
>(nYear
);
296 rDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(nHour
);
297 rDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(nMin
);
298 rDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(nSec
);
304 void SAL_CALL
XMLVersionListPersistence::store( const uno::Reference
< embed::XStorage
>& xRoot
, const uno::Sequence
< util::RevisionTag
>& rVersions
)
306 // no storage, no version list!
310 // get the services needed for writing the xml data
311 Reference
< uno::XComponentContext
> xContext
=
312 comphelper::getProcessComponentContext();
314 Reference
< XWriter
> xWriter
= Writer::create(xContext
);
316 // check whether there's already a sub storage with the version info
318 OUString
sVerName( XMLN_VERSIONSLIST
);
321 // open (create) the sub storage with the version info
322 uno::Reference
< io::XStream
> xVerStream
= xRoot
->openStreamElement(
324 embed::ElementModes::READWRITE
| embed::ElementModes::TRUNCATE
);
325 if ( !xVerStream
.is() )
326 throw uno::RuntimeException();
328 Reference
< io::XOutputStream
> xOut
= xVerStream
->getOutputStream();
330 throw uno::RuntimeException("The stream was successfully opened for writing already!");
332 xWriter
->setOutputStream(xOut
);
334 rtl::Reference
< XMLVersionListExport
> xExp( new XMLVersionListExport( xContext
, rVersions
, sVerName
, xWriter
) );
336 xExp
->exportDoc( ::xmloff::token::XML_VERSION
);
338 xVerStream
.clear(); // use refcounting for now to dispose
340 catch( uno::Exception
& )
342 // TODO: error handling
346 uno::Sequence
< util::RevisionTag
> SAL_CALL
XMLVersionListPersistence::load( const uno::Reference
< embed::XStorage
>& xRoot
)
348 css::uno::Sequence
< css::util::RevisionTag
> aVersions
;
350 const OUString
sDocName( XMLN_VERSIONSLIST
);
353 if ( xRoot
.is() && xRoot
->hasByName( sDocName
) && xRoot
->isStreamElement( sDocName
) )
355 Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
357 InputSource aParserInput
;
359 uno::Reference
< beans::XPropertySet
> xProps( xRoot
, uno::UNO_QUERY
);
360 OSL_ENSURE( xProps
.is(), "Storage must implement XPropertySet!" );
364 xProps
->getPropertyValue("URL") >>= aParserInput
.sSystemId
;
366 catch( uno::Exception
& )
370 uno::Reference
< io::XStream
> xDocStream
= xRoot
->openStreamElement(
372 embed::ElementModes::READ
);
373 if ( !xDocStream
.is() )
374 throw uno::RuntimeException();
376 aParserInput
.aInputStream
= xDocStream
->getInputStream();
377 OSL_ENSURE( aParserInput
.aInputStream
.is(),
378 "The stream was successfully opened for reading, the input part must be accessible!" );
379 if ( !aParserInput
.aInputStream
.is() )
380 throw uno::RuntimeException();
383 rtl::Reference
< XMLVersionListImport
> xImport
= new XMLVersionListImport( xContext
, aVersions
);
388 xImport
->parseStream( aParserInput
);
390 catch( SAXParseException
& ) {}
391 catch( SAXException
& ) {}
392 catch( io::IOException
& ) {}
395 catch( uno::Exception
& )
397 // TODO: error handling
403 OUString
XMLVersionListPersistence::getImplementationName()
405 return "XMLVersionListPersistence";
408 sal_Bool
XMLVersionListPersistence::supportsService(
409 OUString
const & ServiceName
)
411 return cppu::supportsService(this, ServiceName
);
414 css::uno::Sequence
<OUString
>
415 XMLVersionListPersistence::getSupportedServiceNames()
417 return css::uno::Sequence
<OUString
>{
418 "com.sun.star.document.DocumentRevisionListPersistence"};
421 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
422 XMLVersionListPersistence_get_implementation(
423 css::uno::XComponentContext
*,
424 css::uno::Sequence
<css::uno::Any
> const &)
426 return cppu::acquire(new XMLVersionListPersistence
);
429 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */