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