Line data Source code
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 <unotools/xmlaccelcfg.hxx>
21 :
22 : #include <vector>
23 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
24 : #include <cppuhelper/implbase1.hxx>
25 : #include <cppuhelper/queryinterface.hxx>
26 :
27 : using namespace com::sun::star::uno;
28 : using namespace com::sun::star::xml::sax;
29 :
30 : #define ELEMENT_ACCELERATORLIST "acceleratorlist"
31 : #define ELEMENT_ACCELERATORITEM "item"
32 :
33 : #define ATTRIBUTE_KEYCODE "code"
34 : #define ATTRIBUTE_MODIFIER "modifier"
35 : #define ATTRIBUTE_URL "url"
36 :
37 0 : Any SAL_CALL OReadAccelatorDocumentHandler::queryInterface( const Type & rType ) throw( RuntimeException, std::exception )
38 : {
39 0 : Any a = ::cppu::queryInterface( rType ,(static_cast< XDocumentHandler* >(this)));
40 0 : if ( a.hasValue() )
41 0 : return a;
42 : else
43 0 : return OWeakObject::queryInterface( rType );
44 : }
45 :
46 0 : void SAL_CALL OReadAccelatorDocumentHandler::ignorableWhitespace(
47 : const OUString& )
48 : throw( SAXException, RuntimeException, std::exception )
49 : {
50 0 : }
51 :
52 0 : void SAL_CALL OReadAccelatorDocumentHandler::processingInstruction(
53 : const OUString&, const OUString& )
54 : throw( SAXException, RuntimeException, std::exception )
55 : {
56 0 : }
57 :
58 0 : void SAL_CALL OReadAccelatorDocumentHandler::setDocumentLocator(
59 : const Reference< XLocator > &xLocator)
60 : throw( SAXException, RuntimeException, std::exception )
61 : {
62 0 : m_xLocator = xLocator;
63 0 : }
64 :
65 0 : OUString OReadAccelatorDocumentHandler::getErrorLineString()
66 : {
67 : char buffer[32];
68 :
69 0 : if ( m_xLocator.is() )
70 : {
71 0 : return OUString::createFromAscii( buffer );
72 : }
73 : else
74 0 : return OUString();
75 : }
76 :
77 0 : void SAL_CALL OReadAccelatorDocumentHandler::startDocument()
78 : throw ( SAXException, RuntimeException, std::exception )
79 : {
80 0 : }
81 :
82 0 : void SAL_CALL OReadAccelatorDocumentHandler::endDocument()
83 : throw( SAXException, RuntimeException, std::exception )
84 : {
85 0 : if ( m_nElementDepth > 0 )
86 : {
87 0 : OUString aErrorMessage = getErrorLineString();
88 0 : aErrorMessage += "A closing element is missing!";
89 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
90 : }
91 0 : }
92 :
93 0 : void SAL_CALL OReadAccelatorDocumentHandler::startElement(
94 : const OUString& aElementName, const Reference< XAttributeList > &xAttrList )
95 : throw( SAXException, RuntimeException, std::exception )
96 : {
97 0 : m_nElementDepth++;
98 :
99 0 : if ( aElementName == ELEMENT_ACCELERATORLIST )
100 : {
101 : // acceleratorlist
102 0 : if ( m_bAcceleratorMode )
103 : {
104 0 : OUString aErrorMessage = getErrorLineString();
105 0 : aErrorMessage += "Accelerator list used twice!";
106 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
107 : }
108 : else
109 0 : m_bAcceleratorMode = true;
110 : }
111 0 : else if ( aElementName == ELEMENT_ACCELERATORITEM )
112 : {
113 : // accelerator item
114 0 : if ( !m_bAcceleratorMode )
115 : {
116 0 : OUString aErrorMessage = getErrorLineString();
117 0 : aErrorMessage += "Accelerator list element has to be used before!";
118 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
119 : }
120 : else
121 : {
122 : // read accelerator item
123 0 : m_bItemCloseExpected = true;
124 :
125 0 : SvtAcceleratorConfigItem aItem;
126 :
127 : // read attributes for accelerator
128 0 : for ( sal_Int16 i=0; i< xAttrList->getLength(); i++ )
129 : {
130 0 : OUString aName = xAttrList->getNameByIndex( i );
131 0 : OUString aValue = xAttrList->getValueByIndex( i );
132 :
133 0 : if ( aName == ATTRIBUTE_URL )
134 0 : aItem.aCommand = aValue;
135 0 : else if ( aName == ATTRIBUTE_MODIFIER )
136 0 : aItem.nModifier = (sal_uInt16)aValue.toInt32();
137 0 : else if ( aName == ATTRIBUTE_KEYCODE )
138 0 : aItem.nCode = (sal_uInt16)aValue.toInt32();
139 0 : }
140 :
141 0 : m_aReadAcceleratorList.push_back( aItem );
142 : }
143 : }
144 : else
145 : {
146 0 : OUString aErrorMessage = getErrorLineString();
147 0 : aErrorMessage += "Unknown element found!";
148 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
149 : }
150 0 : }
151 :
152 0 : void SAL_CALL OReadAccelatorDocumentHandler::characters(const OUString&)
153 : throw( SAXException, RuntimeException, std::exception )
154 : {
155 0 : }
156 :
157 0 : void SAL_CALL OReadAccelatorDocumentHandler::endElement( const OUString& aName )
158 : throw( SAXException, RuntimeException, std::exception )
159 : {
160 0 : m_nElementDepth--;
161 :
162 0 : if ( aName == ELEMENT_ACCELERATORLIST )
163 : {
164 : // acceleratorlist
165 0 : if ( !m_bAcceleratorMode )
166 : {
167 0 : OUString aErrorMessage = getErrorLineString();
168 0 : aErrorMessage += "Accelerator list used twice!";
169 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
170 : }
171 : }
172 0 : else if ( aName == ELEMENT_ACCELERATORITEM )
173 : {
174 0 : if ( !m_bItemCloseExpected )
175 : {
176 0 : OUString aErrorMessage = getErrorLineString();
177 0 : aErrorMessage += "Closing accelerator item element expected!";
178 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
179 : }
180 : }
181 : else
182 : {
183 0 : OUString aErrorMessage = getErrorLineString();
184 0 : aErrorMessage += "Unknown closing element found!";
185 0 : throw SAXException( aErrorMessage, Reference< XInterface >(), Any() );
186 : }
187 0 : }
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|