Avoid potential negative array index access to cached text.
[LibreOffice.git] / framework / source / xml / imagesdocumenthandler.cxx
blobff5799f5c7758b64d50724b32c2fb3d560b58f3e
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 <rtl/ref.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 constexpr OUString ELEMENT_NS_IMAGESCONTAINER = u"image:imagescontainer"_ustr;
40 constexpr OUString ELEMENT_NS_IMAGES = u"image:images"_ustr;
41 constexpr OUString ELEMENT_NS_ENTRY = u"image:entry"_ustr;
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"
52 constexpr OUStringLiteral ATTRIBUTE_XMLNS_IMAGE = u"xmlns:image";
53 constexpr OUStringLiteral ATTRIBUTE_XMLNS_XLINK = u"xmlns:xlink";
55 constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE = u"xlink:type";
56 constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE_VALUE = u"simple";
58 constexpr OUString XMLNS_IMAGE = u"http://openoffice.org/2001/image"_ustr;
59 constexpr OUString XMLNS_XLINK = u"http://www.w3.org/1999/xlink"_ustr;
60 constexpr OUStringLiteral XMLNS_IMAGE_PREFIX = u"image:";
62 constexpr OUStringLiteral XMLNS_FILTER_SEPARATOR = u"^";
64 constexpr OUStringLiteral IMAGES_DOCTYPE = u"<!DOCTYPE image:imagecontainer PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"image.dtd\">";
66 namespace framework
69 namespace {
71 struct ImageXMLEntryProperty
73 OReadImagesDocumentHandler::Image_XML_Namespace nNamespace;
74 char aEntryName[20];
79 ImageXMLEntryProperty const ImagesEntries[OReadImagesDocumentHandler::IMG_XML_ENTRY_COUNT] =
81 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGECONTAINER },
82 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGES },
83 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_ENTRY },
84 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALIMAGES },
85 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALENTRY },
86 { OReadImagesDocumentHandler::IMG_NS_XLINK, ATTRIBUTE_HREF },
87 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKCOLOR },
88 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_COMMAND },
89 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_BITMAPINDEX },
90 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKURL },
91 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKMODE },
92 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTURL },
93 { OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTMASKURL }
96 OReadImagesDocumentHandler::OReadImagesDocumentHandler( ImageItemDescriptorList& rItems ) :
97 m_rImageList( rItems )
99 // create hash map to speed up lookup
100 for ( int i = 0; i < IMG_XML_ENTRY_COUNT; i++ )
102 OUStringBuffer temp( 20 );
104 if ( ImagesEntries[i].nNamespace == IMG_NS_IMAGE )
105 temp.append( XMLNS_IMAGE );
106 else
107 temp.append( XMLNS_XLINK );
109 temp.append( XMLNS_FILTER_SEPARATOR );
110 temp.appendAscii( ImagesEntries[i].aEntryName );
111 m_aImageMap.emplace( temp.makeStringAndClear(), static_cast<Image_XML_Entry>(i) );
114 // reset states
115 m_bImageContainerStartFound = false;
116 m_bImageContainerEndFound = false;
117 m_bImagesStartFound = false;
120 OReadImagesDocumentHandler::~OReadImagesDocumentHandler()
124 // XDocumentHandler
125 void SAL_CALL OReadImagesDocumentHandler::startDocument()
129 void SAL_CALL OReadImagesDocumentHandler::endDocument()
131 if (m_bImageContainerStartFound != m_bImageContainerEndFound)
133 OUString aErrorMessage = getErrorLineString() + "No matching start or end element 'image:imagecontainer' found!";
134 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
138 void SAL_CALL OReadImagesDocumentHandler::startElement(
139 const OUString& aName, const Reference< XAttributeList > &xAttribs )
141 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
142 if ( pImageEntry == m_aImageMap.end() )
143 return;
145 switch ( pImageEntry->second )
147 case IMG_ELEMENT_IMAGECONTAINER:
149 // image:imagecontainer element (container element for all further image elements)
150 if ( m_bImageContainerStartFound )
152 OUString aErrorMessage = getErrorLineString() + "Element 'image:imagecontainer' cannot be embedded into 'image:imagecontainer'!";
153 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
156 m_bImageContainerStartFound = true;
158 break;
160 case IMG_ELEMENT_IMAGES:
162 if ( !m_bImageContainerStartFound )
164 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' must be embedded into element 'image:imagecontainer'!";
165 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
168 if ( m_bImagesStartFound )
170 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' cannot be embedded into 'image:images'!";
171 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
174 m_bImagesStartFound = true;
176 break;
178 case IMG_ELEMENT_ENTRY:
180 // Check that image:entry is embedded into image:images!
181 if ( !m_bImagesStartFound )
183 OUString aErrorMessage = getErrorLineString() + "Element 'image:entry' must be embedded into element 'image:images'!";
184 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
187 // Create new image item descriptor
188 ImageItemDescriptor aItem;
190 // Read attributes for this image definition
191 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
193 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) );
194 if ( pImageEntry != m_aImageMap.end() )
196 switch ( pImageEntry->second )
198 case IMG_ATTRIBUTE_COMMAND:
200 aItem.aCommandURL = xAttribs->getValueByIndex( n );
202 break;
204 default:
205 break;
210 // Check required attribute "command"
211 if ( aItem.aCommandURL.isEmpty() )
213 OUString aErrorMessage = getErrorLineString() + "Required attribute 'image:command' must have a value!";
214 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
217 m_rImageList.push_back( aItem );
219 break;
221 default:
222 break;
226 void SAL_CALL OReadImagesDocumentHandler::endElement(const OUString& aName)
228 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
229 if ( pImageEntry == m_aImageMap.end() )
230 return;
232 switch ( pImageEntry->second )
234 case IMG_ELEMENT_IMAGECONTAINER:
236 m_bImageContainerEndFound = true;
238 break;
240 case IMG_ELEMENT_IMAGES:
242 m_bImagesStartFound = false;
244 break;
246 default: break;
250 void SAL_CALL OReadImagesDocumentHandler::characters(const OUString&)
254 void SAL_CALL OReadImagesDocumentHandler::ignorableWhitespace(const OUString&)
258 void SAL_CALL OReadImagesDocumentHandler::processingInstruction(
259 const OUString& /*aTarget*/, const OUString& /*aData*/ )
263 void SAL_CALL OReadImagesDocumentHandler::setDocumentLocator(
264 const Reference< XLocator > &xLocator)
266 m_xLocator = xLocator;
269 OUString OReadImagesDocumentHandler::getErrorLineString()
271 if ( m_xLocator.is() )
273 return "Line: " +
274 OUString::number(m_xLocator->getLineNumber()) +
275 " - ";
277 else
278 return OUString();
281 // OWriteImagesDocumentHandler
283 OWriteImagesDocumentHandler::OWriteImagesDocumentHandler(
284 const ImageItemDescriptorList& rItems,
285 Reference< XDocumentHandler > const & rWriteDocumentHandler ) :
286 m_rImageItemList( rItems ),
287 m_xWriteDocumentHandler( rWriteDocumentHandler )
289 m_xEmptyList = new ::comphelper::AttributeList;
290 m_aXMLImageNS = XMLNS_IMAGE_PREFIX;
291 m_aAttributeXlinkType = ATTRIBUTE_XLINK_TYPE;
292 m_aAttributeValueSimple = ATTRIBUTE_XLINK_TYPE_VALUE;
295 OWriteImagesDocumentHandler::~OWriteImagesDocumentHandler()
299 void OWriteImagesDocumentHandler::WriteImagesDocument()
301 m_xWriteDocumentHandler->startDocument();
303 // write DOCTYPE line!
304 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY );
305 if ( xExtendedDocHandler.is() )
307 xExtendedDocHandler->unknown( IMAGES_DOCTYPE );
308 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
311 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
313 pList->AddAttribute( ATTRIBUTE_XMLNS_IMAGE,
314 XMLNS_IMAGE );
316 pList->AddAttribute( ATTRIBUTE_XMLNS_XLINK,
317 XMLNS_XLINK );
319 m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGESCONTAINER, pList );
320 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
322 WriteImageList( &m_rImageItemList );
324 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
325 m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGESCONTAINER );
326 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
327 m_xWriteDocumentHandler->endDocument();
330 // protected member functions
332 void OWriteImagesDocumentHandler::WriteImageList( const ImageItemDescriptorList* pImageList )
334 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
336 // save required attributes
337 pList->AddAttribute( m_aAttributeXlinkType,
338 m_aAttributeValueSimple );
340 m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGES, pList );
341 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
343 for (const ImageItemDescriptor & i : *pImageList)
344 WriteImage( &i );
346 m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGES );
347 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
350 void OWriteImagesDocumentHandler::WriteImage( const ImageItemDescriptor* pImage )
352 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
354 pList->AddAttribute( m_aXMLImageNS + ATTRIBUTE_COMMAND,
355 pImage->aCommandURL );
357 m_xWriteDocumentHandler->startElement( ELEMENT_NS_ENTRY, pList );
358 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
360 m_xWriteDocumentHandler->endElement( ELEMENT_NS_ENTRY );
361 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
364 } // namespace framework
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */