nss: upgrade to release 3.73
[LibreOffice.git] / framework / source / xml / acceleratorconfigurationreader.cxx
blob2ff467f6845ebbf7accdeb3660201fd9f4416883
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 #include <rtl/ustrbuf.hxx>
32 namespace framework{
34 /* Throws a SaxException in case a wrong formatted XML
35 structure was detected.
37 This macro combined the given comment with a generic
38 way to find out the XML line (where the error occurred)
39 to format a suitable message.
41 @param COMMENT
42 an ascii string, which describe the problem.
44 #define THROW_PARSEEXCEPTION(COMMENT) \
45 { \
46 throw css::xml::sax::SAXException( \
47 implts_getErrorLineString() + COMMENT, \
48 static_cast< css::xml::sax::XDocumentHandler* >(this), \
49 css::uno::Any()); \
52 AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer)
53 : m_rContainer (rContainer )
54 , m_bInsideAcceleratorList(false )
55 , m_bInsideAcceleratorItem(false )
59 AcceleratorConfigurationReader::~AcceleratorConfigurationReader()
63 void SAL_CALL AcceleratorConfigurationReader::startDocument()
67 void SAL_CALL AcceleratorConfigurationReader::endDocument()
69 // The xml file seems to be corrupted.
70 // Because we found no end-tags ... at least for
71 // one list or item.
72 if (m_bInsideAcceleratorList || m_bInsideAcceleratorItem)
74 THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!")
78 void SAL_CALL AcceleratorConfigurationReader::startElement(const OUString& sElement ,
79 const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList)
81 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
83 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
84 // Because an item occurs very often ... a list should occur one times only!
85 if (eElement == E_ELEMENT_ITEM)
87 if (!m_bInsideAcceleratorList)
88 THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.")
89 if (m_bInsideAcceleratorItem)
90 THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.")
91 m_bInsideAcceleratorItem = true;
93 OUString sCommand;
94 css::awt::KeyEvent aEvent;
96 sal_Int16 c = xAttributeList->getLength();
97 sal_Int16 i = 0;
98 for (i=0; i<c; ++i)
100 OUString sAttribute = xAttributeList->getNameByIndex(i);
101 OUString sValue = xAttributeList->getValueByIndex(i);
102 EXMLAttribute eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute);
103 switch(eAttribute)
105 case E_ATTRIBUTE_URL :
106 sCommand = sValue.intern();
107 break;
109 case E_ATTRIBUTE_KEYCODE :
110 aEvent.KeyCode = KeyMapping::get().mapIdentifierToCode(sValue);
111 break;
113 case E_ATTRIBUTE_MOD_SHIFT :
114 aEvent.Modifiers |= css::awt::KeyModifier::SHIFT;
115 break;
117 case E_ATTRIBUTE_MOD_MOD1 :
118 aEvent.Modifiers |= css::awt::KeyModifier::MOD1;
119 break;
121 case E_ATTRIBUTE_MOD_MOD2 :
122 aEvent.Modifiers |= css::awt::KeyModifier::MOD2;
123 break;
125 case E_ATTRIBUTE_MOD_MOD3 :
126 aEvent.Modifiers |= css::awt::KeyModifier::MOD3;
130 // validate command and key event.
131 if (
132 sCommand.isEmpty() ||
133 (aEvent.KeyCode == 0 )
136 THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.")
139 // register key event + command inside cache ...
140 // Check for already existing items there.
141 if (!m_rContainer.hasKey(aEvent))
142 m_rContainer.setKeyCommandPair(aEvent, sCommand);
143 else
145 // Attention: It's not really a reason to throw an exception and kill the office, if the configuration contains
146 // multiple registrations for the same key :-) Show a warning ... and ignore the second item.
147 // THROW_PARSEEXCEPTION("Command is registered for the same key more than once.")
148 SAL_INFO("fwk",
149 "AcceleratorConfigurationReader::startElement(): Double registration detected. Command=\"" <<
150 sCommand <<
151 "\" KeyCode=" <<
152 aEvent.KeyCode <<
153 "Modifiers=" <<
154 aEvent.Modifiers);
158 if (eElement == E_ELEMENT_ACCELERATORLIST)
160 if (m_bInsideAcceleratorList)
161 THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.")
162 m_bInsideAcceleratorList = true;
163 return;
167 void SAL_CALL AcceleratorConfigurationReader::endElement(const OUString& sElement)
169 EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement);
171 // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation.
172 // Because an item occurs very often ... a list should occur one times only!
173 if (eElement == E_ELEMENT_ITEM)
175 if (!m_bInsideAcceleratorItem)
176 THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.")
177 m_bInsideAcceleratorItem = false;
180 if (eElement == E_ELEMENT_ACCELERATORLIST)
182 if (!m_bInsideAcceleratorList)
183 THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.")
184 m_bInsideAcceleratorList = false;
188 void SAL_CALL AcceleratorConfigurationReader::characters(const OUString&)
192 void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const OUString&)
196 void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const OUString& /*sTarget*/,
197 const OUString& /*sData*/ )
201 void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator)
203 m_xLocator = xLocator;
206 AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(const OUString& sElement)
208 AcceleratorConfigurationReader::EXMLElement eElement;
210 if (sElement == "http://openoffice.org/2001/accel^acceleratorlist")
211 eElement = E_ELEMENT_ACCELERATORLIST;
212 else if (sElement == "http://openoffice.org/2001/accel^item")
213 eElement = E_ELEMENT_ITEM;
214 else
215 throw css::uno::RuntimeException(
216 "Unknown XML element detected!",
217 css::uno::Reference< css::xml::sax::XDocumentHandler >());
219 return eElement;
222 AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(const OUString& sAttribute)
224 AcceleratorConfigurationReader::EXMLAttribute eAttribute;
226 if (sAttribute == "http://openoffice.org/2001/accel^code")
227 eAttribute = E_ATTRIBUTE_KEYCODE;
228 else if (sAttribute == "http://openoffice.org/2001/accel^shift")
229 eAttribute = E_ATTRIBUTE_MOD_SHIFT;
230 else if (sAttribute == "http://openoffice.org/2001/accel^mod1")
231 eAttribute = E_ATTRIBUTE_MOD_MOD1;
232 else if (sAttribute == "http://openoffice.org/2001/accel^mod2")
233 eAttribute = E_ATTRIBUTE_MOD_MOD2;
234 else if (sAttribute == "http://openoffice.org/2001/accel^mod3")
235 eAttribute = E_ATTRIBUTE_MOD_MOD3;
236 else if (sAttribute == "http://www.w3.org/1999/xlink^href")
237 eAttribute = E_ATTRIBUTE_URL;
238 else
239 throw css::uno::RuntimeException(
240 "Unknown XML attribute detected!",
241 css::uno::Reference< css::xml::sax::XDocumentHandler >());
243 return eAttribute;
246 OUString AcceleratorConfigurationReader::implts_getErrorLineString()
248 if (!m_xLocator.is())
249 return "Error during parsing XML. (No further info available ...)";
251 OUStringBuffer sMsg(256);
252 sMsg.append("Error during parsing XML in\nline = ");
253 sMsg.append (m_xLocator->getLineNumber() );
254 sMsg.append("\ncolumn = " );
255 sMsg.append (m_xLocator->getColumnNumber() );
256 sMsg.append("." );
257 return sMsg.makeStringAndClear();
260 } // namespace framework
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */