1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_framework.hxx" 30 31 #include <stdio.h> 32 33 //_________________________________________________________________________________________________________________ 34 // my own includes 35 //_________________________________________________________________________________________________________________ 36 37 #include <threadhelp/resetableguard.hxx> 38 #include <xml/imagesdocumenthandler.hxx> 39 #include <macros/debug.hxx> 40 41 //_________________________________________________________________________________________________________________ 42 // interface includes 43 //_________________________________________________________________________________________________________________ 44 45 #ifndef __COM_SUN_STAR_XML_SAX_XEXTENDEDDOCUMENTHANDLER_HPP_ 46 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> 47 #endif 48 49 //_________________________________________________________________________________________________________________ 50 // other includes 51 //_________________________________________________________________________________________________________________ 52 #include <vcl/svapp.hxx> 53 #include <vcl/toolbox.hxx> 54 #include <rtl/ustrbuf.hxx> 55 56 #include <comphelper/attributelist.hxx> 57 58 //_________________________________________________________________________________________________________________ 59 // namespace 60 //_________________________________________________________________________________________________________________ 61 62 using namespace ::com::sun::star::uno; 63 using namespace ::com::sun::star::xml::sax; 64 65 #define ELEMENT_IMAGECONTAINER "imagescontainer" 66 #define ELEMENT_IMAGES "images" 67 #define ELEMENT_ENTRY "entry" 68 #define ELEMENT_EXTERNALIMAGES "externalimages" 69 #define ELEMENT_EXTERNALENTRY "externalentry" 70 71 #define ELEMENT_NS_IMAGESCONTAINER "image:imagescontainer" 72 #define ELEMENT_NS_IMAGES "image:images" 73 #define ELEMENT_NS_ENTRY "image:entry" 74 #define ELEMENT_NS_EXTERNALIMAGES "image:externalimages" 75 #define ELEMENT_NS_EXTERNALENTRY "image:externalentry" 76 77 #define ATTRIBUTE_HREF "href" 78 #define ATTRIBUTE_MASKCOLOR "maskcolor" 79 #define ATTRIBUTE_COMMAND "command" 80 #define ATTRIBUTE_BITMAPINDEX "bitmap-index" 81 #define ATTRIBUTE_MASKURL "maskurl" 82 #define ATTRIBUTE_MASKMODE "maskmode" 83 #define ATTRIBUTE_HIGHCONTRASTURL "highcontrasturl" 84 #define ATTRIBUTE_HIGHCONTRASTMASKURL "highcontrastmaskurl" 85 #define ATTRIBUTE_TYPE_CDATA "CDATA" 86 87 #define ATTRIBUTE_MASKMODE_BITMAP "maskbitmap" 88 #define ATTRIBUTE_MASKMODE_COLOR "maskcolor" 89 90 #define ATTRIBUTE_XMLNS_IMAGE "xmlns:image" 91 #define ATTRIBUTE_XMLNS_XLINK "xmlns:xlink" 92 93 #define ATTRIBUTE_XLINK_TYPE "xlink:type" 94 #define ATTRIBUTE_XLINK_TYPE_VALUE "simple" 95 96 #define XMLNS_IMAGE "http://openoffice.org/2001/image" 97 #define XMLNS_XLINK "http://www.w3.org/1999/xlink" 98 #define XMLNS_IMAGE_PREFIX "image:" 99 #define XMLNS_XLINK_PREFIX "xlink:" 100 101 #define XMLNS_FILTER_SEPARATOR "^" 102 103 #define IMAGES_DOCTYPE "<!DOCTYPE image:imagecontainer PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"image.dtd\">" 104 105 namespace framework 106 { 107 108 struct ImageXMLEntryProperty 109 { 110 OReadImagesDocumentHandler::Image_XML_Namespace nNamespace; 111 char aEntryName[20]; 112 }; 113 114 ImageXMLEntryProperty ImagesEntries[OReadImagesDocumentHandler::IMG_XML_ENTRY_COUNT] = 115 { 116 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGECONTAINER }, 117 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGES }, 118 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_ENTRY }, 119 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALIMAGES }, 120 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALENTRY }, 121 { OReadImagesDocumentHandler::IMG_NS_XLINK, ATTRIBUTE_HREF }, 122 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKCOLOR }, 123 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_COMMAND }, 124 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_BITMAPINDEX }, 125 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKURL }, 126 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKMODE }, 127 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTURL }, 128 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTMASKURL } 129 }; 130 131 132 OReadImagesDocumentHandler::OReadImagesDocumentHandler( ImageListsDescriptor& aItems ) : 133 ThreadHelpBase( &Application::GetSolarMutex() ), 134 m_aImageList( aItems ), 135 m_pImages( 0 ), 136 m_pExternalImages( 0 ) 137 { 138 m_aImageList.pImageList = NULL; 139 m_aImageList.pExternalImageList = NULL; 140 141 m_nHashMaskModeBitmap = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE_BITMAP )).hashCode(); 142 m_nHashMaskModeColor = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE_COLOR )).hashCode(); 143 144 // create hash map to speed up lookup 145 for ( int i = 0; i < (int)IMG_XML_ENTRY_COUNT; i++ ) 146 { 147 ::rtl::OUStringBuffer temp( 20 ); 148 149 if ( ImagesEntries[i].nNamespace == IMG_NS_IMAGE ) 150 temp.appendAscii( XMLNS_IMAGE ); 151 else 152 temp.appendAscii( XMLNS_XLINK ); 153 154 temp.appendAscii( XMLNS_FILTER_SEPARATOR ); 155 temp.appendAscii( ImagesEntries[i].aEntryName ); 156 m_aImageMap.insert( ImageHashMap::value_type( temp.makeStringAndClear(), (Image_XML_Entry)i ) ); 157 } 158 159 // reset states 160 m_bImageContainerStartFound = sal_False; 161 m_bImageContainerEndFound = sal_False; 162 m_bImagesStartFound = sal_False; 163 m_bImagesEndFound = sal_False; 164 m_bImageStartFound = sal_False; 165 m_bExternalImagesStartFound = sal_False; 166 m_bExternalImagesEndFound = sal_False; 167 m_bExternalImageStartFound = sal_False; 168 } 169 170 OReadImagesDocumentHandler::~OReadImagesDocumentHandler() 171 { 172 } 173 174 // XDocumentHandler 175 void SAL_CALL OReadImagesDocumentHandler::startDocument(void) 176 throw ( SAXException, RuntimeException ) 177 { 178 } 179 180 void SAL_CALL OReadImagesDocumentHandler::endDocument(void) 181 throw( SAXException, RuntimeException ) 182 { 183 ResetableGuard aGuard( m_aLock ); 184 185 if (( m_bImageContainerStartFound && !m_bImageContainerEndFound ) || 186 ( !m_bImageContainerStartFound && m_bImageContainerEndFound ) ) 187 { 188 ::rtl::OUString aErrorMessage = getErrorLineString(); 189 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "No matching start or end element 'image:imagecontainer' found!" )); 190 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 191 } 192 } 193 194 void SAL_CALL OReadImagesDocumentHandler::startElement( 195 const ::rtl::OUString& aName, const Reference< XAttributeList > &xAttribs ) 196 throw( SAXException, RuntimeException ) 197 { 198 ResetableGuard aGuard( m_aLock ); 199 200 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName ) ; 201 if ( pImageEntry != m_aImageMap.end() ) 202 { 203 switch ( pImageEntry->second ) 204 { 205 case IMG_ELEMENT_IMAGECONTAINER: 206 { 207 // image:imagecontainer element (container element for all further image elements) 208 if ( m_bImageContainerStartFound ) 209 { 210 ::rtl::OUString aErrorMessage = getErrorLineString(); 211 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:imagecontainer' cannot be embeded into 'image:imagecontainer'!" )); 212 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 213 } 214 215 m_bImageContainerStartFound = sal_True; 216 } 217 break; 218 219 case IMG_ELEMENT_IMAGES: 220 { 221 if ( !m_bImageContainerStartFound ) 222 { 223 ::rtl::OUString aErrorMessage = getErrorLineString(); 224 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:images' must be embeded into element 'image:imagecontainer'!" )); 225 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 226 } 227 228 if ( m_bImagesStartFound ) 229 { 230 ::rtl::OUString aErrorMessage = getErrorLineString(); 231 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:images' cannot be embeded into 'image:images'!" )); 232 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 233 } 234 235 if ( !m_aImageList.pImageList ) 236 m_aImageList.pImageList = new ImageListDescriptor; 237 238 m_bImagesStartFound = sal_True; 239 m_pImages = new ImageListItemDescriptor; 240 241 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) 242 { 243 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) ); 244 if ( pImageEntry != m_aImageMap.end() ) 245 { 246 switch ( pImageEntry->second ) 247 { 248 case IMG_ATTRIBUTE_HREF: 249 { 250 m_pImages->aURL = xAttribs->getValueByIndex( n ); 251 } 252 break; 253 254 case IMG_ATTRIBUTE_MASKCOLOR: 255 { 256 ::rtl::OUString aColor = xAttribs->getValueByIndex( n ); 257 258 if ( aColor.getLength() > 0 ) 259 { 260 if ( aColor.getStr()[0] == '#' ) 261 { 262 // the color value is given as #rrggbb and used the hexadecimal system!! 263 sal_uInt32 nColor = aColor.copy( 1 ).toInt32( 16 ); 264 265 m_pImages->aMaskColor = Color( COLORDATA_RGB( nColor ) ); 266 } 267 } 268 } 269 break; 270 271 case IMG_ATTRIBUTE_MASKURL: 272 { 273 m_pImages->aMaskURL = xAttribs->getValueByIndex( n ); 274 } 275 break; 276 277 case IMG_ATTRIBUTE_MASKMODE: 278 { 279 sal_Int32 nHashCode = xAttribs->getValueByIndex( n ).hashCode(); 280 if ( nHashCode == m_nHashMaskModeBitmap ) 281 m_pImages->nMaskMode = ImageMaskMode_Bitmap; 282 else if ( nHashCode == m_nHashMaskModeColor ) 283 m_pImages->nMaskMode = ImageMaskMode_Color; 284 else 285 { 286 delete m_pImages; 287 m_pImages = NULL; 288 289 ::rtl::OUString aErrorMessage = getErrorLineString(); 290 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Attribute image:maskmode must be 'maskcolor' or 'maskbitmap'!" )); 291 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 292 } 293 } 294 break; 295 296 case IMG_ATTRIBUTE_HIGHCONTRASTURL: 297 { 298 m_pImages->aHighContrastURL = xAttribs->getValueByIndex( n ); 299 } 300 break; 301 302 case IMG_ATTRIBUTE_HIGHCONTRASTMASKURL: 303 { 304 m_pImages->aHighContrastMaskURL = xAttribs->getValueByIndex( n ); 305 } 306 break; 307 308 default: 309 break; 310 } 311 } 312 } // for 313 314 if ( m_pImages->aURL.Len() == 0 ) 315 { 316 delete m_pImages; 317 m_pImages = NULL; 318 319 ::rtl::OUString aErrorMessage = getErrorLineString(); 320 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute xlink:href must have a value!" )); 321 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 322 } 323 } 324 break; 325 326 case IMG_ELEMENT_ENTRY: 327 { 328 // Check that image:entry is embeded into image:images! 329 if ( !m_bImagesStartFound ) 330 { 331 delete m_pImages; 332 m_pImages = NULL; 333 334 ::rtl::OUString aErrorMessage = getErrorLineString(); 335 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:entry' must be embeded into element 'image:images'!" )); 336 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 337 } 338 339 if ( !m_pImages->pImageItemList ) 340 m_pImages->pImageItemList = new ImageItemListDescriptor; 341 342 m_bImageStartFound = sal_True; 343 344 // Create new image item descriptor 345 ImageItemDescriptor* pItem = new ImageItemDescriptor; 346 pItem->nIndex = -1; 347 348 // Read attributes for this image definition 349 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) 350 { 351 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) ); 352 if ( pImageEntry != m_aImageMap.end() ) 353 { 354 switch ( pImageEntry->second ) 355 { 356 case IMG_ATTRIBUTE_COMMAND: 357 { 358 pItem->aCommandURL = xAttribs->getValueByIndex( n ); 359 } 360 break; 361 362 case IMG_ATTRIBUTE_BITMAPINDEX: 363 { 364 pItem->nIndex = xAttribs->getValueByIndex( n ).toInt32(); 365 } 366 break; 367 368 default: 369 break; 370 } 371 } 372 } 373 374 // Check required attribute "bitmap-index" 375 if ( pItem->nIndex < 0 ) 376 { 377 delete pItem; 378 delete m_pImages; 379 m_pImages = NULL; 380 381 ::rtl::OUString aErrorMessage = getErrorLineString(); 382 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute 'image:bitmap-index' must have a value >= 0!" )); 383 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 384 } 385 386 // Check required attribute "command" 387 if ( pItem->aCommandURL.Len() == 0 ) 388 { 389 delete pItem; 390 delete m_pImages; 391 m_pImages = NULL; 392 393 ::rtl::OUString aErrorMessage = getErrorLineString(); 394 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute 'image:command' must have a value!" )); 395 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 396 } 397 398 if ( m_pImages ) 399 m_pImages->pImageItemList->Insert( pItem, m_pImages->pImageItemList->Count() ); 400 } 401 break; 402 403 case IMG_ELEMENT_EXTERNALIMAGES: 404 { 405 // Check that image:externalimages is embeded into image:imagecontainer 406 if ( !m_bImageContainerStartFound ) 407 { 408 delete m_pImages; 409 m_pImages = NULL; 410 411 ::rtl::OUString aErrorMessage = getErrorLineString(); 412 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:externalimages' must be embeded into element 'image:imagecontainer'!" )); 413 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 414 } 415 416 // Check that image:externalentry is NOT embeded into image:externalentry 417 if ( m_bExternalImagesStartFound ) 418 { 419 delete m_pImages; 420 m_pImages = NULL; 421 422 ::rtl::OUString aErrorMessage = getErrorLineString(); 423 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:externalimages' cannot be embeded into 'image:externalimages'!" )); 424 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 425 } 426 427 // Create unique external image container 428 m_bExternalImagesStartFound = sal_True; 429 m_pExternalImages = new ExternalImageItemListDescriptor; 430 } 431 break; 432 433 case IMG_ELEMENT_EXTERNALENTRY: 434 { 435 if ( !m_bExternalImagesStartFound ) 436 { 437 delete m_pImages; 438 delete m_pExternalImages; 439 m_pImages = NULL; 440 m_pExternalImages = NULL; 441 442 ::rtl::OUString aErrorMessage = getErrorLineString(); 443 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:externalentry' must be embeded into 'image:externalimages'!" )); 444 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 445 } 446 447 if ( m_bExternalImageStartFound ) 448 { 449 delete m_pImages; 450 delete m_pExternalImages; 451 m_pImages = NULL; 452 m_pExternalImages = NULL; 453 454 ::rtl::OUString aErrorMessage = getErrorLineString(); 455 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Element 'image:externalentry' cannot be embeded into 'image:externalentry'!" )); 456 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 457 } 458 459 m_bExternalImageStartFound = sal_True; 460 461 ExternalImageItemDescriptor* pItem = new ExternalImageItemDescriptor; 462 463 // Read attributes for this external image definition 464 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) 465 { 466 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) ); 467 if ( pImageEntry != m_aImageMap.end() ) 468 { 469 switch ( pImageEntry->second ) 470 { 471 case IMG_ATTRIBUTE_COMMAND: 472 { 473 pItem->aCommandURL = xAttribs->getValueByIndex( n ); 474 } 475 break; 476 477 case IMG_ATTRIBUTE_HREF: 478 { 479 pItem->aURL = xAttribs->getValueByIndex( n ); 480 } 481 break; 482 483 default: 484 break; 485 } 486 } 487 } 488 489 // Check required attribute "command" 490 if ( pItem->aCommandURL.Len() == 0 ) 491 { 492 delete pItem; 493 delete m_pImages; 494 delete m_pExternalImages; 495 m_pImages = NULL; 496 m_pExternalImages = NULL; 497 498 ::rtl::OUString aErrorMessage = getErrorLineString(); 499 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute 'image:command' must have a value!" )); 500 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 501 } 502 503 // Check required attribute "href" 504 if ( pItem->aURL.Len() == 0 ) 505 { 506 delete pItem; 507 delete m_pImages; 508 delete m_pExternalImages; 509 m_pImages = NULL; 510 m_pExternalImages = NULL; 511 512 ::rtl::OUString aErrorMessage = getErrorLineString(); 513 aErrorMessage += ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Required attribute 'xlink:href' must have a value!" )); 514 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); 515 } 516 517 if ( m_pExternalImages ) 518 m_pExternalImages->Insert( pItem, m_pExternalImages->Count() ); 519 } 520 break; 521 522 default: 523 break; 524 } 525 } 526 } 527 528 void SAL_CALL OReadImagesDocumentHandler::endElement(const ::rtl::OUString& aName) 529 throw( SAXException, RuntimeException ) 530 { 531 ResetableGuard aGuard( m_aLock ); 532 533 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName ) ; 534 if ( pImageEntry != m_aImageMap.end() ) 535 { 536 switch ( pImageEntry->second ) 537 { 538 case IMG_ELEMENT_IMAGECONTAINER: 539 { 540 m_bImageContainerEndFound = sal_True; 541 } 542 break; 543 544 case IMG_ELEMENT_IMAGES: 545 { 546 if ( m_pImages ) 547 { 548 if ( m_aImageList.pImageList ) 549 m_aImageList.pImageList->Insert( m_pImages, m_aImageList.pImageList->Count() ); 550 m_pImages = NULL; 551 } 552 m_bImagesStartFound = sal_False; 553 } 554 break; 555 556 case IMG_ELEMENT_ENTRY: 557 { 558 m_bImageStartFound = sal_False; 559 } 560 break; 561 562 case IMG_ELEMENT_EXTERNALIMAGES: 563 { 564 if ( m_pExternalImages && !m_aImageList.pExternalImageList ) 565 { 566 if ( !m_aImageList.pExternalImageList ) 567 m_aImageList.pExternalImageList = m_pExternalImages; 568 } 569 570 m_bExternalImagesStartFound = sal_False; 571 m_pExternalImages = NULL; 572 } 573 break; 574 575 case IMG_ELEMENT_EXTERNALENTRY: 576 { 577 m_bExternalImageStartFound = sal_False; 578 } 579 break; 580 581 default: 582 break; 583 } 584 } 585 } 586 587 void SAL_CALL OReadImagesDocumentHandler::characters(const ::rtl::OUString&) 588 throw( SAXException, RuntimeException ) 589 { 590 } 591 592 void SAL_CALL OReadImagesDocumentHandler::ignorableWhitespace(const ::rtl::OUString&) 593 throw( SAXException, RuntimeException ) 594 { 595 } 596 597 void SAL_CALL OReadImagesDocumentHandler::processingInstruction( 598 const ::rtl::OUString& /*aTarget*/, const ::rtl::OUString& /*aData*/ ) 599 throw( SAXException, RuntimeException ) 600 { 601 } 602 603 void SAL_CALL OReadImagesDocumentHandler::setDocumentLocator( 604 const Reference< XLocator > &xLocator) 605 throw( SAXException, RuntimeException ) 606 { 607 ResetableGuard aGuard( m_aLock ); 608 609 m_xLocator = xLocator; 610 } 611 612 ::rtl::OUString OReadImagesDocumentHandler::getErrorLineString() 613 { 614 ResetableGuard aGuard( m_aLock ); 615 616 char buffer[32]; 617 618 if ( m_xLocator.is() ) 619 { 620 snprintf( buffer, sizeof(buffer), "Line: %ld - ", static_cast<long>( m_xLocator->getLineNumber() )); 621 return ::rtl::OUString::createFromAscii( buffer ); 622 } 623 else 624 return ::rtl::OUString(); 625 } 626 627 628 //_________________________________________________________________________________________________________________ 629 // OWriteImagesDocumentHandler 630 //_________________________________________________________________________________________________________________ 631 632 OWriteImagesDocumentHandler::OWriteImagesDocumentHandler( 633 const ImageListsDescriptor& aItems, 634 Reference< XDocumentHandler > rWriteDocumentHandler ) : 635 ThreadHelpBase( &Application::GetSolarMutex() ), 636 m_aImageListsItems( aItems ), 637 m_xWriteDocumentHandler( rWriteDocumentHandler ) 638 { 639 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 640 m_xEmptyList = Reference< XAttributeList >( (XAttributeList *) pList, UNO_QUERY ); 641 m_aAttributeType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE_CDATA )); 642 m_aXMLImageNS = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_IMAGE_PREFIX )); 643 m_aXMLXlinkNS = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK_PREFIX )); 644 m_aAttributeXlinkType = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XLINK_TYPE )); 645 m_aAttributeValueSimple = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XLINK_TYPE_VALUE )); 646 } 647 648 OWriteImagesDocumentHandler::~OWriteImagesDocumentHandler() 649 { 650 } 651 652 void OWriteImagesDocumentHandler::WriteImagesDocument() throw 653 ( SAXException, RuntimeException ) 654 { 655 ResetableGuard aGuard( m_aLock ); 656 657 m_xWriteDocumentHandler->startDocument(); 658 659 // write DOCTYPE line! 660 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY ); 661 if ( xExtendedDocHandler.is() ) 662 { 663 xExtendedDocHandler->unknown( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMAGES_DOCTYPE )) ); 664 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 665 } 666 667 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 668 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 669 670 pList->AddAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XMLNS_IMAGE )), 671 m_aAttributeType, 672 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_IMAGE )) ); 673 674 pList->AddAttribute( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_XMLNS_XLINK )), 675 m_aAttributeType, 676 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( XMLNS_XLINK )) ); 677 678 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_IMAGESCONTAINER )), pList ); 679 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 680 681 if ( m_aImageListsItems.pImageList ) 682 { 683 ImageListDescriptor* pImageList = m_aImageListsItems.pImageList; 684 685 for ( sal_uInt16 i = 0; i < m_aImageListsItems.pImageList->Count(); i++ ) 686 { 687 const ImageListItemDescriptor* pImageItems = (*pImageList)[i]; 688 WriteImageList( pImageItems ); 689 } 690 } 691 692 if ( m_aImageListsItems.pExternalImageList ) 693 { 694 WriteExternalImageList( m_aImageListsItems.pExternalImageList ); 695 } 696 697 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 698 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_IMAGESCONTAINER )) ); 699 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 700 m_xWriteDocumentHandler->endDocument(); 701 } 702 703 //_________________________________________________________________________________________________________________ 704 // protected member functions 705 //_________________________________________________________________________________________________________________ 706 707 void OWriteImagesDocumentHandler::WriteImageList( const ImageListItemDescriptor* pImageList ) throw 708 ( SAXException, RuntimeException ) 709 { 710 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 711 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 712 713 // save required attributes 714 pList->AddAttribute( m_aAttributeXlinkType, 715 m_aAttributeType, 716 m_aAttributeValueSimple ); 717 718 pList->AddAttribute( m_aXMLXlinkNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_HREF )), 719 m_aAttributeType, 720 pImageList->aURL ); 721 722 if ( pImageList->nMaskMode == ImageMaskMode_Bitmap ) 723 { 724 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE )), 725 m_aAttributeType, 726 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE_BITMAP )) ); 727 728 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKURL )), 729 m_aAttributeType, 730 pImageList->aMaskURL ); 731 732 if ( pImageList->aHighContrastMaskURL.Len() > 0 ) 733 { 734 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_HIGHCONTRASTMASKURL )), 735 m_aAttributeType, 736 pImageList->aHighContrastMaskURL ); 737 } 738 } 739 else 740 { 741 ::rtl::OUStringBuffer aColorStrBuffer( 8 ); 742 sal_Int64 nValue = pImageList->aMaskColor.GetRGBColor(); 743 744 aColorStrBuffer.appendAscii( "#" ); 745 aColorStrBuffer.append( ::rtl::OUString::valueOf( nValue, 16 )); 746 747 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKCOLOR )), 748 m_aAttributeType, 749 aColorStrBuffer.makeStringAndClear() ); 750 751 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE )), 752 m_aAttributeType, 753 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MASKMODE_COLOR )) ); 754 } 755 756 if ( pImageList->aHighContrastURL.Len() > 0 ) 757 { 758 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_HIGHCONTRASTURL )), 759 m_aAttributeType, 760 pImageList->aHighContrastURL ); 761 } 762 763 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_IMAGES )), xList ); 764 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 765 766 ImageItemListDescriptor* pImageItemList = pImageList->pImageItemList; 767 if ( pImageItemList ) 768 { 769 for ( sal_uInt16 i = 0; i < pImageItemList->Count(); i++ ) 770 WriteImage( (*pImageItemList)[i] ); 771 } 772 773 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_IMAGES )) ); 774 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 775 } 776 777 void OWriteImagesDocumentHandler::WriteImage( const ImageItemDescriptor* pImage ) throw 778 ( SAXException, RuntimeException ) 779 { 780 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 781 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 782 783 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_BITMAPINDEX )), 784 m_aAttributeType, 785 ::rtl::OUString::valueOf( (sal_Int32)pImage->nIndex ) ); 786 787 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_COMMAND )), 788 m_aAttributeType, 789 pImage->aCommandURL ); 790 791 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_ENTRY )), xList ); 792 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 793 794 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_ENTRY )) ); 795 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 796 } 797 798 void OWriteImagesDocumentHandler::WriteExternalImageList( const ExternalImageItemListDescriptor* pExternalImageList ) throw 799 ( SAXException, RuntimeException ) 800 { 801 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EXTERNALIMAGES )), m_xEmptyList ); 802 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 803 804 for ( sal_uInt16 i = 0; i < pExternalImageList->Count(); i++ ) 805 { 806 ExternalImageItemDescriptor* pItem = (*pExternalImageList)[i]; 807 WriteExternalImage( pItem ); 808 } 809 810 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 811 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EXTERNALIMAGES )) ); 812 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 813 } 814 815 void OWriteImagesDocumentHandler::WriteExternalImage( const ExternalImageItemDescriptor* pExternalImage ) throw 816 ( SAXException, RuntimeException ) 817 { 818 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList; 819 Reference< XAttributeList > xList( (XAttributeList *) pList , UNO_QUERY ); 820 821 // save required attributes 822 pList->AddAttribute( m_aAttributeXlinkType, 823 m_aAttributeType, 824 m_aAttributeValueSimple ); 825 826 if ( pExternalImage->aURL.Len() > 0 ) 827 { 828 pList->AddAttribute( m_aXMLXlinkNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_HREF )), 829 m_aAttributeType, 830 pExternalImage->aURL ); 831 } 832 833 if ( pExternalImage->aCommandURL.Len() > 0 ) 834 { 835 pList->AddAttribute( m_aXMLImageNS + ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_COMMAND )), 836 m_aAttributeType, 837 pExternalImage->aCommandURL ); 838 } 839 840 m_xWriteDocumentHandler->startElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EXTERNALENTRY )), xList ); 841 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 842 843 m_xWriteDocumentHandler->endElement( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_NS_EXTERNALENTRY )) ); 844 m_xWriteDocumentHandler->ignorableWhitespace( ::rtl::OUString() ); 845 } 846 847 } // namespace framework 848 849 850 851 852