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