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