Branch data Line data Source code
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 : :
21 : : #include <unotools/xmlaccelcfg.hxx>
22 : :
23 : : #include <vector>
24 : : #include <com/sun/star/xml/sax/XAttributeList.hpp>
25 : : #include <cppuhelper/implbase1.hxx>
26 : :
27 : : using namespace com::sun::star::uno;
28 : : using namespace com::sun::star::xml::sax;
29 : :
30 : : using ::rtl::OUString;
31 : :
32 : : #define ELEMENT_ACCELERATORLIST "acceleratorlist"
33 : : #define ELEMENT_ACCELERATORITEM "item"
34 : :
35 : : #define ATTRIBUTE_KEYCODE "code"
36 : : #define ATTRIBUTE_MODIFIER "modifier"
37 : : #define ATTRIBUTE_URL "url"
38 : :
39 : : #define ATTRIBUTE_TYPE_CDATA "CDATA"
40 : :
41 : : // ------------------------------------------------------------------
42 : :
43 : : struct AttributeListImpl_impl;
44 : : class AttributeListImpl : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XAttributeList >
45 : : {
46 : : protected:
47 : : ~AttributeListImpl();
48 : :
49 : : public:
50 : : AttributeListImpl();
51 : : AttributeListImpl( const AttributeListImpl & );
52 : :
53 : : public:
54 : : virtual sal_Int16 SAL_CALL getLength(void) throw (::com::sun::star::uno::RuntimeException);
55 : : virtual ::rtl::OUString SAL_CALL getNameByIndex(sal_Int16 i) throw (::com::sun::star::uno::RuntimeException);
56 : : virtual ::rtl::OUString SAL_CALL getTypeByIndex(sal_Int16 i) throw (::com::sun::star::uno::RuntimeException);
57 : : virtual ::rtl::OUString SAL_CALL getTypeByName(const ::rtl::OUString& aName) throw (::com::sun::star::uno::RuntimeException);
58 : : virtual ::rtl::OUString SAL_CALL getValueByIndex(sal_Int16 i) throw (::com::sun::star::uno::RuntimeException);
59 : : virtual ::rtl::OUString SAL_CALL getValueByName(const ::rtl::OUString& aName) throw (::com::sun::star::uno::RuntimeException);
60 : :
61 : : public:
62 : : void addAttribute( const ::rtl::OUString &sName , const ::rtl::OUString &sType , const ::rtl::OUString &sValue );
63 : : void clear();
64 : :
65 : : private:
66 : : struct AttributeListImpl_impl *m_pImpl;
67 : : };
68 : :
69 : 0 : struct TagAttribute
70 : : {
71 : : TagAttribute(){}
72 : 0 : TagAttribute( const OUString &aName, const OUString &aType , const OUString &aValue )
73 : 0 : {
74 : 0 : sName = aName;
75 : 0 : sType = aType;
76 : 0 : sValue = aValue;
77 : 0 : }
78 : :
79 : : OUString sName;
80 : : OUString sType;
81 : : OUString sValue;
82 : : };
83 : :
84 : 0 : struct AttributeListImpl_impl
85 : : {
86 : 0 : AttributeListImpl_impl()
87 : 0 : {
88 : : // performance improvement during adding
89 [ # # ]: 0 : vecAttribute.reserve(20);
90 : 0 : }
91 : : ::std::vector<struct TagAttribute> vecAttribute;
92 : : };
93 : :
94 : :
95 : :
96 : 0 : sal_Int16 SAL_CALL AttributeListImpl::getLength(void) throw (RuntimeException)
97 : : {
98 : 0 : return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
99 : : }
100 : :
101 : :
102 : 0 : AttributeListImpl::AttributeListImpl( const AttributeListImpl &r ) :
103 : 0 : cppu::WeakImplHelper1<com::sun::star::xml::sax::XAttributeList>(r)
104 : : {
105 [ # # ][ # # ]: 0 : m_pImpl = new AttributeListImpl_impl;
106 [ # # ]: 0 : *m_pImpl = *(r.m_pImpl);
107 : 0 : }
108 : :
109 : 0 : OUString AttributeListImpl::getNameByIndex(sal_Int16 i) throw (RuntimeException)
110 : : {
111 [ # # ]: 0 : if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
112 : 0 : return m_pImpl->vecAttribute[i].sName;
113 : : }
114 : 0 : return OUString();
115 : : }
116 : :
117 : :
118 : 0 : OUString AttributeListImpl::getTypeByIndex(sal_Int16 i) throw (RuntimeException)
119 : : {
120 [ # # ]: 0 : if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
121 : 0 : return m_pImpl->vecAttribute[i].sType;
122 : : }
123 : 0 : return OUString();
124 : : }
125 : :
126 : 0 : OUString AttributeListImpl::getValueByIndex(sal_Int16 i) throw (RuntimeException)
127 : : {
128 [ # # ]: 0 : if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
129 : 0 : return m_pImpl->vecAttribute[i].sValue;
130 : : }
131 : 0 : return OUString();
132 : :
133 : : }
134 : :
135 : 0 : OUString AttributeListImpl::getTypeByName( const OUString& sName ) throw (RuntimeException)
136 : : {
137 : 0 : ::std::vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
138 : :
139 [ # # ][ # # ]: 0 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
140 [ # # ]: 0 : if( (*ii).sName == sName ) {
141 : 0 : return (*ii).sType;
142 : : }
143 : : }
144 : 0 : return OUString();
145 : : }
146 : :
147 : 0 : OUString AttributeListImpl::getValueByName(const OUString& sName) throw (RuntimeException)
148 : : {
149 : 0 : ::std::vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
150 : :
151 [ # # ][ # # ]: 0 : for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
152 [ # # ]: 0 : if( (*ii).sName == sName ) {
153 : 0 : return (*ii).sValue;
154 : : }
155 : : }
156 : 0 : return OUString();
157 : : }
158 : :
159 : :
160 : 0 : AttributeListImpl::AttributeListImpl()
161 : : {
162 [ # # ][ # # ]: 0 : m_pImpl = new AttributeListImpl_impl;
163 : 0 : }
164 : :
165 : :
166 : :
167 : 0 : AttributeListImpl::~AttributeListImpl()
168 : : {
169 [ # # ]: 0 : delete m_pImpl;
170 [ # # ]: 0 : }
171 : :
172 : :
173 : 0 : void AttributeListImpl::addAttribute( const OUString &sName ,
174 : : const OUString &sType ,
175 : : const OUString &sValue )
176 : : {
177 [ # # ]: 0 : m_pImpl->vecAttribute.push_back( TagAttribute( sName , sType , sValue ) );
178 : 0 : }
179 : :
180 : 0 : void AttributeListImpl::clear()
181 : : {
182 [ # # ]: 0 : ::std::vector<struct TagAttribute> dummy;
183 : 0 : m_pImpl->vecAttribute.swap( dummy );
184 : :
185 : 0 : OSL_ASSERT( ! getLength() );
186 : 0 : }
187 : :
188 : : // ------------------------------------------------------------------
189 : :
190 : 0 : Any SAL_CALL OReadAccelatorDocumentHandler::queryInterface( const Type & rType ) throw( RuntimeException )
191 : : {
192 [ # # ]: 0 : Any a = ::cppu::queryInterface( rType ,(static_cast< XDocumentHandler* >(this)));
193 [ # # ]: 0 : if ( a.hasValue() )
194 : 0 : return a;
195 : : else
196 [ # # ]: 0 : return OWeakObject::queryInterface( rType );
197 : : }
198 : :
199 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::ignorableWhitespace(
200 : : const OUString& )
201 : : throw( SAXException, RuntimeException )
202 : : {
203 : 0 : }
204 : :
205 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::processingInstruction(
206 : : const OUString&, const OUString& )
207 : : throw( SAXException, RuntimeException )
208 : : {
209 : 0 : }
210 : :
211 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::setDocumentLocator(
212 : : const Reference< XLocator > &xLocator)
213 : : throw( SAXException, RuntimeException )
214 : : {
215 : 0 : m_xLocator = xLocator;
216 : 0 : }
217 : :
218 : 0 : ::rtl::OUString OReadAccelatorDocumentHandler::getErrorLineString()
219 : : {
220 : : char buffer[32];
221 : :
222 [ # # ]: 0 : if ( m_xLocator.is() )
223 : : {
224 : 0 : return OUString::createFromAscii( buffer );
225 : : }
226 : : else
227 : 0 : return OUString();
228 : : }
229 : :
230 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::startDocument(void)
231 : : throw ( SAXException, RuntimeException )
232 : : {
233 : 0 : }
234 : :
235 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::endDocument(void)
236 : : throw( SAXException, RuntimeException )
237 : : {
238 [ # # ]: 0 : if ( m_nElementDepth > 0 )
239 : : {
240 : 0 : OUString aErrorMessage = getErrorLineString();
241 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "A closing element is missing!" ));
242 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
243 : : }
244 : 0 : }
245 : :
246 : :
247 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::startElement(
248 : : const OUString& aElementName, const Reference< XAttributeList > &xAttrList )
249 : : throw( SAXException, RuntimeException )
250 : : {
251 : 0 : m_nElementDepth++;
252 : :
253 [ # # ]: 0 : if ( aElementName == ELEMENT_ACCELERATORLIST )
254 : : {
255 : : // acceleratorlist
256 [ # # ]: 0 : if ( m_bAcceleratorMode )
257 : : {
258 : 0 : OUString aErrorMessage = getErrorLineString();
259 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
260 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
261 : : }
262 : : else
263 : 0 : m_bAcceleratorMode = sal_True;
264 : : }
265 [ # # ]: 0 : else if ( aElementName == ELEMENT_ACCELERATORITEM )
266 : : {
267 : : // accelerator item
268 [ # # ]: 0 : if ( !m_bAcceleratorMode )
269 : : {
270 : 0 : OUString aErrorMessage = getErrorLineString();
271 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list element has to be used before!" ));
272 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
273 : : }
274 : : else
275 : : {
276 : : // read accelerator item
277 : 0 : m_bItemCloseExpected = sal_True;
278 : :
279 : 0 : SvtAcceleratorConfigItem aItem;
280 : :
281 : : // read attributes for accelerator
282 [ # # ][ # # ]: 0 : for ( sal_Int16 i=0; i< xAttrList->getLength(); i++ )
[ # # ]
283 : : {
284 [ # # ][ # # ]: 0 : OUString aName = xAttrList->getNameByIndex( i );
285 [ # # ][ # # ]: 0 : OUString aValue = xAttrList->getValueByIndex( i );
286 : :
287 [ # # ]: 0 : if ( aName == ATTRIBUTE_URL )
288 : 0 : aItem.aCommand = aValue;
289 [ # # ]: 0 : else if ( aName == ATTRIBUTE_MODIFIER )
290 : 0 : aItem.nModifier = (sal_uInt16)aValue.toInt32();
291 [ # # ]: 0 : else if ( aName == ATTRIBUTE_KEYCODE )
292 : 0 : aItem.nCode = (sal_uInt16)aValue.toInt32();
293 : 0 : }
294 : :
295 [ # # ]: 0 : m_aReadAcceleratorList.push_back( aItem );
296 : : }
297 : : }
298 : : else
299 : : {
300 : 0 : OUString aErrorMessage = getErrorLineString();
301 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown element found!" ));
302 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
303 : : }
304 : 0 : }
305 : :
306 : :
307 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::characters(const rtl::OUString&)
308 : : throw( SAXException, RuntimeException )
309 : : {
310 : 0 : }
311 : :
312 : :
313 : 0 : void SAL_CALL OReadAccelatorDocumentHandler::endElement( const OUString& aName )
314 : : throw( SAXException, RuntimeException )
315 : : {
316 : 0 : m_nElementDepth--;
317 : :
318 [ # # ]: 0 : if ( aName == ELEMENT_ACCELERATORLIST )
319 : : {
320 : : // acceleratorlist
321 [ # # ]: 0 : if ( !m_bAcceleratorMode )
322 : : {
323 : 0 : OUString aErrorMessage = getErrorLineString();
324 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
325 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
326 : : }
327 : : }
328 [ # # ]: 0 : else if ( aName == ELEMENT_ACCELERATORITEM )
329 : : {
330 [ # # ]: 0 : if ( !m_bItemCloseExpected )
331 : : {
332 : 0 : OUString aErrorMessage = getErrorLineString();
333 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Closing accelerator item element expected!" ));
334 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
335 : : }
336 : : }
337 : : else
338 : : {
339 : 0 : OUString aErrorMessage = getErrorLineString();
340 [ # # ]: 0 : aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown closing element found!" ));
341 [ # # ]: 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
342 : : }
343 : 0 : }
344 : :
345 : : // ------------------------------------------------------------------
346 : :
347 : 0 : OWriteAccelatorDocumentHandler::OWriteAccelatorDocumentHandler(
348 : : const SvtAcceleratorItemList& aWriteAcceleratorList, Reference< XDocumentHandler > xDocumentHandler ) :
349 : : m_xWriteDocumentHandler( xDocumentHandler ),
350 : 0 : m_aWriteAcceleratorList( aWriteAcceleratorList )
351 : : {
352 [ # # ]: 0 : m_aAttributeType = OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE_CDATA ));
353 : 0 : }
354 : :
355 : 0 : OWriteAccelatorDocumentHandler::~OWriteAccelatorDocumentHandler()
356 : : {
357 [ # # ]: 0 : }
358 : :
359 : 0 : void OWriteAccelatorDocumentHandler::WriteAcceleratorDocument()
360 : : throw ( SAXException, RuntimeException )
361 : : {
362 [ # # ]: 0 : AttributeListImpl* pList = new AttributeListImpl;
363 [ # # ][ # # ]: 0 : Reference< XAttributeList > rList( (XAttributeList *)pList , UNO_QUERY );
364 : :
365 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->startDocument();
366 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST )), rList );
[ # # ]
367 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
368 : :
369 : 0 : std::list< SvtAcceleratorConfigItem>::const_iterator p;
370 [ # # ]: 0 : for ( p = m_aWriteAcceleratorList.begin(); p != m_aWriteAcceleratorList.end(); ++p )
371 [ # # ]: 0 : WriteAcceleratorItem( *p );
372 : :
373 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST )) );
[ # # ]
374 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->endDocument();
375 : 0 : }
376 : :
377 : 0 : void OWriteAccelatorDocumentHandler::WriteAcceleratorItem(
378 : : const SvtAcceleratorConfigItem& aAcceleratorItem )
379 : : throw( SAXException, RuntimeException )
380 : : {
381 [ # # ]: 0 : AttributeListImpl* pAcceleratorAttributes = new AttributeListImpl;
382 [ # # ][ # # ]: 0 : Reference< XAttributeList > xAcceleratorAttrList( (XAttributeList *)pAcceleratorAttributes , UNO_QUERY );
383 : :
384 : : // set attributes
385 : : pAcceleratorAttributes->addAttribute(
386 : : OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_KEYCODE )),
387 : : m_aAttributeType,
388 [ # # ][ # # ]: 0 : OUString::valueOf( aAcceleratorItem.nCode ));
389 : :
390 : : pAcceleratorAttributes->addAttribute(
391 : : OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MODIFIER )),
392 : : m_aAttributeType,
393 [ # # ][ # # ]: 0 : OUString::valueOf( aAcceleratorItem.nModifier ));
394 : :
395 : : pAcceleratorAttributes->addAttribute(
396 : : OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_URL )),
397 : : m_aAttributeType,
398 [ # # ][ # # ]: 0 : aAcceleratorItem.aCommand );
399 : :
400 : : // write start element
401 [ # # ]: 0 : m_xWriteDocumentHandler->startElement(
402 : : OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM )),
403 [ # # ][ # # ]: 0 : xAcceleratorAttrList );
404 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
405 [ # # ][ # # ]: 0 : m_xWriteDocumentHandler->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM )) );
[ # # ]
406 : 0 : }
407 : :
408 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|