/src/libreoffice/framework/source/fwe/xml/toolboxdocumenthandler.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/toolboxdocumenthandler.hxx> |
21 | | #include <xml/toolboxconfigurationdefines.hxx> |
22 | | |
23 | | #include <com/sun/star/xml/sax/SAXException.hpp> |
24 | | #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> |
25 | | #include <com/sun/star/ui/ItemType.hpp> |
26 | | #include <com/sun/star/ui/ItemStyle.hpp> |
27 | | #include <com/sun/star/beans/XPropertySet.hpp> |
28 | | #include <com/sun/star/container/XIndexAccess.hpp> |
29 | | #include <com/sun/star/container/XIndexContainer.hpp> |
30 | | |
31 | | #include <sal/config.h> |
32 | | #include <sal/macros.h> |
33 | | #include <vcl/settings.hxx> |
34 | | #include <rtl/ref.hxx> |
35 | | #include <rtl/ustrbuf.hxx> |
36 | | |
37 | | #include <comphelper/propertysequence.hxx> |
38 | | #include <comphelper/propertyvalue.hxx> |
39 | | |
40 | | using namespace ::com::sun::star::uno; |
41 | | using namespace ::com::sun::star::beans; |
42 | | using namespace ::com::sun::star::container; |
43 | | using namespace ::com::sun::star::xml::sax; |
44 | | |
45 | | constexpr OUStringLiteral TOOLBAR_DOCTYPE = u"<!DOCTYPE toolbar:toolbar PUBLIC \"-//OpenOffice.org//DTD OfficeDocument 1.0//EN\" \"toolbar.dtd\">"; |
46 | | |
47 | | namespace framework |
48 | | { |
49 | | |
50 | | // Property names of a menu/menu item ItemDescriptor |
51 | | constexpr OUString ITEM_DESCRIPTOR_COMMANDURL = u"CommandURL"_ustr; |
52 | | constexpr OUString ITEM_DESCRIPTOR_LABEL = u"Label"_ustr; |
53 | | constexpr OUString ITEM_DESCRIPTOR_TYPE = u"Type"_ustr; |
54 | | constexpr OUString ITEM_DESCRIPTOR_STYLE = u"Style"_ustr; |
55 | | constexpr OUString ITEM_DESCRIPTOR_VISIBLE = u"IsVisible"_ustr; |
56 | | |
57 | | static void ExtractToolbarParameters( const Sequence< PropertyValue >& rProp, |
58 | | OUString& rCommandURL, |
59 | | OUString& rLabel, |
60 | | sal_Int16& rStyle, |
61 | | bool& rVisible, |
62 | | sal_Int16& rType ) |
63 | 0 | { |
64 | 0 | for ( const PropertyValue& rEntry : rProp ) |
65 | 0 | { |
66 | 0 | if ( rEntry.Name == ITEM_DESCRIPTOR_COMMANDURL ) |
67 | 0 | rEntry.Value >>= rCommandURL; |
68 | 0 | else if ( rEntry.Name == ITEM_DESCRIPTOR_LABEL ) |
69 | 0 | rEntry.Value >>= rLabel; |
70 | 0 | else if ( rEntry.Name == ITEM_DESCRIPTOR_TYPE ) |
71 | 0 | rEntry.Value >>= rType; |
72 | 0 | else if ( rEntry.Name == ITEM_DESCRIPTOR_VISIBLE ) |
73 | 0 | rEntry.Value >>= rVisible; |
74 | 0 | else if ( rEntry.Name == ITEM_DESCRIPTOR_STYLE ) |
75 | 0 | rEntry.Value >>= rStyle; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | | namespace { |
80 | | |
81 | | struct ToolboxStyleItem |
82 | | { |
83 | | sal_Int16 nBit; |
84 | | OUString attrName; |
85 | | }; |
86 | | |
87 | | } |
88 | | |
89 | | constexpr ToolboxStyleItem Styles[ ] = { |
90 | | { css::ui::ItemStyle::RADIO_CHECK, ATTRIBUTE_ITEMSTYLE_RADIO }, |
91 | | { css::ui::ItemStyle::ALIGN_LEFT, ATTRIBUTE_ITEMSTYLE_LEFT }, |
92 | | { css::ui::ItemStyle::AUTO_SIZE, ATTRIBUTE_ITEMSTYLE_AUTO }, |
93 | | { css::ui::ItemStyle::REPEAT, ATTRIBUTE_ITEMSTYLE_REPEAT }, |
94 | | { css::ui::ItemStyle::DROPDOWN_ONLY, ATTRIBUTE_ITEMSTYLE_DROPDOWNONLY }, |
95 | | { css::ui::ItemStyle::DROP_DOWN, ATTRIBUTE_ITEMSTYLE_DROPDOWN }, |
96 | | { css::ui::ItemStyle::ICON, ATTRIBUTE_ITEMSTYLE_IMAGE }, |
97 | | { css::ui::ItemStyle::TEXT, ATTRIBUTE_ITEMSTYLE_TEXT }, |
98 | | }; |
99 | | |
100 | | sal_Int32 const nStyleItemEntries = SAL_N_ELEMENTS(Styles); |
101 | | |
102 | | namespace { |
103 | | |
104 | | struct ToolBarEntryProperty |
105 | | { |
106 | | OReadToolBoxDocumentHandler::ToolBox_XML_Namespace nNamespace; |
107 | | OUString aEntryName; |
108 | | }; |
109 | | |
110 | | } |
111 | | |
112 | | ToolBarEntryProperty constexpr ToolBoxEntries[OReadToolBoxDocumentHandler::TB_XML_ENTRY_COUNT] = |
113 | | { |
114 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, u"toolbar"_ustr }, |
115 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, u"toolbaritem"_ustr }, |
116 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, u"toolbarspace"_ustr }, |
117 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, u"toolbarbreak"_ustr }, |
118 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, u"toolbarseparator"_ustr }, |
119 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_TEXT }, |
120 | | { OReadToolBoxDocumentHandler::TB_NS_XLINK, ATTRIBUTE_URL }, |
121 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_VISIBLE }, |
122 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_ITEMSTYLE }, |
123 | | { OReadToolBoxDocumentHandler::TB_NS_TOOLBAR, ATTRIBUTE_UINAME }, |
124 | | }; |
125 | | |
126 | | OReadToolBoxDocumentHandler::OReadToolBoxDocumentHandler( const Reference< XIndexContainer >& rItemContainer ) : |
127 | 0 | m_rItemContainer( rItemContainer ), |
128 | 0 | m_aType( ITEM_DESCRIPTOR_TYPE ), |
129 | 0 | m_aLabel( ITEM_DESCRIPTOR_LABEL ), |
130 | 0 | m_aStyle( ITEM_DESCRIPTOR_STYLE ), |
131 | 0 | m_aIsVisible( ITEM_DESCRIPTOR_VISIBLE ), |
132 | 0 | m_aCommandURL( ITEM_DESCRIPTOR_COMMANDURL ) |
133 | 0 | { |
134 | | // create hash map |
135 | 0 | for ( int i = 0; i < TB_XML_ENTRY_COUNT; i++ ) |
136 | 0 | { |
137 | 0 | if ( ToolBoxEntries[i].nNamespace == TB_NS_TOOLBAR ) |
138 | 0 | { |
139 | 0 | OUString temp = XMLNS_TOOLBAR XMLNS_FILTER_SEPARATOR + |
140 | 0 | ToolBoxEntries[i].aEntryName; |
141 | 0 | m_aToolBoxMap.emplace( temp, static_cast<ToolBox_XML_Entry>(i) ); |
142 | 0 | } |
143 | 0 | else |
144 | 0 | { |
145 | 0 | OUString temp = XMLNS_XLINK XMLNS_FILTER_SEPARATOR + |
146 | 0 | ToolBoxEntries[i].aEntryName; |
147 | 0 | m_aToolBoxMap.emplace( temp, static_cast<ToolBox_XML_Entry>(i) ); |
148 | 0 | } |
149 | 0 | } |
150 | |
|
151 | 0 | m_bToolBarStartFound = false; |
152 | 0 | m_bToolBarItemStartFound = false; |
153 | 0 | m_bToolBarSpaceStartFound = false; |
154 | 0 | m_bToolBarBreakStartFound = false; |
155 | 0 | m_bToolBarSeparatorStartFound = false; |
156 | 0 | } |
157 | | |
158 | | OReadToolBoxDocumentHandler::~OReadToolBoxDocumentHandler() |
159 | 0 | { |
160 | 0 | } |
161 | | |
162 | | // XDocumentHandler |
163 | | void SAL_CALL OReadToolBoxDocumentHandler::startDocument() |
164 | 0 | { |
165 | 0 | } |
166 | | |
167 | | void SAL_CALL OReadToolBoxDocumentHandler::endDocument() |
168 | 0 | { |
169 | 0 | if ( m_bToolBarStartFound ) |
170 | 0 | { |
171 | 0 | OUString aErrorMessage = getErrorLineString() + "No matching start or end element 'toolbar' found!"; |
172 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | void SAL_CALL OReadToolBoxDocumentHandler::startElement( |
177 | | const OUString& aName, const Reference< XAttributeList > &xAttribs ) |
178 | 0 | { |
179 | 0 | auto pToolBoxEntry = m_aToolBoxMap.find( aName ); |
180 | 0 | if ( pToolBoxEntry == m_aToolBoxMap.end() ) |
181 | 0 | return; |
182 | | |
183 | 0 | switch ( pToolBoxEntry->second ) |
184 | 0 | { |
185 | 0 | case TB_ELEMENT_TOOLBAR: |
186 | 0 | { |
187 | 0 | if ( m_bToolBarStartFound ) |
188 | 0 | { |
189 | 0 | OUString aErrorMessage = getErrorLineString() + "Element 'toolbar:toolbar' cannot be embedded into 'toolbar:toolbar'!"; |
190 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
191 | 0 | } |
192 | 0 | else |
193 | 0 | { |
194 | | // Check if we have a UI name set in our XML file |
195 | 0 | OUString aUIName; |
196 | 0 | for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) |
197 | 0 | { |
198 | 0 | pToolBoxEntry = m_aToolBoxMap.find( xAttribs->getNameByIndex( n ) ); |
199 | 0 | if ( pToolBoxEntry != m_aToolBoxMap.end() ) |
200 | 0 | { |
201 | 0 | switch ( pToolBoxEntry->second ) |
202 | 0 | { |
203 | 0 | case TB_ATTRIBUTE_UINAME: |
204 | 0 | aUIName = xAttribs->getValueByIndex( n ); |
205 | 0 | break; |
206 | 0 | default: |
207 | 0 | break; |
208 | 0 | } |
209 | 0 | } |
210 | 0 | } |
211 | 0 | if ( !aUIName.isEmpty() ) |
212 | 0 | { |
213 | | // Try to set UI name as a container property |
214 | 0 | Reference< XPropertySet > xPropSet( m_rItemContainer, UNO_QUERY ); |
215 | 0 | if ( xPropSet.is() ) |
216 | 0 | { |
217 | 0 | try |
218 | 0 | { |
219 | 0 | xPropSet->setPropertyValue(u"UIName"_ustr, Any( aUIName ) ); |
220 | 0 | } |
221 | 0 | catch ( const UnknownPropertyException& ) |
222 | 0 | { |
223 | 0 | } |
224 | 0 | } |
225 | |
|
226 | 0 | } |
227 | 0 | } |
228 | 0 | m_bToolBarStartFound = true; |
229 | 0 | } |
230 | 0 | break; |
231 | | |
232 | 0 | case TB_ELEMENT_TOOLBARITEM: |
233 | 0 | { |
234 | 0 | if ( !m_bToolBarStartFound ) |
235 | 0 | { |
236 | 0 | OUString aErrorMessage = getErrorLineString() + "Element 'toolbar:toolbaritem' must be embedded into element 'toolbar:toolbar'!"; |
237 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
238 | 0 | } |
239 | | |
240 | 0 | if ( m_bToolBarSeparatorStartFound || |
241 | 0 | m_bToolBarBreakStartFound || |
242 | 0 | m_bToolBarSpaceStartFound || |
243 | 0 | m_bToolBarItemStartFound ) |
244 | 0 | { |
245 | 0 | OUString aErrorMessage = getErrorLineString() + "Element toolbar:toolbaritem is not a container!"; |
246 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
247 | 0 | } |
248 | | |
249 | 0 | bool bAttributeURL = false; |
250 | |
|
251 | 0 | m_bToolBarItemStartFound = true; |
252 | 0 | OUString aLabel; |
253 | 0 | OUString aCommandURL; |
254 | 0 | sal_uInt16 nItemBits( 0 ); |
255 | 0 | bool bVisible( true ); |
256 | |
|
257 | 0 | for ( sal_Int16 n = 0; n < xAttribs->getLength(); n++ ) |
258 | 0 | { |
259 | 0 | pToolBoxEntry = m_aToolBoxMap.find( xAttribs->getNameByIndex( n ) ); |
260 | 0 | if ( pToolBoxEntry != m_aToolBoxMap.end() ) |
261 | 0 | { |
262 | 0 | switch ( pToolBoxEntry->second ) |
263 | 0 | { |
264 | 0 | case TB_ATTRIBUTE_TEXT: |
265 | 0 | { |
266 | 0 | aLabel = xAttribs->getValueByIndex( n ); |
267 | 0 | } |
268 | 0 | break; |
269 | | |
270 | 0 | case TB_ATTRIBUTE_URL: |
271 | 0 | { |
272 | 0 | bAttributeURL = true; |
273 | 0 | aCommandURL = xAttribs->getValueByIndex( n ); |
274 | 0 | } |
275 | 0 | break; |
276 | | |
277 | 0 | case TB_ATTRIBUTE_VISIBLE: |
278 | 0 | { |
279 | 0 | if ( xAttribs->getValueByIndex( n ) == ATTRIBUTE_BOOLEAN_TRUE ) |
280 | 0 | bVisible = true; |
281 | 0 | else if ( xAttribs->getValueByIndex( n ) == ATTRIBUTE_BOOLEAN_FALSE ) |
282 | 0 | bVisible = false; |
283 | 0 | else |
284 | 0 | { |
285 | 0 | OUString aErrorMessage = getErrorLineString() + "Attribute toolbar:visible must have value 'true' or 'false'!"; |
286 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
287 | 0 | } |
288 | 0 | } |
289 | 0 | break; |
290 | | |
291 | 0 | case TB_ATTRIBUTE_STYLE: |
292 | 0 | { |
293 | | // read space separated item style list |
294 | 0 | OUString aTemp = xAttribs->getValueByIndex( n ); |
295 | 0 | sal_Int32 nIndex = 0; |
296 | |
|
297 | 0 | do |
298 | 0 | { |
299 | 0 | OUString aToken = aTemp.getToken( 0, ' ', nIndex ); |
300 | 0 | if ( !aToken.isEmpty() ) |
301 | 0 | { |
302 | 0 | if ( aToken == ATTRIBUTE_ITEMSTYLE_RADIO ) |
303 | 0 | nItemBits |= css::ui::ItemStyle::RADIO_CHECK; |
304 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_LEFT ) |
305 | 0 | nItemBits |= css::ui::ItemStyle::ALIGN_LEFT; |
306 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_AUTOSIZE ) |
307 | 0 | nItemBits |= css::ui::ItemStyle::AUTO_SIZE; |
308 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_REPEAT ) |
309 | 0 | nItemBits |= css::ui::ItemStyle::REPEAT; |
310 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_DROPDOWNONLY ) |
311 | 0 | nItemBits |= css::ui::ItemStyle::DROPDOWN_ONLY; |
312 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_DROPDOWN ) |
313 | 0 | nItemBits |= css::ui::ItemStyle::DROP_DOWN; |
314 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_TEXT ) |
315 | 0 | nItemBits |= css::ui::ItemStyle::TEXT; |
316 | 0 | else if ( aToken == ATTRIBUTE_ITEMSTYLE_IMAGE ) |
317 | 0 | nItemBits |= css::ui::ItemStyle::ICON; |
318 | 0 | } |
319 | 0 | } |
320 | 0 | while ( nIndex >= 0 ); |
321 | 0 | } |
322 | 0 | break; |
323 | | |
324 | 0 | default: |
325 | 0 | break; |
326 | 0 | } |
327 | 0 | } |
328 | 0 | } // for |
329 | | |
330 | 0 | if ( !bAttributeURL ) |
331 | 0 | { |
332 | 0 | OUString aErrorMessage = getErrorLineString() + "Required attribute toolbar:url must have a value!"; |
333 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
334 | 0 | } |
335 | | |
336 | 0 | if ( !aCommandURL.isEmpty() ) |
337 | 0 | { |
338 | | //fix for fdo#39370 |
339 | | /// check whether RTL interface or not |
340 | 0 | if(AllSettings::GetLayoutRTL()){ |
341 | 0 | if (aCommandURL == ".uno:ParaLeftToRight") |
342 | 0 | aCommandURL = ".uno:ParaRightToLeft"; |
343 | 0 | else if (aCommandURL == ".uno:ParaRightToLeft") |
344 | 0 | aCommandURL = ".uno:ParaLeftToRight"; |
345 | 0 | else if (aCommandURL == ".uno:LeftPara") |
346 | 0 | aCommandURL = ".uno:RightPara"; |
347 | 0 | else if (aCommandURL == ".uno:RightPara") |
348 | 0 | aCommandURL = ".uno:LeftPara"; |
349 | 0 | else if (aCommandURL == ".uno:AlignLeft") |
350 | 0 | aCommandURL = ".uno:AlignRight"; |
351 | 0 | else if (aCommandURL == ".uno:AlignRight") |
352 | 0 | aCommandURL = ".uno:AlignLeft"; |
353 | 0 | else if (aCommandURL == ".uno:WrapLeft") |
354 | 0 | aCommandURL = ".uno:WrapRight"; |
355 | 0 | else if (aCommandURL == ".uno:WrapRight") |
356 | 0 | aCommandURL = ".uno:WrapLeft"; |
357 | 0 | } |
358 | |
|
359 | 0 | auto aToolbarItemProp( comphelper::InitPropertySequence( { |
360 | 0 | { m_aCommandURL, css::uno::Any( aCommandURL ) }, |
361 | 0 | { m_aLabel, css::uno::Any( aLabel ) }, |
362 | 0 | { m_aType, css::uno::Any( css::ui::ItemType::DEFAULT ) }, |
363 | 0 | { m_aStyle, css::uno::Any( nItemBits ) }, |
364 | 0 | { m_aIsVisible, css::uno::Any( bVisible ) }, |
365 | 0 | } ) ); |
366 | |
|
367 | 0 | m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), Any( aToolbarItemProp ) ); |
368 | 0 | } |
369 | 0 | } |
370 | 0 | break; |
371 | | |
372 | 0 | case TB_ELEMENT_TOOLBARSPACE: |
373 | 0 | { |
374 | 0 | if ( m_bToolBarSeparatorStartFound || |
375 | 0 | m_bToolBarBreakStartFound || |
376 | 0 | m_bToolBarSpaceStartFound || |
377 | 0 | m_bToolBarItemStartFound ) |
378 | 0 | { |
379 | 0 | OUString aErrorMessage = getErrorLineString() + "Element toolbar:toolbarspace is not a container!"; |
380 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
381 | 0 | } |
382 | | |
383 | 0 | m_bToolBarSpaceStartFound = true; |
384 | |
|
385 | 0 | Sequence< PropertyValue > aToolbarItemProp{ |
386 | 0 | comphelper::makePropertyValue(m_aCommandURL, OUString()), |
387 | 0 | comphelper::makePropertyValue(m_aType, css::ui::ItemType::SEPARATOR_SPACE) |
388 | 0 | }; |
389 | |
|
390 | 0 | m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), Any( aToolbarItemProp ) ); |
391 | 0 | } |
392 | 0 | break; |
393 | | |
394 | 0 | case TB_ELEMENT_TOOLBARBREAK: |
395 | 0 | { |
396 | 0 | if ( m_bToolBarSeparatorStartFound || |
397 | 0 | m_bToolBarBreakStartFound || |
398 | 0 | m_bToolBarSpaceStartFound || |
399 | 0 | m_bToolBarItemStartFound ) |
400 | 0 | { |
401 | 0 | OUString aErrorMessage = getErrorLineString() + "Element toolbar:toolbarbreak is not a container!"; |
402 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
403 | 0 | } |
404 | | |
405 | 0 | m_bToolBarBreakStartFound = true; |
406 | |
|
407 | 0 | Sequence< PropertyValue > aToolbarItemProp{ |
408 | 0 | comphelper::makePropertyValue(m_aCommandURL, OUString()), |
409 | 0 | comphelper::makePropertyValue(m_aType, css::ui::ItemType::SEPARATOR_LINEBREAK) |
410 | 0 | }; |
411 | |
|
412 | 0 | m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), Any( aToolbarItemProp ) ); |
413 | 0 | } |
414 | 0 | break; |
415 | | |
416 | 0 | case TB_ELEMENT_TOOLBARSEPARATOR: |
417 | 0 | { |
418 | 0 | if ( m_bToolBarSeparatorStartFound || |
419 | 0 | m_bToolBarBreakStartFound || |
420 | 0 | m_bToolBarSpaceStartFound || |
421 | 0 | m_bToolBarItemStartFound ) |
422 | 0 | { |
423 | 0 | OUString aErrorMessage = getErrorLineString() + "Element toolbar:toolbarseparator is not a container!"; |
424 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
425 | 0 | } |
426 | | |
427 | 0 | m_bToolBarSeparatorStartFound = true; |
428 | |
|
429 | 0 | Sequence< PropertyValue > aToolbarItemProp{ |
430 | 0 | comphelper::makePropertyValue(m_aCommandURL, OUString()), |
431 | 0 | comphelper::makePropertyValue(m_aType, css::ui::ItemType::SEPARATOR_LINE) |
432 | 0 | }; |
433 | |
|
434 | 0 | m_rItemContainer->insertByIndex( m_rItemContainer->getCount(), Any( aToolbarItemProp ) ); |
435 | 0 | } |
436 | 0 | break; |
437 | | |
438 | 0 | default: |
439 | 0 | break; |
440 | 0 | } |
441 | 0 | } |
442 | | |
443 | | void SAL_CALL OReadToolBoxDocumentHandler::endElement(const OUString& aName) |
444 | 0 | { |
445 | 0 | auto pToolBoxEntry = m_aToolBoxMap.find( aName ); |
446 | 0 | if ( pToolBoxEntry == m_aToolBoxMap.end() ) |
447 | 0 | return; |
448 | | |
449 | 0 | switch ( pToolBoxEntry->second ) |
450 | 0 | { |
451 | 0 | case TB_ELEMENT_TOOLBAR: |
452 | 0 | { |
453 | 0 | if ( !m_bToolBarStartFound ) |
454 | 0 | { |
455 | 0 | OUString aErrorMessage = getErrorLineString() + "End element 'toolbar' found, but no start element 'toolbar'"; |
456 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
457 | 0 | } |
458 | | |
459 | 0 | m_bToolBarStartFound = false; |
460 | 0 | } |
461 | 0 | break; |
462 | | |
463 | 0 | case TB_ELEMENT_TOOLBARITEM: |
464 | 0 | { |
465 | 0 | if ( !m_bToolBarItemStartFound ) |
466 | 0 | { |
467 | 0 | OUString aErrorMessage = getErrorLineString() + "End element 'toolbar:toolbaritem' found, but no start element 'toolbar:toolbaritem'"; |
468 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
469 | 0 | } |
470 | | |
471 | 0 | m_bToolBarItemStartFound = false; |
472 | 0 | } |
473 | 0 | break; |
474 | | |
475 | 0 | case TB_ELEMENT_TOOLBARBREAK: |
476 | 0 | { |
477 | 0 | if ( !m_bToolBarBreakStartFound ) |
478 | 0 | { |
479 | 0 | OUString aErrorMessage = getErrorLineString() + "End element 'toolbar:toolbarbreak' found, but no start element 'toolbar:toolbarbreak'"; |
480 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
481 | 0 | } |
482 | | |
483 | 0 | m_bToolBarBreakStartFound = false; |
484 | 0 | } |
485 | 0 | break; |
486 | | |
487 | 0 | case TB_ELEMENT_TOOLBARSPACE: |
488 | 0 | { |
489 | 0 | if ( !m_bToolBarSpaceStartFound ) |
490 | 0 | { |
491 | 0 | OUString aErrorMessage = getErrorLineString() + "End element 'toolbar:toolbarspace' found, but no start element 'toolbar:toolbarspace'"; |
492 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
493 | 0 | } |
494 | | |
495 | 0 | m_bToolBarSpaceStartFound = false; |
496 | 0 | } |
497 | 0 | break; |
498 | | |
499 | 0 | case TB_ELEMENT_TOOLBARSEPARATOR: |
500 | 0 | { |
501 | 0 | if ( !m_bToolBarSeparatorStartFound ) |
502 | 0 | { |
503 | 0 | OUString aErrorMessage = getErrorLineString() + "End element 'toolbar:toolbarseparator' found, but no start element 'toolbar:toolbarseparator'"; |
504 | 0 | throw SAXException( aErrorMessage, Reference< XInterface >(), Any() ); |
505 | 0 | } |
506 | | |
507 | 0 | m_bToolBarSeparatorStartFound = false; |
508 | 0 | } |
509 | 0 | break; |
510 | | |
511 | 0 | default: break; |
512 | 0 | } |
513 | 0 | } |
514 | | |
515 | | void SAL_CALL OReadToolBoxDocumentHandler::characters(const OUString&) |
516 | 0 | { |
517 | 0 | } |
518 | | |
519 | | void SAL_CALL OReadToolBoxDocumentHandler::ignorableWhitespace(const OUString&) |
520 | 0 | { |
521 | 0 | } |
522 | | |
523 | | void SAL_CALL OReadToolBoxDocumentHandler::processingInstruction( |
524 | | const OUString& /*aTarget*/, const OUString& /*aData*/ ) |
525 | 0 | { |
526 | 0 | } |
527 | | |
528 | | void SAL_CALL OReadToolBoxDocumentHandler::setDocumentLocator( |
529 | | const Reference< XLocator > &xLocator) |
530 | 0 | { |
531 | 0 | m_xLocator = xLocator; |
532 | 0 | } |
533 | | |
534 | | OUString OReadToolBoxDocumentHandler::getErrorLineString() |
535 | 0 | { |
536 | 0 | if ( m_xLocator.is() ) |
537 | 0 | return "Line: " + OUString::number( m_xLocator->getLineNumber() ) + " - "; |
538 | 0 | else |
539 | 0 | return OUString(); |
540 | 0 | } |
541 | | |
542 | | // OWriteToolBoxDocumentHandler |
543 | | |
544 | | OWriteToolBoxDocumentHandler::OWriteToolBoxDocumentHandler( |
545 | | const Reference< XIndexAccess >& rItemAccess, |
546 | | Reference< XDocumentHandler > const & rWriteDocumentHandler ) : |
547 | 0 | m_xWriteDocumentHandler( rWriteDocumentHandler ), |
548 | 0 | m_rItemAccess( rItemAccess ) |
549 | 0 | { |
550 | 0 | m_xEmptyList = new ::comphelper::AttributeList; |
551 | 0 | m_aXMLXlinkNS = XMLNS_XLINK_PREFIX; |
552 | 0 | m_aXMLToolbarNS = XMLNS_TOOLBAR_PREFIX; |
553 | 0 | } |
554 | | |
555 | | OWriteToolBoxDocumentHandler::~OWriteToolBoxDocumentHandler() |
556 | 0 | { |
557 | 0 | } |
558 | | |
559 | | void OWriteToolBoxDocumentHandler::WriteToolBoxDocument() |
560 | 0 | { |
561 | 0 | m_xWriteDocumentHandler->startDocument(); |
562 | | |
563 | | // write DOCTYPE line! |
564 | 0 | Reference< XExtendedDocumentHandler > xExtendedDocHandler( m_xWriteDocumentHandler, UNO_QUERY ); |
565 | 0 | if ( xExtendedDocHandler.is() ) |
566 | 0 | { |
567 | 0 | xExtendedDocHandler->unknown( TOOLBAR_DOCTYPE ); |
568 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
569 | 0 | } |
570 | |
|
571 | 0 | OUString aUIName; |
572 | 0 | Reference< XPropertySet > xPropSet( m_rItemAccess, UNO_QUERY ); |
573 | 0 | if ( xPropSet.is() ) |
574 | 0 | { |
575 | 0 | try |
576 | 0 | { |
577 | 0 | xPropSet->getPropertyValue(u"UIName"_ustr) >>= aUIName; |
578 | 0 | } |
579 | 0 | catch ( const UnknownPropertyException& ) |
580 | 0 | { |
581 | 0 | } |
582 | 0 | } |
583 | |
|
584 | 0 | rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList; |
585 | |
|
586 | 0 | pList->AddAttribute( ATTRIBUTE_XMLNS_TOOLBAR, |
587 | 0 | u"" XMLNS_TOOLBAR ""_ustr ); |
588 | |
|
589 | 0 | pList->AddAttribute( ATTRIBUTE_XMLNS_XLINK, |
590 | 0 | u"" XMLNS_XLINK ""_ustr ); |
591 | |
|
592 | 0 | if ( !aUIName.isEmpty() ) |
593 | 0 | pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_UINAME, |
594 | 0 | aUIName ); |
595 | |
|
596 | 0 | m_xWriteDocumentHandler->startElement( ELEMENT_NS_TOOLBAR, pList ); |
597 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
598 | |
|
599 | 0 | sal_Int32 nItemCount = m_rItemAccess->getCount(); |
600 | 0 | Any aAny; |
601 | |
|
602 | 0 | for ( sal_Int32 nItemPos = 0; nItemPos < nItemCount; nItemPos++ ) |
603 | 0 | { |
604 | 0 | Sequence< PropertyValue > aProps; |
605 | 0 | aAny = m_rItemAccess->getByIndex( nItemPos ); |
606 | 0 | if ( aAny >>= aProps ) |
607 | 0 | { |
608 | 0 | OUString aCommandURL; |
609 | 0 | OUString aLabel; |
610 | 0 | bool bVisible( true ); |
611 | 0 | sal_Int16 nType( css::ui::ItemType::DEFAULT ); |
612 | 0 | sal_Int16 nStyle( 0 ); |
613 | |
|
614 | 0 | ExtractToolbarParameters( aProps, aCommandURL, aLabel, nStyle, bVisible, nType ); |
615 | 0 | if ( nType == css::ui::ItemType::DEFAULT ) |
616 | 0 | WriteToolBoxItem( aCommandURL, aLabel, nStyle, bVisible ); |
617 | 0 | else if ( nType == css::ui::ItemType::SEPARATOR_SPACE ) |
618 | 0 | WriteToolBoxSpace(); |
619 | 0 | else if ( nType == css::ui::ItemType::SEPARATOR_LINE ) |
620 | 0 | WriteToolBoxSeparator(); |
621 | 0 | else if ( nType == css::ui::ItemType::SEPARATOR_LINEBREAK ) |
622 | 0 | WriteToolBoxBreak(); |
623 | 0 | } |
624 | 0 | } |
625 | |
|
626 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
627 | 0 | m_xWriteDocumentHandler->endElement( ELEMENT_NS_TOOLBAR ); |
628 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
629 | 0 | m_xWriteDocumentHandler->endDocument(); |
630 | 0 | } |
631 | | |
632 | | // protected member functions |
633 | | |
634 | | void OWriteToolBoxDocumentHandler::WriteToolBoxItem( |
635 | | const OUString& rCommandURL, |
636 | | const OUString& rLabel, |
637 | | sal_Int16 nStyle, |
638 | | bool bVisible ) |
639 | 0 | { |
640 | 0 | rtl::Reference<::comphelper::AttributeList> pList = new ::comphelper::AttributeList; |
641 | |
|
642 | 0 | if ( m_aAttributeURL.isEmpty() ) |
643 | 0 | { |
644 | 0 | m_aAttributeURL = m_aXMLXlinkNS + ATTRIBUTE_URL; |
645 | 0 | } |
646 | | |
647 | | // save required attribute (URL) |
648 | 0 | pList->AddAttribute( m_aAttributeURL, rCommandURL ); |
649 | |
|
650 | 0 | if ( !rLabel.isEmpty() ) |
651 | 0 | { |
652 | 0 | pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_TEXT, |
653 | 0 | rLabel ); |
654 | 0 | } |
655 | |
|
656 | 0 | if ( !bVisible ) |
657 | 0 | { |
658 | 0 | pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_VISIBLE, |
659 | 0 | ATTRIBUTE_BOOLEAN_FALSE ); |
660 | 0 | } |
661 | |
|
662 | 0 | if ( nStyle > 0 ) |
663 | 0 | { |
664 | 0 | OUStringBuffer aValue; |
665 | 0 | const ToolboxStyleItem* pStyle = Styles; |
666 | |
|
667 | 0 | for ( sal_Int32 nIndex = 0; nIndex < nStyleItemEntries; ++nIndex, ++pStyle ) |
668 | 0 | { |
669 | 0 | if ( nStyle & pStyle->nBit ) |
670 | 0 | { |
671 | 0 | if ( !aValue.isEmpty() ) |
672 | 0 | aValue.append(" "); |
673 | 0 | aValue.append( pStyle->attrName ); |
674 | 0 | } |
675 | 0 | } |
676 | 0 | pList->AddAttribute( m_aXMLToolbarNS + ATTRIBUTE_ITEMSTYLE, |
677 | 0 | aValue.makeStringAndClear() ); |
678 | 0 | } |
679 | |
|
680 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
681 | 0 | m_xWriteDocumentHandler->startElement( ELEMENT_NS_TOOLBARITEM, pList ); |
682 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
683 | 0 | m_xWriteDocumentHandler->endElement( ELEMENT_NS_TOOLBARITEM ); |
684 | 0 | } |
685 | | |
686 | | void OWriteToolBoxDocumentHandler::WriteToolBoxSpace() |
687 | 0 | { |
688 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
689 | 0 | m_xWriteDocumentHandler->startElement( ELEMENT_NS_TOOLBARSPACE, m_xEmptyList ); |
690 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
691 | 0 | m_xWriteDocumentHandler->endElement( ELEMENT_NS_TOOLBARSPACE ); |
692 | 0 | } |
693 | | |
694 | | void OWriteToolBoxDocumentHandler::WriteToolBoxBreak() |
695 | 0 | { |
696 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
697 | 0 | m_xWriteDocumentHandler->startElement( ELEMENT_NS_TOOLBARBREAK, m_xEmptyList ); |
698 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
699 | 0 | m_xWriteDocumentHandler->endElement( ELEMENT_NS_TOOLBARBREAK ); |
700 | 0 | } |
701 | | |
702 | | void OWriteToolBoxDocumentHandler::WriteToolBoxSeparator() |
703 | 0 | { |
704 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
705 | 0 | m_xWriteDocumentHandler->startElement( ELEMENT_NS_TOOLBARSEPARATOR, m_xEmptyList ); |
706 | 0 | m_xWriteDocumentHandler->ignorableWhitespace( OUString() ); |
707 | 0 | m_xWriteDocumentHandler->endElement( ELEMENT_NS_TOOLBARSEPARATOR ); |
708 | 0 | } |
709 | | |
710 | | } // namespace framework |
711 | | |
712 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |