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>
27 #include <xmloff/xmltoken.hxx>
28 #include <comphelper/processfactory.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>
37 #include <cppuhelper/supportsservice.hxx>
39 using namespace ::com::sun::star::xml::sax
;
40 using namespace ::com::sun::star::uno
;
41 using namespace ::com::sun::star
;
43 const char XMLN_VERSIONSLIST
[] = "VersionList.xml";
45 XMLVersionListExport::XMLVersionListExport(
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
)
53 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC
),
54 xmloff::token::GetXMLToken(xmloff::token::XML_N_DC
), XML_NAMESPACE_DC
);
55 GetNamespaceMap_().AddAtIndex( xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST
),
56 xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST
), XML_NAMESPACE_FRAMEWORK
);
59 ErrCode
XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum
)
61 GetDocHandler()->startDocument();
63 addChaffWhenEncryptedStorage();
65 sal_uInt16 nPos
= SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_DC
);
67 AddAttribute( XML_NAMESPACE_NONE
, GetNamespaceMap_().GetAttrNameByIndex( nPos
),
68 GetNamespaceMap_().GetNameByIndex ( nPos
) );
70 nPos
= SvXMLNamespaceMap::GetIndexByKey( XML_NAMESPACE_FRAMEWORK
);
71 AddAttribute( XML_NAMESPACE_NONE
, GetNamespaceMap_().GetAttrNameByIndex( nPos
),
72 GetNamespaceMap_().GetNameByIndex ( nPos
) );
75 // the following object will write all collected attributes in its dtor
76 SvXMLElementExport
aRoot( *this, XML_NAMESPACE_FRAMEWORK
, xmloff::token::XML_VERSION_LIST
, true, true );
78 for ( const util::RevisionTag
& rInfo
: maVersions
)
80 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
81 xmloff::token::XML_TITLE
,
83 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
84 xmloff::token::XML_COMMENT
,
86 AddAttribute( XML_NAMESPACE_FRAMEWORK
,
87 xmloff::token::XML_CREATOR
,
91 SvXMLMetaExport::GetISODateTimeString( rInfo
.TimeStamp
);
93 AddAttribute( XML_NAMESPACE_DC
, xmloff::token::XML_DATE_TIME
, aDateStr
);
95 // the following object will write all collected attributes in its dtor
96 SvXMLElementExport
aEntry( *this, XML_NAMESPACE_FRAMEWORK
, xmloff::token::XML_VERSION_ENTRY
, true, true );
99 GetDocHandler()->endDocument();
103 XMLVersionListImport::XMLVersionListImport(
104 const css::uno::Reference
< css::uno::XComponentContext
>& rContext
,
105 css::uno::Sequence
< css::util::RevisionTag
>& rVersions
)
106 : SvXMLImport(rContext
, ""),
107 maVersions( rVersions
)
111 XMLVersionListImport::~XMLVersionListImport() throw()
114 SvXMLImportContext
*XMLVersionListImport::CreateFastContext( sal_Int32 nElement
,
115 const ::css::uno::Reference
< ::css::xml::sax::XFastAttributeList
>& /*xAttrList*/ )
117 SvXMLImportContext
*pContext
= nullptr;
119 if ( nElement
== XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_VERSION_LIST
) )
121 pContext
= new XMLVersionListContext( *this );
127 XMLVersionListContext::XMLVersionListContext( XMLVersionListImport
& rImport
)
128 : SvXMLImportContext( rImport
)
132 XMLVersionListContext::~XMLVersionListContext()
135 css::uno::Reference
< css::xml::sax::XFastContextHandler
> SAL_CALL
136 XMLVersionListContext::createFastChildContext(sal_Int32 nElement
,
137 const css::uno::Reference
< css::xml::sax::XFastAttributeList
> & xAttrList
)
139 SvXMLImportContext
*pContext
= nullptr;
141 if ( nElement
== XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_VERSION_ENTRY
)
142 || nElement
== XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_VERSION_ENTRY
) )
144 pContext
= new XMLVersionContext( GetImport(), xAttrList
);
150 XMLVersionContext::XMLVersionContext( XMLVersionListImport
& rImport
,
151 const Reference
< XFastAttributeList
> & xAttrList
)
152 : SvXMLImportContext( rImport
)
154 sax_fastparser::FastAttributeList
& rAttribList
=
155 sax_fastparser::castToFastAttributeList( xAttrList
);
156 if ( rAttribList
.getFastAttributeTokens().empty() )
158 util::RevisionTag aInfo
;
159 for (auto &aIter
: rAttribList
)
161 switch( aIter
.getToken() )
163 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_TITLE
):
164 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_TITLE
):
166 aInfo
.Identifier
= aIter
.toString();
169 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_COMMENT
):
170 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_COMMENT
):
172 aInfo
.Comment
= aIter
.toString();
175 case XML_ELEMENT(FRAMEWORK
, xmloff::token::XML_CREATOR
):
176 case XML_ELEMENT(VERSIONS_LIST
, xmloff::token::XML_CREATOR
):
178 aInfo
.Author
= aIter
.toString();
181 case XML_ELEMENT(DC
, xmloff::token::XML_DATE_TIME
):
183 util::DateTime aTime
;
184 if ( ParseISODateTimeString( aIter
.toString(), aTime
) )
185 aInfo
.TimeStamp
= aTime
;
189 XMLOFF_WARN_UNKNOWN("xmloff", aIter
);
194 uno::Sequence
< util::RevisionTag
>& aList
= rImport
.GetList();
195 sal_Int32 nLength
= aList
.getLength();
196 aList
.realloc( nLength
+1 );
197 aList
[nLength
] = aInfo
;
200 XMLVersionContext::~XMLVersionContext()
203 bool XMLVersionContext::ParseISODateTimeString(
204 const OUString
& rString
,
205 util::DateTime
& rDateTime
)
207 bool bSuccess
= true;
209 OUString aDateStr
, aTimeStr
;
210 sal_Int32 nPos
= rString
.indexOf( 'T' );
213 aDateStr
= rString
.copy( 0, nPos
);
214 aTimeStr
= rString
.copy( nPos
+ 1 );
217 aDateStr
= rString
; // no separator: only date part
220 sal_Int32 nMonth
= 1;
226 const sal_Unicode
* pStr
= aDateStr
.getStr();
227 sal_Int32 nDateTokens
= 1;
234 if ( nDateTokens
> 3 || aDateStr
.isEmpty() )
239 nYear
= aDateStr
.getToken( 0, '-', n
).toInt32();
242 else if ( nDateTokens
>= 2 )
244 nMonth
= aDateStr
.getToken( 0, '-', n
).toInt32();
247 else if ( nDateTokens
>= 3 )
249 nDay
= aDateStr
.getToken( 0, '-', n
).toInt32();
256 if ( bSuccess
&& !aTimeStr
.isEmpty() ) // time is optional
258 pStr
= aTimeStr
.getStr();
259 sal_Int32 nTimeTokens
= 1;
266 if ( nTimeTokens
> 3 )
271 nHour
= aTimeStr
.getToken( 0, ':', n
).toInt32();
274 else if ( nTimeTokens
>= 2 )
276 nMin
= aTimeStr
.getToken( 0, ':', n
).toInt32();
279 else if ( nTimeTokens
>= 3 )
281 nSec
= aTimeStr
.getToken( 0, ':', n
).toInt32();
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
);
302 void SAL_CALL
XMLVersionListPersistence::store( const uno::Reference
< embed::XStorage
>& xRoot
, const uno::Sequence
< util::RevisionTag
>& rVersions
)
304 // no storage, no version list!
308 // get the services needed for writing the xml data
309 Reference
< uno::XComponentContext
> xContext
=
310 comphelper::getProcessComponentContext();
312 Reference
< XWriter
> xWriter
= Writer::create(xContext
);
314 // check whether there's already a sub storage with the version info
316 OUString
sVerName( XMLN_VERSIONSLIST
);
319 // open (create) the sub storage with the version info
320 uno::Reference
< io::XStream
> xVerStream
= xRoot
->openStreamElement(
322 embed::ElementModes::READWRITE
| embed::ElementModes::TRUNCATE
);
323 if ( !xVerStream
.is() )
324 throw uno::RuntimeException();
326 Reference
< io::XOutputStream
> xOut
= xVerStream
->getOutputStream();
328 throw uno::RuntimeException(); // the stream was successfully opened for writing already
330 xWriter
->setOutputStream(xOut
);
332 rtl::Reference
< XMLVersionListExport
> xExp( new XMLVersionListExport( xContext
, rVersions
, sVerName
, xWriter
) );
334 xExp
->exportDoc( ::xmloff::token::XML_VERSION
);
336 xVerStream
.clear(); // use refcounting for now to dispose
338 catch( uno::Exception
& )
340 // TODO: error handling
344 uno::Sequence
< util::RevisionTag
> SAL_CALL
XMLVersionListPersistence::load( const uno::Reference
< embed::XStorage
>& xRoot
)
346 css::uno::Sequence
< css::util::RevisionTag
> aVersions
;
348 const OUString
sDocName( XMLN_VERSIONSLIST
);
351 if ( xRoot
.is() && xRoot
->hasByName( sDocName
) && xRoot
->isStreamElement( sDocName
) )
353 Reference
< uno::XComponentContext
> xContext
= comphelper::getProcessComponentContext();
355 InputSource aParserInput
;
357 uno::Reference
< beans::XPropertySet
> xProps( xRoot
, uno::UNO_QUERY
);
358 OSL_ENSURE( xProps
.is(), "Storage must implement XPropertySet!" );
362 xProps
->getPropertyValue("URL") >>= aParserInput
.sSystemId
;
364 catch( uno::Exception
& )
368 uno::Reference
< io::XStream
> xDocStream
= xRoot
->openStreamElement(
370 embed::ElementModes::READ
);
371 if ( !xDocStream
.is() )
372 throw uno::RuntimeException();
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();
381 rtl::Reference
< XMLVersionListImport
> xImport
= new XMLVersionListImport( xContext
, aVersions
);
386 xImport
->parseStream( aParserInput
);
388 catch( SAXParseException
& ) {}
389 catch( SAXException
& ) {}
390 catch( io::IOException
& ) {}
393 catch( uno::Exception
& )
395 // TODO: error handling
401 OUString
XMLVersionListPersistence::getImplementationName()
403 return "XMLVersionListPersistence";
406 sal_Bool
XMLVersionListPersistence::supportsService(
407 OUString
const & ServiceName
)
409 return cppu::supportsService(this, ServiceName
);
412 css::uno::Sequence
<OUString
>
413 XMLVersionListPersistence::getSupportedServiceNames()
415 return css::uno::Sequence
<OUString
>{
416 "com.sun.star.document.DocumentRevisionListPersistence"};
419 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
420 XMLVersionListPersistence_get_implementation(
421 css::uno::XComponentContext
*,
422 css::uno::Sequence
<css::uno::Any
> const &)
424 return cppu::acquire(new XMLVersionListPersistence
);
427 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */