16d739b60SAndrew Rist /**************************************************************
2*e27da661Smseidel  *
36d739b60SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
46d739b60SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
56d739b60SAndrew Rist  * distributed with this work for additional information
66d739b60SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
76d739b60SAndrew Rist  * to you under the Apache License, Version 2.0 (the
86d739b60SAndrew Rist  * "License"); you may not use this file except in compliance
96d739b60SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e27da661Smseidel  *
116d739b60SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e27da661Smseidel  *
136d739b60SAndrew Rist  * Unless required by applicable law or agreed to in writing,
146d739b60SAndrew Rist  * software distributed under the License is distributed on an
156d739b60SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
166d739b60SAndrew Rist  * KIND, either express or implied.  See the License for the
176d739b60SAndrew Rist  * specific language governing permissions and limitations
186d739b60SAndrew Rist  * under the License.
19*e27da661Smseidel  *
206d739b60SAndrew Rist  *************************************************************/
216d739b60SAndrew Rist 
226d739b60SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_framework.hxx"
26cdf0e10cSrcweir #include <xml/acceleratorconfigurationreader.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir //_______________________________________________
29cdf0e10cSrcweir // own includes
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #ifndef __FRAMEWORK_ACCELERATORCONST_H_
32cdf0e10cSrcweir #include <acceleratorconst.h>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //_______________________________________________
36cdf0e10cSrcweir // interface includes
37cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
38cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp>
39cdf0e10cSrcweir #include <com/sun/star/awt/KeyEvent.hpp>
40cdf0e10cSrcweir #include <com/sun/star/awt/Key.hpp>
41cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //_______________________________________________
44cdf0e10cSrcweir // other includes
45cdf0e10cSrcweir #include <vcl/svapp.hxx>
46cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
47cdf0e10cSrcweir 
48cdf0e10cSrcweir //_______________________________________________
49cdf0e10cSrcweir // namespace
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace framework{
52cdf0e10cSrcweir 
53cdf0e10cSrcweir //-----------------------------------------------
5430acf5e8Spfg /* Throws a SaxException in case a wrong formatted XML
55cdf0e10cSrcweir    structure was detected.
56*e27da661Smseidel 
57cdf0e10cSrcweir    This macro combined the given comment with a generic
5807a3d7f1SPedro Giffuni    way to find out the XML line (where the error occurred)
59cdf0e10cSrcweir    to format a suitable message.
60*e27da661Smseidel 
61cdf0e10cSrcweir    @param   COMMENT
62*e27da661Smseidel             an ascii string, which describes the problem.
63cdf0e10cSrcweir  */
64cdf0e10cSrcweir #define THROW_PARSEEXCEPTION(COMMENT)                                   \
65cdf0e10cSrcweir     {                                                                   \
66cdf0e10cSrcweir         ::rtl::OUStringBuffer sMessage(256);                            \
67cdf0e10cSrcweir         sMessage.append     (implts_getErrorLineString());              \
68cdf0e10cSrcweir         sMessage.appendAscii(COMMENT                    );              \
69cdf0e10cSrcweir                                                                         \
70cdf0e10cSrcweir 		throw css::xml::sax::SAXException(                              \
71cdf0e10cSrcweir                 sMessage.makeStringAndClear(),                          \
72cdf0e10cSrcweir                 static_cast< css::xml::sax::XDocumentHandler* >(this),  \
73cdf0e10cSrcweir                 css::uno::Any());                                       \
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76*e27da661Smseidel //-----------------------------------------------
77cdf0e10cSrcweir // XInterface
DEFINE_XINTERFACE_1(AcceleratorConfigurationReader,OWeakObject,DIRECT_INTERFACE (css::xml::sax::XDocumentHandler))78cdf0e10cSrcweir DEFINE_XINTERFACE_1(AcceleratorConfigurationReader                   ,
79cdf0e10cSrcweir                     OWeakObject                                      ,
80cdf0e10cSrcweir                     DIRECT_INTERFACE(css::xml::sax::XDocumentHandler))
81*e27da661Smseidel 
82cdf0e10cSrcweir //-----------------------------------------------
83cdf0e10cSrcweir AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer)
84cdf0e10cSrcweir     : ThreadHelpBase          (&Application::GetSolarMutex())
85cdf0e10cSrcweir     , OWeakObject             (                             )
86cdf0e10cSrcweir     , m_rContainer            (rContainer                   )
87cdf0e10cSrcweir     , m_bInsideAcceleratorList(sal_False                    )
88cdf0e10cSrcweir     , m_bInsideAcceleratorItem(sal_False                    )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir }
91*e27da661Smseidel 
92cdf0e10cSrcweir //-----------------------------------------------
~AcceleratorConfigurationReader()93cdf0e10cSrcweir AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
94cdf0e10cSrcweir {
95cdf0e10cSrcweir }
96*e27da661Smseidel 
97cdf0e10cSrcweir //-----------------------------------------------
startDocument()98cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startDocument()
99cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
100cdf0e10cSrcweir           css::uno::RuntimeException )
101cdf0e10cSrcweir {
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir //-----------------------------------------------
endDocument()105cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endDocument()
106cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
107cdf0e10cSrcweir           css::uno::RuntimeException )
108cdf0e10cSrcweir {
109cdf0e10cSrcweir     // The xml file seems to be corrupted.
110*e27da661Smseidel     // Because we found no end-tags... at least for
111cdf0e10cSrcweir     // one list or item.
112cdf0e10cSrcweir     if (
113cdf0e10cSrcweir         (m_bInsideAcceleratorList) ||
114cdf0e10cSrcweir         (m_bInsideAcceleratorItem)
115cdf0e10cSrcweir        )
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
118cdf0e10cSrcweir     }
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir //-----------------------------------------------
startElement(const::rtl::OUString & sElement,const css::uno::Reference<css::xml::sax::XAttributeList> & xAttributeList)122cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startElement(const ::rtl::OUString&                                      sElement      ,
123cdf0e10cSrcweir                                                            const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
124cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
125cdf0e10cSrcweir           css::uno::RuntimeException )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
128*e27da661Smseidel 
129cdf0e10cSrcweir     // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
130*e27da661Smseidel     // Because an item occurs very often... a list should occur one time only!
131cdf0e10cSrcweir     if (eElement == E_ELEMENT_ITEM)
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         if (!m_bInsideAcceleratorList)
13407a3d7f1SPedro Giffuni             THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
135cdf0e10cSrcweir         if (m_bInsideAcceleratorItem)
136cdf0e10cSrcweir             THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
137cdf0e10cSrcweir         m_bInsideAcceleratorItem = sal_True;
138*e27da661Smseidel 
139*e27da661Smseidel         ::rtl::OUString    sCommand;
140*e27da661Smseidel         css::awt::KeyEvent aEvent ;
141*e27da661Smseidel 
142cdf0e10cSrcweir         sal_Int16 c = xAttributeList->getLength();
143cdf0e10cSrcweir         sal_Int16 i = 0;
144cdf0e10cSrcweir         for (i=0; i<c; ++i)
145cdf0e10cSrcweir         {
146cdf0e10cSrcweir             ::rtl::OUString sAttribute = xAttributeList->getNameByIndex(i);
147cdf0e10cSrcweir             ::rtl::OUString sValue     = xAttributeList->getValueByIndex(i);
148cdf0e10cSrcweir             EXMLAttribute   eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute);
149cdf0e10cSrcweir             switch(eAttribute)
150cdf0e10cSrcweir             {
151cdf0e10cSrcweir                 case E_ATTRIBUTE_URL :
152cdf0e10cSrcweir                     sCommand = sValue.intern();
153cdf0e10cSrcweir                     break;
154*e27da661Smseidel 
155cdf0e10cSrcweir                 case E_ATTRIBUTE_KEYCODE :
156cdf0e10cSrcweir                     aEvent.KeyCode = m_rKeyMapping->mapIdentifierToCode(sValue);
157cdf0e10cSrcweir                     break;
158*e27da661Smseidel 
159cdf0e10cSrcweir                 case E_ATTRIBUTE_MOD_SHIFT :
160cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
161cdf0e10cSrcweir                     break;
162*e27da661Smseidel 
163*e27da661Smseidel                 case E_ATTRIBUTE_MOD_MOD1 :
164cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
165cdf0e10cSrcweir                     break;
166*e27da661Smseidel 
167*e27da661Smseidel                 case E_ATTRIBUTE_MOD_MOD2 :
168cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
169cdf0e10cSrcweir                     break;
170cdf0e10cSrcweir 
171*e27da661Smseidel                 case E_ATTRIBUTE_MOD_MOD3 :
172cdf0e10cSrcweir                     aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
173cdf0e10cSrcweir             }
174cdf0e10cSrcweir         }
175*e27da661Smseidel 
176cdf0e10cSrcweir         // validate command and key event.
177cdf0e10cSrcweir         if (
178cdf0e10cSrcweir             (!sCommand.getLength()) ||
179cdf0e10cSrcweir             (aEvent.KeyCode == 0  )
180cdf0e10cSrcweir            )
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
183cdf0e10cSrcweir         }
184*e27da661Smseidel 
185*e27da661Smseidel         // register key event + command inside cache...
186cdf0e10cSrcweir         // Check for already existing items there.
187cdf0e10cSrcweir         if (!m_rContainer.hasKey(aEvent))
188cdf0e10cSrcweir             m_rContainer.setKeyCommandPair(aEvent, sCommand);
189cdf0e10cSrcweir         #ifdef ENABLE_WARNINGS
190cdf0e10cSrcweir         else
191cdf0e10cSrcweir         {
192*e27da661Smseidel             // Attention: It's not really a reason to throw an exception and kill the office, if the configuration contains
193*e27da661Smseidel             // multiple registrations for the same key :-) Show a warning... and ignore the second item.
194*e27da661Smseidel             // THROW_PARSEEXCEPTION("Command is registered for the same key more than once.")
195cdf0e10cSrcweir             ::rtl::OUStringBuffer sMsg(256);
196cdf0e10cSrcweir             sMsg.appendAscii("Double registration detected.\nCommand = \"");
197cdf0e10cSrcweir             sMsg.append     (sCommand                                     );
198cdf0e10cSrcweir             sMsg.appendAscii("\"\nKeyCode = "                             );
199cdf0e10cSrcweir             sMsg.append     ((sal_Int32)aEvent.KeyCode                    );
200cdf0e10cSrcweir             sMsg.appendAscii("\nModifiers = "                             );
201cdf0e10cSrcweir             sMsg.append     ((sal_Int32)aEvent.Modifiers                  );
202cdf0e10cSrcweir             sMsg.appendAscii("\nIgnore this item!"                        );
203cdf0e10cSrcweir             LOG_WARNING("AcceleratorConfigurationReader::startElement()", U2B(sMsg.makeStringAndClear()))
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir         #endif // ENABLE_WARNINGS
206*e27da661Smseidel     }
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     if (eElement == E_ELEMENT_ACCELERATORLIST)
209cdf0e10cSrcweir     {
210cdf0e10cSrcweir         if (m_bInsideAcceleratorList)
211cdf0e10cSrcweir             THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
212cdf0e10cSrcweir         m_bInsideAcceleratorList = sal_True;
213cdf0e10cSrcweir         return;
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir //-----------------------------------------------
endElement(const::rtl::OUString & sElement)218cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endElement(const ::rtl::OUString& sElement)
219cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
220cdf0e10cSrcweir           css::uno::RuntimeException )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir     EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
223*e27da661Smseidel 
224cdf0e10cSrcweir     // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
225*e27da661Smseidel     // Because an item occurs very often... a list should occur one time only!
226cdf0e10cSrcweir     if (eElement == E_ELEMENT_ITEM)
227cdf0e10cSrcweir     {
228cdf0e10cSrcweir         if (!m_bInsideAcceleratorItem)
229cdf0e10cSrcweir             THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
230cdf0e10cSrcweir         m_bInsideAcceleratorItem = sal_False;
231cdf0e10cSrcweir     }
232*e27da661Smseidel 
233cdf0e10cSrcweir     if (eElement == E_ELEMENT_ACCELERATORLIST)
234cdf0e10cSrcweir     {
235cdf0e10cSrcweir         if (!m_bInsideAcceleratorList)
236cdf0e10cSrcweir             THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
237cdf0e10cSrcweir         m_bInsideAcceleratorList = sal_False;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir //-----------------------------------------------
characters(const::rtl::OUString &)242cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::characters(const ::rtl::OUString&)
243cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
244cdf0e10cSrcweir           css::uno::RuntimeException )
245cdf0e10cSrcweir {
246cdf0e10cSrcweir }
247cdf0e10cSrcweir 
248cdf0e10cSrcweir //-----------------------------------------------
ignorableWhitespace(const::rtl::OUString &)249cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const ::rtl::OUString&)
250cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
251cdf0e10cSrcweir           css::uno::RuntimeException )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir }
254cdf0e10cSrcweir 
255cdf0e10cSrcweir //-----------------------------------------------
processingInstruction(const::rtl::OUString &,const::rtl::OUString &)256cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const ::rtl::OUString& /*sTarget*/,
257*e27da661Smseidel                                                                     const ::rtl::OUString& /*sData*/ )
258cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
259cdf0e10cSrcweir           css::uno::RuntimeException )
260cdf0e10cSrcweir {
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir //-----------------------------------------------
setDocumentLocator(const css::uno::Reference<css::xml::sax::XLocator> & xLocator)264cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
265cdf0e10cSrcweir     throw(css::xml::sax::SAXException,
266cdf0e10cSrcweir           css::uno::RuntimeException )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir     m_xLocator = xLocator;
269cdf0e10cSrcweir }
270cdf0e10cSrcweir 
271cdf0e10cSrcweir //-----------------------------------------------
implst_classifyElement(const::rtl::OUString & sElement)272cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(const ::rtl::OUString& sElement)
273cdf0e10cSrcweir {
274cdf0e10cSrcweir     AcceleratorConfigurationReader::EXMLElement eElement;
275*e27da661Smseidel 
276cdf0e10cSrcweir     if (sElement.equals(NS_ELEMENT_ACCELERATORLIST))
277cdf0e10cSrcweir         eElement = E_ELEMENT_ACCELERATORLIST;
278cdf0e10cSrcweir     else
279cdf0e10cSrcweir     if (sElement.equals(NS_ELEMENT_ITEM))
280cdf0e10cSrcweir         eElement = E_ELEMENT_ITEM;
281cdf0e10cSrcweir     else
282cdf0e10cSrcweir         throw css::uno::RuntimeException(
283cdf0e10cSrcweir                 DECLARE_ASCII("Unknown XML element detected!"),
284cdf0e10cSrcweir                 css::uno::Reference< css::xml::sax::XDocumentHandler >());
285*e27da661Smseidel 
286*e27da661Smseidel     return eElement;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir 
289cdf0e10cSrcweir //-----------------------------------------------
implst_classifyAttribute(const::rtl::OUString & sAttribute)290cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(const ::rtl::OUString& sAttribute)
291cdf0e10cSrcweir {
292cdf0e10cSrcweir     AcceleratorConfigurationReader::EXMLAttribute eAttribute;
293*e27da661Smseidel 
294cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_KEYCODE))
295cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_KEYCODE;
296cdf0e10cSrcweir     else
297cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_SHIFT))
298cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_SHIFT;
299cdf0e10cSrcweir     else
300cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD1))
301cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD1;
302cdf0e10cSrcweir     else
303cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD2))
304cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD2;
305cdf0e10cSrcweir     else
306cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD3))
307cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_MOD_MOD3;
308cdf0e10cSrcweir     else
309cdf0e10cSrcweir     if (sAttribute.equals(NS_ATTRIBUTE_URL))
310cdf0e10cSrcweir         eAttribute = E_ATTRIBUTE_URL;
311cdf0e10cSrcweir     else
312cdf0e10cSrcweir         throw css::uno::RuntimeException(
313cdf0e10cSrcweir                 DECLARE_ASCII("Unknown XML attribute detected!"),
314cdf0e10cSrcweir                 css::uno::Reference< css::xml::sax::XDocumentHandler >());
315*e27da661Smseidel 
316*e27da661Smseidel     return eAttribute;
317cdf0e10cSrcweir }
318cdf0e10cSrcweir 
319cdf0e10cSrcweir //-----------------------------------------------
implts_getErrorLineString()320cdf0e10cSrcweir ::rtl::OUString AcceleratorConfigurationReader::implts_getErrorLineString()
321cdf0e10cSrcweir {
322cdf0e10cSrcweir     if (!m_xLocator.is())
323*e27da661Smseidel         return DECLARE_ASCII("Error during parsing XML. (No further info available...)");
324*e27da661Smseidel 
325cdf0e10cSrcweir     ::rtl::OUStringBuffer sMsg(256);
326cdf0e10cSrcweir     sMsg.appendAscii("Error during parsing XML in\nline = ");
327cdf0e10cSrcweir     sMsg.append     (m_xLocator->getLineNumber()           );
328cdf0e10cSrcweir     sMsg.appendAscii("\ncolumn = "                         );
329cdf0e10cSrcweir     sMsg.append     (m_xLocator->getColumnNumber()         );
330cdf0e10cSrcweir     sMsg.appendAscii("."                                   );
331cdf0e10cSrcweir     return sMsg.makeStringAndClear();
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir } // namespace framework
335*e27da661Smseidel 
336