1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <xml/imagesdocumenthandler.hxx>
#include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
#include <com/sun/star/xml/sax/SAXException.hpp>
#include <rtl/ref.hxx>
#include <rtl/ustrbuf.hxx>
#include <comphelper/attributelist.hxx>
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::xml::sax;
#define ELEMENT_IMAGECONTAINER "imagescontainer"
#define ELEMENT_IMAGES "images"
#define ELEMENT_ENTRY "entry"
#define ELEMENT_EXTERNALIMAGES "externalimages"
#define ELEMENT_EXTERNALENTRY "externalentry"
constexpr OUStringLiteral ELEMENT_NS_IMAGESCONTAINER = u"image:imagescontainer";
constexpr OUStringLiteral ELEMENT_NS_IMAGES = u"image:images";
constexpr OUStringLiteral ELEMENT_NS_ENTRY = u"image:entry";
#define ATTRIBUTE_HREF "href"
#define ATTRIBUTE_MASKCOLOR "maskcolor"
#define ATTRIBUTE_COMMAND "command"
#define ATTRIBUTE_BITMAPINDEX "bitmap-index"
#define ATTRIBUTE_MASKURL "maskurl"
#define ATTRIBUTE_MASKMODE "maskmode"
#define ATTRIBUTE_HIGHCONTRASTURL "highcontrasturl"
#define ATTRIBUTE_HIGHCONTRASTMASKURL "highcontrastmaskurl"
constexpr OUStringLiteral ATTRIBUTE_TYPE_CDATA = u"CDATA";
constexpr OUStringLiteral ATTRIBUTE_XMLNS_IMAGE = u"xmlns:image";
constexpr OUStringLiteral ATTRIBUTE_XMLNS_XLINK = u"xmlns:xlink";
constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE = u"xlink:type";
constexpr OUStringLiteral ATTRIBUTE_XLINK_TYPE_VALUE = u"simple";
constexpr OUStringLiteral XMLNS_IMAGE = u"http://openoffice.org/2001/image";
constexpr OUStringLiteral XMLNS_XLINK = u"http://www.w3.org/1999/xlink";
constexpr OUStringLiteral XMLNS_IMAGE_PREFIX = u"image:";
constexpr OUStringLiteral XMLNS_FILTER_SEPARATOR = u"^";
constexpr OUStringLiteral IMAGES_DOCTYPE = u"<!DOCTYPE image:imagecontainer PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"image.dtd\">";
namespace framework
{
namespace {
struct ImageXMLEntryProperty
{
OReadImagesDocumentHandler::Image_XML_Namespace nNamespace;
char aEntryName[20];
};
}
ImageXMLEntryProperty const ImagesEntries[OReadImagesDocumentHandler::IMG_XML_ENTRY_COUNT] =
{
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGECONTAINER },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_IMAGES },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_ENTRY },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALIMAGES },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ELEMENT_EXTERNALENTRY },
{ OReadImagesDocumentHandler::IMG_NS_XLINK, ATTRIBUTE_HREF },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKCOLOR },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_COMMAND },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_BITMAPINDEX },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKURL },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_MASKMODE },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTURL },
{ OReadImagesDocumentHandler::IMG_NS_IMAGE, ATTRIBUTE_HIGHCONTRASTMASKURL }
};
OReadImagesDocumentHandler::OReadImagesDocumentHandler( ImageItemDescriptorList& rItems ) :
m_rImageList( rItems )
{
// create hash map to speed up lookup
for ( int i = 0; i < IMG_XML_ENTRY_COUNT; i++ )
{
OUStringBuffer temp( 20 );
if ( ImagesEntries[i].nNamespace == IMG_NS_IMAGE )
temp.append( XMLNS_IMAGE );
else
temp.append( XMLNS_XLINK );
temp.append( XMLNS_FILTER_SEPARATOR );
temp.appendAscii( ImagesEntries[i].aEntryName );
m_aImageMap.emplace( temp.makeStringAndClear(), static_cast<Image_XML_Entry>(i) );
}
// reset states
m_bImageContainerStartFound = false;
m_bImageContainerEndFound = false;
m_bImagesStartFound = false;
}
OReadImagesDocumentHandler::~OReadImagesDocumentHandler()
{
}
// XDocumentHandler
void SAL_CALL OReadImagesDocumentHandler::startDocument()
{
}
void SAL_CALL OReadImagesDocumentHandler::endDocument()
{
if (m_bImageContainerStartFound != m_bImageContainerEndFound)
{
OUString aErrorMessage = getErrorLineString() + "No matching start or end element 'image:imagecontainer' found!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
}
void SAL_CALL OReadImagesDocumentHandler::startElement(
const OUString& aName, const Reference< XAttributeList > &xAttribs )
{
ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
if ( pImageEntry == m_aImageMap.end() )
return;
switch ( pImageEntry->second )
{
case IMG_ELEMENT_IMAGECONTAINER:
{
// image:imagecontainer element (container element for all further image elements)
if ( m_bImageContainerStartFound )
{
OUString aErrorMessage = getErrorLineString() + "Element 'image:imagecontainer' cannot be embedded into 'image:imagecontainer'!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
m_bImageContainerStartFound = true;
}
break;
case IMG_ELEMENT_IMAGES:
{
if ( !m_bImageContainerStartFound )
{
OUString aErrorMessage = getErrorLineString() + "Element 'image:images' must be embedded into element 'image:imagecontainer'!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
if ( m_bImagesStartFound )
{
OUString aErrorMessage = getErrorLineString() + "Element 'image:images' cannot be embedded into 'image:images'!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
m_bImagesStartFound = true;
}
break;
case IMG_ELEMENT_ENTRY:
{
// Check that image:entry is embedded into image:images!
if ( !m_bImagesStartFound )
{
OUString aErrorMessage = getErrorLineString() + "Element 'image:entry' must be embedded into element 'image:images'!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
// Create new image item descriptor
ImageItemDescriptor aItem;
// Read attributes for this image definition
for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ )
{
pImageEntry = m_aImageMap.find( xAttribs->getNameByIndex( n ) );
if ( pImageEntry != m_aImageMap.end() )
{
switch ( pImageEntry->second )
{
case IMG_ATTRIBUTE_COMMAND:
{
aItem.aCommandURL = xAttribs->getValueByIndex( n );
}
break;
default:
break;
}
}
}
// Check required attribute "command"
if ( aItem.aCommandURL.isEmpty() )
{
OUString aErrorMessage = getErrorLineString() + "Required attribute 'image:command' must have a value!";
throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
}
m_rImageList.push_back( aItem );
}
break;
default:
break;
}
}
void SAL_CALL OReadImagesDocumentHandler::endElement(const OUString& aName)
{
ImageHashMap::const_iterator pImageEntry = m_aImageMap.find( aName );
if ( pImageEntry == m_aImageMap.end() )
return;
switch ( pImageEntry->second )
{
case IMG_ELEMENT_IMAGECONTAINER:
{
m_bImageContainerEndFound = true;
}
break;
case IMG_ELEMENT_IMAGES:
{
m_bImagesStartFound = false;
}
break;
default: break;
}
}
void SAL_CALL OReadImagesDocumentHandler::characters(const OUString&)
{
}
void SAL_CALL OReadImagesDocumentHandler::ignorableWhitespace(const OUString&)
{
}
void SAL_CALL OReadImagesDocumentHandler::processingInstruction(
const OUString& /*aTarget*/, const OUString& /*aData*/ )
{
}
void SAL_CALL OReadImagesDocumentHandler::setDocumentLocator(
const Reference< XLocator > &xLocator)
{
m_xLocator = xLocator;
}
OUString OReadImagesDocumentHandler::getErrorLineString()
{
if ( m_xLocator.is() )
{
return "Line: " +
OUString::number(m_xLocator->getLineNumber()) +
" - ";
}
else
return OUString();
}
// OWriteImagesDocumentHandler
OWriteImagesDocumentHandler::OWriteImagesDocumentHandler(
const ImageItemDescriptorList& rItems,
Reference< XDocumentHandler > const & rWriteDocumentHandler ) :
m_rImageItemList( rItems ),
m_xWriteDocumentHandler( rWriteDocumentHandler )
{
m_xEmptyList = new ::comphelper::AttributeList;
m_aAttributeType = ATTRIBUTE_TYPE_CDATA;
m_aXMLImageNS = XMLNS_IMAGE_PREFIX;
m_aAttributeXlinkType = ATTRIBUTE_XLINK_TYPE;
m_aAttributeValueSimple = ATTRIBUTE_XLINK_TYPE_VALUE;
}
OWriteImagesDocumentHandler::~OWriteImagesDocumentHandler()
{
}
void OWriteImagesDocumentHandler::WriteImagesDocument()
{
m_xWriteDocumentHandler->startDocument();
// write DOCTYPE line!
Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY );
if ( xExtendedDocHandler.is() )
{
xExtendedDocHandler->unknown( IMAGES_DOCTYPE );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
}
rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
pList->AddAttribute( ATTRIBUTE_XMLNS_IMAGE,
m_aAttributeType,
XMLNS_IMAGE );
pList->AddAttribute( ATTRIBUTE_XMLNS_XLINK,
m_aAttributeType,
XMLNS_XLINK );
m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGESCONTAINER, pList );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
WriteImageList( &m_rImageItemList );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGESCONTAINER );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
m_xWriteDocumentHandler->endDocument();
}
// protected member functions
void OWriteImagesDocumentHandler::WriteImageList( const ImageItemDescriptorList* pImageList )
{
rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
// save required attributes
pList->AddAttribute( m_aAttributeXlinkType,
m_aAttributeType,
m_aAttributeValueSimple );
m_xWriteDocumentHandler->startElement( ELEMENT_NS_IMAGES, pList );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
for (const ImageItemDescriptor & i : *pImageList)
WriteImage( &i );
m_xWriteDocumentHandler->endElement( ELEMENT_NS_IMAGES );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
}
void OWriteImagesDocumentHandler::WriteImage( const ImageItemDescriptor* pImage )
{
rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList;
pList->AddAttribute( m_aXMLImageNS + ATTRIBUTE_COMMAND,
m_aAttributeType,
pImage->aCommandURL );
m_xWriteDocumentHandler->startElement( ELEMENT_NS_ENTRY, pList );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
m_xWriteDocumentHandler->endElement( ELEMENT_NS_ENTRY );
m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
}
} // namespace framework
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|