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 <comphelper/stl_types.hxx>
29 #include <rtl/ustrbuf.hxx>
31 #include <boost/unordered_map.hpp>
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 ::boost::unordered_map
< OUString
, NameKey_Impl
,
39 OUStringHash
, ::comphelper::UStringEqual
>
43 void AddMap( XMLTransformerEventMapEntry
*pInit
);
45 XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry
*pInit
,
46 XMLTransformerEventMapEntry
*pInit2
);
47 ~XMLTransformerOOoEventMap_Impl();
50 void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry
*pInit
)
52 XMLTransformerOOoEventMap_Impl::key_type aKey
;
53 XMLTransformerOOoEventMap_Impl::mapped_type aData
;
54 while( pInit
->m_pOOoName
)
56 aKey
= OUString::createFromAscii(pInit
->m_pOOoName
);
58 OSL_ENSURE( find( aKey
) == end(), "duplicate event map entry" );
60 aData
.m_nPrefix
= pInit
->m_nOASISPrefix
;
61 aData
.m_aLocalName
= OUString::createFromAscii(pInit
->m_pOASISName
);
63 XMLTransformerOOoEventMap_Impl::value_type
aVal( aKey
, aData
);
65 if( !insert( aVal
).second
)
67 OSL_FAIL( "duplicate OOo event name extry" );
74 XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl(
75 XMLTransformerEventMapEntry
*pInit
,
76 XMLTransformerEventMapEntry
*pInit2
)
84 XMLTransformerOOoEventMap_Impl::~XMLTransformerOOoEventMap_Impl()
88 // -----------------------------------------------------------------------------
90 TYPEINIT1( XMLEventOOoTransformerContext
, XMLPersElemContentTContext
);
92 XMLEventOOoTransformerContext::XMLEventOOoTransformerContext(
93 XMLTransformerBase
& rImp
,
94 const OUString
& rQName
,
95 sal_Bool bPersistent
) :
96 XMLPersElemContentTContext( rImp
, rQName
,
97 rImp
.GetNamespaceMap().GetKeyByAttrName( rQName
), XML_EVENT_LISTENER
),
98 m_bPersistent( bPersistent
)
102 XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext()
106 XMLTransformerOOoEventMap_Impl
107 *XMLEventOOoTransformerContext::CreateEventMap()
109 return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap
,
110 aFormTransformerEventMap
);
113 void XMLEventOOoTransformerContext::FlushEventMap(
114 XMLTransformerOOoEventMap_Impl
*p
)
119 sal_uInt16
XMLEventOOoTransformerContext::GetEventName(
120 const OUString
& rName
,
122 XMLTransformerOOoEventMap_Impl
& rMap
)
124 XMLTransformerOOoEventMap_Impl::key_type
aKey( rName
);
125 XMLTransformerOOoEventMap_Impl::const_iterator aIter
= rMap
.find( aKey
);
126 if( aIter
== rMap
.end() )
129 return XML_NAMESPACE_UNKNOWN
;
133 rNewName
= (*aIter
).second
.m_aLocalName
;
134 return (*aIter
).second
.m_nPrefix
;
139 void XMLEventOOoTransformerContext::StartElement(
140 const Reference
< XAttributeList
>& rAttrList
)
142 XMLTransformerActions
*pActions
=
143 GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS
);
144 OSL_ENSURE( pActions
, "go no actions" );
146 OUString aLocation
, aMacroName
;
147 sal_Int16 nMacroName
= -1;
148 Reference
< XAttributeList
> xAttrList( rAttrList
);
149 XMLMutableAttributeList
*pMutableAttrList
= 0;
150 sal_Int16 nAttrCount
= xAttrList
.is() ? xAttrList
->getLength() : 0;
151 for( sal_Int16 i
=0; i
< nAttrCount
; i
++ )
153 const OUString
& rAttrName
= xAttrList
->getNameByIndex( i
);
156 GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName
,
158 XMLTransformerActions::key_type
aKey( nPrefix
, aLocalName
);
159 XMLTransformerActions::const_iterator aIter
=
160 pActions
->find( aKey
);
161 if( !(aIter
== pActions
->end() ) )
163 if( !pMutableAttrList
)
166 new XMLMutableAttributeList( xAttrList
);
167 xAttrList
= pMutableAttrList
;
169 const OUString
& rAttrValue
= xAttrList
->getValueByIndex( i
);
170 switch( (*aIter
).second
.m_nActionType
)
172 case XML_ATACTION_HREF
:
175 case XML_ATACTION_EVENT_NAME
:
176 pMutableAttrList
->SetValueByIndex( i
,
177 GetTransformer().GetEventName( rAttrValue
) );
179 case XML_ATACTION_ADD_NAMESPACE_PREFIX
:
181 OUString
aAttrValue( rAttrValue
);
182 sal_uInt16 nValPrefix
=
183 static_cast<sal_uInt16
>((*aIter
).second
.m_nParam1
);
184 if( GetTransformer().AddNamespacePrefix( aAttrValue
,
186 pMutableAttrList
->SetValueByIndex( i
, aAttrValue
);
189 case XML_ATACTION_MACRO_LOCATION
:
190 aLocation
= rAttrValue
;
191 pMutableAttrList
->RemoveAttributeByIndex( i
);
195 case XML_ATACTION_MACRO_NAME
:
196 aMacroName
= rAttrValue
;
199 case XML_ATACTION_COPY
:
202 OSL_ENSURE( !this, "unknown action" );
208 if( nMacroName
!= -1 && !aLocation
.isEmpty() )
210 if( !IsXMLToken( aLocation
, XML_APPLICATION
) )
211 aLocation
= GetXMLToken( XML_DOCUMENT
);
212 OUStringBuffer
sTmp( aLocation
.getLength() + aMacroName
.getLength() + 1 );
214 sTmp
.append( sal_Unicode( ':' ) );
215 sTmp
.append( aMacroName
);
216 pMutableAttrList
->SetValueByIndex( nMacroName
,
217 sTmp
.makeStringAndClear() );
221 XMLPersElemContentTContext::StartElement( xAttrList
);
223 GetTransformer().GetDocHandler()->startElement( GetExportQName(),
227 void XMLEventOOoTransformerContext::EndElement()
230 XMLPersElemContentTContext::EndElement();
232 GetTransformer().GetDocHandler()->endElement( GetExportQName() );
235 XMLTransformerContext
* XMLEventOOoTransformerContext::CreateChildContext(
237 const OUString
& rLocalName
,
238 const OUString
& rQName
,
239 const Reference
< XAttributeList
>& xAttrList
)
242 return XMLPersElemContentTContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
244 return XMLTransformerContext::CreateChildContext(nPrefix
, rLocalName
, rQName
, xAttrList
);
247 sal_Bool
XMLEventOOoTransformerContext::IsPersistent() const
249 return m_bPersistent
;
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */