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