nss: upgrade to release 3.73
[LibreOffice.git] / framework / source / xml / imagesdocumenthandler.cxx
blob51352b0296d4e5101b84adbc8f8fd70dc0da462c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <xml/imagesdocumenthandler.hxx>
22 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
23 #include <com/sun/star/xml/sax/SAXException.hpp>
25 #include <vcl/svapp.hxx>
26 #include <rtl/ustrbuf.hxx>
28 #include <comphelper/attributelist.hxx>
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::xml::sax;
33 #define ELEMENT_IMAGECONTAINER "imagescontainer"
34 #define ELEMENT_IMAGES "images"
35 #define ELEMENT_ENTRY "entry"
36 #define ELEMENT_EXTERNALIMAGES "externalimages"
37 #define ELEMENT_EXTERNALENTRY "externalentry"
39 #define ELEMENT_NS_IMAGESCONTAINER "image:imagescontainer"
40 #define ELEMENT_NS_IMAGES "image:images"
41 #define ELEMENT_NS_ENTRY "image:entry"
43 #define ATTRIBUTE_HREF "href"
44 #define ATTRIBUTE_MASKCOLOR "maskcolor"
45 #define ATTRIBUTE_COMMAND "command"
46 #define ATTRIBUTE_BITMAPINDEX "bitmap-index"
47 #define ATTRIBUTE_MASKURL "maskurl"
48 #define ATTRIBUTE_MASKMODE "maskmode"
49 #define ATTRIBUTE_HIGHCONTRASTURL "highcontrasturl"
50 #define ATTRIBUTE_HIGHCONTRASTMASKURL "highcontrastmaskurl"
51 #define ATTRIBUTE_TYPE_CDATA "CDATA"
53 #define ATTRIBUTE_XMLNS_IMAGE "xmlns:image"
54 #define ATTRIBUTE_XMLNS_XLINK "xmlns:xlink"
56 #define ATTRIBUTE_XLINK_TYPE "xlink:type"
57 #define ATTRIBUTE_XLINK_TYPE_VALUE "simple"
59 #define XMLNS_IMAGE "http://openoffice.org/2001/image"
60 #define XMLNS_XLINK "http://www.w3.org/1999/xlink"
61 #define XMLNS_IMAGE_PREFIX "image:"
63 #define XMLNS_FILTER_SEPARATOR "^"
65 #define IMAGES_DOCTYPE "<!DOCTYPE image:imagecontainer PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"image.dtd\">"
67 namespace framework
70 namespace {
72 struct ImageXMLEntryProperty
74 OReadImagesDocumentHandler::Image_XML_Namespace nNamespace;
75 char aEntryName[20];
80 ImageXMLEntryProperty const ImagesEntries[OReadImagesDocumentHandler::IMG_XML_ENTRY_COUNT] =
82 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGECONTAINER },
83 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGES },
84 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_ENTRY },
85 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALIMAGES },
86 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALENTRY },
87 { OReadImagesDocumentHandler::IMG_NS_XLINK, ATTRIBUTE_HREF },
88 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKCOLOR },
89 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_COMMAND },
90 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_BITMAPINDEX },
91 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKURL },
92 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKMODE },
93 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTURL },
94 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTMASKURL }
97 OReadImagesDocumentHandler::OReadImagesDocumentHandler( ImageItemDescriptorList& rItems ) :
98 m_rImageList( rItems )
100 // create hash map to speed up lookup
101 for ( int i = 0; i < int(IMG_XML_ENTRY_COUNT); i++ )
103 OUStringBuffer temp( 20 );
105 if ( ImagesEntries[i].nNamespace == IMG_NS_IMAGE )
106 temp.append( XMLNS_IMAGE );
107 else
108 temp.append( XMLNS_XLINK );
110 temp.append( XMLNS_FILTER_SEPARATOR );
111 temp.appendAscii( ImagesEntries[i].aEntryName );
112 m_aImageMap.emplace( temp.makeStringAndClear(), static_cast<Image_XML_Entry>(i) );
115 // reset states
116 m_bImageContainerStartFound = false;
117 m_bImageContainerEndFound = false;
118 m_bImagesStartFound = false;
121 OReadImagesDocumentHandler::~OReadImagesDocumentHandler()
125 // XDocumentHandler
126 void SAL_CALL OReadImagesDocumentHandler::startDocument()
130 void SAL_CALL OReadImagesDocumentHandler::endDocument()
132 SolarMutexGuard g;
134 if (m_bImageContainerStartFound != m_bImageContainerEndFound)
136 OUString aErrorMessage = getErrorLineString() + "No matching start or end element 'image:imagecontainer' found!";
137 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
141 void SAL_CALL OReadImagesDocumentHandler::startElement(
142 const OUString& aName, const Reference< XAttributeList > &xAttribs )
144 SolarMutexGuard g;
146 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
147 if ( pImageEntry == m_aImageMap.end() )
148 return;
150 switch ( pImageEntry->second )
152 case IMG_ELEMENT_IMAGECONTAINER:
154 // image:imagecontainer element (container element for all further image elements)
155 if ( m_bImageContainerStartFound )
157 OUString aErrorMessage = getErrorLineString() + "Element 'image:imagecontainer' cannot be embedded into 'image:imagecontainer'!";
158 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
161 m_bImageContainerStartFound = true;
163 break;
165 case IMG_ELEMENT_IMAGES:
167 if ( !m_bImageContainerStartFound )
169 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' must be embedded into element 'image:imagecontainer'!";
170 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
173 if ( m_bImagesStartFound )
175 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' cannot be embedded into 'image:images'!";
176 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
179 m_bImagesStartFound = true;
181 break;
183 case IMG_ELEMENT_ENTRY:
185 // Check that image:entry is embedded into image:images!
186 if ( !m_bImagesStartFound )
188 OUString aErrorMessage = getErrorLineString() + "Element 'image:entry' must be embedded into element 'image:images'!";
189 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
192 // Create new image item descriptor
193 ImageItemDescriptor aItem;
195 // Read attributes for this image definition
196 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
198 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) );
199 if ( pImageEntry != m_aImageMap.end() )
201 switch ( pImageEntry->second )
203 case IMG_ATTRIBUTE_COMMAND:
205 aItem.aCommandURL = xAttribs->getValueByIndex( n );
207 break;
209 default:
210 break;
215 // Check required attribute "command"
216 if ( aItem.aCommandURL.isEmpty() )
218 OUString aErrorMessage = getErrorLineString() + "Required attribute 'image:command' must have a value!";
219 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
222 m_rImageList.push_back( aItem );
224 break;
226 default:
227 break;
231 void SAL_CALL OReadImagesDocumentHandler::endElement(const OUString& aName)
233 SolarMutexGuard g;
235 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
236 if ( pImageEntry == m_aImageMap.end() )
237 return;
239 switch ( pImageEntry->second )
241 case IMG_ELEMENT_IMAGECONTAINER:
243 m_bImageContainerEndFound = true;
245 break;
247 case IMG_ELEMENT_IMAGES:
249 m_bImagesStartFound = false;
251 break;
253 default: break;
257 void SAL_CALL OReadImagesDocumentHandler::characters(const OUString&)
261 void SAL_CALL OReadImagesDocumentHandler::ignorableWhitespace(const OUString&)
265 void SAL_CALL OReadImagesDocumentHandler::processingInstruction(
266 const OUString& /*aTarget*/, const OUString& /*aData*/ )
270 void SAL_CALL OReadImagesDocumentHandler::setDocumentLocator(
271 const Reference< XLocator > &xLocator)
273 SolarMutexGuard g;
274 m_xLocator = xLocator;
277 OUString OReadImagesDocumentHandler::getErrorLineString()
279 SolarMutexGuard g;
280 if ( m_xLocator.is() )
282 OUStringBuffer buffer("Line: ");
283 buffer.append(m_xLocator->getLineNumber());
284 buffer.append(" - ");
285 return buffer.makeStringAndClear();
287 else
288 return OUString();
291 // OWriteImagesDocumentHandler
293 OWriteImagesDocumentHandler::OWriteImagesDocumentHandler(
294 const ImageItemDescriptorList& rItems,
295 Reference< XDocumentHandler > const & rWriteDocumentHandler ) :
296 m_rImageItemList( rItems ),
297 m_xWriteDocumentHandler( rWriteDocumentHandler )
299 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
300 m_xEmptyList.set( static_cast<XAttributeList *>(pList), UNO_QUERY );
301 m_aAttributeType = ATTRIBUTE_TYPE_CDATA;
302 m_aXMLImageNS = XMLNS_IMAGE_PREFIX;
303 m_aAttributeXlinkType = ATTRIBUTE_XLINK_TYPE;
304 m_aAttributeValueSimple = ATTRIBUTE_XLINK_TYPE_VALUE;
307 OWriteImagesDocumentHandler::~OWriteImagesDocumentHandler()
311 void OWriteImagesDocumentHandler::WriteImagesDocument()
313 SolarMutexGuard g;
315 m_xWriteDocumentHandler->startDocument();
317 // write DOCTYPE line!
318 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY );
319 if ( xExtendedDocHandler.is() )
321 xExtendedDocHandler->unknown( IMAGES_DOCTYPE );
322 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
325 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
327 pList->AddAttribute( ATTRIBUTE_XMLNS_IMAGE,
328 m_aAttributeType,
329 XMLNS_IMAGE );
331 pList->AddAttribute( ATTRIBUTE_XMLNS_XLINK,
332 m_aAttributeType,
333 XMLNS_XLINK );
335 m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGESCONTAINER, pList.get() );
336 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
338 WriteImageList( &m_rImageItemList );
340 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
341 m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGESCONTAINER );
342 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
343 m_xWriteDocumentHandler->endDocument();
346 // protected member functions
348 void OWriteImagesDocumentHandler::WriteImageList( const ImageItemDescriptorList* pImageList )
350 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
351 Reference< XAttributeList > xList( static_cast<XAttributeList *>(pList) , UNO_QUERY );
353 // save required attributes
354 pList->AddAttribute( m_aAttributeXlinkType,
355 m_aAttributeType,
356 m_aAttributeValueSimple );
358 m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGES, xList );
359 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
361 for (const ImageItemDescriptor & i : *pImageList)
362 WriteImage( &i );
364 m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGES );
365 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
368 void OWriteImagesDocumentHandler::WriteImage( const ImageItemDescriptor* pImage )
370 ::comphelper::AttributeList* pList = new ::comphelper::AttributeList;
371 Reference< XAttributeList > xList( static_cast<XAttributeList *>(pList) , UNO_QUERY );
373 pList->AddAttribute( m_aXMLImageNS + ATTRIBUTE_COMMAND,
374 m_aAttributeType,
375 pImage->aCommandURL );
377 m_xWriteDocumentHandler->startElement( ELEMENT_NS_ENTRY, xList );
378 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
380 m_xWriteDocumentHandler->endElement( ELEMENT_NS_ENTRY );
381 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
384 } // namespace framework
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */