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
;
31 #define ELEMENT_ACCELERATORLIST "acceleratorlist"
32 #define ELEMENT_ACCELERATORITEM "item"
34 #define ATTRIBUTE_KEYCODE "code"
35 #define ATTRIBUTE_MODIFIER "modifier"
36 #define ATTRIBUTE_URL "url"
38 #define ATTRIBUTE_TYPE_CDATA "CDATA"
42 struct AttributeListImpl_impl
;
43 class AttributeListImpl
: public ::cppu::WeakImplHelper1
< ::com::sun::star::xml::sax::XAttributeList
>
50 AttributeListImpl( const AttributeListImpl
& );
53 virtual sal_Int16 SAL_CALL
getLength(void) throw (::com::sun::star::uno::RuntimeException
);
54 virtual OUString SAL_CALL
getNameByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
55 virtual OUString SAL_CALL
getTypeByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
56 virtual OUString SAL_CALL
getTypeByName(const OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
);
57 virtual OUString SAL_CALL
getValueByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
58 virtual OUString SAL_CALL
getValueByName(const OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
);
61 void addAttribute( const OUString
&sName
, const OUString
&sType
, const OUString
&sValue
);
64 struct AttributeListImpl_impl
*m_pImpl
;
70 TagAttribute( const OUString
&aName
, const OUString
&aType
, const OUString
&aValue
)
82 struct AttributeListImpl_impl
84 AttributeListImpl_impl()
86 // performance improvement during adding
87 vecAttribute
.reserve(20);
89 ::std::vector
<struct TagAttribute
> vecAttribute
;
94 sal_Int16 SAL_CALL
AttributeListImpl::getLength(void) throw (RuntimeException
)
96 return sal::static_int_cast
< sal_Int16
>(m_pImpl
->vecAttribute
.size());
100 AttributeListImpl::AttributeListImpl( const AttributeListImpl
&r
) :
101 cppu::WeakImplHelper1
<com::sun::star::xml::sax::XAttributeList
>(r
)
103 m_pImpl
= new AttributeListImpl_impl
;
104 *m_pImpl
= *(r
.m_pImpl
);
107 OUString
AttributeListImpl::getNameByIndex(sal_Int16 i
) throw (RuntimeException
)
109 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
110 return m_pImpl
->vecAttribute
[i
].sName
;
116 OUString
AttributeListImpl::getTypeByIndex(sal_Int16 i
) throw (RuntimeException
)
118 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
119 return m_pImpl
->vecAttribute
[i
].sType
;
124 OUString
AttributeListImpl::getValueByIndex(sal_Int16 i
) throw (RuntimeException
)
126 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
127 return m_pImpl
->vecAttribute
[i
].sValue
;
133 OUString
AttributeListImpl::getTypeByName( const OUString
& sName
) throw (RuntimeException
)
135 ::std::vector
<struct TagAttribute
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
137 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ++ii
) {
138 if( (*ii
).sName
== sName
) {
145 OUString
AttributeListImpl::getValueByName(const OUString
& sName
) throw (RuntimeException
)
147 ::std::vector
<struct TagAttribute
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
149 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ++ii
) {
150 if( (*ii
).sName
== sName
) {
158 AttributeListImpl::AttributeListImpl()
160 m_pImpl
= new AttributeListImpl_impl
;
165 AttributeListImpl::~AttributeListImpl()
171 void AttributeListImpl::addAttribute( const OUString
&sName
,
172 const OUString
&sType
,
173 const OUString
&sValue
)
175 m_pImpl
->vecAttribute
.push_back( TagAttribute( sName
, sType
, sValue
) );
178 } // anonymous namespace
180 Any SAL_CALL
OReadAccelatorDocumentHandler::queryInterface( const Type
& rType
) throw( RuntimeException
)
182 Any a
= ::cppu::queryInterface( rType
,(static_cast< XDocumentHandler
* >(this)));
186 return OWeakObject::queryInterface( rType
);
189 void SAL_CALL
OReadAccelatorDocumentHandler::ignorableWhitespace(
191 throw( SAXException
, RuntimeException
)
195 void SAL_CALL
OReadAccelatorDocumentHandler::processingInstruction(
196 const OUString
&, const OUString
& )
197 throw( SAXException
, RuntimeException
)
201 void SAL_CALL
OReadAccelatorDocumentHandler::setDocumentLocator(
202 const Reference
< XLocator
> &xLocator
)
203 throw( SAXException
, RuntimeException
)
205 m_xLocator
= xLocator
;
208 OUString
OReadAccelatorDocumentHandler::getErrorLineString()
212 if ( m_xLocator
.is() )
214 return OUString::createFromAscii( buffer
);
220 void SAL_CALL
OReadAccelatorDocumentHandler::startDocument(void)
221 throw ( SAXException
, RuntimeException
)
225 void SAL_CALL
OReadAccelatorDocumentHandler::endDocument(void)
226 throw( SAXException
, RuntimeException
)
228 if ( m_nElementDepth
> 0 )
230 OUString aErrorMessage
= getErrorLineString();
231 aErrorMessage
+= "A closing element is missing!";
232 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
237 void SAL_CALL
OReadAccelatorDocumentHandler::startElement(
238 const OUString
& aElementName
, const Reference
< XAttributeList
> &xAttrList
)
239 throw( SAXException
, RuntimeException
)
243 if ( aElementName
== ELEMENT_ACCELERATORLIST
)
246 if ( m_bAcceleratorMode
)
248 OUString aErrorMessage
= getErrorLineString();
249 aErrorMessage
+= "Accelerator list used twice!";
250 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
253 m_bAcceleratorMode
= sal_True
;
255 else if ( aElementName
== ELEMENT_ACCELERATORITEM
)
258 if ( !m_bAcceleratorMode
)
260 OUString aErrorMessage
= getErrorLineString();
261 aErrorMessage
+= "Accelerator list element has to be used before!";
262 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
266 // read accelerator item
267 m_bItemCloseExpected
= sal_True
;
269 SvtAcceleratorConfigItem aItem
;
271 // read attributes for accelerator
272 for ( sal_Int16 i
=0; i
< xAttrList
->getLength(); i
++ )
274 OUString aName
= xAttrList
->getNameByIndex( i
);
275 OUString aValue
= xAttrList
->getValueByIndex( i
);
277 if ( aName
== ATTRIBUTE_URL
)
278 aItem
.aCommand
= aValue
;
279 else if ( aName
== ATTRIBUTE_MODIFIER
)
280 aItem
.nModifier
= (sal_uInt16
)aValue
.toInt32();
281 else if ( aName
== ATTRIBUTE_KEYCODE
)
282 aItem
.nCode
= (sal_uInt16
)aValue
.toInt32();
285 m_aReadAcceleratorList
.push_back( aItem
);
290 OUString aErrorMessage
= getErrorLineString();
291 aErrorMessage
+= "Unknown element found!";
292 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
297 void SAL_CALL
OReadAccelatorDocumentHandler::characters(const OUString
&)
298 throw( SAXException
, RuntimeException
)
303 void SAL_CALL
OReadAccelatorDocumentHandler::endElement( const OUString
& aName
)
304 throw( SAXException
, RuntimeException
)
308 if ( aName
== ELEMENT_ACCELERATORLIST
)
311 if ( !m_bAcceleratorMode
)
313 OUString aErrorMessage
= getErrorLineString();
314 aErrorMessage
+= "Accelerator list used twice!";
315 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
318 else if ( aName
== ELEMENT_ACCELERATORITEM
)
320 if ( !m_bItemCloseExpected
)
322 OUString aErrorMessage
= getErrorLineString();
323 aErrorMessage
+= "Closing accelerator item element expected!";
324 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
329 OUString aErrorMessage
= getErrorLineString();
330 aErrorMessage
+= "Unknown closing element found!";
331 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
335 // ------------------------------------------------------------------
337 OWriteAccelatorDocumentHandler::OWriteAccelatorDocumentHandler(
338 const SvtAcceleratorItemList
& aWriteAcceleratorList
, Reference
< XDocumentHandler
> xDocumentHandler
) :
339 m_xWriteDocumentHandler( xDocumentHandler
),
340 m_aWriteAcceleratorList( aWriteAcceleratorList
)
342 m_aAttributeType
= OUString( ATTRIBUTE_TYPE_CDATA
);
345 OWriteAccelatorDocumentHandler::~OWriteAccelatorDocumentHandler()
349 void OWriteAccelatorDocumentHandler::WriteAcceleratorDocument()
350 throw ( SAXException
, RuntimeException
)
352 AttributeListImpl
* pList
= new AttributeListImpl
;
353 Reference
< XAttributeList
> rList( (XAttributeList
*)pList
, UNO_QUERY
);
355 m_xWriteDocumentHandler
->startDocument();
356 m_xWriteDocumentHandler
->startElement( OUString( ELEMENT_ACCELERATORLIST
), rList
);
357 m_xWriteDocumentHandler
->ignorableWhitespace( OUString() );
359 std::list
< SvtAcceleratorConfigItem
>::const_iterator p
;
360 for ( p
= m_aWriteAcceleratorList
.begin(); p
!= m_aWriteAcceleratorList
.end(); ++p
)
361 WriteAcceleratorItem( *p
);
363 m_xWriteDocumentHandler
->endElement( OUString( ELEMENT_ACCELERATORLIST
) );
364 m_xWriteDocumentHandler
->endDocument();
367 void OWriteAccelatorDocumentHandler::WriteAcceleratorItem(
368 const SvtAcceleratorConfigItem
& aAcceleratorItem
)
369 throw( SAXException
, RuntimeException
)
371 AttributeListImpl
* pAcceleratorAttributes
= new AttributeListImpl
;
372 Reference
< XAttributeList
> xAcceleratorAttrList( (XAttributeList
*)pAcceleratorAttributes
, UNO_QUERY
);
375 pAcceleratorAttributes
->addAttribute(
376 OUString( ATTRIBUTE_KEYCODE
),
378 OUString::valueOf( aAcceleratorItem
.nCode
));
380 pAcceleratorAttributes
->addAttribute(
381 OUString( ATTRIBUTE_MODIFIER
),
383 OUString::valueOf( aAcceleratorItem
.nModifier
));
385 pAcceleratorAttributes
->addAttribute(
386 OUString( ATTRIBUTE_URL
),
388 aAcceleratorItem
.aCommand
);
390 // write start element
391 m_xWriteDocumentHandler
->startElement(
392 OUString( ELEMENT_ACCELERATORITEM
),
393 xAcceleratorAttrList
);
394 m_xWriteDocumentHandler
->ignorableWhitespace( OUString() );
395 m_xWriteDocumentHandler
->endElement( OUString( ELEMENT_ACCELERATORITEM
) );
398 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */