LibreOffice Module framework (master) 1
acceleratorconfigurationreader.cxx
Go to the documentation of this file.
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#include <sal/config.h>
21#include <sal/log.hxx>
22
25
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>
29
30namespace framework{
31
32/* Throws a SaxException in case a wrong formatted XML
33 structure was detected.
34
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.
38
39 @param COMMENT
40 an ascii string, which describe the problem.
41 */
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()); \
48 }
49
51 : m_rContainer (rContainer )
52 , m_bInsideAcceleratorList(false )
53 , m_bInsideAcceleratorItem(false )
54{
55}
56
58{
59}
60
62{
63}
64
66{
67 // The xml file seems to be corrupted.
68 // Because we found no end-tags ... at least for
69 // one list or item.
71 {
72 THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
73 }
74}
75
76void SAL_CALL AcceleratorConfigurationReader::startElement(const OUString& sElement ,
77 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
78{
80
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)
84 {
86 THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
88 THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
90
91 OUString sCommand;
92 css::awt::KeyEvent aEvent;
93
94 sal_Int16 c = xAttributeList->getLength();
95 sal_Int16 i = 0;
96 for (i=0; i<c; ++i)
97 {
98 OUString sAttribute = xAttributeList->getNameByIndex(i);
99 OUString sValue = xAttributeList->getValueByIndex(i);
101 switch(eAttribute)
102 {
103 case E_ATTRIBUTE_URL :
104 sCommand = sValue;
105 break;
106
108 aEvent.KeyCode = KeyMapping::get().mapIdentifierToCode(sValue);
109 break;
110
112 aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
113 break;
114
116 aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
117 break;
118
120 aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
121 break;
122
124 aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
125 }
126 }
127
128 // validate command and key event.
129 if (
130 sCommand.isEmpty() ||
131 (aEvent.KeyCode == 0 )
132 )
133 {
134 THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
135 }
136
137 // register key event + command inside cache ...
138 // Check for already existing items there.
141 else
142 {
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);
153 }
154 }
155
156 if (eElement == E_ELEMENT_ACCELERATORLIST)
157 {
159 THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
161 return;
162 }
163}
164
165void SAL_CALL AcceleratorConfigurationReader::endElement(const OUString& sElement)
166{
168
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)
172 {
174 THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
176 }
177
178 if (eElement == E_ELEMENT_ACCELERATORLIST)
179 {
181 THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
183 }
184}
185
186void SAL_CALL AcceleratorConfigurationReader::characters(const OUString&)
187{
188}
189
191{
192}
193
194void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const OUString& /*sTarget*/,
195 const OUString& /*sData*/ )
196{
197}
198
199void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
200{
201 m_xLocator = xLocator;
202}
203
205{
207
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 >());
216
217 return eElement;
218}
219
221{
223
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 >());
240
241 return eAttribute;
242}
243
245{
246 if (!m_xLocator.is())
247 return "Error during parsing XML. (No further info available ...)";
248
249 return "Error during parsing XML in\nline = " +
250 OUString::number(m_xLocator->getLineNumber()) +
251 "\ncolumn = " +
252 OUString::number(m_xLocator->getColumnNumber()) +
253 ".";
254}
255
256} // namespace framework
257
258/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define THROW_PARSEEXCEPTION(COMMENT)
AnyEventRef aEvent
implements a cache for any accelerator configuration.
void setKeyCommandPair(const css::awt::KeyEvent &aKey, const OUString &sCommand)
add a new or change an existing key-command pair of this container.
bool hasKey(const css::awt::KeyEvent &aKey) const
checks if the specified key exists.
virtual void SAL_CALL setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator > &xLocator) override
css::uno::Reference< css::xml::sax::XLocator > m_xLocator
provide information about the parsing state.
static EXMLElement implst_classifyElement(std::u16string_view sElement)
TODO document me.
virtual ~AcceleratorConfigurationReader() override
does nothing real ...
AcceleratorConfigurationReader(AcceleratorCache &rContainer)
connect this new reader/writer instance to an outside container, which should be used flushed to the ...
virtual void SAL_CALL ignorableWhitespace(const OUString &sWhitespaces) override
bool m_bInsideAcceleratorList
used to detect if an accelerator list occurs recursive inside xml.
virtual void SAL_CALL characters(const OUString &sChars) override
AcceleratorCache & m_rContainer
reference to the outside container, where this reader/writer must work on.
static EXMLAttribute implst_classifyAttribute(std::u16string_view sAttribute)
TODO document me.
bool m_bInsideAcceleratorItem
used to detect if an accelerator item occurs recursive inside xml.
virtual void SAL_CALL startElement(const OUString &sElement, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttributeList) override
virtual void SAL_CALL endElement(const OUString &sElement) override
virtual void SAL_CALL processingInstruction(const OUString &sTarget, const OUString &sData) override
static KeyMapping & get()
Definition: keymapping.cxx:159
sal_uInt16 mapIdentifierToCode(const OUString &sIdentifier)
return a suitable key code for the specified key identifier.
Definition: keymapping.cxx:164
float u
#define SAL_INFO(area, stream)
int i