1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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"
43 struct AttributeListImpl_impl
;
44 class AttributeListImpl
: public ::cppu::WeakImplHelper1
< ::com::sun::star::xml::sax::XAttributeList
>
51 AttributeListImpl( const AttributeListImpl
& );
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
);
62 void addAttribute( const ::rtl::OUString
&sName
, const ::rtl::OUString
&sType
, const ::rtl::OUString
&sValue
);
65 struct AttributeListImpl_impl
*m_pImpl
;
71 TagAttribute( const OUString
&aName
, const OUString
&aType
, const OUString
&aValue
)
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
;
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
;
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
;
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
) {
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
) {
159 AttributeListImpl::AttributeListImpl()
161 m_pImpl
= new AttributeListImpl_impl
;
166 AttributeListImpl::~AttributeListImpl()
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)));
187 return OWeakObject::queryInterface( rType
);
190 void SAL_CALL
OReadAccelatorDocumentHandler::ignorableWhitespace(
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()
213 if ( m_xLocator
.is() )
215 return OUString::createFromAscii( buffer
);
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
)
244 if ( aElementName
== ELEMENT_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() );
254 m_bAcceleratorMode
= sal_True
;
256 else if ( aElementName
== ELEMENT_ACCELERATORITEM
)
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() );
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
);
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
)
309 if ( aName
== ELEMENT_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() );
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
);
376 pAcceleratorAttributes
->addAttribute(
377 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_KEYCODE
)),
379 OUString::valueOf( aAcceleratorItem
.nCode
));
381 pAcceleratorAttributes
->addAttribute(
382 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MODIFIER
)),
384 OUString::valueOf( aAcceleratorItem
.nModifier
));
386 pAcceleratorAttributes
->addAttribute(
387 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_URL
)),
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: */