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 "EventOOoTContext.hxx"
21 #include "EventMap.hxx"
22 #include "MutableAttrList.hxx"
23 #include <xmloff/xmlnmspe.hxx>
24 #include "ActionMapTypesOOo.hxx"
25 #include "AttrTransformerAction.hxx"
26 #include "TransformerActions.hxx"
27 #include "TransformerBase.hxx"
28 #include <rtl/ustrbuf.hxx>
29 #include <osl/diagnose.h>
31 #include <unordered_map>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::xml::sax
;
35 using namespace ::xmloff::token
;
37 class XMLTransformerOOoEventMap_Impl
:
38 public std::unordered_map
< OUString
, NameKey_Impl
, OUStringHash
>
42 void AddMap( XMLTransformerEventMapEntry
*pInit
);
44 XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry
*pInit
,
45 XMLTransformerEventMapEntry
*pInit2
);
46 ~XMLTransformerOOoEventMap_Impl();
49 void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry
*pInit
)
51 XMLTransformerOOoEventMap_Impl::key_type aKey
;
52 XMLTransformerOOoEventMap_Impl::mapped_type aData
;
53 while( pInit
->m_pOOoName
)
55 aKey
= OUString::createFromAscii(pInit
->m_pOOoName
);
57 OSL_ENSURE( find( aKey
) == end(), "duplicate event map entry" );
59 aData
.m_nPrefix
= pInit
->m_nOASISPrefix
;
60 aData
.m_aLocalName
= OUString::createFromAscii(pInit
->m_pOASISName
);
62 XMLTransformerOOoEventMap_Impl::value_type
aVal( aKey
, aData
);
64 if( !insert( aVal
).second
)
66 OSL_FAIL( "duplicate OOo event name extry" );
73 XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl(
74 XMLTransformerEventMapEntry
*pInit
,
75 XMLTransformerEventMapEntry
*pInit2
)
83 XMLTransformerOOoEventMap_Impl::~XMLTransformerOOoEventMap_Impl()
87 TYPEINIT1( XMLEventOOoTransformerContext
, XMLPersElemContentTContext
);
89 XMLEventOOoTransformerContext::XMLEventOOoTransformerContext(
90 XMLTransformerBase
& rImp
,
91 const OUString
& rQName
,
93 XMLPersElemContentTContext( rImp
, rQName
,
94 rImp
.GetNamespaceMap().GetKeyByAttrName( rQName
), XML_EVENT_LISTENER
),
95 m_bPersistent( bPersistent
)
99 XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext()
103 XMLTransformerOOoEventMap_Impl
104 *XMLEventOOoTransformerContext::CreateEventMap()
106 return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap
,
107 aFormTransformerEventMap
);
110 void XMLEventOOoTransformerContext::FlushEventMap(
111 XMLTransformerOOoEventMap_Impl
*p
)
116 sal_uInt16
XMLEventOOoTransformerContext::GetEventName(
117 const OUString
& rName
,
119 XMLTransformerOOoEventMap_Impl
& rMap
)
121 XMLTransformerOOoEventMap_Impl::key_type
aKey( rName
);
122 XMLTransformerOOoEventMap_Impl::const_iterator aIter
= rMap
.find( aKey
);
123 if( aIter
== rMap
.end() )
126 return XML_NAMESPACE_UNKNOWN
;
130 rNewName
= (*aIter
).second
.m_aLocalName
;
131 return (*aIter
).second
.m_nPrefix
;
135 void XMLEventOOoTransformerContext::StartElement(
136 const Reference
< XAttributeList
>& rAttrList
)
138 XMLTransformerActions
*pActions
=
139 GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS
);
140 OSL_ENSURE( pActions
, "go no actions" );
142 OUString aLocation
, aMacroName
;
143 sal_Int16 nMacroName
= -1;
144 Reference
< XAttributeList
> xAttrList( rAttrList
);
145 XMLMutableAttributeList
*pMutableAttrList
= 0;
146 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
147 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
149 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
152 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
154 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
155 XMLTransformerActions::const_iterator aIter
=
156 pActions
->find( aKey
);
157 if( !(aIter
== pActions
->end() ) )
159 if( !pMutableAttrList
)
162 new XMLMutableAttributeList( xAttrList
);
163 xAttrList
= pMutableAttrList
;
165 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
166 switch( (*aIter
).second
.m_nActionType
)
168 case XML_ATACTION_HREF
:
171 case XML_ATACTION_EVENT_NAME
:
172 pMutableAttrList
->SetValueByIndex( i
,
173 GetTransformer().GetEventName( rAttrValue
) );
175 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
177 OUString
aAttrValue( rAttrValue
);
178 sal_uInt16 nValPrefix
=
179 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
180 if( GetTransformer().AddNamespacePrefix( aAttrValue
,
182 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
185 case XML_ATACTION_MACRO_LOCATION
:
186 aLocation
= rAttrValue
;
187 pMutableAttrList
->RemoveAttributeByIndex( i
);
191 case XML_ATACTION_MACRO_NAME
:
192 aMacroName
= rAttrValue
;
195 case XML_ATACTION_COPY
:
198 OSL_ENSURE( false, "unknown action" );
204 if( nMacroName
!= -1 && !aLocation
.isEmpty() )
206 if( !IsXMLToken( aLocation
, XML_APPLICATION
) )
207 aLocation
= GetXMLToken( XML_DOCUMENT
);
208 OUStringBuffer
sTmp( aLocation
.getLength() + aMacroName
.getLength() + 1 );
211 sTmp
.append( aMacroName
);
212 pMutableAttrList
->SetValueByIndex( nMacroName
,
213 sTmp
.makeStringAndClear() );
217 XMLPersElemContentTContext::StartElement( xAttrList
);
219 GetTransformer().GetDocHandler()->startElement( GetExportQName(),
223 void XMLEventOOoTransformerContext::EndElement()
226 XMLPersElemContentTContext::EndElement();
228 GetTransformer().GetDocHandler()->endElement( GetExportQName() );
231 XMLTransformerContext
* XMLEventOOoTransformerContext::CreateChildContext(
233 const OUString
& rLocalName
,
234 const OUString
& rQName
,
235 const Reference
< XAttributeList
>& xAttrList
)
238 return XMLPersElemContentTContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
240 return XMLTransformerContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
243 bool XMLEventOOoTransformerContext::IsPersistent() const
245 return m_bPersistent
;
248 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */