1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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.
40 an ascii string, which describe the problem.
42 #define THROW_PARSEEXCEPTION(COMMENT) \
44 throw css::xml::sax::SAXException( \
45 implts_getErrorLineString() + COMMENT, \
46 static_cast< css::xml::sax::XDocumentHandler* >(this), \
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
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;
92 css::awt::KeyEvent aEvent
;
94 sal_Int16 c
= xAttributeList
->getLength();
98 OUString sAttribute
= xAttributeList
->getNameByIndex(i
);
99 OUString sValue
= xAttributeList
->getValueByIndex(i
);
100 EXMLAttribute eAttribute
= AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute
);
103 case E_ATTRIBUTE_URL
:
107 case E_ATTRIBUTE_KEYCODE
:
108 aEvent
.KeyCode
= KeyMapping::get().mapIdentifierToCode(sValue
);
111 case E_ATTRIBUTE_MOD_SHIFT
:
112 aEvent
.Modifiers
|= css::awt::KeyModifier::SHIFT
;
115 case E_ATTRIBUTE_MOD_MOD1
:
116 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD1
;
119 case E_ATTRIBUTE_MOD_MOD2
:
120 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD2
;
123 case E_ATTRIBUTE_MOD_MOD3
:
124 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD3
;
128 // validate command and key event.
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
);
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.")
147 "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" <<
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;
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
;
213 throw css::uno::RuntimeException(
214 "Unknown XML element detected!",
215 css::uno::Reference
< css::xml::sax::XDocumentHandler
>());
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
;
237 throw css::uno::RuntimeException(
238 "Unknown XML attribute detected!",
239 css::uno::Reference
< css::xml::sax::XDocumentHandler
>());
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()) +
252 OUString::number(m_xLocator
->getColumnNumber()) +
256 } // namespace framework
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */