1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xmlaccelcfg.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
34 #include "xmlaccelcfg.hxx"
37 #if OSL_DEBUG_LEVEL == 0
43 #include <com/sun/star/xml/sax/XAttributeList.hpp>
44 #include <cppuhelper/implbase1.hxx>
47 using namespace com::sun::star::uno
;
48 using namespace com::sun::star::xml::sax
;
50 #define ELEMENT_ACCELERATORLIST "acceleratorlist"
51 #define ELEMENT_ACCELERATORITEM "item"
53 #define ATTRIBUTE_KEYCODE "code"
54 #define ATTRIBUTE_MODIFIER "modifier"
55 #define ATTRIBUTE_URL "url"
57 #define ATTRIBUTE_TYPE_CDATA "CDATA"
59 // ------------------------------------------------------------------
61 struct AttributeListImpl_impl
;
62 class AttributeListImpl
: public ::cppu::WeakImplHelper1
< ::com::sun::star::xml::sax::XAttributeList
>
69 AttributeListImpl( const AttributeListImpl
& );
72 virtual sal_Int16 SAL_CALL
getLength(void) throw (::com::sun::star::uno::RuntimeException
);
73 virtual ::rtl::OUString SAL_CALL
getNameByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
74 virtual ::rtl::OUString SAL_CALL
getTypeByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
75 virtual ::rtl::OUString SAL_CALL
getTypeByName(const ::rtl::OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
);
76 virtual ::rtl::OUString SAL_CALL
getValueByIndex(sal_Int16 i
) throw (::com::sun::star::uno::RuntimeException
);
77 virtual ::rtl::OUString SAL_CALL
getValueByName(const ::rtl::OUString
& aName
) throw (::com::sun::star::uno::RuntimeException
);
80 void addAttribute( const ::rtl::OUString
&sName
, const ::rtl::OUString
&sType
, const ::rtl::OUString
&sValue
);
84 struct AttributeListImpl_impl
*m_pImpl
;
90 TagAttribute( const OUString
&aName
, const OUString
&aType
, const OUString
&aValue
)
102 struct AttributeListImpl_impl
104 AttributeListImpl_impl()
106 // performance improvement during adding
107 vecAttribute
.reserve(20);
109 ::std::vector
<struct TagAttribute
> vecAttribute
;
114 sal_Int16 SAL_CALL
AttributeListImpl::getLength(void) throw (RuntimeException
)
116 return sal::static_int_cast
< sal_Int16
>(m_pImpl
->vecAttribute
.size());
120 AttributeListImpl::AttributeListImpl( const AttributeListImpl
&r
) :
121 cppu::WeakImplHelper1
<com::sun::star::xml::sax::XAttributeList
>(r
)
123 m_pImpl
= new AttributeListImpl_impl
;
124 *m_pImpl
= *(r
.m_pImpl
);
127 OUString
AttributeListImpl::getNameByIndex(sal_Int16 i
) throw (RuntimeException
)
129 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
130 return m_pImpl
->vecAttribute
[i
].sName
;
136 OUString
AttributeListImpl::getTypeByIndex(sal_Int16 i
) throw (RuntimeException
)
138 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
139 return m_pImpl
->vecAttribute
[i
].sType
;
144 OUString
AttributeListImpl::getValueByIndex(sal_Int16 i
) throw (RuntimeException
)
146 if( i
< sal::static_int_cast
<sal_Int16
>(m_pImpl
->vecAttribute
.size()) ) {
147 return m_pImpl
->vecAttribute
[i
].sValue
;
153 OUString
AttributeListImpl::getTypeByName( const OUString
& sName
) throw (RuntimeException
)
155 ::std::vector
<struct TagAttribute
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
157 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ii
++ ) {
158 if( (*ii
).sName
== sName
) {
165 OUString
AttributeListImpl::getValueByName(const OUString
& sName
) throw (RuntimeException
)
167 ::std::vector
<struct TagAttribute
>::iterator ii
= m_pImpl
->vecAttribute
.begin();
169 for( ; ii
!= m_pImpl
->vecAttribute
.end() ; ii
++ ) {
170 if( (*ii
).sName
== sName
) {
178 AttributeListImpl::AttributeListImpl()
180 m_pImpl
= new AttributeListImpl_impl
;
185 AttributeListImpl::~AttributeListImpl()
191 void AttributeListImpl::addAttribute( const OUString
&sName
,
192 const OUString
&sType
,
193 const OUString
&sValue
)
195 m_pImpl
->vecAttribute
.push_back( TagAttribute( sName
, sType
, sValue
) );
198 void AttributeListImpl::clear()
200 ::std::vector
<struct TagAttribute
> dummy
;
201 m_pImpl
->vecAttribute
.swap( dummy
);
203 assert( ! getLength() );
206 // ------------------------------------------------------------------
208 Any SAL_CALL
OReadAccelatorDocumentHandler::queryInterface( const Type
& rType
) throw( RuntimeException
)
210 Any a
= ::cppu::queryInterface( rType
,SAL_STATIC_CAST( XDocumentHandler
*, this ));
214 return OWeakObject::queryInterface( rType
);
217 void SAL_CALL
OReadAccelatorDocumentHandler::ignorableWhitespace(
219 throw( SAXException
, RuntimeException
)
223 void SAL_CALL
OReadAccelatorDocumentHandler::processingInstruction(
224 const OUString
&, const OUString
& )
225 throw( SAXException
, RuntimeException
)
229 void SAL_CALL
OReadAccelatorDocumentHandler::setDocumentLocator(
230 const Reference
< XLocator
> &xLocator
)
231 throw( SAXException
, RuntimeException
)
233 m_xLocator
= xLocator
;
236 ::rtl::OUString
OReadAccelatorDocumentHandler::getErrorLineString()
240 if ( m_xLocator
.is() )
242 return OUString::createFromAscii( buffer
);
248 void SAL_CALL
OReadAccelatorDocumentHandler::startDocument(void)
249 throw ( SAXException
, RuntimeException
)
253 void SAL_CALL
OReadAccelatorDocumentHandler::endDocument(void)
254 throw( SAXException
, RuntimeException
)
256 if ( m_nElementDepth
> 0 )
258 OUString aErrorMessage
= getErrorLineString();
259 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "A closing element is missing!" ));
260 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
265 void SAL_CALL
OReadAccelatorDocumentHandler::startElement(
266 const OUString
& aElementName
, const Reference
< XAttributeList
> &xAttrList
)
267 throw( SAXException
, RuntimeException
)
271 if ( aElementName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ELEMENT_ACCELERATORLIST
)))
274 if ( m_bAcceleratorMode
)
276 OUString aErrorMessage
= getErrorLineString();
277 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
278 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
281 m_bAcceleratorMode
= sal_True
;
283 else if ( aElementName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ELEMENT_ACCELERATORITEM
)))
286 if ( !m_bAcceleratorMode
)
288 OUString aErrorMessage
= getErrorLineString();
289 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list element has to be used before!" ));
290 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
294 // read accelerator item
295 m_bItemCloseExpected
= sal_True
;
297 SvtAcceleratorConfigItem aItem
;
299 // read attributes for accelerator
300 for ( sal_Int16 i
=0; i
< xAttrList
->getLength(); i
++ )
302 OUString aName
= xAttrList
->getNameByIndex( i
);
303 OUString aValue
= xAttrList
->getValueByIndex( i
);
305 if ( aName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ATTRIBUTE_URL
)))
306 aItem
.aCommand
= aValue
;
307 else if ( aName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ATTRIBUTE_MODIFIER
)))
308 aItem
.nModifier
= (sal_uInt16
)aValue
.toInt32();
309 else if ( aName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ATTRIBUTE_KEYCODE
)))
310 aItem
.nCode
= (sal_uInt16
)aValue
.toInt32();
313 m_aReadAcceleratorList
.push_back( aItem
);
318 OUString aErrorMessage
= getErrorLineString();
319 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown element found!" ));
320 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
325 void SAL_CALL
OReadAccelatorDocumentHandler::characters(const rtl::OUString
&)
326 throw( SAXException
, RuntimeException
)
331 void SAL_CALL
OReadAccelatorDocumentHandler::endElement( const OUString
& aName
)
332 throw( SAXException
, RuntimeException
)
336 if ( aName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ELEMENT_ACCELERATORLIST
)))
339 if ( !m_bAcceleratorMode
)
341 OUString aErrorMessage
= getErrorLineString();
342 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Accelerator list used twice!" ));
343 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
346 else if ( aName
.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( ELEMENT_ACCELERATORITEM
)))
348 if ( !m_bItemCloseExpected
)
350 OUString aErrorMessage
= getErrorLineString();
351 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Closing accelerator item element expected!" ));
352 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
357 OUString aErrorMessage
= getErrorLineString();
358 aErrorMessage
+= OUString( RTL_CONSTASCII_USTRINGPARAM( "Unknown closing element found!" ));
359 throw SAXException( aErrorMessage
, Reference
< XInterface
>(), Any() );
363 // ------------------------------------------------------------------
365 OWriteAccelatorDocumentHandler::OWriteAccelatorDocumentHandler(
366 const SvtAcceleratorItemList
& aWriteAcceleratorList
, Reference
< XDocumentHandler
> xDocumentHandler
) :
367 m_xWriteDocumentHandler( xDocumentHandler
),
368 m_aWriteAcceleratorList( aWriteAcceleratorList
)
370 m_aAttributeType
= OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_TYPE_CDATA
));
373 OWriteAccelatorDocumentHandler::~OWriteAccelatorDocumentHandler()
377 void OWriteAccelatorDocumentHandler::WriteAcceleratorDocument()
378 throw ( SAXException
, RuntimeException
)
380 AttributeListImpl
* pList
= new AttributeListImpl
;
381 Reference
< XAttributeList
> rList( (XAttributeList
*)pList
, UNO_QUERY
);
383 m_xWriteDocumentHandler
->startDocument();
384 m_xWriteDocumentHandler
->startElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST
)), rList
);
385 m_xWriteDocumentHandler
->ignorableWhitespace( OUString() );
387 std::list
< SvtAcceleratorConfigItem
>::const_iterator p
;
388 for ( p
= m_aWriteAcceleratorList
.begin(); p
!= m_aWriteAcceleratorList
.end(); p
++ )
389 WriteAcceleratorItem( *p
);
391 m_xWriteDocumentHandler
->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORLIST
)) );
392 m_xWriteDocumentHandler
->endDocument();
395 void OWriteAccelatorDocumentHandler::WriteAcceleratorItem(
396 const SvtAcceleratorConfigItem
& aAcceleratorItem
)
397 throw( SAXException
, RuntimeException
)
399 AttributeListImpl
* pAcceleratorAttributes
= new AttributeListImpl
;
400 Reference
< XAttributeList
> xAcceleratorAttrList( (XAttributeList
*)pAcceleratorAttributes
, UNO_QUERY
);
403 pAcceleratorAttributes
->addAttribute(
404 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_KEYCODE
)),
406 OUString::valueOf( aAcceleratorItem
.nCode
));
408 pAcceleratorAttributes
->addAttribute(
409 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_MODIFIER
)),
411 OUString::valueOf( aAcceleratorItem
.nModifier
));
413 pAcceleratorAttributes
->addAttribute(
414 OUString( RTL_CONSTASCII_USTRINGPARAM( ATTRIBUTE_URL
)),
416 aAcceleratorItem
.aCommand
);
418 // write start element
419 m_xWriteDocumentHandler
->startElement(
420 OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM
)),
421 xAcceleratorAttrList
);
422 m_xWriteDocumentHandler
->ignorableWhitespace( OUString() );
423 m_xWriteDocumentHandler
->endElement( OUString( RTL_CONSTASCII_USTRINGPARAM( ELEMENT_ACCELERATORITEM
)) );