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 "ActionMapTypesOOo.hxx"
24 #include "AttrTransformerAction.hxx"
25 #include "TransformerActions.hxx"
26 #include "TransformerBase.hxx"
27 #include <osl/diagnose.h>
29 #include <unordered_map>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::xml::sax
;
33 using namespace ::xmloff::token
;
35 class XMLTransformerOOoEventMap_Impl
:
36 public std::unordered_map
< OUString
, NameKey_Impl
>
40 void AddMap( XMLTransformerEventMapEntry
const *pInit
);
42 XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry
const *pInit
,
43 XMLTransformerEventMapEntry
const *pInit2
);
46 void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry
const *pInit
)
48 XMLTransformerOOoEventMap_Impl::key_type aKey
;
49 XMLTransformerOOoEventMap_Impl::mapped_type aData
;
50 while( !pInit
->m_pOOoName
.isEmpty() )
52 aKey
= pInit
->m_pOOoName
;
54 OSL_ENSURE( find( aKey
) == end(), "duplicate event map entry" );
56 aData
.m_nPrefix
= pInit
->m_nOASISPrefix
;
57 aData
.m_aLocalName
= pInit
->m_pOASISName
;
59 XMLTransformerOOoEventMap_Impl::value_type
aVal( aKey
, aData
);
61 if( !insert( aVal
).second
)
63 OSL_FAIL( "duplicate OOo event name entry" );
70 XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl(
71 XMLTransformerEventMapEntry
const *pInit
,
72 XMLTransformerEventMapEntry
const *pInit2
)
80 XMLEventOOoTransformerContext::XMLEventOOoTransformerContext(
81 XMLTransformerBase
& rImp
,
82 const OUString
& rQName
,
84 XMLPersElemContentTContext( rImp
, rQName
,
85 rImp
.GetNamespaceMap().GetKeyByAttrValueQName(rQName
, nullptr),
87 m_bPersistent( bPersistent
)
91 XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext()
95 XMLTransformerOOoEventMap_Impl
96 *XMLEventOOoTransformerContext::CreateEventMap()
98 return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap
,
99 aFormTransformerEventMap
);
102 void XMLEventOOoTransformerContext::FlushEventMap(
103 XMLTransformerOOoEventMap_Impl
*p
)
108 sal_uInt16
XMLEventOOoTransformerContext::GetEventName(
109 const OUString
& rName
,
111 XMLTransformerOOoEventMap_Impl
& rMap
)
113 const XMLTransformerOOoEventMap_Impl::key_type
& aKey( rName
);
114 XMLTransformerOOoEventMap_Impl::const_iterator aIter
= rMap
.find( aKey
);
115 if( aIter
== rMap
.end() )
118 return XML_NAMESPACE_UNKNOWN
;
122 rNewName
= (*aIter
).second
.m_aLocalName
;
123 return (*aIter
).second
.m_nPrefix
;
127 void XMLEventOOoTransformerContext::StartElement(
128 const Reference
< XAttributeList
>& rAttrList
)
130 XMLTransformerActions
*pActions
=
131 GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS
);
132 OSL_ENSURE( pActions
, "go no actions" );
134 OUString aLocation
, aMacroName
;
135 sal_Int16 nMacroName
= -1;
136 Reference
< XAttributeList
> xAttrList( rAttrList
);
137 rtl::Reference
<XMLMutableAttributeList
> pMutableAttrList
;
138 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
139 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
141 const OUString aAttrName
= xAttrList
->getNameByIndex( i
);
144 GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName
,
146 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
147 XMLTransformerActions::const_iterator aIter
=
148 pActions
->find( aKey
);
149 if( aIter
!= pActions
->end() )
151 if( !pMutableAttrList
)
154 new XMLMutableAttributeList( xAttrList
);
155 xAttrList
= pMutableAttrList
;
157 const OUString aAttrValue
= xAttrList
->getValueByIndex( i
);
158 switch( (*aIter
).second
.m_nActionType
)
160 case XML_ATACTION_HREF
:
163 case XML_ATACTION_EVENT_NAME
:
164 pMutableAttrList
->SetValueByIndex( i
,
165 GetTransformer().GetEventName( aAttrValue
) );
167 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
169 OUString
aAttrValue2( aAttrValue
);
170 sal_uInt16 nValPrefix
=
171 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
172 GetTransformer().AddNamespacePrefix( aAttrValue2
,
174 pMutableAttrList
->SetValueByIndex( i
, aAttrValue2
);
177 case XML_ATACTION_MACRO_LOCATION
:
178 aLocation
= aAttrValue
;
179 pMutableAttrList
->RemoveAttributeByIndex( i
);
183 case XML_ATACTION_MACRO_NAME
:
184 aMacroName
= aAttrValue
;
187 case XML_ATACTION_COPY
:
190 OSL_ENSURE( false, "unknown action" );
196 if( nMacroName
!= -1 && !aLocation
.isEmpty() )
198 if( !IsXMLToken( aLocation
, XML_APPLICATION
) )
199 aLocation
= GetXMLToken( XML_DOCUMENT
);
200 OUString sTmp
= aLocation
+ ":" + aMacroName
;
201 pMutableAttrList
->SetValueByIndex( nMacroName
, sTmp
);
205 XMLPersElemContentTContext::StartElement( xAttrList
);
207 GetTransformer().GetDocHandler()->startElement( GetExportQName(), xAttrList
);
210 void XMLEventOOoTransformerContext::EndElement()
213 XMLPersElemContentTContext::EndElement();
215 GetTransformer().GetDocHandler()->endElement( GetExportQName() );
218 rtl::Reference
<XMLTransformerContext
> XMLEventOOoTransformerContext::CreateChildContext(
220 const OUString
& rLocalName
,
221 const OUString
& rQName
,
222 const Reference
< XAttributeList
>& xAttrList
)
225 return XMLPersElemContentTContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
227 return XMLTransformerContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
230 bool XMLEventOOoTransformerContext::IsPersistent() const
232 return m_bPersistent
;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */