/src/libreoffice/xmloff/source/transform/EventOOoTContext.cxx
Line | Count | Source |
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 "EventOOoTContext.hxx" |
21 | | #include "EventMap.hxx" |
22 | | #include "MutableAttrList.hxx" |
23 | | #include "ActionMapTypesOOo.hxx" |
24 | | #include "AttrTransformerAction.hxx" |
25 | | #include "TransformerActions.hxx" |
26 | | #include "TransformerBase.hxx" |
27 | | #include <osl/diagnose.h> |
28 | | |
29 | | #include <unordered_map> |
30 | | |
31 | | using namespace ::com::sun::star::uno; |
32 | | using namespace ::com::sun::star::xml::sax; |
33 | | using namespace ::xmloff::token; |
34 | | |
35 | | class XMLTransformerOOoEventMap_Impl: |
36 | | public std::unordered_map< OUString, NameKey_Impl > |
37 | | { |
38 | | public: |
39 | | |
40 | | void AddMap( XMLTransformerEventMapEntry const *pInit ); |
41 | | |
42 | | XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry const *pInit, |
43 | | XMLTransformerEventMapEntry const *pInit2 ); |
44 | | }; |
45 | | |
46 | | void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry const *pInit ) |
47 | 0 | { |
48 | 0 | XMLTransformerOOoEventMap_Impl::key_type aKey; |
49 | 0 | XMLTransformerOOoEventMap_Impl::mapped_type aData; |
50 | 0 | while( !pInit->m_pOOoName.isEmpty() ) |
51 | 0 | { |
52 | 0 | aKey = pInit->m_pOOoName; |
53 | |
|
54 | 0 | OSL_ENSURE( find( aKey ) == end(), "duplicate event map entry" ); |
55 | |
|
56 | 0 | aData.m_nPrefix = pInit->m_nOASISPrefix; |
57 | 0 | aData.m_aLocalName = pInit->m_pOASISName; |
58 | |
|
59 | 0 | XMLTransformerOOoEventMap_Impl::value_type aVal( aKey, aData ); |
60 | |
|
61 | 0 | if( !insert( aVal ).second ) |
62 | 0 | { |
63 | 0 | OSL_FAIL( "duplicate OOo event name entry" ); |
64 | 0 | } |
65 | |
|
66 | 0 | ++pInit; |
67 | 0 | } |
68 | 0 | } |
69 | | |
70 | | XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl( |
71 | | XMLTransformerEventMapEntry const *pInit, |
72 | | XMLTransformerEventMapEntry const *pInit2 ) |
73 | 0 | { |
74 | 0 | if( pInit ) |
75 | 0 | AddMap( pInit ); |
76 | 0 | if( pInit2 ) |
77 | 0 | AddMap( pInit2 ); |
78 | 0 | } |
79 | | |
80 | | XMLEventOOoTransformerContext::XMLEventOOoTransformerContext( |
81 | | XMLTransformerBase& rImp, |
82 | | const OUString& rQName, |
83 | | bool bPersistent ) : |
84 | 0 | XMLPersElemContentTContext( rImp, rQName, |
85 | 0 | rImp.GetNamespaceMap().GetKeyByAttrValueQName(rQName, nullptr), |
86 | 0 | XML_EVENT_LISTENER), |
87 | 0 | m_bPersistent( bPersistent ) |
88 | 0 | { |
89 | 0 | } |
90 | | |
91 | | XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext() |
92 | 0 | { |
93 | 0 | } |
94 | | |
95 | | XMLTransformerOOoEventMap_Impl |
96 | | *XMLEventOOoTransformerContext::CreateEventMap() |
97 | 0 | { |
98 | 0 | return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap, |
99 | 0 | aFormTransformerEventMap ); |
100 | 0 | } |
101 | | |
102 | | void XMLEventOOoTransformerContext::FlushEventMap( |
103 | | XMLTransformerOOoEventMap_Impl *p ) |
104 | 0 | { |
105 | 0 | delete p; |
106 | 0 | } |
107 | | |
108 | | sal_uInt16 XMLEventOOoTransformerContext::GetEventName( |
109 | | const OUString& rName, |
110 | | OUString& rNewName, |
111 | | XMLTransformerOOoEventMap_Impl& rMap ) |
112 | 0 | { |
113 | 0 | const XMLTransformerOOoEventMap_Impl::key_type& aKey( rName ); |
114 | 0 | XMLTransformerOOoEventMap_Impl::const_iterator aIter = rMap.find( aKey ); |
115 | 0 | if( aIter == rMap.end() ) |
116 | 0 | { |
117 | 0 | rNewName = rName; |
118 | 0 | return XML_NAMESPACE_UNKNOWN; |
119 | 0 | } |
120 | 0 | else |
121 | 0 | { |
122 | 0 | rNewName = (*aIter).second.m_aLocalName; |
123 | 0 | return (*aIter).second.m_nPrefix; |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | void XMLEventOOoTransformerContext::StartElement( |
128 | | const Reference< XAttributeList >& rAttrList ) |
129 | 0 | { |
130 | 0 | XMLTransformerActions *pActions = |
131 | 0 | GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS ); |
132 | 0 | OSL_ENSURE( pActions, "go no actions" ); |
133 | |
|
134 | 0 | OUString aLocation, aMacroName; |
135 | 0 | sal_Int16 nMacroName = -1; |
136 | 0 | Reference< XAttributeList > xAttrList( rAttrList ); |
137 | 0 | rtl::Reference<XMLMutableAttributeList> pMutableAttrList; |
138 | 0 | sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0; |
139 | 0 | for( sal_Int16 i=0; i < nAttrCount; i++ ) |
140 | 0 | { |
141 | 0 | const OUString aAttrName = xAttrList->getNameByIndex( i ); |
142 | 0 | OUString aLocalName; |
143 | 0 | sal_uInt16 nPrefix = |
144 | 0 | GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName, |
145 | 0 | &aLocalName ); |
146 | 0 | XMLTransformerActions::key_type aKey( nPrefix, aLocalName ); |
147 | 0 | XMLTransformerActions::const_iterator aIter = |
148 | 0 | pActions->find( aKey ); |
149 | 0 | if( aIter != pActions->end() ) |
150 | 0 | { |
151 | 0 | if( !pMutableAttrList ) |
152 | 0 | { |
153 | 0 | pMutableAttrList = |
154 | 0 | new XMLMutableAttributeList( xAttrList ); |
155 | 0 | xAttrList = pMutableAttrList; |
156 | 0 | } |
157 | 0 | const OUString aAttrValue = xAttrList->getValueByIndex( i ); |
158 | 0 | switch( (*aIter).second.m_nActionType ) |
159 | 0 | { |
160 | 0 | case XML_ATACTION_HREF: |
161 | | // TODO |
162 | 0 | break; |
163 | 0 | case XML_ATACTION_EVENT_NAME: |
164 | 0 | pMutableAttrList->SetValueByIndex( i, |
165 | 0 | GetTransformer().GetEventName( aAttrValue ) ); |
166 | 0 | break; |
167 | 0 | case XML_ATACTION_ADD_NAMESPACE_PREFIX: |
168 | 0 | { |
169 | 0 | OUString aAttrValue2( aAttrValue ); |
170 | 0 | sal_uInt16 nValPrefix = |
171 | 0 | static_cast<sal_uInt16>((*aIter).second.m_nParam1); |
172 | 0 | GetTransformer().AddNamespacePrefix( aAttrValue2, |
173 | 0 | nValPrefix ); |
174 | 0 | pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
175 | 0 | } |
176 | 0 | break; |
177 | 0 | case XML_ATACTION_MACRO_LOCATION: |
178 | 0 | aLocation = aAttrValue; |
179 | 0 | pMutableAttrList->RemoveAttributeByIndex( i ); |
180 | 0 | --i; |
181 | 0 | --nAttrCount; |
182 | 0 | break; |
183 | 0 | case XML_ATACTION_MACRO_NAME: |
184 | 0 | aMacroName = aAttrValue; |
185 | 0 | nMacroName = i; |
186 | 0 | break; |
187 | 0 | case XML_ATACTION_COPY: |
188 | 0 | break; |
189 | 0 | default: |
190 | 0 | OSL_ENSURE( false, "unknown action" ); |
191 | 0 | break; |
192 | 0 | } |
193 | 0 | } |
194 | 0 | } |
195 | | |
196 | 0 | if( nMacroName != -1 && !aLocation.isEmpty() ) |
197 | 0 | { |
198 | 0 | if( !IsXMLToken( aLocation, XML_APPLICATION ) ) |
199 | 0 | aLocation = GetXMLToken( XML_DOCUMENT ); |
200 | 0 | OUString sTmp = aLocation + ":" + aMacroName; |
201 | 0 | pMutableAttrList->SetValueByIndex( nMacroName, sTmp ); |
202 | 0 | } |
203 | |
|
204 | 0 | if( m_bPersistent ) |
205 | 0 | XMLPersElemContentTContext::StartElement( xAttrList ); |
206 | 0 | else |
207 | 0 | GetTransformer().GetDocHandler()->startElement( GetExportQName(), xAttrList ); |
208 | 0 | } |
209 | | |
210 | | void XMLEventOOoTransformerContext::EndElement() |
211 | 0 | { |
212 | 0 | if( m_bPersistent ) |
213 | 0 | XMLPersElemContentTContext::EndElement(); |
214 | 0 | else |
215 | 0 | GetTransformer().GetDocHandler()->endElement( GetExportQName() ); |
216 | 0 | } |
217 | | |
218 | | rtl::Reference<XMLTransformerContext> XMLEventOOoTransformerContext::CreateChildContext( |
219 | | sal_uInt16 nPrefix, |
220 | | const OUString& rLocalName, |
221 | | const OUString& rQName, |
222 | | const Reference< XAttributeList >& xAttrList ) |
223 | 0 | { |
224 | 0 | if( m_bPersistent ) |
225 | 0 | return XMLPersElemContentTContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList); |
226 | 0 | else |
227 | 0 | return XMLTransformerContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList); |
228 | 0 | } |
229 | | |
230 | | bool XMLEventOOoTransformerContext::IsPersistent() const |
231 | 0 | { |
232 | 0 | return m_bPersistent; |
233 | 0 | } |
234 | | |
235 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |