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# 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 // ------------------------------------------------------------------------ 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# 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 // ------------------------------------------------------------------------ 128 XMLVersionListImport::~XMLVersionListImport( void ) throw() 129 {} 130 131 // ------------------------------------------------------------------------ 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 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 // ------------------------------------------------------------------------ 166 XMLVersionListContext::~XMLVersionListContext( void ) 167 {} 168 169 // ------------------------------------------------------------------------ 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 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 // ------------------------------------------------------------------------ 247 XMLVersionContext::~XMLVersionContext( void ) 248 {} 249 250 // ------------------------------------------------------------------------ 251 // static 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 355 void SAL_CALL XMLVersionListPersistence::store( const uno::Reference< embed::XStorage >& xRoot, const uno::Sequence< util::RevisionTag >& rVersions ) 356 throw (::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException) 357 { 358 // no storage, no version list! 359 if ( xRoot.is() ) 360 { 361 // get the services needed for writing the xml data 362 Reference< lang::XMultiServiceFactory > xServiceFactory = 363 comphelper::getProcessServiceFactory(); 364 DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" ); 365 366 Reference< XInterface > xWriter (xServiceFactory->createInstance( 367 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer")))); 368 DBG_ASSERT( xWriter.is(), "com.sun.star.xml.sax.Writer service missing" ); 369 370 // check wether there's already a sub storage with the version info 371 // and delete it 372 OUString sVerName( RTL_CONSTASCII_USTRINGPARAM( XMLN_VERSIONSLIST ) ); 373 374 // is this really needed, we set the size to zero before doing 375 // anything with this stream? 376 /* if ( xRoot->IsContained( sVerName ) ) 377 { 378 xRoot->Remove( sVerName ); 379 xRoot->Commit(); 380 } 381 */ 382 try { 383 // open (create) the sub storage with the version info 384 uno::Reference< io::XStream > xVerStream = xRoot->openStreamElement( 385 sVerName, 386 embed::ElementModes::READWRITE | embed::ElementModes::TRUNCATE ); 387 if ( !xVerStream.is() ) 388 throw uno::RuntimeException(); 389 390 //REMOVE // SetSize should not be neccessary because OpenStream( WRITE|TRUNC ) should already 391 //REMOVE // have set the size to zero 392 //REMOVE // xVerStream->SetSize ( 0L ); 393 //REMOVE xVerStream->SetBufferSize( 16*1024 ); 394 395 Reference< io::XOutputStream > xOut = xVerStream->getOutputStream(); 396 if ( !xOut.is() ) 397 throw uno::RuntimeException(); // the stream was successfuly opened for writing already 398 399 Reference< io::XActiveDataSource > xSrc( xWriter, uno::UNO_QUERY ); 400 xSrc->setOutputStream(xOut); 401 402 Reference< XDocumentHandler > xHandler( xWriter, uno::UNO_QUERY ); 403 404 // XMLVersionListExport aExp( pList, sVerName, xHandler ); 405 XMLVersionListExport aExp( xServiceFactory, rVersions, sVerName, xHandler ); 406 407 aExp.exportDoc( ::xmloff::token::XML_VERSION ); 408 409 //REMOVE xVerStream->Commit(); 410 xVerStream = uno::Reference< io::XStream >(); // use refcounting for now to dispose 411 // xRoot->Commit(); 412 } 413 catch( uno::Exception& ) 414 { 415 // TODO: error handling 416 } 417 } 418 } 419 420 // ------------------------------------------------------------------------ 421 uno::Sequence< util::RevisionTag > SAL_CALL XMLVersionListPersistence::load( const uno::Reference< embed::XStorage >& xRoot ) 422 throw (::com::sun::star::container::NoSuchElementException, ::com::sun::star::io::IOException, ::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException) 423 { 424 com::sun::star::uno::Sequence < com::sun::star::util::RevisionTag > aVersions; 425 426 const OUString sDocName( RTL_CONSTASCII_USTRINGPARAM( XMLN_VERSIONSLIST ) ); 427 uno::Reference< container::XNameAccess > xRootNames( xRoot, uno::UNO_QUERY ); 428 429 try { 430 if ( xRootNames.is() && xRootNames->hasByName( sDocName ) && xRoot->isStreamElement( sDocName ) ) 431 { 432 Reference< lang::XMultiServiceFactory > xServiceFactory = 433 comphelper::getProcessServiceFactory(); 434 DBG_ASSERT( xServiceFactory.is(), "XMLReader::Read: got no service manager" ); 435 436 InputSource aParserInput; 437 438 uno::Reference< beans::XPropertySet > xProps( xRoot, uno::UNO_QUERY ); 439 OSL_ENSURE( xProps.is(), "Storage must implement XPropertySet!\n" ); 440 if ( xProps.is() ) 441 { 442 try { 443 xProps->getPropertyValue( ::rtl::OUString::createFromAscii( "URL" ) ) >>= aParserInput.sSystemId; 444 } 445 catch( uno::Exception& ) 446 {} 447 } 448 449 uno::Reference< io::XStream > xDocStream = xRoot->openStreamElement( 450 sDocName, 451 embed::ElementModes::READ ); 452 if ( !xDocStream.is() ) 453 throw uno::RuntimeException(); 454 455 //REMOVE xDocStream->Seek( 0L ); 456 //REMOVE xDocStream->SetBufferSize( 16*1024 ); 457 458 aParserInput.aInputStream = xDocStream->getInputStream(); 459 OSL_ENSURE( aParserInput.aInputStream.is(), 460 "The stream was successfuly opened for reading, the input part must be accessible!\n" ); 461 if ( !aParserInput.aInputStream.is() ) 462 throw uno::RuntimeException(); 463 464 // get parser 465 Reference< XInterface > xXMLParser = xServiceFactory->createInstance( 466 OUString::createFromAscii("com.sun.star.xml.sax.Parser") ); 467 DBG_ASSERT( xXMLParser.is(), 468 "XMLReader::Read: com.sun.star.xml.sax.Parser service missing" ); 469 470 // get filter 471 // Reference< XDocumentHandler > xFilter = new XMLVersionListImport( pList ); 472 Reference< XDocumentHandler > xFilter = new XMLVersionListImport( xServiceFactory, aVersions ); 473 474 // connect parser and filter 475 Reference< XParser > xParser( xXMLParser, UNO_QUERY ); 476 xParser->setDocumentHandler( xFilter ); 477 478 // parse 479 try 480 { 481 xParser->parseStream( aParserInput ); 482 } 483 catch( SAXParseException& ) {} 484 catch( SAXException& ) {} 485 catch( io::IOException& ) {} 486 } 487 } 488 catch( uno::Exception& ) 489 { 490 // TODO: error handling 491 } 492 493 return aVersions; 494 } 495 496 uno::Sequence< rtl::OUString > SAL_CALL XMLVersionListPersistence_getSupportedServiceNames() 497 throw() 498 { 499 const rtl::OUString aServiceName( 500 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.DocumentRevisionListPersistence" ) ); 501 const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 ); 502 return aSeq; 503 } 504 505 rtl::OUString SAL_CALL XMLVersionListPersistence_getImplementationName() throw() 506 { 507 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLVersionListPersistence" ) ); 508 } 509 510 uno::Reference< uno::XInterface > SAL_CALL XMLVersionListPersistence_createInstance( 511 const uno::Reference< lang::XMultiServiceFactory > &) 512 throw( uno::Exception ) 513 { 514 return (cppu::OWeakObject*)new XMLVersionListPersistence; 515 } 516 517 uno::Sequence< rtl::OUString > SAL_CALL XMLVersionImExportOOO_getSupportedServiceNames() 518 throw() 519 { 520 const rtl::OUString aServiceName( 521 RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.DocumentRevisionListPersistence" ) ); 522 const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 ); 523 return aSeq; 524 } 525 526 rtl::OUString SAL_CALL XMLVersionImExportOOO_getImplementationName() throw() 527 { 528 return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLVersionImExportOOo" ) ); 529 } 530 531 uno::Reference< uno::XInterface > SAL_CALL XMLVersionImExportOOO_createInstance( 532 const uno::Reference< lang::XMultiServiceFactory > &) 533 throw( uno::Exception ) 534 { 535 return (cppu::OWeakObject*)new XMLVersionListPersistence; 536 } 537 538