Branch libreoffice-5-0-4
[LibreOffice.git] / framework / source / xml / acceleratorconfigurationreader.cxx
blob7e768f98da37e9da4e019f8876f218a80e10a9c2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <xml/acceleratorconfigurationreader.hxx>
22 #include <acceleratorconst.h>
24 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
25 #include <com/sun/star/awt/KeyModifier.hpp>
26 #include <com/sun/star/awt/KeyEvent.hpp>
27 #include <com/sun/star/awt/Key.hpp>
28 #include <com/sun/star/container/ElementExistException.hpp>
30 #include <vcl/svapp.hxx>
31 #include <rtl/ustrbuf.hxx>
33 namespace framework{
35 /* Throws a SaxException in case a wrong formatted XML
36 structure was detected.
38 This macro combined the given comment with a generic
39 way to find out the XML line (where the error occurred)
40 to format a suitable message.
42 @param COMMENT
43 an ascii string, which describe the problem.
45 #define THROW_PARSEEXCEPTION(COMMENT) \
46 { \
47 OUStringBuffer sMessage(256); \
48 sMessage.append (implts_getErrorLineString()); \
49 sMessage.appendAscii(COMMENT ); \
51 throw css::xml::sax::SAXException( \
52 sMessage.makeStringAndClear(), \
53 static_cast< css::xml::sax::XDocumentHandler* >(this), \
54 css::uno::Any()); \
57 AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer)
58 : m_rContainer (rContainer )
59 , m_bInsideAcceleratorList(false )
60 , m_bInsideAcceleratorItem(false )
64 AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
68 void SAL_CALL AcceleratorConfigurationReader::startDocument()
69 throw(css::xml::sax::SAXException,
70 css::uno::RuntimeException, std::exception )
74 void SAL_CALL AcceleratorConfigurationReader::endDocument()
75 throw(css::xml::sax::SAXException,
76 css::uno::RuntimeException, std::exception )
78 // The xml file seems to be corrupted.
79 // Because we found no end-tags ... at least for
80 // one list or item.
81 if (
82 (m_bInsideAcceleratorList) ||
83 (m_bInsideAcceleratorItem)
86 THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
90 void SAL_CALL AcceleratorConfigurationReader::startElement(const OUString& sElement ,
91 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
92 throw(css::xml::sax::SAXException,
93 css::uno::RuntimeException, std::exception )
95 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
97 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
98 // Because an item occurs very often ... a list should occur one times only!
99 if (eElement == E_ELEMENT_ITEM)
101 if (!m_bInsideAcceleratorList)
102 THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
103 if (m_bInsideAcceleratorItem)
104 THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
105 m_bInsideAcceleratorItem = true;
107 OUString sCommand;
108 css::awt::KeyEvent aEvent;
110 sal_Int16 c = xAttributeList->getLength();
111 sal_Int16 i = 0;
112 for (i=0; i<c; ++i)
114 OUString sAttribute = xAttributeList->getNameByIndex(i);
115 OUString sValue = xAttributeList->getValueByIndex(i);
116 EXMLAttribute eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute);
117 switch(eAttribute)
119 case E_ATTRIBUTE_URL :
120 sCommand = sValue.intern();
121 break;
123 case E_ATTRIBUTE_KEYCODE :
124 aEvent.KeyCode = m_rKeyMapping->mapIdentifierToCode(sValue);
125 break;
127 case E_ATTRIBUTE_MOD_SHIFT :
128 aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
129 break;
131 case E_ATTRIBUTE_MOD_MOD1 :
132 aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
133 break;
135 case E_ATTRIBUTE_MOD_MOD2 :
136 aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
137 break;
139 case E_ATTRIBUTE_MOD_MOD3 :
140 aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
144 // validate command and key event.
145 if (
146 sCommand.isEmpty() ||
147 (aEvent.KeyCode == 0 )
150 THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
153 // register key event + command inside cache ...
154 // Check for already existing items there.
155 if (!m_rContainer.hasKey(aEvent))
156 m_rContainer.setKeyCommandPair(aEvent, sCommand);
157 else
159 // Attention: Its not really a reason to throw an exception and kill the office, if the configuration contains
160 // multiple registrations for the same key :-) Show a warning ... and ignore the second item.
161 // THROW_PARSEEXCEPTION("Command is registered for the same key more than once.")
162 SAL_INFO("fwk",
163 "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" <<
164 sCommand <<
165 "\" KeyCode=" <<
166 aEvent.KeyCode <<
167 "Modifiers=" <<
168 aEvent.Modifiers);
172 if (eElement == E_ELEMENT_ACCELERATORLIST)
174 if (m_bInsideAcceleratorList)
175 THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
176 m_bInsideAcceleratorList = true;
177 return;
181 void SAL_CALL AcceleratorConfigurationReader::endElement(const OUString& sElement)
182 throw(css::xml::sax::SAXException,
183 css::uno::RuntimeException, std::exception )
185 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
187 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
188 // Because an item occurs very often ... a list should occur one times only!
189 if (eElement == E_ELEMENT_ITEM)
191 if (!m_bInsideAcceleratorItem)
192 THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
193 m_bInsideAcceleratorItem = false;
196 if (eElement == E_ELEMENT_ACCELERATORLIST)
198 if (!m_bInsideAcceleratorList)
199 THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
200 m_bInsideAcceleratorList = false;
204 void SAL_CALL AcceleratorConfigurationReader::characters(const OUString&)
205 throw(css::xml::sax::SAXException,
206 css::uno::RuntimeException, std::exception )
210 void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const OUString&)
211 throw(css::xml::sax::SAXException,
212 css::uno::RuntimeException, std::exception )
216 void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const OUString& /*sTarget*/,
217 const OUString& /*sData*/ )
218 throw(css::xml::sax::SAXException,
219 css::uno::RuntimeException, std::exception )
223 void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
224 throw(css::xml::sax::SAXException,
225 css::uno::RuntimeException, std::exception )
227 m_xLocator = xLocator;
230 AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(const OUString& sElement)
232 AcceleratorConfigurationReader::EXMLElement eElement;
234 if (sElement == "http://openoffice.org/2001/accel^acceleratorlist")
235 eElement = E_ELEMENT_ACCELERATORLIST;
236 else if (sElement == "http://openoffice.org/2001/accel^item")
237 eElement = E_ELEMENT_ITEM;
238 else
239 throw css::uno::RuntimeException(
240 "Unknown XML element detected!",
241 css::uno::Reference< css::xml::sax::XDocumentHandler >());
243 return eElement;
246 AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(const OUString& sAttribute)
248 AcceleratorConfigurationReader::EXMLAttribute eAttribute;
250 if (sAttribute == "http://openoffice.org/2001/accel^code")
251 eAttribute = E_ATTRIBUTE_KEYCODE;
252 else if (sAttribute == "http://openoffice.org/2001/accel^shift")
253 eAttribute = E_ATTRIBUTE_MOD_SHIFT;
254 else if (sAttribute == "http://openoffice.org/2001/accel^mod1")
255 eAttribute = E_ATTRIBUTE_MOD_MOD1;
256 else if (sAttribute == "http://openoffice.org/2001/accel^mod2")
257 eAttribute = E_ATTRIBUTE_MOD_MOD2;
258 else if (sAttribute == "http://openoffice.org/2001/accel^mod3")
259 eAttribute = E_ATTRIBUTE_MOD_MOD3;
260 else if (sAttribute == "http://www.w3.org/1999/xlink^href")
261 eAttribute = E_ATTRIBUTE_URL;
262 else
263 throw css::uno::RuntimeException(
264 "Unknown XML attribute detected!",
265 css::uno::Reference< css::xml::sax::XDocumentHandler >());
267 return eAttribute;
270 OUString AcceleratorConfigurationReader::implts_getErrorLineString()
272 if (!m_xLocator.is())
273 return OUString("Error during parsing XML. (No further info available ...)");
275 OUStringBuffer sMsg(256);
276 sMsg.appendAscii("Error during parsing XML in\nline = ");
277 sMsg.append (m_xLocator->getLineNumber() );
278 sMsg.appendAscii("\ncolumn = " );
279 sMsg.append (m_xLocator->getColumnNumber() );
280 sMsg.appendAscii("." );
281 return sMsg.makeStringAndClear();
284 } // namespace framework
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */