1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "EventOASISTContext.hxx"
21 #include "EventMap.hxx"
22 #include "MutableAttrList.hxx"
23 #include <xmloff/xmlnmspe.hxx>
24 #include "ActionMapTypesOASIS.hxx"
25 #include "AttrTransformerAction.hxx"
26 #include "TransformerActions.hxx"
27 #include "TransformerBase.hxx"
28 #include <osl/diagnose.h>
30 // Used to parse Scripting Framework URLs
31 #include <com/sun/star/uri/UriReferenceFactory.hpp>
32 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
33 #include <comphelper/processfactory.hxx>
35 #include <unordered_map>
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::xml::sax
;
39 using namespace ::xmloff::token
;
41 class XMLTransformerOASISEventMap_Impl
:
42 public std::unordered_map
< NameKey_Impl
, OUString
,
43 NameHash_Impl
, NameHash_Impl
>
46 explicit XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry
*pInit
);
47 ~XMLTransformerOASISEventMap_Impl();
50 XMLTransformerOASISEventMap_Impl::XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry
*pInit
)
54 XMLTransformerOASISEventMap_Impl::key_type aKey
;
55 XMLTransformerOASISEventMap_Impl::mapped_type aData
;
56 while( pInit
->m_pOASISName
)
58 aKey
.m_nPrefix
= pInit
->m_nOASISPrefix
;
59 aKey
.m_aLocalName
= OUString::createFromAscii(pInit
->m_pOASISName
);
61 OSL_ENSURE( find( aKey
) == end(), "duplicate event map entry" );
63 aData
= OUString::createFromAscii(pInit
->m_pOOoName
);
65 XMLTransformerOASISEventMap_Impl::value_type
aVal( aKey
, aData
);
73 XMLTransformerOASISEventMap_Impl::~XMLTransformerOASISEventMap_Impl()
77 XMLEventOASISTransformerContext::XMLEventOASISTransformerContext(
78 XMLTransformerBase
& rImp
,
79 const OUString
& rQName
) :
80 XMLRenameElemTransformerContext( rImp
, rQName
,
81 rImp
.GetNamespaceMap().GetKeyByAttrName( rQName
), XML_EVENT
)
85 XMLEventOASISTransformerContext::~XMLEventOASISTransformerContext()
89 XMLTransformerOASISEventMap_Impl
90 *XMLEventOASISTransformerContext::CreateEventMap()
92 return new XMLTransformerOASISEventMap_Impl( aTransformerEventMap
);
95 XMLTransformerOASISEventMap_Impl
96 *XMLEventOASISTransformerContext::CreateFormEventMap()
98 return new XMLTransformerOASISEventMap_Impl( aFormTransformerEventMap
);
101 void XMLEventOASISTransformerContext::FlushEventMap(
102 XMLTransformerOASISEventMap_Impl
*p
)
107 OUString
XMLEventOASISTransformerContext::GetEventName(
109 const OUString
& rName
,
110 XMLTransformerOASISEventMap_Impl
& rMap
,
111 XMLTransformerOASISEventMap_Impl
*pMap2
)
113 XMLTransformerOASISEventMap_Impl::key_type
aKey( nPrefix
, rName
);
116 XMLTransformerOASISEventMap_Impl::const_iterator aIter
=
118 if( !(aIter
== pMap2
->end()) )
119 return (*aIter
).second
;
122 XMLTransformerOASISEventMap_Impl::const_iterator aIter
= rMap
.find( aKey
);
123 if( aIter
== rMap
.end() )
126 return (*aIter
).second
;
130 const OUString
& rAttrValue
,
131 OUString
* pName
, OUString
* pLocation
)
133 Reference
< css::uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
135 Reference
< css::uri::XUriReferenceFactory
> xFactory
= css::uri::UriReferenceFactory::create(xContext
);
137 Reference
< css::uri::XVndSunStarScriptUrl
> xUrl ( xFactory
->parse( rAttrValue
), UNO_QUERY
);
141 const OUString
& aLanguageKey
= GetXMLToken( XML_LANGUAGE
);
142 if ( xUrl
.is() && xUrl
->hasParameter( aLanguageKey
) )
144 OUString aLanguage
= xUrl
->getParameter( aLanguageKey
);
146 if ( aLanguage
.equalsIgnoreAsciiCase("basic") )
148 *pName
= xUrl
->getName();
151 xUrl
->getParameter( GetXMLToken( XML_LOCATION
) );
153 const OUString
& doc
= GetXMLToken( XML_DOCUMENT
);
155 if ( tmp
.equalsIgnoreAsciiCase( doc
) )
161 *pLocation
= GetXMLToken( XML_APPLICATION
);
170 void XMLEventOASISTransformerContext::StartElement(
171 const Reference
< XAttributeList
>& rAttrList
)
173 SAL_INFO("xmloff.transform", "XMLEventOASISTransformerContext::StartElement");
175 XMLTransformerActions
*pActions
=
176 GetTransformer().GetUserDefinedActions( OASIS_EVENT_ACTIONS
);
177 SAL_WARN_IF( pActions
== nullptr, "xmloff.transform", "got no actions" );
179 Reference
< XAttributeList
> xAttrList( rAttrList
);
180 XMLMutableAttributeList
*pMutableAttrList
= nullptr;
181 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
182 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
184 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
187 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
189 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
190 XMLTransformerActions::const_iterator aIter
=
191 pActions
->find( aKey
);
192 if( !(aIter
== pActions
->end() ) )
194 if( !pMutableAttrList
)
197 new XMLMutableAttributeList( xAttrList
);
198 xAttrList
= pMutableAttrList
;
200 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
201 switch( (*aIter
).second
.m_nActionType
)
203 case XML_ATACTION_HREF
:
205 OUString aName
, aLocation
;
207 bool bNeedsTransform
=
208 ParseURL( rAttrValue
, &aName
, &aLocation
);
210 if ( bNeedsTransform
)
212 pMutableAttrList
->RemoveAttributeByIndex( i
);
215 GetTransformer().GetNamespaceMap().GetQNameByKey(
216 XML_NAMESPACE_SCRIPT
,
217 ::xmloff::token::GetXMLToken( XML_MACRO_NAME
) ) );
219 pMutableAttrList
->AddAttribute( aAttrQName
, aName
);
221 sal_Int16 idx
= pMutableAttrList
->GetIndexByName(
222 GetTransformer().GetNamespaceMap().GetQNameByKey(
223 XML_NAMESPACE_SCRIPT
,
224 GetXMLToken( XML_LANGUAGE
) ) );
226 pMutableAttrList
->SetValueByIndex( idx
,
230 GetTransformer().GetNamespaceMap().GetQNameByKey(
231 XML_NAMESPACE_SCRIPT
,
232 GetXMLToken( XML_LOCATION
) ) );
234 pMutableAttrList
->AddAttribute( aLocQName
, aLocation
);
238 case XML_ATACTION_EVENT_NAME
:
240 // Check if the event belongs to a form or control by
241 // checking the 2nd ancestor element, f.i.:
242 // <form:button><form:event-listeners><form:event-listener>
243 const XMLTransformerContext
*pObjContext
=
244 GetTransformer().GetAncestorContext( 1 );
245 bool bForm
= pObjContext
&&
247 pObjContext
->HasNamespace(XML_NAMESPACE_FORM
);
248 pMutableAttrList
->SetValueByIndex( i
,
249 GetTransformer().GetEventName( rAttrValue
,
253 case XML_ATACTION_REMOVE_NAMESPACE_PREFIX
:
255 OUString
aAttrValue( rAttrValue
);
256 sal_uInt16 nValPrefix
=
257 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
258 if( GetTransformer().RemoveNamespacePrefix(
259 aAttrValue
, nValPrefix
) )
260 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
263 case XML_ATACTION_MACRO_NAME
:
265 OUString aName
, aLocation
;
266 bool bNeedsTransform
=
267 ParseURL( rAttrValue
, &aName
, &aLocation
);
269 if ( bNeedsTransform
)
271 pMutableAttrList
->SetValueByIndex( i
, aName
);
273 sal_Int16 idx
= pMutableAttrList
->GetIndexByName(
274 GetTransformer().GetNamespaceMap().GetQNameByKey(
275 XML_NAMESPACE_SCRIPT
,
276 GetXMLToken( XML_LANGUAGE
) ) );
278 pMutableAttrList
->SetValueByIndex( idx
,
282 GetTransformer().GetNamespaceMap().GetQNameByKey(
283 XML_NAMESPACE_SCRIPT
,
284 GetXMLToken( XML_LOCATION
) ) );
286 pMutableAttrList
->AddAttribute( aLocQName
, aLocation
);
290 const OUString
& rApp
= GetXMLToken( XML_APPLICATION
);
291 const OUString
& rDoc
= GetXMLToken( XML_DOCUMENT
);
293 if( rAttrValue
.getLength() > rApp
.getLength()+1 &&
294 rAttrValue
.copy(0,rApp
.getLength()).
295 equalsIgnoreAsciiCase( rApp
) &&
296 ':' == rAttrValue
[rApp
.getLength()] )
299 aAttrValue
= rAttrValue
.copy( rApp
.getLength()+1 );
301 else if( rAttrValue
.getLength() > rDoc
.getLength()+1 &&
302 rAttrValue
.copy(0,rDoc
.getLength()).
303 equalsIgnoreAsciiCase( rDoc
) &&
304 ':' == rAttrValue
[rDoc
.getLength()] )
307 aAttrValue
= rAttrValue
.copy( rDoc
.getLength()+1 );
309 if( !aAttrValue
.isEmpty() )
310 pMutableAttrList
->SetValueByIndex( i
,
312 if( !aLocation
.isEmpty() )
314 OUString
aAttrQName( GetTransformer().GetNamespaceMap().
315 GetQNameByKey( XML_NAMESPACE_SCRIPT
,
316 ::xmloff::token::GetXMLToken( XML_LOCATION
) ) );
317 pMutableAttrList
->AddAttribute( aAttrQName
, aLocation
);
319 aAttrQName
= GetTransformer().GetNamespaceMap().
320 GetQNameByKey( XML_NAMESPACE_SCRIPT
,
321 ::xmloff::token::GetXMLToken( XML_LIBRARY
) );
322 pMutableAttrList
->AddAttribute( aAttrQName
, aLocation
);
327 case XML_ATACTION_COPY
:
330 SAL_WARN( "xmloff.transform", "unknown action" );
336 XMLRenameElemTransformerContext::StartElement( xAttrList
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */