16d739b60SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 36d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 46d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file 56d739b60SAndrew Rist * distributed with this work for additional information 66d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file 76d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the 86d739b60SAndrew Rist * "License"); you may not use this file except in compliance 96d739b60SAndrew Rist * with the License. You may obtain a copy of the License at 106d739b60SAndrew Rist * 116d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 126d739b60SAndrew Rist * 136d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing, 146d739b60SAndrew Rist * software distributed under the License is distributed on an 156d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 166d739b60SAndrew Rist * KIND, either express or implied. See the License for the 176d739b60SAndrew Rist * specific language governing permissions and limitations 186d739b60SAndrew Rist * under the License. 196d739b60SAndrew Rist * 206d739b60SAndrew Rist *************************************************************/ 216d739b60SAndrew Rist 226d739b60SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_framework.hxx" 26cdf0e10cSrcweir #include <xml/acceleratorconfigurationreader.hxx> 27cdf0e10cSrcweir 28cdf0e10cSrcweir //_______________________________________________ 29cdf0e10cSrcweir // own includes 30cdf0e10cSrcweir 31cdf0e10cSrcweir #ifndef __FRAMEWORK_ACCELERATORCONST_H_ 32cdf0e10cSrcweir #include <acceleratorconst.h> 33cdf0e10cSrcweir #endif 34cdf0e10cSrcweir 35cdf0e10cSrcweir //_______________________________________________ 36cdf0e10cSrcweir // interface includes 37cdf0e10cSrcweir #include <com/sun/star/xml/sax/XExtendedDocumentHandler.hpp> 38cdf0e10cSrcweir #include <com/sun/star/awt/KeyModifier.hpp> 39cdf0e10cSrcweir #include <com/sun/star/awt/KeyEvent.hpp> 40cdf0e10cSrcweir #include <com/sun/star/awt/Key.hpp> 41cdf0e10cSrcweir #include <com/sun/star/container/ElementExistException.hpp> 42cdf0e10cSrcweir 43cdf0e10cSrcweir //_______________________________________________ 44cdf0e10cSrcweir // other includes 45cdf0e10cSrcweir #include <vcl/svapp.hxx> 46cdf0e10cSrcweir #include <rtl/ustrbuf.hxx> 47cdf0e10cSrcweir 48cdf0e10cSrcweir //_______________________________________________ 49cdf0e10cSrcweir // namespace 50cdf0e10cSrcweir 51cdf0e10cSrcweir namespace framework{ 52cdf0e10cSrcweir 53cdf0e10cSrcweir //----------------------------------------------- 54*30acf5e8Spfg /* Throws a SaxException in case a wrong formatted XML 55cdf0e10cSrcweir structure was detected. 56cdf0e10cSrcweir 57cdf0e10cSrcweir This macro combined the given comment with a generic 5807a3d7f1SPedro Giffuni way to find out the XML line (where the error occurred) 59cdf0e10cSrcweir to format a suitable message. 60cdf0e10cSrcweir 61cdf0e10cSrcweir @param COMMENT 62cdf0e10cSrcweir an ascii string, which describe the problem. 63cdf0e10cSrcweir */ 64cdf0e10cSrcweir #define THROW_PARSEEXCEPTION(COMMENT) \ 65cdf0e10cSrcweir { \ 66cdf0e10cSrcweir ::rtl::OUStringBuffer sMessage(256); \ 67cdf0e10cSrcweir sMessage.append (implts_getErrorLineString()); \ 68cdf0e10cSrcweir sMessage.appendAscii(COMMENT ); \ 69cdf0e10cSrcweir \ 70cdf0e10cSrcweir throw css::xml::sax::SAXException( \ 71cdf0e10cSrcweir sMessage.makeStringAndClear(), \ 72cdf0e10cSrcweir static_cast< css::xml::sax::XDocumentHandler* >(this), \ 73cdf0e10cSrcweir css::uno::Any()); \ 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir //----------------------------------------------- 77cdf0e10cSrcweir // XInterface 78cdf0e10cSrcweir DEFINE_XINTERFACE_1(AcceleratorConfigurationReader , 79cdf0e10cSrcweir OWeakObject , 80cdf0e10cSrcweir DIRECT_INTERFACE(css::xml::sax::XDocumentHandler)) 81cdf0e10cSrcweir 82cdf0e10cSrcweir //----------------------------------------------- 83cdf0e10cSrcweir AcceleratorConfigurationReader::AcceleratorConfigurationReader(AcceleratorCache& rContainer) 84cdf0e10cSrcweir : ThreadHelpBase (&Application::GetSolarMutex()) 85cdf0e10cSrcweir , OWeakObject ( ) 86cdf0e10cSrcweir , m_rContainer (rContainer ) 87cdf0e10cSrcweir , m_bInsideAcceleratorList(sal_False ) 88cdf0e10cSrcweir , m_bInsideAcceleratorItem(sal_False ) 89cdf0e10cSrcweir { 90cdf0e10cSrcweir } 91cdf0e10cSrcweir 92cdf0e10cSrcweir //----------------------------------------------- 93cdf0e10cSrcweir AcceleratorConfigurationReader::~AcceleratorConfigurationReader() 94cdf0e10cSrcweir { 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir //----------------------------------------------- 98cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startDocument() 99cdf0e10cSrcweir throw(css::xml::sax::SAXException, 100cdf0e10cSrcweir css::uno::RuntimeException ) 101cdf0e10cSrcweir { 102cdf0e10cSrcweir } 103cdf0e10cSrcweir 104cdf0e10cSrcweir //----------------------------------------------- 105cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endDocument() 106cdf0e10cSrcweir throw(css::xml::sax::SAXException, 107cdf0e10cSrcweir css::uno::RuntimeException ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir // The xml file seems to be corrupted. 110cdf0e10cSrcweir // Because we found no end-tags ... at least for 111cdf0e10cSrcweir // one list or item. 112cdf0e10cSrcweir if ( 113cdf0e10cSrcweir (m_bInsideAcceleratorList) || 114cdf0e10cSrcweir (m_bInsideAcceleratorItem) 115cdf0e10cSrcweir ) 116cdf0e10cSrcweir { 117cdf0e10cSrcweir THROW_PARSEEXCEPTION("No matching start or end element 'acceleratorlist' found!") 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir //----------------------------------------------- 122cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::startElement(const ::rtl::OUString& sElement , 123cdf0e10cSrcweir const css::uno::Reference< css::xml::sax::XAttributeList >& xAttributeList) 124cdf0e10cSrcweir throw(css::xml::sax::SAXException, 125cdf0e10cSrcweir css::uno::RuntimeException ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement); 128cdf0e10cSrcweir 129cdf0e10cSrcweir // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation. 130*30acf5e8Spfg // Because an item occurs very often ... a list should occur one times only! 131cdf0e10cSrcweir if (eElement == E_ELEMENT_ITEM) 132cdf0e10cSrcweir { 133cdf0e10cSrcweir if (!m_bInsideAcceleratorList) 13407a3d7f1SPedro Giffuni THROW_PARSEEXCEPTION("An element \"accel:item\" must be embedded into 'accel:acceleratorlist'.") 135cdf0e10cSrcweir if (m_bInsideAcceleratorItem) 136cdf0e10cSrcweir THROW_PARSEEXCEPTION("An element \"accel:item\" is not a container.") 137cdf0e10cSrcweir m_bInsideAcceleratorItem = sal_True; 138cdf0e10cSrcweir 139cdf0e10cSrcweir ::rtl::OUString sCommand; 140cdf0e10cSrcweir css::awt::KeyEvent aEvent ; 141cdf0e10cSrcweir 142cdf0e10cSrcweir sal_Int16 c = xAttributeList->getLength(); 143cdf0e10cSrcweir sal_Int16 i = 0; 144cdf0e10cSrcweir for (i=0; i<c; ++i) 145cdf0e10cSrcweir { 146cdf0e10cSrcweir ::rtl::OUString sAttribute = xAttributeList->getNameByIndex(i); 147cdf0e10cSrcweir ::rtl::OUString sValue = xAttributeList->getValueByIndex(i); 148cdf0e10cSrcweir EXMLAttribute eAttribute = AcceleratorConfigurationReader::implst_classifyAttribute(sAttribute); 149cdf0e10cSrcweir switch(eAttribute) 150cdf0e10cSrcweir { 151cdf0e10cSrcweir case E_ATTRIBUTE_URL : 152cdf0e10cSrcweir sCommand = sValue.intern(); 153cdf0e10cSrcweir break; 154cdf0e10cSrcweir 155cdf0e10cSrcweir case E_ATTRIBUTE_KEYCODE : 156cdf0e10cSrcweir aEvent.KeyCode = m_rKeyMapping->mapIdentifierToCode(sValue); 157cdf0e10cSrcweir break; 158cdf0e10cSrcweir 159cdf0e10cSrcweir case E_ATTRIBUTE_MOD_SHIFT : 160cdf0e10cSrcweir aEvent.Modifiers |= css::awt::KeyModifier::SHIFT; 161cdf0e10cSrcweir break; 162cdf0e10cSrcweir 163cdf0e10cSrcweir case E_ATTRIBUTE_MOD_MOD1 : 164cdf0e10cSrcweir aEvent.Modifiers |= css::awt::KeyModifier::MOD1; 165cdf0e10cSrcweir break; 166cdf0e10cSrcweir 167cdf0e10cSrcweir case E_ATTRIBUTE_MOD_MOD2 : 168cdf0e10cSrcweir aEvent.Modifiers |= css::awt::KeyModifier::MOD2; 169cdf0e10cSrcweir break; 170cdf0e10cSrcweir 171cdf0e10cSrcweir case E_ATTRIBUTE_MOD_MOD3 : 172cdf0e10cSrcweir aEvent.Modifiers |= css::awt::KeyModifier::MOD3; 173cdf0e10cSrcweir } 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir // validate command and key event. 177cdf0e10cSrcweir if ( 178cdf0e10cSrcweir (!sCommand.getLength()) || 179cdf0e10cSrcweir (aEvent.KeyCode == 0 ) 180cdf0e10cSrcweir ) 181cdf0e10cSrcweir { 182cdf0e10cSrcweir THROW_PARSEEXCEPTION("XML element does not describe a valid accelerator nor a valid command.") 183cdf0e10cSrcweir } 184cdf0e10cSrcweir 185cdf0e10cSrcweir // register key event + command inside cache ... 186cdf0e10cSrcweir // Check for already existing items there. 187cdf0e10cSrcweir if (!m_rContainer.hasKey(aEvent)) 188cdf0e10cSrcweir m_rContainer.setKeyCommandPair(aEvent, sCommand); 189cdf0e10cSrcweir #ifdef ENABLE_WARNINGS 190cdf0e10cSrcweir else 191cdf0e10cSrcweir { 19207a3d7f1SPedro Giffuni // Attention: Its not really a reason to throw an exception and kill the office, if the configuration contains 193cdf0e10cSrcweir // multiple registrations for the same key :-) Show a warning ... and ignore the second item. 194cdf0e10cSrcweir // THROW_PARSEEXCEPTION("Command is registered for the same key more then once.") 195cdf0e10cSrcweir ::rtl::OUStringBuffer sMsg(256); 196cdf0e10cSrcweir sMsg.appendAscii("Double registration detected.\nCommand = \""); 197cdf0e10cSrcweir sMsg.append (sCommand ); 198cdf0e10cSrcweir sMsg.appendAscii("\"\nKeyCode = " ); 199cdf0e10cSrcweir sMsg.append ((sal_Int32)aEvent.KeyCode ); 200cdf0e10cSrcweir sMsg.appendAscii("\nModifiers = " ); 201cdf0e10cSrcweir sMsg.append ((sal_Int32)aEvent.Modifiers ); 202cdf0e10cSrcweir sMsg.appendAscii("\nIgnore this item!" ); 203cdf0e10cSrcweir LOG_WARNING("AcceleratorConfigurationReader::startElement()", U2B(sMsg.makeStringAndClear())) 204cdf0e10cSrcweir } 205cdf0e10cSrcweir #endif // ENABLE_WARNINGS 206cdf0e10cSrcweir } 207cdf0e10cSrcweir 208cdf0e10cSrcweir if (eElement == E_ELEMENT_ACCELERATORLIST) 209cdf0e10cSrcweir { 210cdf0e10cSrcweir if (m_bInsideAcceleratorList) 211cdf0e10cSrcweir THROW_PARSEEXCEPTION("An element \"accel:acceleratorlist\" cannot be used recursive.") 212cdf0e10cSrcweir m_bInsideAcceleratorList = sal_True; 213cdf0e10cSrcweir return; 214cdf0e10cSrcweir } 215cdf0e10cSrcweir } 216cdf0e10cSrcweir 217cdf0e10cSrcweir //----------------------------------------------- 218cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::endElement(const ::rtl::OUString& sElement) 219cdf0e10cSrcweir throw(css::xml::sax::SAXException, 220cdf0e10cSrcweir css::uno::RuntimeException ) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir EXMLElement eElement = AcceleratorConfigurationReader::implst_classifyElement(sElement); 223cdf0e10cSrcweir 224cdf0e10cSrcweir // Note: We handle "accel:item" before "accel:acceleratorlist" to perform this operation. 225*30acf5e8Spfg // Because an item occurs very often ... a list should occur one times only! 226cdf0e10cSrcweir if (eElement == E_ELEMENT_ITEM) 227cdf0e10cSrcweir { 228cdf0e10cSrcweir if (!m_bInsideAcceleratorItem) 229cdf0e10cSrcweir THROW_PARSEEXCEPTION("Found end element 'accel:item', but no start element.") 230cdf0e10cSrcweir m_bInsideAcceleratorItem = sal_False; 231cdf0e10cSrcweir } 232cdf0e10cSrcweir 233cdf0e10cSrcweir if (eElement == E_ELEMENT_ACCELERATORLIST) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir if (!m_bInsideAcceleratorList) 236cdf0e10cSrcweir THROW_PARSEEXCEPTION("Found end element 'accel:acceleratorlist', but no start element.") 237cdf0e10cSrcweir m_bInsideAcceleratorList = sal_False; 238cdf0e10cSrcweir } 239cdf0e10cSrcweir } 240cdf0e10cSrcweir 241cdf0e10cSrcweir //----------------------------------------------- 242cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::characters(const ::rtl::OUString&) 243cdf0e10cSrcweir throw(css::xml::sax::SAXException, 244cdf0e10cSrcweir css::uno::RuntimeException ) 245cdf0e10cSrcweir { 246cdf0e10cSrcweir } 247cdf0e10cSrcweir 248cdf0e10cSrcweir //----------------------------------------------- 249cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::ignorableWhitespace(const ::rtl::OUString&) 250cdf0e10cSrcweir throw(css::xml::sax::SAXException, 251cdf0e10cSrcweir css::uno::RuntimeException ) 252cdf0e10cSrcweir { 253cdf0e10cSrcweir } 254cdf0e10cSrcweir 255cdf0e10cSrcweir //----------------------------------------------- 256cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::processingInstruction(const ::rtl::OUString& /*sTarget*/, 257cdf0e10cSrcweir const ::rtl::OUString& /*sData*/ ) 258cdf0e10cSrcweir throw(css::xml::sax::SAXException, 259cdf0e10cSrcweir css::uno::RuntimeException ) 260cdf0e10cSrcweir { 261cdf0e10cSrcweir } 262cdf0e10cSrcweir 263cdf0e10cSrcweir //----------------------------------------------- 264cdf0e10cSrcweir void SAL_CALL AcceleratorConfigurationReader::setDocumentLocator(const css::uno::Reference< css::xml::sax::XLocator >& xLocator) 265cdf0e10cSrcweir throw(css::xml::sax::SAXException, 266cdf0e10cSrcweir css::uno::RuntimeException ) 267cdf0e10cSrcweir { 268cdf0e10cSrcweir m_xLocator = xLocator; 269cdf0e10cSrcweir } 270cdf0e10cSrcweir 271cdf0e10cSrcweir //----------------------------------------------- 272cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLElement AcceleratorConfigurationReader::implst_classifyElement(const ::rtl::OUString& sElement) 273cdf0e10cSrcweir { 274cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLElement eElement; 275cdf0e10cSrcweir 276cdf0e10cSrcweir if (sElement.equals(NS_ELEMENT_ACCELERATORLIST)) 277cdf0e10cSrcweir eElement = E_ELEMENT_ACCELERATORLIST; 278cdf0e10cSrcweir else 279cdf0e10cSrcweir if (sElement.equals(NS_ELEMENT_ITEM)) 280cdf0e10cSrcweir eElement = E_ELEMENT_ITEM; 281cdf0e10cSrcweir else 282cdf0e10cSrcweir throw css::uno::RuntimeException( 283cdf0e10cSrcweir DECLARE_ASCII("Unknown XML element detected!"), 284cdf0e10cSrcweir css::uno::Reference< css::xml::sax::XDocumentHandler >()); 285cdf0e10cSrcweir 286cdf0e10cSrcweir return eElement; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir //----------------------------------------------- 290cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLAttribute AcceleratorConfigurationReader::implst_classifyAttribute(const ::rtl::OUString& sAttribute) 291cdf0e10cSrcweir { 292cdf0e10cSrcweir AcceleratorConfigurationReader::EXMLAttribute eAttribute; 293cdf0e10cSrcweir 294cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_KEYCODE)) 295cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_KEYCODE; 296cdf0e10cSrcweir else 297cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_MOD_SHIFT)) 298cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_MOD_SHIFT; 299cdf0e10cSrcweir else 300cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD1)) 301cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_MOD_MOD1; 302cdf0e10cSrcweir else 303cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD2)) 304cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_MOD_MOD2; 305cdf0e10cSrcweir else 306cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_MOD_MOD3)) 307cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_MOD_MOD3; 308cdf0e10cSrcweir else 309cdf0e10cSrcweir if (sAttribute.equals(NS_ATTRIBUTE_URL)) 310cdf0e10cSrcweir eAttribute = E_ATTRIBUTE_URL; 311cdf0e10cSrcweir else 312cdf0e10cSrcweir throw css::uno::RuntimeException( 313cdf0e10cSrcweir DECLARE_ASCII("Unknown XML attribute detected!"), 314cdf0e10cSrcweir css::uno::Reference< css::xml::sax::XDocumentHandler >()); 315cdf0e10cSrcweir 316cdf0e10cSrcweir return eAttribute; 317cdf0e10cSrcweir } 318cdf0e10cSrcweir 319cdf0e10cSrcweir //----------------------------------------------- 320cdf0e10cSrcweir ::rtl::OUString AcceleratorConfigurationReader::implts_getErrorLineString() 321cdf0e10cSrcweir { 322cdf0e10cSrcweir if (!m_xLocator.is()) 323cdf0e10cSrcweir return DECLARE_ASCII("Error during parsing XML. (No further info available ...)"); 324cdf0e10cSrcweir 325cdf0e10cSrcweir ::rtl::OUStringBuffer sMsg(256); 326cdf0e10cSrcweir sMsg.appendAscii("Error during parsing XML in\nline = "); 327cdf0e10cSrcweir sMsg.append (m_xLocator->getLineNumber() ); 328cdf0e10cSrcweir sMsg.appendAscii("\ncolumn = " ); 329cdf0e10cSrcweir sMsg.append (m_xLocator->getColumnNumber() ); 330cdf0e10cSrcweir sMsg.appendAscii("." ); 331cdf0e10cSrcweir return sMsg.makeStringAndClear(); 332cdf0e10cSrcweir } 333cdf0e10cSrcweir 334cdf0e10cSrcweir } // namespace framework 335