1*63bba73cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*63bba73cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*63bba73cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*63bba73cSAndrew Rist * distributed with this work for additional information
6*63bba73cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*63bba73cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*63bba73cSAndrew Rist * "License"); you may not use this file except in compliance
9*63bba73cSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*63bba73cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*63bba73cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*63bba73cSAndrew Rist * software distributed under the License is distributed on an
15*63bba73cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*63bba73cSAndrew Rist * KIND, either express or implied. See the License for the
17*63bba73cSAndrew Rist * specific language governing permissions and limitations
18*63bba73cSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*63bba73cSAndrew Rist *************************************************************/
21*63bba73cSAndrew Rist
22*63bba73cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
26cdf0e10cSrcweir #include "EventOOoTContext.hxx"
27cdf0e10cSrcweir #include "EventMap.hxx"
28cdf0e10cSrcweir #include "MutableAttrList.hxx"
29cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
30cdf0e10cSrcweir #ifndef _XMLOFF_ACTIONMAPTYPESOOo_HXX
31cdf0e10cSrcweir #include "ActionMapTypesOOo.hxx"
32cdf0e10cSrcweir #endif
33cdf0e10cSrcweir #include "AttrTransformerAction.hxx"
34cdf0e10cSrcweir #include "TransformerActions.hxx"
35cdf0e10cSrcweir #ifndef _XMLOFF_TRANSFORMERBASE_HXX
36cdf0e10cSrcweir #include "TransformerBase.hxx"
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
39cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
40cdf0e10cSrcweir
41cdf0e10cSrcweir #include <hash_map>
42cdf0e10cSrcweir
43cdf0e10cSrcweir using ::rtl::OUString;
44cdf0e10cSrcweir using ::rtl::OUStringBuffer;
45cdf0e10cSrcweir using namespace ::com::sun::star::uno;
46cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
47cdf0e10cSrcweir using namespace ::xmloff::token;
48cdf0e10cSrcweir
49cdf0e10cSrcweir class XMLTransformerOOoEventMap_Impl:
50cdf0e10cSrcweir public ::std::hash_map< ::rtl::OUString, NameKey_Impl,
51cdf0e10cSrcweir ::rtl::OUStringHash, ::comphelper::UStringEqual >
52cdf0e10cSrcweir {
53cdf0e10cSrcweir public:
54cdf0e10cSrcweir
55cdf0e10cSrcweir void AddMap( XMLTransformerEventMapEntry *pInit );
56cdf0e10cSrcweir
57cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry *pInit,
58cdf0e10cSrcweir XMLTransformerEventMapEntry *pInit2 );
59cdf0e10cSrcweir ~XMLTransformerOOoEventMap_Impl();
60cdf0e10cSrcweir };
61cdf0e10cSrcweir
AddMap(XMLTransformerEventMapEntry * pInit)62cdf0e10cSrcweir void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry *pInit )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::key_type aKey;
65cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::data_type aData;
66cdf0e10cSrcweir while( pInit->m_pOOoName )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir aKey = OUString::createFromAscii(pInit->m_pOOoName);
69cdf0e10cSrcweir
70cdf0e10cSrcweir OSL_ENSURE( find( aKey ) == end(), "duplicate event map entry" );
71cdf0e10cSrcweir
72cdf0e10cSrcweir aData.m_nPrefix = pInit->m_nOASISPrefix;
73cdf0e10cSrcweir aData.m_aLocalName = OUString::createFromAscii(pInit->m_pOASISName);
74cdf0e10cSrcweir
75cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::value_type aVal( aKey, aData );
76cdf0e10cSrcweir
77cdf0e10cSrcweir if( !insert( aVal ).second )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir OSL_ENSURE( false, "duplicate OOo event name extry" );
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
82cdf0e10cSrcweir ++pInit;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir }
85cdf0e10cSrcweir
XMLTransformerOOoEventMap_Impl(XMLTransformerEventMapEntry * pInit,XMLTransformerEventMapEntry * pInit2)86cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl(
87cdf0e10cSrcweir XMLTransformerEventMapEntry *pInit,
88cdf0e10cSrcweir XMLTransformerEventMapEntry *pInit2 )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir if( pInit )
91cdf0e10cSrcweir AddMap( pInit );
92cdf0e10cSrcweir if( pInit )
93cdf0e10cSrcweir AddMap( pInit2 );
94cdf0e10cSrcweir }
95cdf0e10cSrcweir
~XMLTransformerOOoEventMap_Impl()96cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::~XMLTransformerOOoEventMap_Impl()
97cdf0e10cSrcweir {
98cdf0e10cSrcweir }
99cdf0e10cSrcweir
100cdf0e10cSrcweir // -----------------------------------------------------------------------------
101cdf0e10cSrcweir
102cdf0e10cSrcweir TYPEINIT1( XMLEventOOoTransformerContext, XMLPersElemContentTContext );
103cdf0e10cSrcweir
XMLEventOOoTransformerContext(XMLTransformerBase & rImp,const OUString & rQName,sal_Bool bPersistent)104cdf0e10cSrcweir XMLEventOOoTransformerContext::XMLEventOOoTransformerContext(
105cdf0e10cSrcweir XMLTransformerBase& rImp,
106cdf0e10cSrcweir const OUString& rQName,
107cdf0e10cSrcweir sal_Bool bPersistent ) :
108cdf0e10cSrcweir XMLPersElemContentTContext( rImp, rQName,
109cdf0e10cSrcweir rImp.GetNamespaceMap().GetKeyByAttrName( rQName ), XML_EVENT_LISTENER ),
110cdf0e10cSrcweir m_bPersistent( bPersistent )
111cdf0e10cSrcweir {
112cdf0e10cSrcweir }
113cdf0e10cSrcweir
~XMLEventOOoTransformerContext()114cdf0e10cSrcweir XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext()
115cdf0e10cSrcweir {
116cdf0e10cSrcweir }
117cdf0e10cSrcweir
118cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl
CreateEventMap()119cdf0e10cSrcweir *XMLEventOOoTransformerContext::CreateEventMap()
120cdf0e10cSrcweir {
121cdf0e10cSrcweir return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap,
122cdf0e10cSrcweir aFormTransformerEventMap );
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
FlushEventMap(XMLTransformerOOoEventMap_Impl * p)125cdf0e10cSrcweir void XMLEventOOoTransformerContext::FlushEventMap(
126cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl *p )
127cdf0e10cSrcweir {
128cdf0e10cSrcweir delete p;
129cdf0e10cSrcweir }
130cdf0e10cSrcweir
GetEventName(const OUString & rName,OUString & rNewName,XMLTransformerOOoEventMap_Impl & rMap)131cdf0e10cSrcweir sal_uInt16 XMLEventOOoTransformerContext::GetEventName(
132cdf0e10cSrcweir const OUString& rName,
133cdf0e10cSrcweir OUString& rNewName,
134cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl& rMap )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::key_type aKey( rName );
137cdf0e10cSrcweir XMLTransformerOOoEventMap_Impl::const_iterator aIter = rMap.find( aKey );
138cdf0e10cSrcweir if( aIter == rMap.end() )
139cdf0e10cSrcweir {
140cdf0e10cSrcweir rNewName = rName;
141cdf0e10cSrcweir return XML_NAMESPACE_UNKNOWN;
142cdf0e10cSrcweir }
143cdf0e10cSrcweir else
144cdf0e10cSrcweir {
145cdf0e10cSrcweir rNewName = (*aIter).second.m_aLocalName;
146cdf0e10cSrcweir return (*aIter).second.m_nPrefix;
147cdf0e10cSrcweir }
148cdf0e10cSrcweir }
149cdf0e10cSrcweir
150cdf0e10cSrcweir
StartElement(const Reference<XAttributeList> & rAttrList)151cdf0e10cSrcweir void XMLEventOOoTransformerContext::StartElement(
152cdf0e10cSrcweir const Reference< XAttributeList >& rAttrList )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir XMLTransformerActions *pActions =
155cdf0e10cSrcweir GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS );
156cdf0e10cSrcweir OSL_ENSURE( pActions, "go no actions" );
157cdf0e10cSrcweir
158cdf0e10cSrcweir OUString aLocation, aMacroName;
159cdf0e10cSrcweir sal_Int16 nMacroName = -1;
160cdf0e10cSrcweir Reference< XAttributeList > xAttrList( rAttrList );
161cdf0e10cSrcweir XMLMutableAttributeList *pMutableAttrList = 0;
162cdf0e10cSrcweir sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
163cdf0e10cSrcweir for( sal_Int16 i=0; i < nAttrCount; i++ )
164cdf0e10cSrcweir {
165cdf0e10cSrcweir const OUString& rAttrName = xAttrList->getNameByIndex( i );
166cdf0e10cSrcweir OUString aLocalName;
167cdf0e10cSrcweir sal_uInt16 nPrefix =
168cdf0e10cSrcweir GetTransformer().GetNamespaceMap().GetKeyByAttrName( rAttrName,
169cdf0e10cSrcweir &aLocalName );
170cdf0e10cSrcweir XMLTransformerActions::key_type aKey( nPrefix, aLocalName );
171cdf0e10cSrcweir XMLTransformerActions::const_iterator aIter =
172cdf0e10cSrcweir pActions->find( aKey );
173cdf0e10cSrcweir if( !(aIter == pActions->end() ) )
174cdf0e10cSrcweir {
175cdf0e10cSrcweir if( !pMutableAttrList )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir pMutableAttrList =
178cdf0e10cSrcweir new XMLMutableAttributeList( xAttrList );
179cdf0e10cSrcweir xAttrList = pMutableAttrList;
180cdf0e10cSrcweir }
181cdf0e10cSrcweir const OUString& rAttrValue = xAttrList->getValueByIndex( i );
182cdf0e10cSrcweir switch( (*aIter).second.m_nActionType )
183cdf0e10cSrcweir {
184cdf0e10cSrcweir case XML_ATACTION_HREF:
185cdf0e10cSrcweir // TODO
186cdf0e10cSrcweir break;
187cdf0e10cSrcweir case XML_ATACTION_EVENT_NAME:
188cdf0e10cSrcweir pMutableAttrList->SetValueByIndex( i,
189cdf0e10cSrcweir GetTransformer().GetEventName( rAttrValue ) );
190cdf0e10cSrcweir break;
191cdf0e10cSrcweir case XML_ATACTION_ADD_NAMESPACE_PREFIX:
192cdf0e10cSrcweir {
193cdf0e10cSrcweir OUString aAttrValue( rAttrValue );
194cdf0e10cSrcweir sal_uInt16 nValPrefix =
195cdf0e10cSrcweir static_cast<sal_uInt16>((*aIter).second.m_nParam1);
196cdf0e10cSrcweir if( GetTransformer().AddNamespacePrefix( aAttrValue,
197cdf0e10cSrcweir nValPrefix ) )
198cdf0e10cSrcweir pMutableAttrList->SetValueByIndex( i, aAttrValue );
199cdf0e10cSrcweir }
200cdf0e10cSrcweir break;
201cdf0e10cSrcweir case XML_ATACTION_MACRO_LOCATION:
202cdf0e10cSrcweir aLocation = rAttrValue;
203cdf0e10cSrcweir pMutableAttrList->RemoveAttributeByIndex( i );
204cdf0e10cSrcweir --i;
205cdf0e10cSrcweir --nAttrCount;
206cdf0e10cSrcweir break;
207cdf0e10cSrcweir case XML_ATACTION_MACRO_NAME:
208cdf0e10cSrcweir aMacroName = rAttrValue;
209cdf0e10cSrcweir nMacroName = i;
210cdf0e10cSrcweir break;
211cdf0e10cSrcweir case XML_ATACTION_COPY:
212cdf0e10cSrcweir break;
213cdf0e10cSrcweir default:
214cdf0e10cSrcweir OSL_ENSURE( !this, "unknown action" );
215cdf0e10cSrcweir break;
216cdf0e10cSrcweir }
217cdf0e10cSrcweir }
218cdf0e10cSrcweir }
219cdf0e10cSrcweir
220cdf0e10cSrcweir if( nMacroName != -1 && aLocation.getLength() > 0 )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir if( !IsXMLToken( aLocation, XML_APPLICATION ) )
223cdf0e10cSrcweir aLocation = GetXMLToken( XML_DOCUMENT );
224cdf0e10cSrcweir OUStringBuffer sTmp( aLocation.getLength() + aMacroName.getLength() + 1 );
225cdf0e10cSrcweir sTmp = aLocation;
226cdf0e10cSrcweir sTmp.append( sal_Unicode( ':' ) );
227cdf0e10cSrcweir sTmp.append( aMacroName );
228cdf0e10cSrcweir pMutableAttrList->SetValueByIndex( nMacroName,
229cdf0e10cSrcweir sTmp.makeStringAndClear() );
230cdf0e10cSrcweir }
231cdf0e10cSrcweir
232cdf0e10cSrcweir if( m_bPersistent )
233cdf0e10cSrcweir XMLPersElemContentTContext::StartElement( xAttrList );
234cdf0e10cSrcweir else
235cdf0e10cSrcweir GetTransformer().GetDocHandler()->startElement( GetExportQName(),
236cdf0e10cSrcweir xAttrList );
237cdf0e10cSrcweir }
238cdf0e10cSrcweir
EndElement()239cdf0e10cSrcweir void XMLEventOOoTransformerContext::EndElement()
240cdf0e10cSrcweir {
241cdf0e10cSrcweir if( m_bPersistent )
242cdf0e10cSrcweir XMLPersElemContentTContext::EndElement();
243cdf0e10cSrcweir else
244cdf0e10cSrcweir GetTransformer().GetDocHandler()->endElement( GetExportQName() );
245cdf0e10cSrcweir }
246cdf0e10cSrcweir
CreateChildContext(sal_uInt16 nPrefix,const OUString & rLocalName,const OUString & rQName,const Reference<XAttributeList> & xAttrList)247cdf0e10cSrcweir XMLTransformerContext * XMLEventOOoTransformerContext::CreateChildContext(
248cdf0e10cSrcweir sal_uInt16 nPrefix,
249cdf0e10cSrcweir const OUString& rLocalName,
250cdf0e10cSrcweir const OUString& rQName,
251cdf0e10cSrcweir const Reference< XAttributeList >& xAttrList )
252cdf0e10cSrcweir {
253cdf0e10cSrcweir if( m_bPersistent )
254cdf0e10cSrcweir return XMLPersElemContentTContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList);
255cdf0e10cSrcweir else
256cdf0e10cSrcweir return XMLTransformerContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList);
257cdf0e10cSrcweir }
258cdf0e10cSrcweir
IsPersistent() const259cdf0e10cSrcweir sal_Bool XMLEventOOoTransformerContext::IsPersistent() const
260cdf0e10cSrcweir {
261cdf0e10cSrcweir return m_bPersistent;
262cdf0e10cSrcweir }
263