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