/src/libreoffice/framework/source/xml/acceleratorconfigurationreader.cxx
Line | Count | Source |
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 | | |
23 | | #include <accelerators/keymapping.hxx> |
24 | | #include <xml/acceleratorconfigurationreader.hxx> |
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 | | |
30 | | namespace 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 | 0 | { \ |
44 | 0 | throw css::xml::sax::SAXException( \ |
45 | 0 | implts_getErrorLineString() + COMMENT, \ |
46 | 0 | static_cast< css::xml::sax::XDocumentHandler* >(this), \ |
47 | 0 | css::uno::Any()); \ |
48 | 0 | } |
49 | | |
50 | | AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer) |
51 | 0 | : m_rContainer (rContainer ) |
52 | 0 | , m_bInsideAcceleratorList(false ) |
53 | 0 | , m_bInsideAcceleratorItem(false ) |
54 | 0 | { |
55 | 0 | } |
56 | | |
57 | | AcceleratorConfigurationReader::~AcceleratorConfigurationReader() |
58 | 0 | { |
59 | 0 | } |
60 | | |
61 | | void SAL_CALL AcceleratorConfigurationReader::startDocument() |
62 | 0 | { |
63 | 0 | } |
64 | | |
65 | | void SAL_CALL AcceleratorConfigurationReader::endDocument() |
66 | 0 | { |
67 | | // The xml file seems to be corrupted. |
68 | | // Because we found no end-tags ... at least for |
69 | | // one list or item. |
70 | 0 | if (m_bInsideAcceleratorList || m_bInsideAcceleratorItem) |
71 | 0 | { |
72 | 0 | THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!") |
73 | 0 | } |
74 | 0 | } |
75 | | |
76 | | void SAL_CALL AcceleratorConfigurationReader::startElement(const OUString& sElement , |
77 | | const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList) |
78 | 0 | { |
79 | 0 | EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement); |
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 | 0 | if (eElement == E_ELEMENT_ITEM) |
84 | 0 | { |
85 | 0 | if (!m_bInsideAcceleratorList) |
86 | 0 | THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.") |
87 | 0 | if (m_bInsideAcceleratorItem) |
88 | 0 | THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.") |
89 | 0 | m_bInsideAcceleratorItem = true; |
90 | |
|
91 | 0 | OUString sCommand; |
92 | 0 | css::awt::KeyEvent aEvent; |
93 | |
|
94 | 0 | sal_Int16 c = xAttributeList->getLength(); |
95 | 0 | sal_Int16 i = 0; |
96 | 0 | for (i=0; i<c; ++i) |
97 | 0 | { |
98 | 0 | OUString sAttribute = xAttributeList->getNameByIndex(i); |
99 | 0 | OUString sValue = xAttributeList->getValueByIndex(i); |
100 | 0 | EXMLAttribute eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute); |
101 | 0 | switch(eAttribute) |
102 | 0 | { |
103 | 0 | case E_ATTRIBUTE_URL : |
104 | 0 | sCommand = sValue; |
105 | 0 | break; |
106 | | |
107 | 0 | case E_ATTRIBUTE_KEYCODE : |
108 | 0 | aEvent.KeyCode = KeyMapping::get().mapIdentifierToCode(sValue); |
109 | 0 | break; |
110 | | |
111 | 0 | case E_ATTRIBUTE_MOD_SHIFT : |
112 | 0 | aEvent.Modifiers |= css::awt::KeyModifier::SHIFT; |
113 | 0 | break; |
114 | | |
115 | 0 | case E_ATTRIBUTE_MOD_MOD1 : |
116 | 0 | aEvent.Modifiers |= css::awt::KeyModifier::MOD1; |
117 | 0 | break; |
118 | | |
119 | 0 | case E_ATTRIBUTE_MOD_MOD2 : |
120 | 0 | aEvent.Modifiers |= css::awt::KeyModifier::MOD2; |
121 | 0 | break; |
122 | | |
123 | 0 | case E_ATTRIBUTE_MOD_MOD3 : |
124 | 0 | aEvent.Modifiers |= css::awt::KeyModifier::MOD3; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | | // validate command and key event. |
129 | 0 | if ( |
130 | 0 | sCommand.isEmpty() || |
131 | 0 | (aEvent.KeyCode == 0 ) |
132 | 0 | ) |
133 | 0 | { |
134 | 0 | THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.") |
135 | 0 | } |
136 | | |
137 | | // register key event + command inside cache ... |
138 | | // Check for already existing items there. |
139 | 0 | if (!m_rContainer.hasKey(aEvent)) |
140 | 0 | m_rContainer.setKeyCommandPair(aEvent, sCommand); |
141 | 0 | else |
142 | 0 | { |
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 | 0 | SAL_INFO("fwk", |
147 | 0 | "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" << |
148 | 0 | sCommand << |
149 | 0 | "\" KeyCode=" << |
150 | 0 | aEvent.KeyCode << |
151 | 0 | "Modifiers=" << |
152 | 0 | aEvent.Modifiers); |
153 | 0 | } |
154 | 0 | } |
155 | | |
156 | 0 | if (eElement == E_ELEMENT_ACCELERATORLIST) |
157 | 0 | { |
158 | 0 | if (m_bInsideAcceleratorList) |
159 | 0 | THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.") |
160 | 0 | m_bInsideAcceleratorList = true; |
161 | 0 | return; |
162 | 0 | } |
163 | 0 | } |
164 | | |
165 | | void SAL_CALL AcceleratorConfigurationReader::endElement(const OUString& sElement) |
166 | 0 | { |
167 | 0 | EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement); |
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 | 0 | if (eElement == E_ELEMENT_ITEM) |
172 | 0 | { |
173 | 0 | if (!m_bInsideAcceleratorItem) |
174 | 0 | THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.") |
175 | 0 | m_bInsideAcceleratorItem = false; |
176 | 0 | } |
177 | | |
178 | 0 | if (eElement == E_ELEMENT_ACCELERATORLIST) |
179 | 0 | { |
180 | 0 | if (!m_bInsideAcceleratorList) |
181 | 0 | THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.") |
182 | 0 | m_bInsideAcceleratorList = false; |
183 | 0 | } |
184 | 0 | } |
185 | | |
186 | | void SAL_CALL AcceleratorConfigurationReader::characters(const OUString&) |
187 | 0 | { |
188 | 0 | } |
189 | | |
190 | | void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const OUString&) |
191 | 0 | { |
192 | 0 | } |
193 | | |
194 | | void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const OUString& /*sTarget*/, |
195 | | const OUString& /*sData*/ ) |
196 | 0 | { |
197 | 0 | } |
198 | | |
199 | | void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator) |
200 | 0 | { |
201 | 0 | m_xLocator = xLocator; |
202 | 0 | } |
203 | | |
204 | | AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(std::u16string_view sElement) |
205 | 0 | { |
206 | 0 | AcceleratorConfigurationReader::EXMLElement eElement; |
207 | |
|
208 | 0 | if (sElement == u"http://openoffice.org/2001/accel^acceleratorlist") |
209 | 0 | eElement = E_ELEMENT_ACCELERATORLIST; |
210 | 0 | else if (sElement == u"http://openoffice.org/2001/accel^item") |
211 | 0 | eElement = E_ELEMENT_ITEM; |
212 | 0 | else |
213 | 0 | throw css::uno::RuntimeException( |
214 | 0 | u"Unknown XML element detected!"_ustr, |
215 | 0 | css::uno::Reference< css::xml::sax::XDocumentHandler >()); |
216 | | |
217 | 0 | return eElement; |
218 | 0 | } |
219 | | |
220 | | AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(std::u16string_view sAttribute) |
221 | 0 | { |
222 | 0 | AcceleratorConfigurationReader::EXMLAttribute eAttribute; |
223 | |
|
224 | 0 | if (sAttribute == u"http://openoffice.org/2001/accel^code") |
225 | 0 | eAttribute = E_ATTRIBUTE_KEYCODE; |
226 | 0 | else if (sAttribute == u"http://openoffice.org/2001/accel^shift") |
227 | 0 | eAttribute = E_ATTRIBUTE_MOD_SHIFT; |
228 | 0 | else if (sAttribute == u"http://openoffice.org/2001/accel^mod1") |
229 | 0 | eAttribute = E_ATTRIBUTE_MOD_MOD1; |
230 | 0 | else if (sAttribute == u"http://openoffice.org/2001/accel^mod2") |
231 | 0 | eAttribute = E_ATTRIBUTE_MOD_MOD2; |
232 | 0 | else if (sAttribute == u"http://openoffice.org/2001/accel^mod3") |
233 | 0 | eAttribute = E_ATTRIBUTE_MOD_MOD3; |
234 | 0 | else if (sAttribute == u"http://www.w3.org/1999/xlink^href") |
235 | 0 | eAttribute = E_ATTRIBUTE_URL; |
236 | 0 | else |
237 | 0 | throw css::uno::RuntimeException( |
238 | 0 | u"Unknown XML attribute detected!"_ustr, |
239 | 0 | css::uno::Reference< css::xml::sax::XDocumentHandler >()); |
240 | | |
241 | 0 | return eAttribute; |
242 | 0 | } |
243 | | |
244 | | OUString AcceleratorConfigurationReader::implts_getErrorLineString() |
245 | 0 | { |
246 | 0 | if (!m_xLocator.is()) |
247 | 0 | return u"Error during parsing XML. (No further info available ...)"_ustr; |
248 | | |
249 | 0 | return "Error during parsing XML in\nline = " + |
250 | 0 | OUString::number(m_xLocator->getLineNumber()) + |
251 | 0 | "\ncolumn = " + |
252 | 0 | OUString::number(m_xLocator->getColumnNumber()) + |
253 | 0 | "."; |
254 | 0 | } |
255 | | |
256 | | } // namespace framework |
257 | | |
258 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |