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 "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 :
29 : #ifndef OASIS_FILTER_OOO_1X
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>
34 : #endif
35 :
36 : #include <boost/unordered_map.hpp>
37 :
38 :
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::com::sun::star::xml::sax;
41 : using namespace ::xmloff::token;
42 :
43 : class XMLTransformerOASISEventMap_Impl:
44 : public ::boost::unordered_map< NameKey_Impl, OUString,
45 : NameHash_Impl, NameHash_Impl >
46 : {
47 : public:
48 : XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry *pInit );
49 : ~XMLTransformerOASISEventMap_Impl();
50 : };
51 :
52 0 : XMLTransformerOASISEventMap_Impl::XMLTransformerOASISEventMap_Impl( XMLTransformerEventMapEntry *pInit )
53 : {
54 0 : if( pInit )
55 : {
56 0 : XMLTransformerOASISEventMap_Impl::key_type aKey;
57 0 : XMLTransformerOASISEventMap_Impl::mapped_type aData;
58 0 : while( pInit->m_pOASISName )
59 : {
60 0 : aKey.m_nPrefix = pInit->m_nOASISPrefix;
61 0 : aKey.m_aLocalName = OUString::createFromAscii(pInit->m_pOASISName);
62 :
63 : OSL_ENSURE( find( aKey ) == end(), "duplicate event map entry" );
64 :
65 0 : aData = OUString::createFromAscii(pInit->m_pOOoName);
66 :
67 0 : XMLTransformerOASISEventMap_Impl::value_type aVal( aKey, aData );
68 :
69 0 : insert( aVal );
70 0 : ++pInit;
71 0 : }
72 : }
73 0 : }
74 :
75 0 : XMLTransformerOASISEventMap_Impl::~XMLTransformerOASISEventMap_Impl()
76 : {
77 0 : }
78 :
79 : // -----------------------------------------------------------------------------
80 :
81 0 : TYPEINIT1( XMLEventOASISTransformerContext, XMLRenameElemTransformerContext);
82 :
83 0 : XMLEventOASISTransformerContext::XMLEventOASISTransformerContext(
84 : XMLTransformerBase& rImp,
85 : const OUString& rQName ) :
86 : XMLRenameElemTransformerContext( rImp, rQName,
87 0 : rImp.GetNamespaceMap().GetKeyByAttrName( rQName ), XML_EVENT )
88 : {
89 0 : }
90 :
91 0 : XMLEventOASISTransformerContext::~XMLEventOASISTransformerContext()
92 : {
93 0 : }
94 :
95 : XMLTransformerOASISEventMap_Impl
96 0 : *XMLEventOASISTransformerContext::CreateEventMap()
97 : {
98 0 : return new XMLTransformerOASISEventMap_Impl( aTransformerEventMap );
99 : }
100 :
101 : XMLTransformerOASISEventMap_Impl
102 0 : *XMLEventOASISTransformerContext::CreateFormEventMap()
103 : {
104 0 : return new XMLTransformerOASISEventMap_Impl( aFormTransformerEventMap );
105 : }
106 :
107 44 : void XMLEventOASISTransformerContext::FlushEventMap(
108 : XMLTransformerOASISEventMap_Impl *p )
109 : {
110 44 : delete p;
111 44 : }
112 :
113 0 : OUString XMLEventOASISTransformerContext::GetEventName(
114 : sal_uInt16 nPrefix,
115 : const OUString& rName,
116 : XMLTransformerOASISEventMap_Impl& rMap,
117 : XMLTransformerOASISEventMap_Impl *pMap2)
118 : {
119 0 : XMLTransformerOASISEventMap_Impl::key_type aKey( nPrefix, rName );
120 0 : if( pMap2 )
121 : {
122 : XMLTransformerOASISEventMap_Impl::const_iterator aIter =
123 0 : pMap2->find( aKey );
124 0 : if( !(aIter == pMap2->end()) )
125 0 : return (*aIter).second;
126 : }
127 :
128 0 : XMLTransformerOASISEventMap_Impl::const_iterator aIter = rMap.find( aKey );
129 0 : if( aIter == rMap.end() )
130 0 : return rName;
131 : else
132 0 : return (*aIter).second;
133 : }
134 :
135 0 : bool ParseURLAsString(
136 : const OUString& rAttrValue,
137 : OUString* pName, OUString* pLocation )
138 : {
139 0 : OUString SCHEME( "vnd.sun.star.script:" );
140 :
141 0 : sal_Int32 params = rAttrValue.indexOf( '?' );
142 0 : if ( rAttrValue.indexOf( SCHEME ) != 0 || params < 0 )
143 : {
144 0 : return sal_False;
145 : }
146 :
147 0 : sal_Int32 start = SCHEME.getLength();
148 0 : *pName = rAttrValue.copy( start, params - start );
149 :
150 0 : OUString aToken;
151 0 : OUString aLanguage;
152 0 : params++;
153 0 : do
154 : {
155 0 : aToken = rAttrValue.getToken( 0, '&', params );
156 0 : sal_Int32 dummy = 0;
157 :
158 0 : if ( aToken.match( GetXMLToken( XML_LANGUAGE ) ) )
159 : {
160 0 : aLanguage = aToken.getToken( 1, '=', dummy );
161 : }
162 0 : else if ( aToken.match( GetXMLToken( XML_LOCATION ) ) )
163 : {
164 0 : OUString tmp = aToken.getToken( 1, '=', dummy );
165 0 : if ( tmp.equalsIgnoreAsciiCase( GetXMLToken( XML_DOCUMENT ) ) )
166 : {
167 0 : *pLocation = GetXMLToken( XML_DOCUMENT );
168 : }
169 : else
170 : {
171 0 : *pLocation = GetXMLToken( XML_APPLICATION );
172 0 : }
173 : }
174 0 : } while ( params >= 0 );
175 :
176 0 : if ( aLanguage.equalsIgnoreAsciiCase("basic") )
177 : {
178 0 : return sal_True;
179 : }
180 0 : return sal_False;
181 : }
182 :
183 0 : bool ParseURL(
184 : const OUString& rAttrValue,
185 : OUString* pName, OUString* pLocation )
186 : {
187 : #ifdef OASIS_FILTER_OOO_1X
188 : return ParseURLAsString( rAttrValue, pName, pLocation );
189 : #else
190 : Reference< com::sun::star::uno::XComponentContext >
191 0 : xContext = ::comphelper::getProcessComponentContext();
192 :
193 : Reference< com::sun::star::uri::XUriReferenceFactory > xFactory =
194 0 : com::sun::star::uri::UriReferenceFactory::create(xContext);
195 :
196 : Reference< com::sun::star::uri::XVndSunStarScriptUrl > xUrl (
197 0 : xFactory->parse( rAttrValue ), UNO_QUERY );
198 :
199 0 : if ( xUrl.is() )
200 : {
201 0 : OUString aLanguageKey = GetXMLToken( XML_LANGUAGE );
202 0 : if ( xUrl.is() && xUrl->hasParameter( aLanguageKey ) )
203 : {
204 0 : OUString aLanguage = xUrl->getParameter( aLanguageKey );
205 :
206 0 : if ( aLanguage.equalsIgnoreAsciiCase("basic") )
207 : {
208 0 : *pName = xUrl->getName();
209 :
210 : OUString tmp =
211 0 : xUrl->getParameter( GetXMLToken( XML_LOCATION ) );
212 :
213 0 : OUString doc = GetXMLToken( XML_DOCUMENT );
214 :
215 0 : if ( tmp.equalsIgnoreAsciiCase( doc ) )
216 : {
217 0 : *pLocation = doc;
218 : }
219 : else
220 : {
221 0 : *pLocation = GetXMLToken( XML_APPLICATION );
222 : }
223 0 : return sal_True;
224 0 : }
225 0 : }
226 : }
227 0 : return sal_False;
228 : #endif
229 : }
230 :
231 0 : void XMLEventOASISTransformerContext::StartElement(
232 : const Reference< XAttributeList >& rAttrList )
233 : {
234 : SAL_INFO("xmloff.transform", "XMLEventOASISTransformerContext::StartElement");
235 :
236 : XMLTransformerActions *pActions =
237 0 : GetTransformer().GetUserDefinedActions( OASIS_EVENT_ACTIONS );
238 : SAL_WARN_IF( pActions == NULL, "xmloff.transform", "got no actions" );
239 :
240 0 : Reference< XAttributeList > xAttrList( rAttrList );
241 0 : XMLMutableAttributeList *pMutableAttrList = 0;
242 0 : sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
243 0 : for( sal_Int16 i=0; i < nAttrCount; i++ )
244 : {
245 0 : const OUString& rAttrName = xAttrList->getNameByIndex( i );
246 0 : OUString aLocalName;
247 : sal_uInt16 nPrefix =
248 0 : GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
249 0 : &aLocalName );
250 0 : XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
251 : XMLTransformerActions::const_iterator aIter =
252 0 : pActions->find( aKey );
253 0 : if( !(aIter == pActions->end() ) )
254 : {
255 0 : if( !pMutableAttrList )
256 : {
257 : pMutableAttrList =
258 0 : new XMLMutableAttributeList( xAttrList );
259 0 : xAttrList = pMutableAttrList;
260 : }
261 0 : const OUString& rAttrValue = xAttrList->getValueByIndex( i );
262 0 : switch( (*aIter).second.m_nActionType )
263 : {
264 : case XML_ATACTION_HREF:
265 : {
266 0 : OUString aName, aLocation;
267 :
268 : bool bNeedsTransform =
269 0 : ParseURL( rAttrValue, &aName, &aLocation );
270 :
271 0 : if ( bNeedsTransform )
272 : {
273 0 : pMutableAttrList->RemoveAttributeByIndex( i );
274 :
275 : OUString aAttrQName(
276 0 : GetTransformer().GetNamespaceMap().GetQNameByKey(
277 : XML_NAMESPACE_SCRIPT,
278 0 : ::xmloff::token::GetXMLToken( XML_MACRO_NAME ) ) );
279 :
280 0 : pMutableAttrList->AddAttribute( aAttrQName, aName );
281 :
282 : sal_Int16 idx = pMutableAttrList->GetIndexByName(
283 0 : GetTransformer().GetNamespaceMap().GetQNameByKey(
284 : XML_NAMESPACE_SCRIPT,
285 0 : GetXMLToken( XML_LANGUAGE ) ) );
286 :
287 : pMutableAttrList->SetValueByIndex( idx,
288 0 : OUString("StarBasic") );
289 :
290 : OUString aLocQName(
291 0 : GetTransformer().GetNamespaceMap().GetQNameByKey(
292 : XML_NAMESPACE_SCRIPT,
293 0 : GetXMLToken( XML_LOCATION ) ) );
294 :
295 0 : pMutableAttrList->AddAttribute( aLocQName, aLocation );
296 0 : }
297 : }
298 0 : break;
299 : case XML_ATACTION_EVENT_NAME:
300 : {
301 : // Check if the event belongs to a form or control by
302 : // cehcking the 2nd ancestor element, f.i.:
303 : // <form:button><form:event-listeners><form:event-listener>
304 : const XMLTransformerContext *pObjContext =
305 0 : GetTransformer().GetAncestorContext( 1 );
306 0 : sal_Bool bForm = pObjContext &&
307 :
308 0 : pObjContext->HasNamespace(XML_NAMESPACE_FORM );
309 : pMutableAttrList->SetValueByIndex( i,
310 0 : GetTransformer().GetEventName( rAttrValue,
311 0 : bForm ) );
312 : }
313 0 : break;
314 : case XML_ATACTION_REMOVE_NAMESPACE_PREFIX:
315 : {
316 0 : OUString aAttrValue( rAttrValue );
317 : sal_uInt16 nValPrefix =
318 0 : static_cast<sal_uInt16>((*aIter).second.m_nParam1);
319 0 : if( GetTransformer().RemoveNamespacePrefix(
320 0 : aAttrValue, nValPrefix ) )
321 0 : pMutableAttrList->SetValueByIndex( i, aAttrValue );
322 : }
323 0 : break;
324 : case XML_ATACTION_MACRO_NAME:
325 : {
326 0 : OUString aName, aLocation;
327 : bool bNeedsTransform =
328 0 : ParseURL( rAttrValue, &aName, &aLocation );
329 :
330 0 : if ( bNeedsTransform )
331 : {
332 0 : pMutableAttrList->SetValueByIndex( i, aName );
333 :
334 : sal_Int16 idx = pMutableAttrList->GetIndexByName(
335 0 : GetTransformer().GetNamespaceMap().GetQNameByKey(
336 : XML_NAMESPACE_SCRIPT,
337 0 : GetXMLToken( XML_LANGUAGE ) ) );
338 :
339 : pMutableAttrList->SetValueByIndex( idx,
340 0 : OUString("StarBasic") );
341 :
342 : OUString aLocQName(
343 0 : GetTransformer().GetNamespaceMap().GetQNameByKey(
344 : XML_NAMESPACE_SCRIPT,
345 0 : GetXMLToken( XML_LOCATION ) ) );
346 :
347 0 : pMutableAttrList->AddAttribute( aLocQName, aLocation );
348 : }
349 : else
350 : {
351 0 : const OUString& rApp = GetXMLToken( XML_APPLICATION );
352 0 : const OUString& rDoc = GetXMLToken( XML_DOCUMENT );
353 0 : OUString aAttrValue;
354 0 : if( rAttrValue.getLength() > rApp.getLength()+1 &&
355 : rAttrValue.copy(0,rApp.getLength()).
356 0 : equalsIgnoreAsciiCase( rApp ) &&
357 0 : ':' == rAttrValue[rApp.getLength()] )
358 : {
359 0 : aLocation = rApp;
360 0 : aAttrValue = rAttrValue.copy( rApp.getLength()+1 );
361 : }
362 0 : else if( rAttrValue.getLength() > rDoc.getLength()+1 &&
363 : rAttrValue.copy(0,rDoc.getLength()).
364 0 : equalsIgnoreAsciiCase( rDoc ) &&
365 0 : ':' == rAttrValue[rDoc.getLength()] )
366 : {
367 0 : aLocation= rDoc;
368 0 : aAttrValue = rAttrValue.copy( rDoc.getLength()+1 );
369 : }
370 0 : if( !aAttrValue.isEmpty() )
371 : pMutableAttrList->SetValueByIndex( i,
372 0 : aAttrValue );
373 0 : if( !aLocation.isEmpty() )
374 : {
375 0 : OUString aAttrQName( GetTransformer().GetNamespaceMap().
376 : GetQNameByKey( XML_NAMESPACE_SCRIPT,
377 0 : ::xmloff::token::GetXMLToken( XML_LOCATION ) ) );
378 0 : pMutableAttrList->AddAttribute( aAttrQName, aLocation );
379 : // draw bug
380 0 : aAttrQName = GetTransformer().GetNamespaceMap().
381 : GetQNameByKey( XML_NAMESPACE_SCRIPT,
382 0 : ::xmloff::token::GetXMLToken( XML_LIBRARY ) );
383 0 : pMutableAttrList->AddAttribute( aAttrQName, aLocation );
384 0 : }
385 0 : }
386 : }
387 0 : break;
388 : case XML_ATACTION_COPY:
389 0 : break;
390 : default:
391 : SAL_WARN( "xmloff.transform", "unknown action" );
392 0 : break;
393 0 : }
394 : }
395 0 : }
396 :
397 0 : XMLRenameElemTransformerContext::StartElement( xAttrList );
398 0 : }
399 :
400 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|