| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
|
| 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> |
| 28 |
|
| 29 |
#include <unordered_map> |
| 30 |
|
| 31 |
using namespace ::com::sun::star::uno; |
| 32 |
using namespace ::com::sun::star::xml::sax; |
| 33 |
using namespace ::xmloff::token; |
| 34 |
|
| 35 |
class XMLTransformerOOoEventMap_Impl: |
| 36 |
public std::unordered_map< OUString, NameKey_Impl > |
| 37 |
{ |
| 38 |
public: |
| 39 |
|
| 40 |
void AddMap( XMLTransformerEventMapEntry const *pInit ); |
| 41 |
|
| 42 |
XMLTransformerOOoEventMap_Impl( XMLTransformerEventMapEntry const *pInit, |
| 43 |
XMLTransformerEventMapEntry const *pInit2 ); |
| 44 |
}; |
| 45 |
|
| 46 |
void XMLTransformerOOoEventMap_Impl::AddMap( XMLTransformerEventMapEntry const *pInit ) |
| 47 |
{ |
| 48 |
XMLTransformerOOoEventMap_Impl::key_type aKey; |
| 49 |
XMLTransformerOOoEventMap_Impl::mapped_type aData; |
| 50 |
while( !pInit->m_pOOoName.isEmpty() ) |
| 51 |
{ |
| 52 |
aKey = pInit->m_pOOoName; |
| 53 |
|
| 54 |
OSL_ENSURE( find( aKey ) == end(), "duplicate event map entry" ); |
| 55 |
|
| 56 |
aData.m_nPrefix = pInit->m_nOASISPrefix; |
| 57 |
aData.m_aLocalName = pInit->m_pOASISName; |
| 58 |
|
| 59 |
XMLTransformerOOoEventMap_Impl::value_type aVal( aKey, aData ); |
| 60 |
|
| 61 |
if( !insert( aVal ).second ) |
| 62 |
{ |
| 63 |
OSL_FAIL( "duplicate OOo event name entry" ); |
| 64 |
} |
| 65 |
|
| 66 |
++pInit; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
XMLTransformerOOoEventMap_Impl::XMLTransformerOOoEventMap_Impl( |
| 71 |
XMLTransformerEventMapEntry const *pInit, |
| 72 |
XMLTransformerEventMapEntry const *pInit2 ) |
| 73 |
{ |
| 74 |
if( pInit ) |
| 75 |
AddMap( pInit ); |
| 76 |
if( pInit2 ) |
| 77 |
AddMap( pInit2 ); |
| 78 |
} |
| 79 |
|
| 80 |
XMLEventOOoTransformerContext::XMLEventOOoTransformerContext( |
| 81 |
XMLTransformerBase& rImp, |
| 82 |
const OUString& rQName, |
| 83 |
bool bPersistent ) : |
| 84 |
XMLPersElemContentTContext( rImp, rQName, |
| 85 |
rImp.GetNamespaceMap().GetKeyByAttrValueQName(rQName, nullptr), |
| 86 |
XML_EVENT_LISTENER), |
| 87 |
m_bPersistent( bPersistent ) |
| 88 |
{ |
| 89 |
} |
| 90 |
|
| 91 |
XMLEventOOoTransformerContext::~XMLEventOOoTransformerContext() |
| 92 |
{ |
| 93 |
} |
| 94 |
|
| 95 |
XMLTransformerOOoEventMap_Impl |
| 96 |
*XMLEventOOoTransformerContext::CreateEventMap() |
| 97 |
{ |
| 98 |
return new XMLTransformerOOoEventMap_Impl( aTransformerEventMap, |
| 99 |
aFormTransformerEventMap ); |
| 100 |
} |
| 101 |
|
| 102 |
void XMLEventOOoTransformerContext::FlushEventMap( |
| 103 |
XMLTransformerOOoEventMap_Impl *p ) |
| 104 |
{ |
| 105 |
delete p; |
| 106 |
} |
| 107 |
|
| 108 |
sal_uInt16 XMLEventOOoTransformerContext::GetEventName( |
| 109 |
const OUString& rName, |
| 110 |
OUString& rNewName, |
| 111 |
XMLTransformerOOoEventMap_Impl& rMap ) |
| 112 |
{ |
| 113 |
const XMLTransformerOOoEventMap_Impl::key_type& aKey( rName ); |
| 114 |
XMLTransformerOOoEventMap_Impl::const_iterator aIter = rMap.find( aKey ); |
| 115 |
if( aIter == rMap.end() ) |
| 116 |
{ |
| 117 |
rNewName = rName; |
| 118 |
return XML_NAMESPACE_UNKNOWN; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
rNewName = (*aIter).second.m_aLocalName; |
| 123 |
return (*aIter).second.m_nPrefix; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void XMLEventOOoTransformerContext::StartElement( |
| 128 |
const Reference< XAttributeList >& rAttrList ) |
| 129 |
{ |
| 130 |
XMLTransformerActions *pActions = |
| 131 |
GetTransformer().GetUserDefinedActions( OOO_EVENT_ACTIONS ); |
| 132 |
OSL_ENSURE( pActions, "go no actions" ); |
| 133 |
|
| 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++ ) |
| 140 |
{ |
| 141 |
const OUString aAttrName = xAttrList->getNameByIndex( i ); |
| 142 |
OUString aLocalName; |
| 143 |
sal_uInt16 nPrefix = |
| 144 |
GetTransformer().GetNamespaceMap().GetKeyByAttrName( aAttrName, |
| 145 |
&aLocalName ); |
| 146 |
XMLTransformerActions::key_type aKey( nPrefix, aLocalName ); |
| 147 |
XMLTransformerActions::const_iterator aIter = |
| 148 |
pActions->find( aKey ); |
| 149 |
if( aIter != pActions->end() ) |
| 150 |
{ |
| 151 |
if( !pMutableAttrList ) |
| 152 |
{ |
| 153 |
pMutableAttrList = |
| 154 |
new XMLMutableAttributeList( xAttrList ); |
| 155 |
xAttrList = pMutableAttrList; |
| 156 |
} |
| 157 |
const OUString aAttrValue = xAttrList->getValueByIndex( i ); |
| 158 |
switch( (*aIter).second.m_nActionType ) |
| 159 |
{ |
| 160 |
case XML_ATACTION_HREF: |
| 161 |
// TODO |
| 162 |
break; |
| 163 |
case XML_ATACTION_EVENT_NAME: |
| 164 |
pMutableAttrList->SetValueByIndex( i, |
| 165 |
GetTransformer().GetEventName( aAttrValue ) ); |
| 166 |
break; |
| 167 |
case XML_ATACTION_ADD_NAMESPACE_PREFIX: |
| 168 |
{ |
| 169 |
OUString aAttrValue2( aAttrValue ); |
| 170 |
sal_uInt16 nValPrefix = |
| 171 |
static_cast<sal_uInt16>((*aIter).second.m_nParam1); |
| 172 |
GetTransformer().AddNamespacePrefix( aAttrValue2, |
| 173 |
nValPrefix ); |
| 174 |
pMutableAttrList->SetValueByIndex( i, aAttrValue2 ); |
| 175 |
} |
| 176 |
break; |
| 177 |
case XML_ATACTION_MACRO_LOCATION: |
| 178 |
aLocation = aAttrValue; |
| 179 |
pMutableAttrList->RemoveAttributeByIndex( i ); |
| 180 |
--i; |
| 181 |
--nAttrCount; |
| 182 |
break; |
| 183 |
case XML_ATACTION_MACRO_NAME: |
| 184 |
aMacroName = aAttrValue; |
| 185 |
nMacroName = i; |
| 186 |
break; |
| 187 |
case XML_ATACTION_COPY: |
| 188 |
break; |
| 189 |
default: |
| 190 |
OSL_ENSURE( false, "unknown action" ); |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
if( nMacroName != -1 && !aLocation.isEmpty() ) |
| 197 |
{ |
| 198 |
if( !IsXMLToken( aLocation, XML_APPLICATION ) ) |
| 199 |
aLocation = GetXMLToken( XML_DOCUMENT ); |
| 200 |
OUString sTmp = aLocation + ":" + aMacroName; |
| 201 |
pMutableAttrList->SetValueByIndex( nMacroName, sTmp ); |
| 202 |
} |
| 203 |
|
| 204 |
if( m_bPersistent ) |
| 205 |
XMLPersElemContentTContext::StartElement( xAttrList ); |
| 206 |
else |
| 207 |
GetTransformer().GetDocHandler()->startElement( GetExportQName(), xAttrList ); |
| 208 |
} |
| 209 |
|
| 210 |
void XMLEventOOoTransformerContext::EndElement() |
| 211 |
{ |
| 212 |
if( m_bPersistent ) |
| 213 |
XMLPersElemContentTContext::EndElement(); |
| 214 |
else |
| 215 |
GetTransformer().GetDocHandler()->endElement( GetExportQName() ); |
| 216 |
} |
| 217 |
|
| 218 |
rtl::Reference<XMLTransformerContext> XMLEventOOoTransformerContext::CreateChildContext( |
| 219 |
sal_uInt16 nPrefix, |
| 220 |
const OUString& rLocalName, |
| 221 |
const OUString& rQName, |
| 222 |
const Reference< XAttributeList >& xAttrList ) |
| 223 |
{ |
| 224 |
if( m_bPersistent ) |
| 225 |
return XMLPersElemContentTContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList); |
| 226 |
else |
| 227 |
return XMLTransformerContext::CreateChildContext(nPrefix, rLocalName, rQName, xAttrList); |
| 228 |
} |
| 229 |
|
| 230 |
bool XMLEventOOoTransformerContext::IsPersistent() const |
| 231 |
{ |
| 232 |
return m_bPersistent; |
| 233 |
} |
| 234 |
|
| 235 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |