Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / framework / source / xml / acceleratorconfigurationreader.cxx
blob7cbb81de9bcd53550b90d1e9c5cb0bd208a3c1aa
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 <sal/config.h>
21 #include <sal/log.hxx>
23 #include <accelerators/keymapping.hxx>
24 #include <xml/acceleratorconfigurationreader.hxx>
26 #include <com/sun/star/xml/sax/SAXException.hpp>
27 #include <com/sun/star/awt/KeyModifier.hpp>
28 #include <com/sun/star/awt/KeyEvent.hpp>
30 namespace framework{
32 /* Throws a SaxException in case a wrong formatted XML
33 structure was detected.
35 This macro combined the given comment with a generic
36 way to find out the XML line (where the error occurred)
37 to format a suitable message.
39 @param COMMENT
40 an ascii string, which describe the problem.
42 #define THROW_PARSEEXCEPTION(COMMENT) \
43 { \
44 throw css::xml::sax::SAXException( \
45 implts_getErrorLineString() + COMMENT, \
46 static_cast< css::xml::sax::XDocumentHandler* >(this), \
47 css::uno::Any()); \
50 AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer)
51 : m_rContainer (rContainer )
52 , m_bInsideAcceleratorList(false )
53 , m_bInsideAcceleratorItem(false )
57 AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
61 void SAL_CALL AcceleratorConfigurationReader::startDocument()
65 void SAL_CALL AcceleratorConfigurationReader::endDocument()
67 // The xml file seems to be corrupted.
68 // Because we found no end-tags ... at least for
69 // one list or item.
70 if (m_bInsideAcceleratorList || m_bInsideAcceleratorItem)
72 THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
76 void SAL_CALL AcceleratorConfigurationReader::startElement(const OUString& sElement ,
77 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
79 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
81 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
82 // Because an item occurs very often ... a list should occur one times only!
83 if (eElement == E_ELEMENT_ITEM)
85 if (!m_bInsideAcceleratorList)
86 THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
87 if (m_bInsideAcceleratorItem)
88 THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
89 m_bInsideAcceleratorItem = true;
91 OUString sCommand;
92 css::awt::KeyEvent aEvent;
94 sal_Int16 c = xAttributeList->getLength();
95 sal_Int16 i = 0;
96 for (i=0; i<c; ++i)
98 OUString sAttribute = xAttributeList->getNameByIndex(i);
99 OUString sValue = xAttributeList->getValueByIndex(i);
100 EXMLAttribute eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute);
101 switch(eAttribute)
103 case E_ATTRIBUTE_URL :
104 sCommand = sValue;
105 break;
107 case E_ATTRIBUTE_KEYCODE :
108 aEvent.KeyCode = KeyMapping::get().mapIdentifierToCode(sValue);
109 break;
111 case E_ATTRIBUTE_MOD_SHIFT :
112 aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
113 break;
115 case E_ATTRIBUTE_MOD_MOD1 :
116 aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
117 break;
119 case E_ATTRIBUTE_MOD_MOD2 :
120 aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
121 break;
123 case E_ATTRIBUTE_MOD_MOD3 :
124 aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
128 // validate command and key event.
129 if (
130 sCommand.isEmpty() ||
131 (aEvent.KeyCode == 0 )
134 THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
137 // register key event + command inside cache ...
138 // Check for already existing items there.
139 if (!m_rContainer.hasKey(aEvent))
140 m_rContainer.setKeyCommandPair(aEvent, sCommand);
141 else
143 // Attention: It's not really a reason to throw an exception and kill the office, if the configuration contains
144 // multiple registrations for the same key :-) Show a warning ... and ignore the second item.
145 // THROW_PARSEEXCEPTION("Command is registered for the same key more than once.")
146 SAL_INFO("fwk",
147 "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" <<
148 sCommand <<
149 "\" KeyCode=" <<
150 aEvent.KeyCode <<
151 "Modifiers=" <<
152 aEvent.Modifiers);
156 if (eElement == E_ELEMENT_ACCELERATORLIST)
158 if (m_bInsideAcceleratorList)
159 THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
160 m_bInsideAcceleratorList = true;
161 return;
165 void SAL_CALL AcceleratorConfigurationReader::endElement(const OUString& sElement)
167 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
169 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
170 // Because an item occurs very often ... a list should occur one times only!
171 if (eElement == E_ELEMENT_ITEM)
173 if (!m_bInsideAcceleratorItem)
174 THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
175 m_bInsideAcceleratorItem = false;
178 if (eElement == E_ELEMENT_ACCELERATORLIST)
180 if (!m_bInsideAcceleratorList)
181 THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
182 m_bInsideAcceleratorList = false;
186 void SAL_CALL AcceleratorConfigurationReader::characters(const OUString&)
190 void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const OUString&)
194 void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const OUString& /*sTarget*/,
195 const OUString& /*sData*/ )
199 void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
201 m_xLocator = xLocator;
204 AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(std::u16string_view sElement)
206 AcceleratorConfigurationReader::EXMLElement eElement;
208 if (sElement == u"http://openoffice.org/2001/accel^acceleratorlist")
209 eElement = E_ELEMENT_ACCELERATORLIST;
210 else if (sElement == u"http://openoffice.org/2001/accel^item")
211 eElement = E_ELEMENT_ITEM;
212 else
213 throw css::uno::RuntimeException(
214 "Unknown XML element detected!",
215 css::uno::Reference< css::xml::sax::XDocumentHandler >());
217 return eElement;
220 AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(std::u16string_view sAttribute)
222 AcceleratorConfigurationReader::EXMLAttribute eAttribute;
224 if (sAttribute == u"http://openoffice.org/2001/accel^code")
225 eAttribute = E_ATTRIBUTE_KEYCODE;
226 else if (sAttribute == u"http://openoffice.org/2001/accel^shift")
227 eAttribute = E_ATTRIBUTE_MOD_SHIFT;
228 else if (sAttribute == u"http://openoffice.org/2001/accel^mod1")
229 eAttribute = E_ATTRIBUTE_MOD_MOD1;
230 else if (sAttribute == u"http://openoffice.org/2001/accel^mod2")
231 eAttribute = E_ATTRIBUTE_MOD_MOD2;
232 else if (sAttribute == u"http://openoffice.org/2001/accel^mod3")
233 eAttribute = E_ATTRIBUTE_MOD_MOD3;
234 else if (sAttribute == u"http://www.w3.org/1999/xlink^href")
235 eAttribute = E_ATTRIBUTE_URL;
236 else
237 throw css::uno::RuntimeException(
238 "Unknown XML attribute detected!",
239 css::uno::Reference< css::xml::sax::XDocumentHandler >());
241 return eAttribute;
244 OUString AcceleratorConfigurationReader::implts_getErrorLineString()
246 if (!m_xLocator.is())
247 return "Error during parsing XML. (No further info available ...)";
249 return "Error during parsing XML in\nline = " +
250 OUString::number(m_xLocator->getLineNumber()) +
251 "\ncolumn = " +
252 OUString::number(m_xLocator->getColumnNumber()) +
253 ".";
256 } // namespace framework
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */