Line data Source code
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 <tools/debug.hxx>
22 : #include <unotools/streamwrap.hxx>
23 : #include <xmlversion.hxx>
24 : #include <xmloff/xmlmetae.hxx>
25 :
26 : #include <xmloff/xmltoken.hxx>
27 : #include <comphelper/processfactory.hxx>
28 : #include <com/sun/star/io/XActiveDataSource.hpp>
29 : #include <com/sun/star/io/XOutputStream.hpp>
30 : #include <com/sun/star/util/DateTime.hpp>
31 : #include <com/sun/star/util/MeasureUnit.hpp>
32 : #include <com/sun/star/xml/sax/InputSource.hpp>
33 : #include <com/sun/star/xml/sax/Parser.hpp>
34 : #include <com/sun/star/xml/sax/Writer.hpp>
35 :
36 : using namespace ::com::sun::star::xml::sax;
37 : using namespace ::com::sun::star::uno;
38 : using namespace ::com::sun::star;
39 :
40 : // ------------------------------------------------------------------------
41 :
42 : const char XMLN_VERSIONSLIST[] = "VersionList.xml";
43 :
44 : // ------------------------------------------------------------------------
45 : // #110897#
46 0 : XMLVersionListExport::XMLVersionListExport(
47 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
48 : const com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions,
49 : const OUString &rFileName,
50 : Reference< XDocumentHandler > &rHandler )
51 : : SvXMLExport( xContext, rFileName, util::MeasureUnit::CM, rHandler ),
52 0 : maVersions( rVersions )
53 : {
54 0 : _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_DC_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_DC),
55 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_DC), XML_NAMESPACE_DC );
56 0 : _GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
57 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
58 0 : }
59 :
60 : // ------------------------------------------------------------------------
61 0 : sal_uInt32 XMLVersionListExport::exportDoc( enum ::xmloff::token::XMLTokenEnum )
62 : {
63 0 : GetDocHandler()->startDocument();
64 :
65 0 : addChaffWhenEncryptedStorage();
66 :
67 0 : sal_uInt16 nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_DC );
68 :
69 0 : AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
70 0 : _GetNamespaceMap().GetNameByIndex ( nPos ) );
71 :
72 0 : nPos = _GetNamespaceMap().GetIndexByKey( XML_NAMESPACE_FRAMEWORK );
73 0 : AddAttribute( XML_NAMESPACE_NONE, _GetNamespaceMap().GetAttrNameByIndex( nPos ),
74 0 : _GetNamespaceMap().GetNameByIndex ( nPos ) );
75 :
76 : {
77 : // the following object will write all collected attributes in its dtor
78 0 : SvXMLElementExport aRoot( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_LIST, sal_True, sal_True );
79 :
80 0 : for ( sal_Int32 n=0; n<maVersions.getLength(); n++ )
81 : {
82 0 : const util::RevisionTag& rInfo = maVersions[n];
83 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
84 : xmloff::token::XML_TITLE,
85 0 : OUString( rInfo.Identifier ) );
86 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
87 : xmloff::token::XML_COMMENT,
88 0 : OUString( rInfo.Comment ) );
89 : AddAttribute( XML_NAMESPACE_FRAMEWORK,
90 : xmloff::token::XML_CREATOR,
91 0 : OUString( rInfo.Author ) );
92 :
93 : OUString aDateStr =
94 0 : SvXMLMetaExport::GetISODateTimeString( rInfo.TimeStamp );
95 :
96 0 : AddAttribute( XML_NAMESPACE_DC, xmloff::token::XML_DATE_TIME, aDateStr );
97 :
98 : // the following object will write all collected attributes in its dtor
99 0 : SvXMLElementExport aEntry( *this, XML_NAMESPACE_FRAMEWORK, xmloff::token::XML_VERSION_ENTRY, sal_True, sal_True );
100 0 : }
101 : }
102 0 : GetDocHandler()->endDocument();
103 0 : return 0;
104 : }
105 :
106 : // ------------------------------------------------------------------------
107 0 : XMLVersionListImport::XMLVersionListImport(
108 : const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > xContext,
109 : com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag >& rVersions )
110 : : SvXMLImport(xContext),
111 0 : maVersions( rVersions )
112 : {
113 0 : GetNamespaceMap().AddAtIndex( XML_NAMESPACE_FRAMEWORK_IDX, xmloff::token::GetXMLToken(xmloff::token::XML_NP_VERSIONS_LIST),
114 0 : xmloff::token::GetXMLToken(xmloff::token::XML_N_VERSIONS_LIST), XML_NAMESPACE_FRAMEWORK );
115 0 : }
116 :
117 : // ------------------------------------------------------------------------
118 0 : XMLVersionListImport::~XMLVersionListImport( void ) throw()
119 0 : {}
120 :
121 : // ------------------------------------------------------------------------
122 0 : SvXMLImportContext *XMLVersionListImport::CreateContext(
123 : sal_uInt16 nPrefix,
124 : const OUString& rLocalName,
125 : const Reference< XAttributeList > & xAttrList )
126 : {
127 0 : SvXMLImportContext *pContext = 0;
128 :
129 0 : if ( XML_NAMESPACE_FRAMEWORK == nPrefix &&
130 0 : rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_LIST) )
131 : {
132 0 : pContext = new XMLVersionListContext( *this, nPrefix, rLocalName, xAttrList );
133 : }
134 : else
135 : {
136 0 : pContext = SvXMLImport::CreateContext( nPrefix, rLocalName, xAttrList );
137 : }
138 :
139 0 : return pContext;
140 : }
141 :
142 :
143 : // ------------------------------------------------------------------------
144 0 : XMLVersionListContext::XMLVersionListContext( XMLVersionListImport& rImport,
145 : sal_uInt16 nPrefix,
146 : const OUString& rLocalName,
147 : const Reference< XAttributeList > & )
148 : : SvXMLImportContext( rImport, nPrefix, rLocalName )
149 0 : , rLocalRef( rImport )
150 : {
151 0 : }
152 :
153 : // ------------------------------------------------------------------------
154 0 : XMLVersionListContext::~XMLVersionListContext( void )
155 0 : {}
156 :
157 : // ------------------------------------------------------------------------
158 0 : SvXMLImportContext *XMLVersionListContext::CreateChildContext( sal_uInt16 nPrefix,
159 : const OUString& rLocalName,
160 : const Reference< XAttributeList > & xAttrList )
161 : {
162 0 : SvXMLImportContext *pContext = 0;
163 :
164 0 : if ( nPrefix == XML_NAMESPACE_FRAMEWORK &&
165 0 : rLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_VERSION_ENTRY) )
166 : {
167 0 : pContext = new XMLVersionContext( rLocalRef, nPrefix, rLocalName, xAttrList );
168 : }
169 : else
170 : {
171 0 : pContext = new SvXMLImportContext( rLocalRef, nPrefix, rLocalName );
172 : }
173 :
174 0 : return pContext;
175 : }
176 :
177 : // ------------------------------------------------------------------------
178 0 : XMLVersionContext::XMLVersionContext( XMLVersionListImport& rImport,
179 : sal_uInt16 nPref,
180 : const OUString& rLocalName,
181 : const Reference< XAttributeList > & xAttrList )
182 : : SvXMLImportContext( rImport, nPref, rLocalName )
183 0 : , rLocalRef( rImport )
184 : {
185 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
186 :
187 0 : if ( !nAttrCount )
188 0 : return;
189 :
190 0 : util::RevisionTag aInfo;
191 0 : for ( sal_Int16 i=0; i < nAttrCount; i++ )
192 : {
193 0 : OUString aLocalName;
194 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
195 0 : sal_uInt16 nPrefix = rImport.GetNamespaceMap().GetKeyByAttrName( rAttrName, &aLocalName );
196 :
197 0 : if ( XML_NAMESPACE_FRAMEWORK == nPrefix )
198 : {
199 0 : if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_TITLE) )
200 : {
201 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
202 0 : aInfo.Identifier = rAttrValue;
203 : }
204 0 : else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_COMMENT) )
205 : {
206 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
207 0 : aInfo.Comment = rAttrValue;
208 : }
209 0 : else if ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_CREATOR) )
210 : {
211 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
212 0 : aInfo.Author = rAttrValue;
213 : }
214 : }
215 0 : else if ( ( XML_NAMESPACE_DC == nPrefix ) &&
216 0 : ( aLocalName == xmloff::token::GetXMLToken(xmloff::token::XML_DATE_TIME) ) )
217 : {
218 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
219 0 : util::DateTime aTime;
220 0 : if ( ParseISODateTimeString( rAttrValue, aTime ) )
221 0 : aInfo.TimeStamp = aTime;
222 : }
223 0 : }
224 :
225 0 : uno::Sequence < util::RevisionTag >& aList = rLocalRef.GetList();
226 0 : sal_Int32 nLength = aList.getLength();
227 0 : aList.realloc( nLength+1 );
228 0 : aList[nLength] = aInfo;
229 : }
230 :
231 :
232 : // ------------------------------------------------------------------------
233 0 : XMLVersionContext::~XMLVersionContext( void )
234 0 : {}
235 :
236 : // ------------------------------------------------------------------------
237 0 : sal_Bool XMLVersionContext::ParseISODateTimeString(
238 : const OUString& rString,
239 : util::DateTime& rDateTime )
240 : {
241 0 : sal_Bool bSuccess = sal_True;
242 :
243 0 : OUString aDateStr, aTimeStr;
244 0 : sal_Int32 nPos = rString.indexOf( (sal_Unicode) 'T' );
245 0 : if ( nPos >= 0 )
246 : {
247 0 : aDateStr = rString.copy( 0, nPos );
248 0 : aTimeStr = rString.copy( nPos + 1 );
249 : }
250 : else
251 0 : aDateStr = rString; // no separator: only date part
252 :
253 0 : sal_Int32 nYear = 0;
254 0 : sal_Int32 nMonth = 1;
255 0 : sal_Int32 nDay = 1;
256 0 : sal_Int32 nHour = 0;
257 0 : sal_Int32 nMin = 0;
258 0 : sal_Int32 nSec = 0;
259 :
260 0 : const sal_Unicode* pStr = aDateStr.getStr();
261 0 : sal_Int32 nDateTokens = 1;
262 0 : while ( *pStr )
263 : {
264 0 : if ( *pStr == '-' )
265 0 : nDateTokens++;
266 0 : pStr++;
267 : }
268 0 : if ( nDateTokens > 3 || aDateStr.isEmpty() )
269 0 : bSuccess = sal_False;
270 : else
271 : {
272 0 : sal_Int32 n = 0;
273 0 : nYear = aDateStr.getToken( 0, '-', n ).toInt32();
274 0 : if ( nYear > 9999 )
275 0 : bSuccess = sal_False;
276 0 : else if ( nDateTokens >= 2 )
277 : {
278 0 : nMonth = aDateStr.getToken( 0, '-', n ).toInt32();
279 0 : if ( nMonth > 12 )
280 0 : bSuccess = sal_False;
281 0 : else if ( nDateTokens >= 3 )
282 : {
283 0 : nDay = aDateStr.getToken( 0, '-', n ).toInt32();
284 0 : if ( nDay > 31 )
285 0 : bSuccess = sal_False;
286 : }
287 : }
288 : }
289 :
290 0 : if ( bSuccess && !aTimeStr.isEmpty() ) // time is optional
291 : {
292 0 : pStr = aTimeStr.getStr();
293 0 : sal_Int32 nTimeTokens = 1;
294 0 : while ( *pStr )
295 : {
296 0 : if ( *pStr == ':' )
297 0 : nTimeTokens++;
298 0 : pStr++;
299 : }
300 0 : if ( nTimeTokens > 3 )
301 0 : bSuccess = sal_False;
302 : else
303 : {
304 0 : sal_Int32 n = 0;
305 0 : nHour = aTimeStr.getToken( 0, ':', n ).toInt32();
306 0 : if ( nHour > 23 )
307 0 : bSuccess = sal_False;
308 0 : else if ( nTimeTokens >= 2 )
309 : {
310 0 : nMin = aTimeStr.getToken( 0, ':', n ).toInt32();
311 0 : if ( nMin > 59 )
312 0 : bSuccess = sal_False;
313 0 : else if ( nTimeTokens >= 3 )
314 : {
315 0 : nSec = aTimeStr.getToken( 0, ':', n ).toInt32();
316 0 : if ( nSec > 59 )
317 0 : bSuccess = sal_False;
318 : }
319 : }
320 : }
321 : }
322 :
323 0 : if ( bSuccess )
324 : {
325 0 : rDateTime.Day = sal::static_int_cast< sal_uInt16 >(nDay);
326 0 : rDateTime.Month = sal::static_int_cast< sal_uInt16 >(nMonth);
327 0 : rDateTime.Year = sal::static_int_cast< sal_uInt16 >(nYear);
328 0 : rDateTime.Hours = sal::static_int_cast< sal_uInt16 >(nHour);
329 0 : rDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(nMin);
330 0 : rDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(nSec);
331 : }
332 :
333 0 : return bSuccess;
334 : }
335 :
336 :
337 : // ------------------------------------------------------------------------
338 0 : void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions )
339 : throw (::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
340 : {
341 : // no storage, no version list!
342 0 : if ( xRoot.is() )
343 : {
344 : // get the services needed for writing the xml data
345 : Reference< uno::XComponentContext > xContext =
346 0 : comphelper::getProcessComponentContext();
347 :
348 0 : Reference< XWriter > xWriter = Writer::create(xContext);
349 :
350 : // check whether there's already a sub storage with the version info
351 : // and delete it
352 0 : OUString sVerName( XMLN_VERSIONSLIST );
353 :
354 : try {
355 : // open (create) the sub storage with the version info
356 0 : uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement(
357 : sVerName,
358 0 : embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE );
359 0 : if ( !xVerStream.is() )
360 0 : throw uno::RuntimeException();
361 :
362 0 : Reference< io::XOutputStream > xOut = xVerStream->getOutputStream();
363 0 : if ( !xOut.is() )
364 0 : throw uno::RuntimeException(); // the stream was successfully opened for writing already
365 :
366 0 : Reference< io::XActiveDataSource > xSrc( xWriter, uno::UNO_QUERY );
367 0 : xSrc->setOutputStream(xOut);
368 :
369 0 : Reference< XDocumentHandler > xHandler( xWriter, uno::UNO_QUERY );
370 :
371 0 : XMLVersionListExport aExp( xContext, rVersions, sVerName, xHandler );
372 :
373 0 : aExp.exportDoc( ::xmloff::token::XML_VERSION );
374 :
375 0 : xVerStream = uno::Reference< io::XStream >(); // use refcounting for now to dispose
376 : }
377 0 : catch( uno::Exception& )
378 : {
379 : // TODO: error handling
380 0 : }
381 : }
382 0 : }
383 :
384 : // ------------------------------------------------------------------------
385 486 : uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot )
386 : throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException)
387 : {
388 486 : com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag > aVersions;
389 :
390 972 : const OUString sDocName( XMLN_VERSIONSLIST );
391 972 : uno::Reference< container::XNameAccess > xRootNames( xRoot, uno::UNO_QUERY );
392 :
393 : try {
394 486 : if ( xRootNames.is() && xRootNames->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) )
395 : {
396 0 : Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
397 :
398 0 : InputSource aParserInput;
399 :
400 0 : uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY );
401 : OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!\n" );
402 0 : if ( xProps.is() )
403 : {
404 : try {
405 0 : xProps->getPropertyValue("URL") >>= aParserInput.sSystemId;
406 : }
407 0 : catch( uno::Exception& )
408 : {}
409 : }
410 :
411 0 : uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement(
412 : sDocName,
413 0 : embed::ElementModes::READ );
414 0 : if ( !xDocStream.is() )
415 0 : throw uno::RuntimeException();
416 :
417 0 : aParserInput.aInputStream = xDocStream->getInputStream();
418 : OSL_ENSURE( aParserInput.aInputStream.is(),
419 : "The stream was successfully opened for reading, the input part must be accessible!\n" );
420 0 : if ( !aParserInput.aInputStream.is() )
421 0 : throw uno::RuntimeException();
422 :
423 : // get filter
424 0 : Reference< XDocumentHandler > xFilter = new XMLVersionListImport( xContext, aVersions );
425 :
426 : // connect parser and filter
427 0 : Reference< XParser > xParser = xml::sax::Parser::create(xContext);
428 0 : xParser->setDocumentHandler( xFilter );
429 :
430 : // parse
431 : try
432 : {
433 0 : xParser->parseStream( aParserInput );
434 : }
435 0 : catch( SAXParseException& ) {}
436 0 : catch( SAXException& ) {}
437 0 : catch( io::IOException& ) {}
438 : }
439 : }
440 185 : catch( uno::Exception& )
441 : {
442 : // TODO: error handling
443 : }
444 :
445 972 : return aVersions;
446 : }
447 :
448 42 : uno::Sequence< OUString > SAL_CALL XMLVersionListPersistence_getSupportedServiceNames()
449 : throw()
450 : {
451 : const OUString aServiceName(
452 42 : "com.sun.star.document.DocumentRevisionListPersistence" );
453 42 : const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
454 42 : return aSeq;
455 : }
456 :
457 84 : OUString SAL_CALL XMLVersionListPersistence_getImplementationName() throw()
458 : {
459 84 : return OUString( "XMLVersionListPersistence" );
460 : }
461 :
462 486 : uno::Reference< uno::XInterface > SAL_CALL XMLVersionListPersistence_createInstance(
463 : const uno::Reference< lang::XMultiServiceFactory > &)
464 : throw( uno::Exception )
465 : {
466 486 : return (cppu::OWeakObject*)new XMLVersionListPersistence;
467 : }
468 :
469 0 : uno::Sequence< OUString > SAL_CALL XMLVersionImExportOOO_getSupportedServiceNames()
470 : throw()
471 : {
472 : const OUString aServiceName(
473 0 : "com.sun.star.document.DocumentRevisionListPersistence" );
474 0 : const uno::Sequence< OUString > aSeq( &aServiceName, 1 );
475 0 : return aSeq;
476 : }
477 :
478 0 : OUString SAL_CALL XMLVersionImExportOOO_getImplementationName() throw()
479 : {
480 0 : return OUString( "XMLVersionImExportOOo" );
481 : }
482 :
483 0 : uno::Reference< uno::XInterface > SAL_CALL XMLVersionImExportOOO_createInstance(
484 : const uno::Reference< lang::XMultiServiceFactory > &)
485 : throw( uno::Exception )
486 : {
487 0 : return (cppu::OWeakObject*)new XMLVersionListPersistence;
488 : }
489 :
490 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|