xref: /AOO41X/main/framework/source/xml/acceleratorconfigurationreader.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir #include <xml/acceleratorconfigurationreader.hxx>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir //_______________________________________________
33*cdf0e10cSrcweir // own includes
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #ifndef __FRAMEWORK_ACCELERATORCONST_H_
36*cdf0e10cSrcweir #include <acceleratorconst.h>
37*cdf0e10cSrcweir #endif
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //_______________________________________________
40*cdf0e10cSrcweir // interface includes
41*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/awt/KeyEvent.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/awt/Key.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir //_______________________________________________
48*cdf0e10cSrcweir // other includes
49*cdf0e10cSrcweir #include <vcl/svapp.hxx>
50*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //_______________________________________________
53*cdf0e10cSrcweir // namespace
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace framework{
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir //-----------------------------------------------
58*cdf0e10cSrcweir /* Throws a SaxException in case a wrong formated XML
59*cdf0e10cSrcweir    structure was detected.
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir    This macro combined the given comment with a generic
62*cdf0e10cSrcweir    way to find out the XML line (where the error occured)
63*cdf0e10cSrcweir    to format a suitable message.
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir    @param   COMMENT
66*cdf0e10cSrcweir             an ascii string, which describe the problem.
67*cdf0e10cSrcweir  */
68*cdf0e10cSrcweir #define THROW_PARSEEXCEPTION(COMMENT)                                   \
69*cdf0e10cSrcweir     {                                                                   \
70*cdf0e10cSrcweir         ::rtl::OUStringBuffer sMessage(256);                            \
71*cdf0e10cSrcweir         sMessage.append     (implts_getErrorLineString());              \
72*cdf0e10cSrcweir         sMessage.appendAscii(COMMENT                    );              \
73*cdf0e10cSrcweir                                                                         \
74*cdf0e10cSrcweir 		throw css::xml::sax::SAXException(                              \
75*cdf0e10cSrcweir                 sMessage.makeStringAndClear(),                          \
76*cdf0e10cSrcweir                 static_cast< css::xml::sax::XDocumentHandler* >(this),  \
77*cdf0e10cSrcweir                 css::uno::Any());                                       \
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir //-----------------------------------------------
81*cdf0e10cSrcweir // XInterface
82*cdf0e10cSrcweir DEFINE_XINTERFACE_1(AcceleratorConfigurationReader                   ,
83*cdf0e10cSrcweir                     OWeakObject                                      ,
84*cdf0e10cSrcweir                     DIRECT_INTERFACE(css::xml::sax::XDocumentHandler))
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir //-----------------------------------------------
87*cdf0e10cSrcweir AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer)
88*cdf0e10cSrcweir     : ThreadHelpBase          (&Application::GetSolarMutex())
89*cdf0e10cSrcweir     , OWeakObject             (                             )
90*cdf0e10cSrcweir     , m_rContainer            (rContainer                   )
91*cdf0e10cSrcweir     , m_bInsideAcceleratorList(sal_False                    )
92*cdf0e10cSrcweir     , m_bInsideAcceleratorItem(sal_False                    )
93*cdf0e10cSrcweir {
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir //-----------------------------------------------
97*cdf0e10cSrcweir AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir //-----------------------------------------------
102*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startDocument()
103*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
104*cdf0e10cSrcweir           css::uno::RuntimeException )
105*cdf0e10cSrcweir {
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir //-----------------------------------------------
109*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endDocument()
110*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
111*cdf0e10cSrcweir           css::uno::RuntimeException )
112*cdf0e10cSrcweir {
113*cdf0e10cSrcweir     // The xml file seems to be corrupted.
114*cdf0e10cSrcweir     // Because we found no end-tags ... at least for
115*cdf0e10cSrcweir     // one list or item.
116*cdf0e10cSrcweir     if (
117*cdf0e10cSrcweir         (m_bInsideAcceleratorList) ||
118*cdf0e10cSrcweir         (m_bInsideAcceleratorItem)
119*cdf0e10cSrcweir        )
120*cdf0e10cSrcweir     {
121*cdf0e10cSrcweir         THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
122*cdf0e10cSrcweir     }
123*cdf0e10cSrcweir }
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir //-----------------------------------------------
126*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startElement(const ::rtl::OUString&                                      sElement      ,
127*cdf0e10cSrcweir                                                            const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
128*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
129*cdf0e10cSrcweir           css::uno::RuntimeException )
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir     EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
134*cdf0e10cSrcweir     // Because an item occures very often ... a list should occure one times only!
135*cdf0e10cSrcweir     if (eElement == E_ELEMENT_ITEM)
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         if (!m_bInsideAcceleratorList)
138*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("An element \"accel:item\" must be embeded into 'accel:acceleratorlist'.")
139*cdf0e10cSrcweir         if (m_bInsideAcceleratorItem)
140*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
141*cdf0e10cSrcweir         m_bInsideAcceleratorItem = sal_True;
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir         ::rtl::OUString    sCommand;
144*cdf0e10cSrcweir         css::awt::KeyEvent aEvent  ;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir         sal_Int16 c = xAttributeList->getLength();
147*cdf0e10cSrcweir         sal_Int16 i = 0;
148*cdf0e10cSrcweir         for (i=0; i<c; ++i)
149*cdf0e10cSrcweir         {
150*cdf0e10cSrcweir             ::rtl::OUString sAttribute = xAttributeList->getNameByIndex(i);
151*cdf0e10cSrcweir             ::rtl::OUString sValue     = xAttributeList->getValueByIndex(i);
152*cdf0e10cSrcweir             EXMLAttribute   eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute);
153*cdf0e10cSrcweir             switch(eAttribute)
154*cdf0e10cSrcweir             {
155*cdf0e10cSrcweir                 case E_ATTRIBUTE_URL :
156*cdf0e10cSrcweir                     sCommand = sValue.intern();
157*cdf0e10cSrcweir                     break;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir                 case E_ATTRIBUTE_KEYCODE :
160*cdf0e10cSrcweir                     aEvent.KeyCode = m_rKeyMapping->mapIdentifierToCode(sValue);
161*cdf0e10cSrcweir                     break;
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir                 case E_ATTRIBUTE_MOD_SHIFT :
164*cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
165*cdf0e10cSrcweir                     break;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir                 case E_ATTRIBUTE_MOD_MOD1  :
168*cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
169*cdf0e10cSrcweir                     break;
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir                 case E_ATTRIBUTE_MOD_MOD2  :
172*cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
173*cdf0e10cSrcweir                     break;
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir                 case E_ATTRIBUTE_MOD_MOD3  :
176*cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
177*cdf0e10cSrcweir             }
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir         // validate command and key event.
181*cdf0e10cSrcweir         if (
182*cdf0e10cSrcweir             (!sCommand.getLength()) ||
183*cdf0e10cSrcweir             (aEvent.KeyCode == 0  )
184*cdf0e10cSrcweir            )
185*cdf0e10cSrcweir         {
186*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         // register key event + command inside cache ...
190*cdf0e10cSrcweir         // Check for already existing items there.
191*cdf0e10cSrcweir         if (!m_rContainer.hasKey(aEvent))
192*cdf0e10cSrcweir             m_rContainer.setKeyCommandPair(aEvent, sCommand);
193*cdf0e10cSrcweir         #ifdef ENABLE_WARNINGS
194*cdf0e10cSrcweir         else
195*cdf0e10cSrcweir         {
196*cdf0e10cSrcweir             // Attention: Its not realy a reason to throw an exception and kill the office, if the configuration contains
197*cdf0e10cSrcweir             // multiple registrations for the same key :-) Show a warning ... and ignore the second item.
198*cdf0e10cSrcweir             // THROW_PARSEEXCEPTION("Command is registered for the same key more then once.")
199*cdf0e10cSrcweir             ::rtl::OUStringBuffer sMsg(256);
200*cdf0e10cSrcweir             sMsg.appendAscii("Double registration detected.\nCommand = \"");
201*cdf0e10cSrcweir             sMsg.append     (sCommand                                     );
202*cdf0e10cSrcweir             sMsg.appendAscii("\"\nKeyCode = "                             );
203*cdf0e10cSrcweir             sMsg.append     ((sal_Int32)aEvent.KeyCode                    );
204*cdf0e10cSrcweir             sMsg.appendAscii("\nModifiers = "                             );
205*cdf0e10cSrcweir             sMsg.append     ((sal_Int32)aEvent.Modifiers                  );
206*cdf0e10cSrcweir             sMsg.appendAscii("\nIgnore this item!"                        );
207*cdf0e10cSrcweir             LOG_WARNING("AcceleratorConfigurationReader::startElement()", U2B(sMsg.makeStringAndClear()))
208*cdf0e10cSrcweir         }
209*cdf0e10cSrcweir         #endif // ENABLE_WARNINGS
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     if (eElement == E_ELEMENT_ACCELERATORLIST)
213*cdf0e10cSrcweir     {
214*cdf0e10cSrcweir         if (m_bInsideAcceleratorList)
215*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
216*cdf0e10cSrcweir         m_bInsideAcceleratorList = sal_True;
217*cdf0e10cSrcweir         return;
218*cdf0e10cSrcweir     }
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir //-----------------------------------------------
222*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endElement(const ::rtl::OUString& sElement)
223*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
224*cdf0e10cSrcweir           css::uno::RuntimeException )
225*cdf0e10cSrcweir {
226*cdf0e10cSrcweir     EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir     // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
229*cdf0e10cSrcweir     // Because an item occures very often ... a list should occure one times only!
230*cdf0e10cSrcweir     if (eElement == E_ELEMENT_ITEM)
231*cdf0e10cSrcweir     {
232*cdf0e10cSrcweir         if (!m_bInsideAcceleratorItem)
233*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
234*cdf0e10cSrcweir         m_bInsideAcceleratorItem = sal_False;
235*cdf0e10cSrcweir     }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir     if (eElement == E_ELEMENT_ACCELERATORLIST)
238*cdf0e10cSrcweir     {
239*cdf0e10cSrcweir         if (!m_bInsideAcceleratorList)
240*cdf0e10cSrcweir             THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
241*cdf0e10cSrcweir         m_bInsideAcceleratorList = sal_False;
242*cdf0e10cSrcweir     }
243*cdf0e10cSrcweir }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir //-----------------------------------------------
246*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::characters(const ::rtl::OUString&)
247*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
248*cdf0e10cSrcweir           css::uno::RuntimeException )
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir //-----------------------------------------------
253*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const ::rtl::OUString&)
254*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
255*cdf0e10cSrcweir           css::uno::RuntimeException )
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir }
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir //-----------------------------------------------
260*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const ::rtl::OUString& /*sTarget*/,
261*cdf0e10cSrcweir                                                                     const ::rtl::OUString& /*sData*/  )
262*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
263*cdf0e10cSrcweir           css::uno::RuntimeException )
264*cdf0e10cSrcweir {
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir //-----------------------------------------------
268*cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
269*cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
270*cdf0e10cSrcweir           css::uno::RuntimeException )
271*cdf0e10cSrcweir {
272*cdf0e10cSrcweir     m_xLocator = xLocator;
273*cdf0e10cSrcweir }
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir //-----------------------------------------------
276*cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(const ::rtl::OUString& sElement)
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir     AcceleratorConfigurationReader::EXMLElement eElement;
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir     if (sElement.equals(NS_ELEMENT_ACCELERATORLIST))
281*cdf0e10cSrcweir         eElement = E_ELEMENT_ACCELERATORLIST;
282*cdf0e10cSrcweir     else
283*cdf0e10cSrcweir     if (sElement.equals(NS_ELEMENT_ITEM))
284*cdf0e10cSrcweir         eElement = E_ELEMENT_ITEM;
285*cdf0e10cSrcweir     else
286*cdf0e10cSrcweir         throw css::uno::RuntimeException(
287*cdf0e10cSrcweir                 DECLARE_ASCII("Unknown XML element detected!"),
288*cdf0e10cSrcweir                 css::uno::Reference< css::xml::sax::XDocumentHandler >());
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir     return eElement;
291*cdf0e10cSrcweir }
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir //-----------------------------------------------
294*cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(const ::rtl::OUString& sAttribute)
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir     AcceleratorConfigurationReader::EXMLAttribute eAttribute;
297*cdf0e10cSrcweir 
298*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_KEYCODE))
299*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_KEYCODE;
300*cdf0e10cSrcweir     else
301*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_SHIFT))
302*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_SHIFT;
303*cdf0e10cSrcweir     else
304*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD1))
305*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD1;
306*cdf0e10cSrcweir     else
307*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD2))
308*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD2;
309*cdf0e10cSrcweir     else
310*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD3))
311*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD3;
312*cdf0e10cSrcweir     else
313*cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_URL))
314*cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_URL;
315*cdf0e10cSrcweir     else
316*cdf0e10cSrcweir         throw css::uno::RuntimeException(
317*cdf0e10cSrcweir                 DECLARE_ASCII("Unknown XML attribute detected!"),
318*cdf0e10cSrcweir                 css::uno::Reference< css::xml::sax::XDocumentHandler >());
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     return eAttribute;
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir //-----------------------------------------------
324*cdf0e10cSrcweir ::rtl::OUString AcceleratorConfigurationReader::implts_getErrorLineString()
325*cdf0e10cSrcweir {
326*cdf0e10cSrcweir     if (!m_xLocator.is())
327*cdf0e10cSrcweir         return DECLARE_ASCII("Error during parsing XML. (No further info available ...)");
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir     ::rtl::OUStringBuffer sMsg(256);
330*cdf0e10cSrcweir     sMsg.appendAscii("Error during parsing XML in\nline = ");
331*cdf0e10cSrcweir     sMsg.append     (m_xLocator->getLineNumber()           );
332*cdf0e10cSrcweir     sMsg.appendAscii("\ncolumn = "                         );
333*cdf0e10cSrcweir     sMsg.append     (m_xLocator->getColumnNumber()         );
334*cdf0e10cSrcweir     sMsg.appendAscii("."                                   );
335*cdf0e10cSrcweir     return sMsg.makeStringAndClear();
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir } // namespace framework
339