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