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