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>
29 #include <sal/log.hxx>
31 // Used to parse Scripting Framework URLs
32 #include <com/sun/star/uri/UriReferenceFactory.hpp>
33 #include <com/sun/star/uri/XVndSunStarScriptUrl.hpp>
34 #include <comphelper/processfactory.hxx>
36 #include <unordered_map>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::xml::sax
;
40 using namespace ::xmloff::token
;
42 class XMLTransformerOASISEventMap_Impl
:
43 public std::unordered_map
< NameKey_Impl
, OUString
,
44 NameHash_Impl
, NameHash_Impl
>
47 explicit XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry
const *pInit
);
50 XMLTransformerOASISEventMap_Impl::XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry
const *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 XMLEventOASISTransformerContext::XMLEventOASISTransformerContext(
74 XMLTransformerBase
& rImp
,
75 const OUString
& rQName
) :
76 XMLRenameElemTransformerContext( rImp
, rQName
,
77 rImp
.GetNamespaceMap().GetKeyByAttrName( rQName
), XML_EVENT
)
81 XMLEventOASISTransformerContext::~XMLEventOASISTransformerContext()
85 XMLTransformerOASISEventMap_Impl
86 *XMLEventOASISTransformerContext::CreateEventMap()
88 return new XMLTransformerOASISEventMap_Impl( aTransformerEventMap
);
91 XMLTransformerOASISEventMap_Impl
92 *XMLEventOASISTransformerContext::CreateFormEventMap()
94 return new XMLTransformerOASISEventMap_Impl( aFormTransformerEventMap
);
97 void XMLEventOASISTransformerContext::FlushEventMap(
98 XMLTransformerOASISEventMap_Impl
*p
)
103 OUString
XMLEventOASISTransformerContext::GetEventName(
105 const OUString
& rName
,
106 XMLTransformerOASISEventMap_Impl
& rMap
,
107 XMLTransformerOASISEventMap_Impl
*pMap2
)
109 XMLTransformerOASISEventMap_Impl::key_type
aKey( nPrefix
, rName
);
112 XMLTransformerOASISEventMap_Impl::const_iterator aIter
=
114 if( aIter
!= pMap2
->end() )
115 return (*aIter
).second
;
118 XMLTransformerOASISEventMap_Impl::const_iterator aIter
= rMap
.find( aKey
);
119 if( aIter
== rMap
.end() )
122 return (*aIter
).second
;
125 static bool ParseURL(
126 const OUString
& rAttrValue
,
127 OUString
* pName
, OUString
* pLocation
)
129 Reference
< css::uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
131 Reference
< css::uri::XUriReferenceFactory
> xFactory
= css::uri::UriReferenceFactory::create(xContext
);
133 Reference
< css::uri::XVndSunStarScriptUrl
> xUrl ( xFactory
->parse( rAttrValue
), UNO_QUERY
);
137 const OUString
& aLanguageKey
= GetXMLToken( XML_LANGUAGE
);
138 if ( xUrl
.is() && xUrl
->hasParameter( aLanguageKey
) )
140 OUString aLanguage
= xUrl
->getParameter( aLanguageKey
);
142 if ( aLanguage
.equalsIgnoreAsciiCase("basic") )
144 *pName
= xUrl
->getName();
147 xUrl
->getParameter( GetXMLToken( XML_LOCATION
) );
149 const OUString
& doc
= GetXMLToken( XML_DOCUMENT
);
151 if ( tmp
.equalsIgnoreAsciiCase( doc
) )
157 *pLocation
= GetXMLToken( XML_APPLICATION
);
166 void XMLEventOASISTransformerContext::StartElement(
167 const Reference
< XAttributeList
>& rAttrList
)
169 SAL_INFO("xmloff.transform", "XMLEventOASISTransformerContext::StartElement");
171 XMLTransformerActions
*pActions
=
172 GetTransformer().GetUserDefinedActions( OASIS_EVENT_ACTIONS
);
173 SAL_WARN_IF( pActions
== nullptr, "xmloff.transform", "got no actions" );
175 Reference
< XAttributeList
> xAttrList( rAttrList
);
176 XMLMutableAttributeList
*pMutableAttrList
= nullptr;
177 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
178 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
180 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
183 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
185 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
186 XMLTransformerActions::const_iterator aIter
=
187 pActions
->find( aKey
);
188 if( aIter
!= pActions
->end() )
190 if( !pMutableAttrList
)
193 new XMLMutableAttributeList( xAttrList
);
194 xAttrList
= pMutableAttrList
;
196 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
197 switch( (*aIter
).second
.m_nActionType
)
199 case XML_ATACTION_HREF
:
201 OUString aName
, aLocation
;
203 bool bNeedsTransform
=
204 ParseURL( rAttrValue
, &aName
, &aLocation
);
206 if ( bNeedsTransform
)
208 pMutableAttrList
->RemoveAttributeByIndex( i
);
211 GetTransformer().GetNamespaceMap().GetQNameByKey(
212 XML_NAMESPACE_SCRIPT
,
213 ::xmloff::token::GetXMLToken( XML_MACRO_NAME
) ) );
215 pMutableAttrList
->AddAttribute( aAttrQName
, aName
);
217 sal_Int16 idx
= pMutableAttrList
->GetIndexByName(
218 GetTransformer().GetNamespaceMap().GetQNameByKey(
219 XML_NAMESPACE_SCRIPT
,
220 GetXMLToken( XML_LANGUAGE
) ) );
222 pMutableAttrList
->SetValueByIndex( idx
,
226 GetTransformer().GetNamespaceMap().GetQNameByKey(
227 XML_NAMESPACE_SCRIPT
,
228 GetXMLToken( XML_LOCATION
) ) );
230 pMutableAttrList
->AddAttribute( aLocQName
, aLocation
);
234 case XML_ATACTION_EVENT_NAME
:
236 // Check if the event belongs to a form or control by
237 // checking the 2nd ancestor element, f.i.:
238 // <form:button><form:event-listeners><form:event-listener>
239 const XMLTransformerContext
*pObjContext
=
240 GetTransformer().GetAncestorContext( 1 );
241 bool bForm
= pObjContext
&&
243 pObjContext
->HasNamespace(XML_NAMESPACE_FORM
);
244 pMutableAttrList
->SetValueByIndex( i
,
245 GetTransformer().GetEventName( rAttrValue
,
249 case XML_ATACTION_REMOVE_NAMESPACE_PREFIX
:
251 OUString
aAttrValue( rAttrValue
);
252 sal_uInt16 nValPrefix
=
253 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
254 if( GetTransformer().RemoveNamespacePrefix(
255 aAttrValue
, nValPrefix
) )
256 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
259 case XML_ATACTION_MACRO_NAME
:
261 OUString aName
, aLocation
;
262 bool bNeedsTransform
=
263 ParseURL( rAttrValue
, &aName
, &aLocation
);
265 if ( bNeedsTransform
)
267 pMutableAttrList
->SetValueByIndex( i
, aName
);
269 sal_Int16 idx
= pMutableAttrList
->GetIndexByName(
270 GetTransformer().GetNamespaceMap().GetQNameByKey(
271 XML_NAMESPACE_SCRIPT
,
272 GetXMLToken( XML_LANGUAGE
) ) );
274 pMutableAttrList
->SetValueByIndex( idx
,
278 GetTransformer().GetNamespaceMap().GetQNameByKey(
279 XML_NAMESPACE_SCRIPT
,
280 GetXMLToken( XML_LOCATION
) ) );
282 pMutableAttrList
->AddAttribute( aLocQName
, aLocation
);
286 const OUString
& rApp
= GetXMLToken( XML_APPLICATION
);
287 const OUString
& rDoc
= GetXMLToken( XML_DOCUMENT
);
289 if( rAttrValue
.getLength() > rApp
.getLength()+1 &&
290 rAttrValue
.copy(0,rApp
.getLength()).
291 equalsIgnoreAsciiCase( rApp
) &&
292 ':' == rAttrValue
[rApp
.getLength()] )
295 aAttrValue
= rAttrValue
.copy( rApp
.getLength()+1 );
297 else if( rAttrValue
.getLength() > rDoc
.getLength()+1 &&
298 rAttrValue
.copy(0,rDoc
.getLength()).
299 equalsIgnoreAsciiCase( rDoc
) &&
300 ':' == rAttrValue
[rDoc
.getLength()] )
303 aAttrValue
= rAttrValue
.copy( rDoc
.getLength()+1 );
305 if( !aAttrValue
.isEmpty() )
306 pMutableAttrList
->SetValueByIndex( i
,
308 if( !aLocation
.isEmpty() )
310 OUString
aAttrQName( GetTransformer().GetNamespaceMap().
311 GetQNameByKey( XML_NAMESPACE_SCRIPT
,
312 ::xmloff::token::GetXMLToken( XML_LOCATION
) ) );
313 pMutableAttrList
->AddAttribute( aAttrQName
, aLocation
);
315 aAttrQName
= GetTransformer().GetNamespaceMap().
316 GetQNameByKey( XML_NAMESPACE_SCRIPT
,
317 ::xmloff::token::GetXMLToken( XML_LIBRARY
) );
318 pMutableAttrList
->AddAttribute( aAttrQName
, aLocation
);
323 case XML_ATACTION_COPY
:
326 SAL_WARN( "xmloff.transform", "unknown action" );
332 XMLRenameElemTransformerContext::StartElement( xAttrList
);
335 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */