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 .
21 #include <XMLEventImportHelper.hxx>
22 #include <tools/debug.hxx>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/namespacemap.hxx>
25 #include <xmloff/xmlnamespace.hxx>
26 #include <xmloff/xmlerror.hxx>
28 using ::com::sun::star::uno::Reference
;
30 XMLEventImportHelper::XMLEventImportHelper() :
31 pEventNameMap(new NameMap
)
35 XMLEventImportHelper::~XMLEventImportHelper()
41 pEventNameMap
.reset();
44 void XMLEventImportHelper::RegisterFactory(
45 const OUString
& rLanguage
,
46 std::unique_ptr
<XMLEventContextFactory
> pFactory
)
49 aFactoryMap
[rLanguage
] = std::move(pFactory
);
52 void XMLEventImportHelper::AddTranslationTable(
53 const XMLEventNameTranslation
* pTransTable
)
55 if (nullptr == pTransTable
)
58 // put translation table into map
59 for(const XMLEventNameTranslation
* pTrans
= pTransTable
;
60 pTrans
->sAPIName
!= nullptr;
63 XMLEventName
aName( pTrans
->nPrefix
, pTrans
->sXMLName
);
65 // check for conflicting entries
66 DBG_ASSERT(pEventNameMap
->find(aName
) == pEventNameMap
->end(),
67 "conflicting event translations");
69 // assign new translation
70 (*pEventNameMap
)[aName
] =
71 OUString::createFromAscii(pTrans
->sAPIName
);
76 void XMLEventImportHelper::PushTranslationTable()
78 // save old map and install new one
79 aEventNameMapVector
.push_back(std::move(pEventNameMap
));
80 pEventNameMap
.reset( new NameMap
);
83 void XMLEventImportHelper::PopTranslationTable()
85 DBG_ASSERT(!aEventNameMapVector
.empty(),
86 "no translation tables left to pop");
87 if ( !aEventNameMapVector
.empty() )
89 // delete current and install old map
90 pEventNameMap
= std::move(aEventNameMapVector
.back());
91 aEventNameMapVector
.pop_back();
96 SvXMLImportContext
* XMLEventImportHelper::CreateContext(
98 const Reference
<css::xml::sax::XFastAttributeList
> & xAttrList
,
99 XMLEventsImportContext
* rEvents
,
100 const OUString
& rXmlEventName
,
101 const OUString
& rLanguage
)
103 SvXMLImportContext
* pContext
= nullptr;
105 // translate event name from xml to api
107 sal_uInt16 nMacroPrefix
=
108 rImport
.GetNamespaceMap().GetKeyByAttrValueQName(rXmlEventName
,
110 XMLEventName
aEventName( nMacroPrefix
, sMacroName
);
111 NameMap::iterator aNameIter
= pEventNameMap
->find(aEventName
);
112 if (aNameIter
!= pEventNameMap
->end())
114 OUString aScriptLanguage
;
115 sal_uInt16 nScriptPrefix
= rImport
.GetNamespaceMap().
116 GetKeyByAttrValueQName(rLanguage
, &aScriptLanguage
);
117 if( XML_NAMESPACE_OOO
!= nScriptPrefix
)
118 aScriptLanguage
= rLanguage
;
121 FactoryMap::iterator aFactoryIterator
=
122 aFactoryMap
.find(aScriptLanguage
);
123 if (aFactoryIterator
!= aFactoryMap
.end())
125 // delegate to factory
126 pContext
= aFactoryIterator
->second
->CreateContext(
128 rEvents
, aNameIter
->second
);
132 // default context (if no context was created above)
133 if( nullptr == pContext
)
135 pContext
= new SvXMLImportContext(rImport
);
137 rImport
.SetError(XMLERROR_FLAG_ERROR
| XMLERROR_ILLEGAL_EVENT
,
138 { rXmlEventName
, rLanguage
});
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */