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 <acceleratorconst.h>
28 #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp>
29 #include <com/sun/star/xml/sax/SAXException.hpp>
30 #include <com/sun/star/awt/KeyModifier.hpp>
31 #include <com/sun/star/awt/KeyEvent.hpp>
32 #include <com/sun/star/awt/Key.hpp>
33 #include <com/sun/star/container/ElementExistException.hpp>
35 #include <vcl/svapp.hxx>
36 #include <rtl/ustrbuf.hxx>
40 /* Throws a SaxException in case a wrong formatted XML
41 structure was detected.
43 This macro combined the given comment with a generic
44 way to find out the XML line (where the error occurred)
45 to format a suitable message.
48 an ascii string, which describe the problem.
50 #define THROW_PARSEEXCEPTION(COMMENT) \
52 throw css::xml::sax::SAXException( \
53 implts_getErrorLineString() + COMMENT, \
54 static_cast< css::xml::sax::XDocumentHandler* >(this), \
58 AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache
& rContainer
)
59 : m_rContainer (rContainer
)
60 , m_bInsideAcceleratorList(false )
61 , m_bInsideAcceleratorItem(false )
65 AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
69 void SAL_CALL
AcceleratorConfigurationReader::startDocument()
73 void SAL_CALL
AcceleratorConfigurationReader::endDocument()
75 // The xml file seems to be corrupted.
76 // Because we found no end-tags ... at least for
78 if (m_bInsideAcceleratorList
|| m_bInsideAcceleratorItem
)
80 THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
84 void SAL_CALL
AcceleratorConfigurationReader::startElement(const OUString
& sElement
,
85 const css::uno::Reference
< css::xml::sax::XAttributeList
>& xAttributeList
)
87 EXMLElement eElement
= AcceleratorConfigurationReader::implst_classifyElement(sElement
);
89 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
90 // Because an item occurs very often ... a list should occur one times only!
91 if (eElement
== E_ELEMENT_ITEM
)
93 if (!m_bInsideAcceleratorList
)
94 THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
95 if (m_bInsideAcceleratorItem
)
96 THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
97 m_bInsideAcceleratorItem
= true;
100 css::awt::KeyEvent aEvent
;
102 sal_Int16 c
= xAttributeList
->getLength();
106 OUString sAttribute
= xAttributeList
->getNameByIndex(i
);
107 OUString sValue
= xAttributeList
->getValueByIndex(i
);
108 EXMLAttribute eAttribute
= AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute
);
111 case E_ATTRIBUTE_URL
:
112 sCommand
= sValue
.intern();
115 case E_ATTRIBUTE_KEYCODE
:
116 aEvent
.KeyCode
= KeyMapping::get().mapIdentifierToCode(sValue
);
119 case E_ATTRIBUTE_MOD_SHIFT
:
120 aEvent
.Modifiers
|= css::awt::KeyModifier::SHIFT
;
123 case E_ATTRIBUTE_MOD_MOD1
:
124 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD1
;
127 case E_ATTRIBUTE_MOD_MOD2
:
128 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD2
;
131 case E_ATTRIBUTE_MOD_MOD3
:
132 aEvent
.Modifiers
|= css::awt::KeyModifier::MOD3
;
136 // validate command and key event.
138 sCommand
.isEmpty() ||
139 (aEvent
.KeyCode
== 0 )
142 THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
145 // register key event + command inside cache ...
146 // Check for already existing items there.
147 if (!m_rContainer
.hasKey(aEvent
))
148 m_rContainer
.setKeyCommandPair(aEvent
, sCommand
);
151 // Attention: It's not really a reason to throw an exception and kill the office, if the configuration contains
152 // multiple registrations for the same key :-) Show a warning ... and ignore the second item.
153 // THROW_PARSEEXCEPTION("Command is registered for the same key more than once.")
155 "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" <<
164 if (eElement
== E_ELEMENT_ACCELERATORLIST
)
166 if (m_bInsideAcceleratorList
)
167 THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
168 m_bInsideAcceleratorList
= true;
173 void SAL_CALL
AcceleratorConfigurationReader::endElement(const OUString
& sElement
)
175 EXMLElement eElement
= AcceleratorConfigurationReader::implst_classifyElement(sElement
);
177 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
178 // Because an item occurs very often ... a list should occur one times only!
179 if (eElement
== E_ELEMENT_ITEM
)
181 if (!m_bInsideAcceleratorItem
)
182 THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
183 m_bInsideAcceleratorItem
= false;
186 if (eElement
== E_ELEMENT_ACCELERATORLIST
)
188 if (!m_bInsideAcceleratorList
)
189 THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
190 m_bInsideAcceleratorList
= false;
194 void SAL_CALL
AcceleratorConfigurationReader::characters(const OUString
&)
198 void SAL_CALL
AcceleratorConfigurationReader::ignorableWhitespace(const OUString
&)
202 void SAL_CALL
AcceleratorConfigurationReader::processingInstruction(const OUString
& /*sTarget*/,
203 const OUString
& /*sData*/ )
207 void SAL_CALL
AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference
< css::xml::sax::XLocator
>& xLocator
)
209 m_xLocator
= xLocator
;
212 AcceleratorConfigurationReader::EXMLElement
AcceleratorConfigurationReader::implst_classifyElement(const OUString
& sElement
)
214 AcceleratorConfigurationReader::EXMLElement eElement
;
216 if (sElement
== "http://openoffice.org/2001/accel^acceleratorlist")
217 eElement
= E_ELEMENT_ACCELERATORLIST
;
218 else if (sElement
== "http://openoffice.org/2001/accel^item")
219 eElement
= E_ELEMENT_ITEM
;
221 throw css::uno::RuntimeException(
222 "Unknown XML element detected!",
223 css::uno::Reference
< css::xml::sax::XDocumentHandler
>());
228 AcceleratorConfigurationReader::EXMLAttribute
AcceleratorConfigurationReader::implst_classifyAttribute(const OUString
& sAttribute
)
230 AcceleratorConfigurationReader::EXMLAttribute eAttribute
;
232 if (sAttribute
== "http://openoffice.org/2001/accel^code")
233 eAttribute
= E_ATTRIBUTE_KEYCODE
;
234 else if (sAttribute
== "http://openoffice.org/2001/accel^shift")
235 eAttribute
= E_ATTRIBUTE_MOD_SHIFT
;
236 else if (sAttribute
== "http://openoffice.org/2001/accel^mod1")
237 eAttribute
= E_ATTRIBUTE_MOD_MOD1
;
238 else if (sAttribute
== "http://openoffice.org/2001/accel^mod2")
239 eAttribute
= E_ATTRIBUTE_MOD_MOD2
;
240 else if (sAttribute
== "http://openoffice.org/2001/accel^mod3")
241 eAttribute
= E_ATTRIBUTE_MOD_MOD3
;
242 else if (sAttribute
== "http://www.w3.org/1999/xlink^href")
243 eAttribute
= E_ATTRIBUTE_URL
;
245 throw css::uno::RuntimeException(
246 "Unknown XML attribute detected!",
247 css::uno::Reference
< css::xml::sax::XDocumentHandler
>());
252 OUString
AcceleratorConfigurationReader::implts_getErrorLineString()
254 if (!m_xLocator
.is())
255 return "Error during parsing XML. (No further info available ...)";
257 OUStringBuffer
sMsg(256);
258 sMsg
.append("Error during parsing XML in\nline = ");
259 sMsg
.append (m_xLocator
->getLineNumber() );
260 sMsg
.append("\ncolumn = " );
261 sMsg
.append (m_xLocator
->getColumnNumber() );
263 return sMsg
.makeStringAndClear();
266 } // namespace framework
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */