Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / unotools / source / config / xmlaccelcfg.cxx
blob543f3496599f0f109c1341ece65d8deb42717d2c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <unotools/xmlaccelcfg.hxx>
23 #include <vector>
24 #include <com/sun/star/xml/sax/XAttributeList.hpp>
25 #include <cppuhelper/implbase1.hxx>
27 using namespace com::sun::star::uno;
28 using namespace com::sun::star::xml::sax;
30 using ::rtl::OUString;
32 #define ELEMENT_ACCELERATORLIST "acceleratorlist"
33 #define ELEMENT_ACCELERATORITEM "item"
35 #define ATTRIBUTE_KEYCODE "code"
36 #define ATTRIBUTE_MODIFIER "modifier"
37 #define ATTRIBUTE_URL "url"
39 #define ATTRIBUTE_TYPE_CDATA "CDATA"
41 namespace {
43 struct AttributeListImpl_impl;
44 class AttributeListImpl : public ::cppu::WeakImplHelper1< ::com::sun::star::xml::sax::XAttributeList >
46 protected:
47 ~AttributeListImpl();
49 public:
50 AttributeListImpl();
51 AttributeListImpl( const AttributeListImpl & );
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);
61 public:
62 void addAttribute( const ::rtl::OUString &sName , const ::rtl::OUString &sType , const ::rtl::OUString &sValue );
64 private:
65 struct AttributeListImpl_impl *m_pImpl;
68 struct TagAttribute
70 TagAttribute(){}
71 TagAttribute( const OUString &aName, const OUString &aType , const OUString &aValue )
73 sName = aName;
74 sType = aType;
75 sValue = aValue;
78 OUString sName;
79 OUString sType;
80 OUString sValue;
83 struct AttributeListImpl_impl
85 AttributeListImpl_impl()
87 // performance improvement during adding
88 vecAttribute.reserve(20);
90 ::std::vector<struct TagAttribute> vecAttribute;
95 sal_Int16 SAL_CALL AttributeListImpl::getLength(void) throw (RuntimeException)
97 return sal::static_int_cast< sal_Int16 >(m_pImpl->vecAttribute.size());
101 AttributeListImpl::AttributeListImpl( const AttributeListImpl &r ) :
102 cppu::WeakImplHelper1<com::sun::star::xml::sax::XAttributeList>(r)
104 m_pImpl = new AttributeListImpl_impl;
105 *m_pImpl = *(r.m_pImpl);
108 OUString AttributeListImpl::getNameByIndex(sal_Int16 i) throw (RuntimeException)
110 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
111 return m_pImpl->vecAttribute[i].sName;
113 return OUString();
117 OUString AttributeListImpl::getTypeByIndex(sal_Int16 i) throw (RuntimeException)
119 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
120 return m_pImpl->vecAttribute[i].sType;
122 return OUString();
125 OUString AttributeListImpl::getValueByIndex(sal_Int16 i) throw (RuntimeException)
127 if( i < sal::static_int_cast<sal_Int16>(m_pImpl->vecAttribute.size()) ) {
128 return m_pImpl->vecAttribute[i].sValue;
130 return OUString();
134 OUString AttributeListImpl::getTypeByName( const OUString& sName ) throw (RuntimeException)
136 ::std::vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
138 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
139 if( (*ii).sName == sName ) {
140 return (*ii).sType;
143 return OUString();
146 OUString AttributeListImpl::getValueByName(const OUString& sName) throw (RuntimeException)
148 ::std::vector<struct TagAttribute>::iterator ii = m_pImpl->vecAttribute.begin();
150 for( ; ii != m_pImpl->vecAttribute.end() ; ++ii ) {
151 if( (*ii).sName == sName ) {
152 return (*ii).sValue;
155 return OUString();
159 AttributeListImpl::AttributeListImpl()
161 m_pImpl = new AttributeListImpl_impl;
166 AttributeListImpl::~AttributeListImpl()
168 delete m_pImpl;
172 void AttributeListImpl::addAttribute( const OUString &sName ,
173 const OUString &sType ,
174 const OUString &sValue )
176 m_pImpl->vecAttribute.push_back( TagAttribute( sName , sType , sValue ) );
179 } // anonymous namespace
181 Any SAL_CALL OReadAccelatorDocumentHandler::queryInterface( const Type & rType ) throw( RuntimeException )
183 Any a = ::cppu::queryInterface( rType ,(static_cast< XDocumentHandler* >(this)));
184 if ( a.hasValue() )
185 return a;
186 else
187 return OWeakObject::queryInterface( rType );
190 void SAL_CALL OReadAccelatorDocumentHandler::ignorableWhitespace(
191 const OUString& )
192 throw( SAXException, RuntimeException )
196 void SAL_CALL OReadAccelatorDocumentHandler::processingInstruction(
197 const OUString&, const OUString& )
198 throw( SAXException, RuntimeException )
202 void SAL_CALL OReadAccelatorDocumentHandler::setDocumentLocator(
203 const Reference< XLocator > &xLocator)
204 throw( SAXException, RuntimeException )
206 m_xLocator = xLocator;
209 ::rtl::OUString OReadAccelatorDocumentHandler::getErrorLineString()
211 char buffer[32];
213 if ( m_xLocator.is() )
215 return OUString::createFromAscii( buffer );
217 else
218 return OUString();
221 void SAL_CALL OReadAccelatorDocumentHandler::startDocument(void)
222 throw ( SAXException, RuntimeException )
226 void SAL_CALL OReadAccelatorDocumentHandler::endDocument(void)
227 throw( SAXException, RuntimeException )
229 if ( m_nElementDepth > 0 )
231 OUString aErrorMessage = getErrorLineString();
232 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "A closing element is missing!" ));
233 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
238 void SAL_CALL OReadAccelatorDocumentHandler::startElement(
239 const OUString& aElementName, const Reference< XAttributeList > &xAttrList )
240 throw( SAXException, RuntimeException )
242 m_nElementDepth++;
244 if ( aElementName == ELEMENT_ACCELERATORLIST )
246 // acceleratorlist
247 if ( m_bAcceleratorMode )
249 OUString aErrorMessage = getErrorLineString();
250 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
251 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
253 else
254 m_bAcceleratorMode = sal_True;
256 else if ( aElementName == ELEMENT_ACCELERATORITEM )
258 // accelerator item
259 if ( !m_bAcceleratorMode )
261 OUString aErrorMessage = getErrorLineString();
262 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list element has to be used before!" ));
263 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
265 else
267 // read accelerator item
268 m_bItemCloseExpected = sal_True;
270 SvtAcceleratorConfigItem aItem;
272 // read attributes for accelerator
273 for ( sal_Int16 i=0; i< xAttrList->getLength(); i++ )
275 OUString aName = xAttrList->getNameByIndex( i );
276 OUString aValue = xAttrList->getValueByIndex( i );
278 if ( aName == ATTRIBUTE_URL )
279 aItem.aCommand = aValue;
280 else if ( aName == ATTRIBUTE_MODIFIER )
281 aItem.nModifier = (sal_uInt16)aValue.toInt32();
282 else if ( aName == ATTRIBUTE_KEYCODE )
283 aItem.nCode = (sal_uInt16)aValue.toInt32();
286 m_aReadAcceleratorList.push_back( aItem );
289 else
291 OUString aErrorMessage = getErrorLineString();
292 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown element found!" ));
293 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
298 void SAL_CALL OReadAccelatorDocumentHandler::characters(const rtl::OUString&)
299 throw( SAXException, RuntimeException )
304 void SAL_CALL OReadAccelatorDocumentHandler::endElement( const OUString& aName )
305 throw( SAXException, RuntimeException )
307 m_nElementDepth--;
309 if ( aName == ELEMENT_ACCELERATORLIST )
311 // acceleratorlist
312 if ( !m_bAcceleratorMode )
314 OUString aErrorMessage = getErrorLineString();
315 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
316 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
319 else if ( aName == ELEMENT_ACCELERATORITEM )
321 if ( !m_bItemCloseExpected )
323 OUString aErrorMessage = getErrorLineString();
324 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Closing accelerator item element expected!" ));
325 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
328 else
330 OUString aErrorMessage = getErrorLineString();
331 aErrorMessage += OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown closing element found!" ));
332 throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
336 // ------------------------------------------------------------------
338 OWriteAccelatorDocumentHandler::OWriteAccelatorDocumentHandler(
339 const SvtAcceleratorItemList& aWriteAcceleratorList, Reference< XDocumentHandler > xDocumentHandler ) :
340 m_xWriteDocumentHandler( xDocumentHandler ),
341 m_aWriteAcceleratorList( aWriteAcceleratorList )
343 m_aAttributeType = OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE_CDATA ));
346 OWriteAccelatorDocumentHandler::~OWriteAccelatorDocumentHandler()
350 void OWriteAccelatorDocumentHandler::WriteAcceleratorDocument()
351 throw ( SAXException, RuntimeException )
353 AttributeListImpl* pList = new AttributeListImpl;
354 Reference< XAttributeList > rList( (XAttributeList *)pList , UNO_QUERY );
356 m_xWriteDocumentHandler->startDocument();
357 m_xWriteDocumentHandler->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST )), rList );
358 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
360 std::list< SvtAcceleratorConfigItem>::const_iterator p;
361 for ( p = m_aWriteAcceleratorList.begin(); p != m_aWriteAcceleratorList.end(); ++p )
362 WriteAcceleratorItem( *p );
364 m_xWriteDocumentHandler->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST )) );
365 m_xWriteDocumentHandler->endDocument();
368 void OWriteAccelatorDocumentHandler::WriteAcceleratorItem(
369 const SvtAcceleratorConfigItem& aAcceleratorItem )
370 throw( SAXException, RuntimeException )
372 AttributeListImpl* pAcceleratorAttributes = new AttributeListImpl;
373 Reference< XAttributeList > xAcceleratorAttrList( (XAttributeList *)pAcceleratorAttributes , UNO_QUERY );
375 // set attributes
376 pAcceleratorAttributes->addAttribute(
377 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_KEYCODE )),
378 m_aAttributeType,
379 OUString::valueOf( aAcceleratorItem.nCode ));
381 pAcceleratorAttributes->addAttribute(
382 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MODIFIER )),
383 m_aAttributeType,
384 OUString::valueOf( aAcceleratorItem.nModifier ));
386 pAcceleratorAttributes->addAttribute(
387 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_URL )),
388 m_aAttributeType,
389 aAcceleratorItem.aCommand );
391 // write start element
392 m_xWriteDocumentHandler->startElement(
393 OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM )),
394 xAcceleratorAttrList );
395 m_xWriteDocumentHandler->ignorableWhitespace( OUString() );
396 m_xWriteDocumentHandler->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM )) );
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */