LibreOffice Module framework (master) 1
imagesdocumenthandler.cxx
Go to the documentation of this file.
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
21
22#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
23#include <com/sun/star/xml/sax/SAXException.hpp>
24
25#include <rtl/ref.hxx>
26#include <rtl/ustrbuf.hxx>
27
29
30using namespace ::com::sun::star::uno;
31using namespace ::com::sun::star::xml::sax;
32
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"
38
39constexpr OUStringLiteral ELEMENT_NS_IMAGESCONTAINER = u"image:imagescontainer";
40constexpr OUStringLiteral ELEMENT_NS_IMAGES = u"image:images";
41constexpr OUStringLiteral ELEMENT_NS_ENTRY = u"image:entry";
42
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
52constexpr OUStringLiteral ATTRIBUTE_XMLNS_IMAGE = u"xmlns:image";
53constexpr OUStringLiteral ATTRIBUTE_XMLNS_XLINK = u"xmlns:xlink";
54
55constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE = u"xlink:type";
56constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE_VALUE = u"simple";
57
58constexpr OUStringLiteral XMLNS_IMAGE = u"http://openoffice.org/2001/image";
59constexpr OUStringLiteral XMLNS_XLINK = u"http://www.w3.org/1999/xlink";
60constexpr OUStringLiteral XMLNS_IMAGE_PREFIX = u"image:";
61
62constexpr OUStringLiteral XMLNS_FILTER_SEPARATOR = u"^";
63
64constexpr OUStringLiteral IMAGES_DOCTYPE = u"<!DOCTYPE image:imagecontainer PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"image.dtd\">";
65
66namespace framework
67{
68
69namespace {
70
71struct ImageXMLEntryProperty
72{
74 char aEntryName[20];
75};
76
77}
78
80{
94};
95
97 m_rImageList( rItems )
98{
99 // create hash map to speed up lookup
100 for ( int i = 0; i < IMG_XML_ENTRY_COUNT; i++ )
101 {
102 OUStringBuffer temp( 20 );
103
105 temp.append( XMLNS_IMAGE );
106 else
107 temp.append( XMLNS_XLINK );
108
109 temp.append( XMLNS_FILTER_SEPARATOR );
110 temp.appendAscii( ImagesEntries[i].aEntryName );
111 m_aImageMap.emplace( temp.makeStringAndClear(), static_cast<Image_XML_Entry>(i) );
112 }
113
114 // reset states
117 m_bImagesStartFound = false;
118}
119
121{
122}
123
124// XDocumentHandler
126{
127}
128
130{
132 {
133 OUString aErrorMessage = getErrorLineString() + "No matching start or end element 'image:imagecontainer' found!";
134 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
135 }
136}
137
139 const OUString& aName, const Reference< XAttributeList > &xAttribs )
140{
141 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
142 if ( pImageEntry == m_aImageMap.end() )
143 return;
144
145 switch ( pImageEntry->second )
146 {
148 {
149 // image:imagecontainer element (container element for all further image elements)
151 {
152 OUString aErrorMessage = getErrorLineString() + "Element 'image:imagecontainer' cannot be embedded into 'image:imagecontainer'!";
153 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
154 }
155
157 }
158 break;
159
161 {
163 {
164 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' must be embedded into element 'image:imagecontainer'!";
165 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
166 }
167
169 {
170 OUString aErrorMessage = getErrorLineString() + "Element 'image:images' cannot be embedded into 'image:images'!";
171 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
172 }
173
174 m_bImagesStartFound = true;
175 }
176 break;
177
179 {
180 // Check that image:entry is embedded into image:images!
181 if ( !m_bImagesStartFound )
182 {
183 OUString aErrorMessage = getErrorLineString() + "Element 'image:entry' must be embedded into element 'image:images'!";
184 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
185 }
186
187 // Create new image item descriptor
189
190 // Read attributes for this image definition
191 for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
192 {
193 pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) );
194 if ( pImageEntry != m_aImageMap.end() )
195 {
196 switch ( pImageEntry->second )
197 {
199 {
200 aItem.aCommandURL = xAttribs->getValueByIndex( n );
201 }
202 break;
203
204 default:
205 break;
206 }
207 }
208 }
209
210 // Check required attribute "command"
211 if ( aItem.aCommandURL.isEmpty() )
212 {
213 OUString aErrorMessage = getErrorLineString() + "Required attribute 'image:command' must have a value!";
214 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
215 }
216
217 m_rImageList.push_back( aItem );
218 }
219 break;
220
221 default:
222 break;
223 }
224}
225
226void SAL_CALL OReadImagesDocumentHandler::endElement(const OUString& aName)
227{
228 ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
229 if ( pImageEntry == m_aImageMap.end() )
230 return;
231
232 switch ( pImageEntry->second )
233 {
235 {
237 }
238 break;
239
241 {
242 m_bImagesStartFound = false;
243 }
244 break;
245
246 default: break;
247 }
248}
249
250void SAL_CALL OReadImagesDocumentHandler::characters(const OUString&)
251{
252}
253
255{
256}
257
259 const OUString& /*aTarget*/, const OUString& /*aData*/ )
260{
261}
262
264 const Reference< XLocator > &xLocator)
265{
266 m_xLocator = xLocator;
267}
268
270{
271 if ( m_xLocator.is() )
272 {
273 return "Line: " +
274 OUString::number(m_xLocator->getLineNumber()) +
275 " - ";
276 }
277 else
278 return OUString();
279}
280
281// OWriteImagesDocumentHandler
282
284 const ImageItemDescriptorList& rItems,
285 Reference< XDocumentHandler > const & rWriteDocumentHandler ) :
286 m_rImageItemList( rItems ),
287 m_xWriteDocumentHandler( rWriteDocumentHandler )
288{
289 m_xEmptyList = new ::comphelper::AttributeList;
293}
294
296{
297}
298
300{
301 m_xWriteDocumentHandler->startDocument();
302
303 // write DOCTYPE line!
304 Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY );
305 if ( xExtendedDocHandler.is() )
306 {
307 xExtendedDocHandler->unknown( IMAGES_DOCTYPE );
308 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
309 }
310
311 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
312
313 pList->AddAttribute( ATTRIBUTE_XMLNS_IMAGE,
314 XMLNS_IMAGE );
315
316 pList->AddAttribute( ATTRIBUTE_XMLNS_XLINK,
317 XMLNS_XLINK );
318
320 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
321
323
324 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
326 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
327 m_xWriteDocumentHandler->endDocument();
328}
329
330// protected member functions
331
333{
334 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
335
336 // save required attributes
337 pList->AddAttribute( m_aAttributeXlinkType,
339
340 m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGES, pList );
341 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
342
343 for (const ImageItemDescriptor & i : *pImageList)
344 WriteImage( &i );
345
347 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
348}
349
351{
352 rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
353
354 pList->AddAttribute( m_aXMLImageNS + ATTRIBUTE_COMMAND,
355 pImage->aCommandURL );
356
357 m_xWriteDocumentHandler->startElement( ELEMENT_NS_ENTRY, pList );
358 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
359
361 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
362}
363
364} // namespace framework
365
366/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL startElement(const OUString &aName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttribs) override
virtual void SAL_CALL startDocument() override
virtual void SAL_CALL ignorableWhitespace(const OUString &aWhitespaces) override
virtual void SAL_CALL characters(const OUString &aChars) override
virtual void SAL_CALL endDocument() override
OReadImagesDocumentHandler(ImageItemDescriptorList &aItems)
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &xLocator) override
virtual void SAL_CALL processingInstruction(const OUString &aTarget, const OUString &aData) override
virtual void SAL_CALL endElement(const OUString &aName) override
css::uno::Reference< css::xml::sax::XLocator > m_xLocator
css::uno::Reference< css::xml::sax::XAttributeList > m_xEmptyList
css::uno::Reference< css::xml::sax::XDocumentHandler > m_xWriteDocumentHandler
void WriteImageList(const ImageItemDescriptorList *)
void WriteImage(const ImageItemDescriptor *)
const ImageItemDescriptorList & m_rImageItemList
OWriteImagesDocumentHandler(const ImageItemDescriptorList &aItems, css::uno::Reference< css::xml::sax::XDocumentHandler > const &rWriteDocumentHandler)
float u
constexpr OUStringLiteral ELEMENT_NS_IMAGES
#define ATTRIBUTE_HIGHCONTRASTMASKURL
constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE
constexpr OUStringLiteral XMLNS_IMAGE
constexpr OUStringLiteral XMLNS_FILTER_SEPARATOR
#define ATTRIBUTE_MASKCOLOR
#define ATTRIBUTE_HREF
#define ELEMENT_ENTRY
constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE_VALUE
#define ELEMENT_IMAGES
#define ELEMENT_EXTERNALENTRY
#define ELEMENT_EXTERNALIMAGES
#define ELEMENT_IMAGECONTAINER
OReadImagesDocumentHandler::Image_XML_Namespace nNamespace
#define ATTRIBUTE_MASKURL
#define ATTRIBUTE_HIGHCONTRASTURL
#define ATTRIBUTE_BITMAPINDEX
constexpr OUStringLiteral ELEMENT_NS_ENTRY
#define ATTRIBUTE_COMMAND
constexpr OUStringLiteral ATTRIBUTE_XMLNS_IMAGE
constexpr OUStringLiteral ELEMENT_NS_IMAGESCONTAINER
#define ATTRIBUTE_MASKMODE
constexpr OUStringLiteral XMLNS_IMAGE_PREFIX
constexpr OUStringLiteral IMAGES_DOCTYPE
char aEntryName[20]
constexpr OUStringLiteral ATTRIBUTE_XMLNS_XLINK
constexpr OUStringLiteral XMLNS_XLINK
OUString aName
sal_Int64 n
ImageXMLEntryProperty const ImagesEntries[OReadImagesDocumentHandler::IMG_XML_ENTRY_COUNT]
std::vector< ImageItemDescriptor > ImageItemDescriptorList
int i